2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-04-02 03:19:41 -05:00
|
|
|
use abi;
|
2014-09-13 11:06:01 -05:00
|
|
|
use ast::{Ident, Generics, Expr};
|
2012-12-23 16:41:37 -06:00
|
|
|
use ast;
|
2013-05-17 08:51:25 -05:00
|
|
|
use ast_util;
|
2014-05-23 10:39:26 -05:00
|
|
|
use attr;
|
2014-06-13 16:56:42 -05:00
|
|
|
use codemap::{Span, respan, Spanned, DUMMY_SP, Pos};
|
2013-05-17 06:27:17 -05:00
|
|
|
use ext::base::ExtCtxt;
|
2014-03-19 09:52:37 -05:00
|
|
|
use owned_slice::OwnedSlice;
|
2014-01-27 06:18:36 -06:00
|
|
|
use parse::token::special_idents;
|
2014-05-28 22:19:05 -05:00
|
|
|
use parse::token::InternedString;
|
2014-01-10 16:02:36 -06:00
|
|
|
use parse::token;
|
2014-09-13 11:06:01 -05:00
|
|
|
use ptr::P;
|
2013-02-14 23:50:03 -06:00
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
// Transitional reexports so qquote can find the paths it is looking for
|
|
|
|
mod syntax {
|
|
|
|
pub use ext;
|
|
|
|
pub use parse;
|
|
|
|
}
|
|
|
|
|
2013-05-17 08:51:25 -05:00
|
|
|
pub trait AstBuilder {
|
|
|
|
// paths
|
2014-02-28 15:09:09 -06:00
|
|
|
fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
|
2013-09-01 19:50:59 -05:00
|
|
|
fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path;
|
2014-02-28 15:09:09 -06:00
|
|
|
fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn path_all(&self, sp: Span,
|
2013-05-19 00:53:42 -05:00
|
|
|
global: bool,
|
2014-02-28 15:09:09 -06:00
|
|
|
idents: Vec<ast::Ident> ,
|
2014-03-07 09:50:40 -06:00
|
|
|
lifetimes: Vec<ast::Lifetime>,
|
2014-11-28 22:08:30 -06:00
|
|
|
types: Vec<P<ast::Ty>>,
|
|
|
|
bindings: Vec<P<ast::TypeBinding>> )
|
2013-07-05 05:15:21 -05:00
|
|
|
-> ast::Path;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2015-02-04 13:00:28 -06:00
|
|
|
fn qpath(&self, self_type: P<ast::Ty>,
|
2015-01-31 13:20:24 -06:00
|
|
|
trait_path: ast::Path,
|
|
|
|
ident: ast::Ident)
|
2015-02-17 11:29:13 -06:00
|
|
|
-> (ast::QSelf, ast::Path);
|
2015-02-04 13:00:28 -06:00
|
|
|
fn qpath_all(&self, self_type: P<ast::Ty>,
|
2015-01-31 13:20:24 -06:00
|
|
|
trait_path: ast::Path,
|
2015-02-04 13:00:28 -06:00
|
|
|
ident: ast::Ident,
|
|
|
|
lifetimes: Vec<ast::Lifetime>,
|
|
|
|
types: Vec<P<ast::Ty>>,
|
2015-01-31 13:20:24 -06:00
|
|
|
bindings: Vec<P<ast::TypeBinding>>)
|
2015-02-17 11:29:13 -06:00
|
|
|
-> (ast::QSelf, ast::Path);
|
2015-02-04 13:00:28 -06:00
|
|
|
|
2013-05-17 08:51:25 -05:00
|
|
|
// types
|
2014-01-09 07:05:33 -06:00
|
|
|
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty>;
|
2014-11-20 14:08:48 -06:00
|
|
|
fn ty_path(&self, ast::Path) -> P<ast::Ty>;
|
|
|
|
fn ty_sum(&self, ast::Path, OwnedSlice<ast::TyParamBound>) -> P<ast::Ty>;
|
2013-11-30 16:00:39 -06:00
|
|
|
fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
fn ty_rptr(&self, span: Span,
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: P<ast::Ty>,
|
2013-07-05 07:33:52 -05:00
|
|
|
lifetime: Option<ast::Lifetime>,
|
2013-11-30 16:00:39 -06:00
|
|
|
mutbl: ast::Mutability) -> P<ast::Ty>;
|
2014-08-27 08:19:17 -05:00
|
|
|
fn ty_ptr(&self, span: Span,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability) -> P<ast::Ty>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty>;
|
|
|
|
fn ty_infer(&self, sp: Span) -> P<ast::Ty>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-03-19 09:52:37 -05:00
|
|
|
fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
|
|
|
|
fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> ;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-01-30 11:28:02 -06:00
|
|
|
fn typaram(&self,
|
2014-04-02 19:53:57 -05:00
|
|
|
span: Span,
|
2014-01-30 11:28:02 -06:00
|
|
|
id: ast::Ident,
|
2014-03-19 09:52:37 -05:00
|
|
|
bounds: OwnedSlice<ast::TyParamBound>,
|
2014-01-30 11:28:02 -06:00
|
|
|
default: Option<P<ast::Ty>>) -> ast::TyParam;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
|
2015-02-05 02:46:01 -06:00
|
|
|
fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef;
|
2013-07-05 05:15:21 -05:00
|
|
|
fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
|
2014-02-21 14:04:03 -06:00
|
|
|
fn lifetime(&self, span: Span, ident: ast::Name) -> ast::Lifetime;
|
2014-08-05 21:59:24 -05:00
|
|
|
fn lifetime_def(&self,
|
|
|
|
span: Span,
|
|
|
|
name: ast::Name,
|
|
|
|
bounds: Vec<ast::Lifetime>)
|
|
|
|
-> ast::LifetimeDef;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
|
|
|
// statements
|
2014-09-13 11:06:01 -05:00
|
|
|
fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt>;
|
|
|
|
fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident, ex: P<ast::Expr>) -> P<ast::Stmt>;
|
2013-08-08 13:38:10 -05:00
|
|
|
fn stmt_let_typed(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: Span,
|
2013-08-08 13:38:10 -05:00
|
|
|
mutbl: bool,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident,
|
2013-11-30 16:00:39 -06:00
|
|
|
typ: P<ast::Ty>,
|
2014-09-13 11:06:01 -05:00
|
|
|
ex: P<ast::Expr>)
|
|
|
|
-> P<ast::Stmt>;
|
|
|
|
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
|
|
|
// blocks
|
2014-09-13 11:06:01 -05:00
|
|
|
fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
|
|
|
|
expr: Option<P<ast::Expr>>) -> P<ast::Block>;
|
|
|
|
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn block_all(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
stmts: Vec<P<ast::Stmt>>,
|
|
|
|
expr: Option<P<ast::Expr>>) -> P<ast::Block>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
|
|
|
// expressions
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr>;
|
|
|
|
fn expr_path(&self, path: ast::Path) -> P<ast::Expr>;
|
2015-02-17 11:29:13 -06:00
|
|
|
fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr>;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr>;
|
2013-09-01 20:45:37 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_self(&self, span: Span) -> P<ast::Expr>;
|
2015-01-12 21:24:37 -06:00
|
|
|
fn expr_binary(&self, sp: Span, op: ast::BinOp_,
|
2014-09-13 11:06:01 -05:00
|
|
|
lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
|
|
|
|
fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_field_access(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
|
|
|
|
fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>,
|
2015-01-17 17:33:05 -06:00
|
|
|
idx: usize) -> P<ast::Expr>;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
|
|
|
|
fn expr_call_ident(&self, span: Span, id: ast::Ident, args: Vec<P<ast::Expr>>) -> P<ast::Expr>;
|
|
|
|
fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident>,
|
|
|
|
args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn expr_method_call(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
expr: P<ast::Expr>, ident: ast::Ident,
|
|
|
|
args: Vec<P<ast::Expr>> ) -> P<ast::Expr>;
|
|
|
|
fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr>;
|
|
|
|
fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr>;
|
|
|
|
|
|
|
|
fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field;
|
|
|
|
fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
fn expr_struct_ident(&self, span: Span, id: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
fields: Vec<ast::Field>) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2015-01-17 17:49:08 -06:00
|
|
|
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr>;
|
2015-05-01 05:22:38 -05:00
|
|
|
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr>;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr>;
|
2015-02-21 05:56:46 -06:00
|
|
|
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr>;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
|
|
|
|
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr>;
|
|
|
|
fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
|
|
|
|
fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_none(&self, sp: Span) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-10-02 22:28:15 -05:00
|
|
|
fn expr_break(&self, sp: Span) -> P<ast::Expr>;
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr>;
|
2014-07-01 11:39:41 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr>;
|
|
|
|
fn expr_unreachable(&self, span: Span) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_ok(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_err(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn expr_try(&self, span: Span, head: P<ast::Expr>) -> P<ast::Expr>;
|
2014-05-16 02:16:13 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat>;
|
|
|
|
fn pat_wild(&self, span: Span) -> P<ast::Pat>;
|
|
|
|
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat>;
|
|
|
|
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
|
|
|
fn pat_ident_binding_mode(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
bm: ast::BindingMode) -> P<ast::Pat>;
|
|
|
|
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>> ) -> P<ast::Pat>;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn pat_struct(&self, span: Span,
|
2014-10-05 19:36:53 -05:00
|
|
|
path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>> ) -> P<ast::Pat>;
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat>;
|
2014-07-27 17:11:02 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
|
|
|
|
fn pat_none(&self, span: Span) -> P<ast::Pat>;
|
2014-07-27 17:11:02 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
|
|
|
|
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn arm(&self, span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm;
|
2013-09-01 20:45:37 -05:00
|
|
|
fn arm_unreachable(&self, span: Span) -> ast::Arm;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm> ) -> P<ast::Expr>;
|
2013-08-31 11:13:04 -05:00
|
|
|
fn expr_if(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
cond: P<ast::Expr>, then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr>;
|
|
|
|
fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn lambda_fn_decl(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Block>) -> P<ast::Expr>;
|
|
|
|
fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr>;
|
|
|
|
fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident> , blk: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr>;
|
|
|
|
fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr>;
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-05-16 02:16:13 -05:00
|
|
|
fn lambda_stmts(&self, span: Span, ids: Vec<ast::Ident>,
|
2014-09-13 11:06:01 -05:00
|
|
|
blk: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
|
|
|
|
fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr>;
|
|
|
|
fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
|
|
|
|
ident: ast::Ident) -> P<ast::Expr>;
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2013-05-17 08:51:25 -05:00
|
|
|
// items
|
2013-08-31 11:13:04 -05:00
|
|
|
fn item(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
name: Ident, attrs: Vec<ast::Attribute> , node: ast::Item_) -> P<ast::Item>;
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn arg(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> ast::Arg;
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME unused self
|
2014-02-28 15:09:09 -06:00
|
|
|
fn fn_decl(&self, inputs: Vec<ast::Arg> , output: P<ast::Ty>) -> P<ast::FnDecl>;
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2013-05-17 08:51:25 -05:00
|
|
|
fn item_fn_poly(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-02-28 15:09:09 -06:00
|
|
|
inputs: Vec<ast::Arg> ,
|
2013-11-30 16:00:39 -06:00
|
|
|
output: P<ast::Ty>,
|
2013-05-17 08:51:25 -05:00
|
|
|
generics: Generics,
|
2014-09-13 11:06:01 -05:00
|
|
|
body: P<ast::Block>) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
fn item_fn(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-02-28 15:09:09 -06:00
|
|
|
inputs: Vec<ast::Arg> ,
|
2013-11-30 16:00:39 -06:00
|
|
|
output: P<ast::Ty>,
|
2014-09-13 11:06:01 -05:00
|
|
|
body: P<ast::Block>) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-02-28 15:09:09 -06:00
|
|
|
fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant;
|
2013-05-17 08:51:25 -05:00
|
|
|
fn item_enum_poly(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-01-09 07:05:33 -06:00
|
|
|
enum_definition: ast::EnumDef,
|
2014-09-13 11:06:01 -05:00
|
|
|
generics: Generics) -> P<ast::Item>;
|
|
|
|
fn item_enum(&self, span: Span, name: Ident, enum_def: ast::EnumDef) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
|
|
|
fn item_struct_poly(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-01-09 07:05:33 -06:00
|
|
|
struct_def: ast::StructDef,
|
2014-09-13 11:06:01 -05:00
|
|
|
generics: Generics) -> P<ast::Item>;
|
|
|
|
fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-04-26 15:05:45 -05:00
|
|
|
fn item_mod(&self, span: Span, inner_span: Span,
|
2014-05-16 02:16:13 -05:00
|
|
|
name: Ident, attrs: Vec<ast::Attribute>,
|
2015-01-13 09:30:17 -06:00
|
|
|
items: Vec<P<ast::Item>>) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-06-10 15:54:13 -05:00
|
|
|
fn item_static(&self,
|
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability,
|
2014-09-13 11:06:01 -05:00
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item>;
|
2014-06-10 15:54:13 -05:00
|
|
|
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
fn item_const(&self,
|
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item>;
|
|
|
|
|
2013-05-17 08:51:25 -05:00
|
|
|
fn item_ty_poly(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: P<ast::Ty>,
|
2014-09-13 11:06:01 -05:00
|
|
|
generics: Generics) -> P<ast::Item>;
|
|
|
|
fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item>;
|
2013-05-17 09:19:28 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute;
|
2013-05-17 09:19:28 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem>;
|
2014-01-08 12:35:15 -06:00
|
|
|
fn meta_list(&self,
|
|
|
|
sp: Span,
|
|
|
|
name: InternedString,
|
2014-09-13 11:06:01 -05:00
|
|
|
mis: Vec<P<ast::MetaItem>> )
|
|
|
|
-> P<ast::MetaItem>;
|
2014-01-08 12:35:15 -06:00
|
|
|
fn meta_name_value(&self,
|
|
|
|
sp: Span,
|
|
|
|
name: InternedString,
|
|
|
|
value: ast::Lit_)
|
2014-09-13 11:06:01 -05:00
|
|
|
-> P<ast::MetaItem>;
|
2013-05-17 09:19:28 -05:00
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use(&self, sp: Span,
|
|
|
|
vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
|
|
|
|
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
|
|
|
|
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
|
|
|
|
ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
|
|
|
|
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
|
|
|
|
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
|
|
|
|
fn item_use_glob(&self, sp: Span,
|
|
|
|
vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2013-12-25 12:10:33 -06:00
|
|
|
impl<'a> AstBuilder for ExtCtxt<'a> {
|
2014-02-28 15:09:09 -06:00
|
|
|
fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
|
2014-11-28 22:08:30 -06:00
|
|
|
self.path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-09-01 19:50:59 -05:00
|
|
|
fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
|
2014-02-28 15:09:09 -06:00
|
|
|
self.path(span, vec!(id))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
2014-02-28 15:09:09 -06:00
|
|
|
fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
|
2014-11-28 22:08:30 -06:00
|
|
|
self.path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-08-07 11:47:28 -05:00
|
|
|
fn path_all(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: Span,
|
2013-05-19 00:53:42 -05:00
|
|
|
global: bool,
|
2014-02-28 15:09:09 -06:00
|
|
|
mut idents: Vec<ast::Ident> ,
|
2014-03-07 09:50:40 -06:00
|
|
|
lifetimes: Vec<ast::Lifetime>,
|
2014-11-28 22:08:30 -06:00
|
|
|
types: Vec<P<ast::Ty>>,
|
|
|
|
bindings: Vec<P<ast::TypeBinding>> )
|
2013-08-07 11:47:28 -05:00
|
|
|
-> ast::Path {
|
2013-12-23 09:20:52 -06:00
|
|
|
let last_identifier = idents.pop().unwrap();
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut segments: Vec<ast::PathSegment> = idents.into_iter()
|
2013-08-08 13:38:10 -05:00
|
|
|
.map(|ident| {
|
2013-08-07 11:47:28 -05:00
|
|
|
ast::PathSegment {
|
|
|
|
identifier: ident,
|
2014-11-03 20:52:52 -06:00
|
|
|
parameters: ast::PathParameters::none(),
|
2013-08-07 11:47:28 -05:00
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
identifier: last_identifier,
|
2014-11-03 20:52:52 -06:00
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
types: OwnedSlice::from_vec(types),
|
2014-11-28 22:08:30 -06:00
|
|
|
bindings: OwnedSlice::from_vec(bindings),
|
2014-11-03 20:52:52 -06:00
|
|
|
})
|
2013-08-07 11:47:28 -05:00
|
|
|
});
|
2013-07-05 05:15:21 -05:00
|
|
|
ast::Path {
|
2013-05-19 00:53:42 -05:00
|
|
|
span: sp,
|
|
|
|
global: global,
|
2013-08-07 11:47:28 -05:00
|
|
|
segments: segments,
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 13:00:28 -06:00
|
|
|
/// Constructs a qualified path.
|
|
|
|
///
|
2015-01-31 13:20:24 -06:00
|
|
|
/// Constructs a path like `<self_type as trait_path>::ident`.
|
2015-02-04 13:00:28 -06:00
|
|
|
fn qpath(&self,
|
|
|
|
self_type: P<ast::Ty>,
|
2015-01-31 13:20:24 -06:00
|
|
|
trait_path: ast::Path,
|
2015-02-04 13:00:28 -06:00
|
|
|
ident: ast::Ident)
|
2015-02-17 11:29:13 -06:00
|
|
|
-> (ast::QSelf, ast::Path) {
|
2015-01-31 13:20:24 -06:00
|
|
|
self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
|
2015-02-04 13:00:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a qualified path.
|
|
|
|
///
|
2015-01-31 13:20:24 -06:00
|
|
|
/// Constructs a path like `<self_type as trait_path>::ident<'a, T, A=Bar>`.
|
2015-02-04 13:00:28 -06:00
|
|
|
fn qpath_all(&self,
|
|
|
|
self_type: P<ast::Ty>,
|
2015-01-31 13:20:24 -06:00
|
|
|
trait_path: ast::Path,
|
2015-02-04 13:00:28 -06:00
|
|
|
ident: ast::Ident,
|
|
|
|
lifetimes: Vec<ast::Lifetime>,
|
|
|
|
types: Vec<P<ast::Ty>>,
|
2015-01-31 13:20:24 -06:00
|
|
|
bindings: Vec<P<ast::TypeBinding>>)
|
2015-02-17 11:29:13 -06:00
|
|
|
-> (ast::QSelf, ast::Path) {
|
2015-01-31 13:20:24 -06:00
|
|
|
let mut path = trait_path;
|
|
|
|
path.segments.push(ast::PathSegment {
|
2015-02-04 13:00:28 -06:00
|
|
|
identifier: ident,
|
|
|
|
parameters: ast::AngleBracketedParameters(ast::AngleBracketedParameterData {
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
types: OwnedSlice::from_vec(types),
|
|
|
|
bindings: OwnedSlice::from_vec(bindings),
|
|
|
|
})
|
2015-01-31 13:20:24 -06:00
|
|
|
});
|
2015-02-04 13:00:28 -06:00
|
|
|
|
2015-02-17 11:29:13 -06:00
|
|
|
(ast::QSelf {
|
|
|
|
ty: self_type,
|
|
|
|
position: path.segments.len() - 1
|
|
|
|
}, path)
|
2015-02-04 13:00:28 -06:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
|
|
|
|
ast::MutTy {
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: ty,
|
2013-05-19 00:53:42 -05:00
|
|
|
mutbl: mutbl
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn ty(&self, span: Span, ty: ast::Ty_) -> P<ast::Ty> {
|
2013-11-30 16:00:39 -06:00
|
|
|
P(ast::Ty {
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-15 17:55:57 -05:00
|
|
|
span: span,
|
2013-05-19 00:53:42 -05:00
|
|
|
node: ty
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:08:48 -06:00
|
|
|
fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
|
2015-02-17 11:29:13 -06:00
|
|
|
self.ty(path.span, ast::TyPath(None, path))
|
2014-11-20 14:08:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_sum(&self, path: ast::Path, bounds: OwnedSlice<ast::TyParamBound>) -> P<ast::Ty> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.ty(path.span,
|
2014-11-20 14:08:48 -06:00
|
|
|
ast::TyObjectSum(self.ty_path(path),
|
|
|
|
bounds))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2013-06-17 14:16:30 -05:00
|
|
|
// Might need to take bounds as an argument in the future, if you ever want
|
|
|
|
// to generate a bounded existential trait type.
|
2013-09-01 19:50:59 -05:00
|
|
|
fn ty_ident(&self, span: Span, ident: ast::Ident)
|
2013-11-30 16:00:39 -06:00
|
|
|
-> P<ast::Ty> {
|
2014-11-20 14:08:48 -06:00
|
|
|
self.ty_path(self.path_ident(span, ident))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ty_rptr(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-11-30 16:00:39 -06:00
|
|
|
ty: P<ast::Ty>,
|
2013-07-05 07:33:52 -05:00
|
|
|
lifetime: Option<ast::Lifetime>,
|
2013-09-01 20:45:37 -05:00
|
|
|
mutbl: ast::Mutability)
|
2013-11-30 16:00:39 -06:00
|
|
|
-> P<ast::Ty> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.ty(span,
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::TyRptr(lifetime, self.ty_mt(ty, mutbl)))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-12-31 14:55:39 -06:00
|
|
|
|
2014-08-27 08:19:17 -05:00
|
|
|
fn ty_ptr(&self,
|
|
|
|
span: Span,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability)
|
|
|
|
-> P<ast::Ty> {
|
|
|
|
self.ty(span,
|
|
|
|
ast::TyPtr(self.ty_mt(ty, mutbl)))
|
|
|
|
}
|
2013-12-31 14:55:39 -06:00
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn ty_option(&self, ty: P<ast::Ty>) -> P<ast::Ty> {
|
2013-05-17 08:51:25 -05:00
|
|
|
self.ty_path(
|
2014-01-01 00:53:22 -06:00
|
|
|
self.path_all(DUMMY_SP,
|
2013-05-19 00:53:42 -05:00
|
|
|
true,
|
2015-07-29 19:01:14 -05:00
|
|
|
self.std_path(&["option", "Option"]),
|
2014-03-07 09:50:40 -06:00
|
|
|
Vec::new(),
|
2014-11-28 22:08:30 -06:00
|
|
|
vec!( ty ),
|
|
|
|
Vec::new()))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn ty_infer(&self, span: Span) -> P<ast::Ty> {
|
2014-01-09 07:05:33 -06:00
|
|
|
self.ty(span, ast::TyInfer)
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-01-30 11:28:02 -06:00
|
|
|
fn typaram(&self,
|
2014-04-02 19:53:57 -05:00
|
|
|
span: Span,
|
2014-01-30 11:28:02 -06:00
|
|
|
id: ast::Ident,
|
2014-03-19 09:52:37 -05:00
|
|
|
bounds: OwnedSlice<ast::TyParamBound>,
|
2014-01-30 11:28:02 -06:00
|
|
|
default: Option<P<ast::Ty>>) -> ast::TyParam {
|
|
|
|
ast::TyParam {
|
|
|
|
ident: id,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
bounds: bounds,
|
2014-04-02 19:53:57 -05:00
|
|
|
default: default,
|
|
|
|
span: span
|
2014-01-30 11:28:02 -06:00
|
|
|
}
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// these are strange, and probably shouldn't be used outside of
|
|
|
|
// pipes. Specifically, the global version possible generates
|
|
|
|
// incorrect code.
|
2014-03-19 09:52:37 -05:00
|
|
|
fn ty_vars(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
|
|
|
|
ty_params.iter().map(|p| self.ty_ident(DUMMY_SP, p.ident)).collect()
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-03-19 09:52:37 -05:00
|
|
|
fn ty_vars_global(&self, ty_params: &OwnedSlice<ast::TyParam>) -> Vec<P<ast::Ty>> {
|
2014-11-20 14:08:48 -06:00
|
|
|
ty_params
|
|
|
|
.iter()
|
|
|
|
.map(|p| self.ty_path(self.path_global(DUMMY_SP, vec!(p.ident))))
|
|
|
|
.collect()
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
|
|
|
|
ast::TraitRef {
|
2013-05-19 00:53:42 -05:00
|
|
|
path: path,
|
2014-09-05 14:21:02 -05:00
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
2014-11-07 05:53:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 02:46:01 -06:00
|
|
|
fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
|
2014-11-07 05:53:45 -06:00
|
|
|
ast::PolyTraitRef {
|
|
|
|
bound_lifetimes: Vec::new(),
|
2015-02-05 02:46:01 -06:00
|
|
|
trait_ref: self.trait_ref(path),
|
|
|
|
span: span,
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2013-07-05 05:15:21 -05:00
|
|
|
fn typarambound(&self, path: ast::Path) -> ast::TyParamBound {
|
2015-02-05 02:46:01 -06:00
|
|
|
ast::TraitTyParamBound(self.poly_trait_ref(path.span, path), ast::TraitBoundModifier::None)
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-03-06 20:10:52 -06:00
|
|
|
fn lifetime(&self, span: Span, name: ast::Name) -> ast::Lifetime {
|
|
|
|
ast::Lifetime { id: ast::DUMMY_NODE_ID, span: span, name: name }
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-08-05 21:59:24 -05:00
|
|
|
fn lifetime_def(&self,
|
|
|
|
span: Span,
|
|
|
|
name: ast::Name,
|
|
|
|
bounds: Vec<ast::Lifetime>)
|
|
|
|
-> ast::LifetimeDef {
|
|
|
|
ast::LifetimeDef {
|
|
|
|
lifetime: self.lifetime(span, name),
|
|
|
|
bounds: bounds
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn stmt_expr(&self, expr: P<ast::Expr>) -> P<ast::Stmt> {
|
|
|
|
P(respan(expr.span, ast::StmtSemi(expr, ast::DUMMY_NODE_ID)))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-05-16 02:16:13 -05:00
|
|
|
fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
ex: P<ast::Expr>) -> P<ast::Stmt> {
|
2013-10-20 07:31:23 -05:00
|
|
|
let pat = if mutbl {
|
|
|
|
self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
|
|
|
|
} else {
|
|
|
|
self.pat_ident(sp, ident)
|
|
|
|
};
|
2014-09-13 11:06:01 -05:00
|
|
|
let local = P(ast::Local {
|
2013-07-19 00:38:55 -05:00
|
|
|
pat: pat,
|
2015-01-02 05:55:31 -06:00
|
|
|
ty: None,
|
2013-07-19 00:38:55 -05:00
|
|
|
init: Some(ex),
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-19 00:38:55 -05:00
|
|
|
span: sp,
|
2014-09-13 11:06:01 -05:00
|
|
|
});
|
2013-09-01 20:45:37 -05:00
|
|
|
let decl = respan(sp, ast::DeclLocal(local));
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2013-08-08 13:38:10 -05:00
|
|
|
fn stmt_let_typed(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: Span,
|
2013-08-08 13:38:10 -05:00
|
|
|
mutbl: bool,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident,
|
2013-11-30 16:00:39 -06:00
|
|
|
typ: P<ast::Ty>,
|
2014-09-13 11:06:01 -05:00
|
|
|
ex: P<ast::Expr>)
|
|
|
|
-> P<ast::Stmt> {
|
2013-10-20 07:31:23 -05:00
|
|
|
let pat = if mutbl {
|
|
|
|
self.pat_ident_binding_mode(sp, ident, ast::BindByValue(ast::MutMutable))
|
|
|
|
} else {
|
|
|
|
self.pat_ident(sp, ident)
|
|
|
|
};
|
2014-09-13 11:06:01 -05:00
|
|
|
let local = P(ast::Local {
|
2013-08-08 13:38:10 -05:00
|
|
|
pat: pat,
|
2015-01-02 05:55:31 -06:00
|
|
|
ty: Some(typ),
|
2013-08-08 13:38:10 -05:00
|
|
|
init: Some(ex),
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-08-08 13:38:10 -05:00
|
|
|
span: sp,
|
2014-09-13 11:06:01 -05:00
|
|
|
});
|
2013-09-01 20:45:37 -05:00
|
|
|
let decl = respan(sp, ast::DeclLocal(local));
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
|
2013-08-08 13:38:10 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
|
|
|
|
expr: Option<P<Expr>>) -> P<ast::Block> {
|
2015-01-13 09:30:17 -06:00
|
|
|
self.block_all(span, stmts, expr)
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
|
2014-06-10 15:54:13 -05:00
|
|
|
let decl = respan(sp, ast::DeclItem(item));
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp, ast::StmtDecl(P(decl), ast::DUMMY_NODE_ID)))
|
2014-06-10 15:54:13 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
|
2015-01-13 09:30:17 -06:00
|
|
|
self.block_all(expr.span, Vec::new(), Some(expr))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-08-01 10:34:59 -05:00
|
|
|
fn block_all(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
stmts: Vec<P<ast::Stmt>>,
|
|
|
|
expr: Option<P<ast::Expr>>) -> P<ast::Block> {
|
2013-11-30 16:00:39 -06:00
|
|
|
P(ast::Block {
|
2013-07-16 13:08:35 -05:00
|
|
|
stmts: stmts,
|
|
|
|
expr: expr,
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-27 03:25:59 -05:00
|
|
|
rules: ast::DefaultBlock,
|
2013-07-16 13:08:35 -05:00
|
|
|
span: span,
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr(&self, span: Span, node: ast::Expr_) -> P<ast::Expr> {
|
|
|
|
P(ast::Expr {
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-17 08:51:25 -05:00
|
|
|
node: node,
|
|
|
|
span: span,
|
2014-09-13 11:06:01 -05:00
|
|
|
})
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
|
2015-02-17 11:29:13 -06:00
|
|
|
self.expr(path.span, ast::ExprPath(None, path))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2015-02-04 13:00:28 -06:00
|
|
|
/// Constructs a QPath expression.
|
2015-02-17 11:29:13 -06:00
|
|
|
fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
|
|
|
|
self.expr(span, ast::ExprPath(Some(qself), path))
|
2015-02-04 13:00:28 -06:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.expr_path(self.path_ident(span, id))
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_self(&self, span: Span) -> P<ast::Expr> {
|
2014-01-27 06:18:36 -06:00
|
|
|
self.expr_ident(span, special_idents::self_)
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2015-01-12 21:24:37 -06:00
|
|
|
fn expr_binary(&self, sp: Span, op: ast::BinOp_,
|
2014-09-13 11:06:01 -05:00
|
|
|
lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
|
2015-01-12 21:24:37 -06:00
|
|
|
self.expr(sp, ast::ExprBinary(Spanned { node: op, span: sp }, lhs, rhs))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr_unary(sp, ast::UnDeref, e)
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
|
2014-02-26 08:06:45 -06:00
|
|
|
self.expr(sp, ast::ExprUnary(op, e))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_field_access(&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
|
2014-06-13 16:56:42 -05:00
|
|
|
let field_span = Span {
|
2015-07-28 11:07:20 -05:00
|
|
|
lo: sp.lo - Pos::from_usize(ident.name.as_str().len()),
|
2014-06-13 16:56:42 -05:00
|
|
|
hi: sp.hi,
|
2014-09-17 11:01:33 -05:00
|
|
|
expn_id: sp.expn_id,
|
2014-06-13 16:56:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let id = Spanned { node: ident, span: field_span };
|
2014-11-23 05:14:35 -06:00
|
|
|
self.expr(sp, ast::ExprField(expr, id))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2015-01-17 17:33:05 -06:00
|
|
|
fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
|
2014-08-09 22:54:33 -05:00
|
|
|
let field_span = Span {
|
2015-01-17 17:49:08 -06:00
|
|
|
lo: sp.lo - Pos::from_usize(idx.to_string().len()),
|
2014-08-09 22:54:33 -05:00
|
|
|
hi: sp.hi,
|
2014-09-17 11:01:33 -05:00
|
|
|
expn_id: sp.expn_id,
|
2014-08-09 22:54:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let id = Spanned { node: idx, span: field_span };
|
2014-11-23 05:14:35 -06:00
|
|
|
self.expr(sp, ast::ExprTupField(expr, id))
|
2014-08-09 22:54:33 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr(sp, ast::ExprAddrOf(ast::MutImmutable, e))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr(sp, ast::ExprAddrOf(ast::MutMutable, e))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_call(&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2014-02-14 02:28:32 -06:00
|
|
|
self.expr(span, ast::ExprCall(expr, args))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-05-16 02:16:13 -05:00
|
|
|
fn expr_call_ident(&self, span: Span, id: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2014-02-14 02:28:32 -06:00
|
|
|
self.expr(span, ast::ExprCall(self.expr_ident(span, id), args))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-02-28 15:09:09 -06:00
|
|
|
fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
|
2014-09-13 11:06:01 -05:00
|
|
|
args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
|
2013-05-19 00:53:42 -05:00
|
|
|
let pathexpr = self.expr_path(self.path_global(sp, fn_path));
|
|
|
|
self.expr_call(sp, pathexpr, args)
|
|
|
|
}
|
2013-08-31 11:13:04 -05:00
|
|
|
fn expr_method_call(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
expr: P<ast::Expr>,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
|
2014-04-23 16:19:23 -05:00
|
|
|
let id = Spanned { node: ident, span: span };
|
2014-10-15 01:05:01 -05:00
|
|
|
args.insert(0, expr);
|
2014-04-23 16:19:23 -05:00
|
|
|
self.expr(span, ast::ExprMethodCall(id, Vec::new(), args))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr(b.span, ast::ExprBlock(b))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn field_imm(&self, span: Span, name: Ident, e: P<ast::Expr>) -> ast::Field {
|
2013-10-28 21:22:42 -05:00
|
|
|
ast::Field { ident: respan(span, name), expr: e, span: span }
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_struct(&self, span: Span, path: ast::Path, fields: Vec<ast::Field>) -> P<ast::Expr> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr(span, ast::ExprStruct(path, fields, None))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-08-31 11:13:04 -05:00
|
|
|
fn expr_struct_ident(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.expr_struct(span, self.path_ident(span, id), fields)
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_lit(&self, sp: Span, lit: ast::Lit_) -> P<ast::Expr> {
|
|
|
|
self.expr(sp, ast::ExprLit(P(respan(sp, lit))))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2015-01-17 17:49:08 -06:00
|
|
|
fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
|
2015-03-24 15:26:16 -05:00
|
|
|
self.expr_lit(span, ast::LitInt(i as u64, ast::UnsignedIntLit(ast::TyUs)))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2015-05-01 05:22:38 -05:00
|
|
|
fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
|
2015-03-24 15:26:16 -05:00
|
|
|
self.expr_lit(sp, ast::LitInt(i as u64, ast::SignedIntLit(ast::TyIs,
|
2015-01-08 03:13:14 -06:00
|
|
|
ast::Sign::new(i))))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2015-02-22 21:07:38 -06:00
|
|
|
fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
|
|
|
|
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU32)))
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
|
2014-08-05 02:59:03 -05:00
|
|
|
self.expr_lit(sp, ast::LitInt(u as u64, ast::UnsignedIntLit(ast::TyU8)))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
|
2014-01-09 07:05:33 -06:00
|
|
|
self.expr_lit(sp, ast::LitBool(value))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2014-04-04 05:12:18 -05:00
|
|
|
self.expr(sp, ast::ExprVec(exprs))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
self.expr_call_global(sp, self.std_path(&["vec", "Vec", "new"]),
|
2014-02-28 14:54:01 -06:00
|
|
|
Vec::new())
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
self.expr_addr_of(sp, self.expr_vec(sp, exprs))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_str(&self, sp: Span, s: InternedString) -> P<ast::Expr> {
|
2014-01-09 07:05:33 -06:00
|
|
|
self.expr_lit(sp, ast::LitStr(s, ast::CookedStr))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
|
2013-09-16 23:12:18 -05:00
|
|
|
self.expr(sp, ast::ExprCast(expr, ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let some = self.std_path(&["option", "Option", "Some"]);
|
2014-02-28 15:09:09 -06:00
|
|
|
self.expr_call_global(sp, some, vec!(expr))
|
2013-09-16 23:12:18 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_none(&self, sp: Span) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let none = self.std_path(&["option", "Option", "None"]);
|
|
|
|
let none = self.path_global(sp, none);
|
2013-09-16 23:12:18 -05:00
|
|
|
self.expr_path(none)
|
|
|
|
}
|
|
|
|
|
2014-10-02 22:28:15 -05:00
|
|
|
|
|
|
|
fn expr_break(&self, sp: Span) -> P<ast::Expr> {
|
|
|
|
self.expr(sp, ast::ExprBreak(None))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2014-07-01 11:39:41 -05:00
|
|
|
self.expr(sp, ast::ExprTup(exprs))
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_fail(&self, span: Span, msg: InternedString) -> P<ast::Expr> {
|
2013-05-19 00:53:42 -05:00
|
|
|
let loc = self.codemap().lookup_char_pos(span.lo);
|
2014-07-28 23:26:21 -05:00
|
|
|
let expr_file = self.expr_str(span,
|
2015-02-20 13:08:14 -06:00
|
|
|
token::intern_and_get_ident(&loc.file.name));
|
2015-04-11 04:46:57 -05:00
|
|
|
let expr_line = self.expr_u32(span, loc.line as u32);
|
2014-07-28 23:26:21 -05:00
|
|
|
let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line));
|
|
|
|
let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple);
|
2013-05-19 00:53:42 -05:00
|
|
|
self.expr_call_global(
|
|
|
|
span,
|
2015-07-29 19:01:14 -05:00
|
|
|
self.std_path(&["rt", "begin_unwind"]),
|
2014-02-28 15:09:09 -06:00
|
|
|
vec!(
|
2013-09-17 09:45:49 -05:00
|
|
|
self.expr_str(span, msg),
|
2014-07-28 23:26:21 -05:00
|
|
|
expr_file_line_ptr))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
|
2014-01-10 16:02:36 -06:00
|
|
|
self.expr_fail(span,
|
|
|
|
InternedString::new(
|
|
|
|
"internal error: entered unreachable code"))
|
2013-09-17 09:45:49 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let ok = self.std_path(&["result", "Result", "Ok"]);
|
2014-03-18 12:58:26 -05:00
|
|
|
self.expr_call_global(sp, ok, vec!(expr))
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let err = self.std_path(&["result", "Result", "Err"]);
|
2014-03-18 12:58:26 -05:00
|
|
|
self.expr_call_global(sp, err, vec!(expr))
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let ok = self.std_path(&["result", "Result", "Ok"]);
|
2014-09-07 16:57:26 -05:00
|
|
|
let ok_path = self.path_global(sp, ok);
|
2015-07-29 19:01:14 -05:00
|
|
|
let err = self.std_path(&["result", "Result", "Err"]);
|
2014-09-07 16:57:26 -05:00
|
|
|
let err_path = self.path_global(sp, err);
|
2014-03-18 12:58:26 -05:00
|
|
|
|
|
|
|
let binding_variable = self.ident_of("__try_var");
|
|
|
|
let binding_pat = self.pat_ident(sp, binding_variable);
|
|
|
|
let binding_expr = self.expr_ident(sp, binding_variable);
|
|
|
|
|
|
|
|
// Ok(__try_var) pattern
|
2014-09-13 11:06:01 -05:00
|
|
|
let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
|
2014-03-18 12:58:26 -05:00
|
|
|
|
|
|
|
// Err(__try_var) (pattern and expression resp.)
|
2014-09-07 16:57:26 -05:00
|
|
|
let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
|
|
|
|
let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
|
|
|
|
vec!(binding_expr.clone()));
|
2014-03-18 12:58:26 -05:00
|
|
|
// return Err(__try_var)
|
|
|
|
let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
|
|
|
|
|
|
|
|
// Ok(__try_var) => __try_var
|
|
|
|
let ok_arm = self.arm(sp, vec!(ok_pat), binding_expr);
|
|
|
|
// Err(__try_var) => return Err(__try_var)
|
|
|
|
let err_arm = self.arm(sp, vec!(err_pat), err_expr);
|
|
|
|
|
|
|
|
// match head { Ok() => ..., Err() => ... }
|
|
|
|
self.expr_match(sp, head, vec!(ok_arm, err_arm))
|
|
|
|
}
|
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat(&self, span: Span, pat: ast::Pat_) -> P<ast::Pat> {
|
|
|
|
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span: span })
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_wild(&self, span: Span) -> P<ast::Pat> {
|
2014-08-06 10:04:44 -05:00
|
|
|
self.pat(span, ast::PatWild(ast::PatWildSingle))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
|
2013-09-01 20:45:37 -05:00
|
|
|
self.pat(span, ast::PatLit(expr))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
|
2013-10-20 07:31:23 -05:00
|
|
|
self.pat_ident_binding_mode(span, ident, ast::BindByValue(ast::MutImmutable))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_ident_binding_mode(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
ident: ast::Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
bm: ast::BindingMode) -> P<ast::Pat> {
|
2014-06-30 20:02:14 -05:00
|
|
|
let pat = ast::PatIdent(bm, Spanned{span: span, node: ident}, None);
|
2013-05-19 00:53:42 -05:00
|
|
|
self.pat(span, pat)
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_enum(&self, span: Span, path: ast::Path, subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
|
2013-09-01 20:45:37 -05:00
|
|
|
let pat = ast::PatEnum(path, Some(subpats));
|
2013-05-19 00:53:42 -05:00
|
|
|
self.pat(span, pat)
|
|
|
|
}
|
2013-08-31 11:13:04 -05:00
|
|
|
fn pat_struct(&self, span: Span,
|
2014-10-05 19:36:53 -05:00
|
|
|
path: ast::Path, field_pats: Vec<Spanned<ast::FieldPat>>) -> P<ast::Pat> {
|
2013-09-01 20:45:37 -05:00
|
|
|
let pat = ast::PatStruct(path, field_pats, false);
|
2013-05-19 00:53:42 -05:00
|
|
|
self.pat(span, pat)
|
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
|
2014-11-09 09:14:15 -06:00
|
|
|
self.pat(span, ast::PatTup(pats))
|
2014-07-27 17:11:02 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let some = self.std_path(&["option", "Option", "Some"]);
|
2014-07-27 17:11:02 -05:00
|
|
|
let path = self.path_global(span, some);
|
|
|
|
self.pat_enum(span, path, vec!(pat))
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_none(&self, span: Span) -> P<ast::Pat> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let some = self.std_path(&["option", "Option", "None"]);
|
2014-07-27 17:11:02 -05:00
|
|
|
let path = self.path_global(span, some);
|
|
|
|
self.pat_enum(span, path, vec!())
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let some = self.std_path(&["result", "Result", "Ok"]);
|
2014-07-27 17:11:02 -05:00
|
|
|
let path = self.path_global(span, some);
|
|
|
|
self.pat_enum(span, path, vec!(pat))
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2015-07-29 19:01:14 -05:00
|
|
|
let some = self.std_path(&["result", "Result", "Err"]);
|
2014-07-27 17:11:02 -05:00
|
|
|
let path = self.path_global(span, some);
|
|
|
|
self.pat_enum(span, path, vec!(pat))
|
|
|
|
}
|
2013-05-19 00:53:42 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn arm(&self, _span: Span, pats: Vec<P<ast::Pat>>, expr: P<ast::Expr>) -> ast::Arm {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::Arm {
|
2014-04-22 23:54:48 -05:00
|
|
|
attrs: vec!(),
|
2013-05-19 00:53:42 -05:00
|
|
|
pats: pats,
|
|
|
|
guard: None,
|
2014-03-03 01:41:47 -06:00
|
|
|
body: expr
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
fn arm_unreachable(&self, span: Span) -> ast::Arm {
|
2014-02-28 15:09:09 -06:00
|
|
|
self.arm(span, vec!(self.pat_wild(span)), self.expr_unreachable(span))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
|
2014-12-19 16:58:02 -06:00
|
|
|
self.expr(span, ast::ExprMatch(arg, arms, ast::MatchSource::Normal))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_if(&self, span: Span, cond: P<ast::Expr>,
|
|
|
|
then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
|
2013-09-20 01:08:47 -05:00
|
|
|
let els = els.map(|x| self.expr_block(self.block_expr(x)));
|
2013-09-01 20:45:37 -05:00
|
|
|
self.expr(span, ast::ExprIf(cond, self.block_expr(then), els))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
|
2014-07-27 17:11:02 -05:00
|
|
|
self.expr(span, ast::ExprLoop(block, None))
|
|
|
|
}
|
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn lambda_fn_decl(&self, span: Span,
|
2014-09-13 11:06:01 -05:00
|
|
|
fn_decl: P<ast::FnDecl>, blk: P<ast::Block>) -> P<ast::Expr> {
|
2015-02-03 10:34:05 -06:00
|
|
|
self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda(&self, span: Span, ids: Vec<ast::Ident>, blk: P<ast::Block>) -> P<ast::Expr> {
|
2013-05-19 00:53:42 -05:00
|
|
|
let fn_decl = self.fn_decl(
|
2014-03-28 14:42:34 -05:00
|
|
|
ids.iter().map(|id| self.arg(span, *id, self.ty_infer(span))).collect(),
|
2013-05-19 00:53:42 -05:00
|
|
|
self.ty_infer(span));
|
|
|
|
|
2015-02-03 10:34:05 -06:00
|
|
|
self.expr(span, ast::ExprClosure(ast::CaptureByRef, fn_decl, blk))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda0(&self, span: Span, blk: P<ast::Block>) -> P<ast::Expr> {
|
2014-02-28 14:54:01 -06:00
|
|
|
self.lambda(span, Vec::new(), blk)
|
2013-08-15 01:06:33 -05:00
|
|
|
}
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda1(&self, span: Span, blk: P<ast::Block>, ident: ast::Ident) -> P<ast::Expr> {
|
2014-02-28 14:54:01 -06:00
|
|
|
self.lambda(span, vec!(ident), blk)
|
2013-08-15 01:06:33 -05:00
|
|
|
}
|
2013-05-15 17:55:57 -05:00
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_expr(&self, span: Span, ids: Vec<ast::Ident>,
|
|
|
|
expr: P<ast::Expr>) -> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda(span, ids, self.block_expr(expr))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_expr_0(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda0(span, self.block_expr(expr))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_expr_1(&self, span: Span, expr: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda1(span, self.block_expr(expr), ident)
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 14:54:01 -06:00
|
|
|
fn lambda_stmts(&self,
|
|
|
|
span: Span,
|
|
|
|
ids: Vec<ast::Ident>,
|
2014-09-13 11:06:01 -05:00
|
|
|
stmts: Vec<P<ast::Stmt>>)
|
|
|
|
-> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda(span, ids, self.block(span, stmts, None))
|
2013-05-19 00:53:42 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_stmts_0(&self, span: Span, stmts: Vec<P<ast::Stmt>>) -> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda0(span, self.block(span, stmts, None))
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
2014-09-13 11:06:01 -05:00
|
|
|
fn lambda_stmts_1(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
|
|
|
|
ident: ast::Ident) -> P<ast::Expr> {
|
2013-08-01 10:34:59 -05:00
|
|
|
self.lambda1(span, self.block(span, stmts, None), ident)
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn arg(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Arg {
|
2013-05-19 00:53:42 -05:00
|
|
|
let arg_pat = self.pat_ident(span, ident);
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Arg {
|
2013-05-17 08:51:25 -05:00
|
|
|
ty: ty,
|
2013-05-19 00:53:42 -05:00
|
|
|
pat: arg_pat,
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
2013-05-15 17:55:57 -05:00
|
|
|
}
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME unused self
|
2014-11-09 09:14:15 -06:00
|
|
|
fn fn_decl(&self, inputs: Vec<ast::Arg>, output: P<ast::Ty>) -> P<ast::FnDecl> {
|
2014-01-09 07:05:33 -06:00
|
|
|
P(ast::FnDecl {
|
2013-05-17 08:51:25 -05:00
|
|
|
inputs: inputs,
|
2014-11-09 09:14:15 -06:00
|
|
|
output: ast::Return(output),
|
2013-10-25 00:56:34 -05:00
|
|
|
variadic: false
|
2013-11-30 16:00:39 -06:00
|
|
|
})
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn item(&self, span: Span, name: Ident,
|
|
|
|
attrs: Vec<ast::Attribute>, node: ast::Item_) -> P<ast::Item> {
|
2014-01-26 02:43:42 -06:00
|
|
|
// FIXME: Would be nice if our generated code didn't violate
|
2013-05-17 08:51:25 -05:00
|
|
|
// Rust coding conventions
|
2014-09-13 11:06:01 -05:00
|
|
|
P(ast::Item {
|
|
|
|
ident: name,
|
|
|
|
attrs: attrs,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: node,
|
|
|
|
vis: ast::Inherited,
|
|
|
|
span: span
|
|
|
|
})
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
fn item_fn_poly(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-02-28 15:09:09 -06:00
|
|
|
inputs: Vec<ast::Arg> ,
|
2013-11-30 16:00:39 -06:00
|
|
|
output: P<ast::Ty>,
|
2013-05-17 08:51:25 -05:00
|
|
|
generics: Generics,
|
2014-09-13 11:06:01 -05:00
|
|
|
body: P<ast::Block>) -> P<ast::Item> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.item(span,
|
|
|
|
name,
|
2014-02-28 15:09:09 -06:00
|
|
|
Vec::new(),
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemFn(self.fn_decl(inputs, output),
|
2014-12-09 09:36:46 -06:00
|
|
|
ast::Unsafety::Normal,
|
2015-02-25 14:05:07 -06:00
|
|
|
ast::Constness::NotConst,
|
2014-04-02 03:19:41 -05:00
|
|
|
abi::Rust,
|
2014-01-09 07:05:33 -06:00
|
|
|
generics,
|
|
|
|
body))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn item_fn(&self,
|
2013-08-31 11:13:04 -05:00
|
|
|
span: Span,
|
2013-09-01 19:50:59 -05:00
|
|
|
name: Ident,
|
2014-02-28 15:09:09 -06:00
|
|
|
inputs: Vec<ast::Arg> ,
|
2013-11-30 16:00:39 -06:00
|
|
|
output: P<ast::Ty>,
|
|
|
|
body: P<ast::Block>
|
2014-09-13 11:06:01 -05:00
|
|
|
) -> P<ast::Item> {
|
2013-05-17 08:51:25 -05:00
|
|
|
self.item_fn_poly(
|
2013-05-19 00:53:42 -05:00
|
|
|
span,
|
2013-05-17 08:51:25 -05:00
|
|
|
name,
|
|
|
|
inputs,
|
|
|
|
output,
|
|
|
|
ast_util::empty_generics(),
|
2013-05-19 00:53:42 -05:00
|
|
|
body)
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 15:09:09 -06:00
|
|
|
fn variant(&self, span: Span, name: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
|
2014-09-14 22:27:36 -05:00
|
|
|
let args = tys.into_iter().map(|ty| {
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::VariantArg { ty: ty, id: ast::DUMMY_NODE_ID }
|
2013-07-05 23:57:11 -05:00
|
|
|
}).collect();
|
2013-05-17 08:51:25 -05:00
|
|
|
|
2013-05-19 00:53:42 -05:00
|
|
|
respan(span,
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::Variant_ {
|
2013-05-19 00:53:42 -05:00
|
|
|
name: name,
|
2014-02-28 15:09:09 -06:00
|
|
|
attrs: Vec::new(),
|
2014-01-09 07:05:33 -06:00
|
|
|
kind: ast::TupleVariantKind(args),
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-19 00:53:42 -05:00
|
|
|
disr_expr: None,
|
|
|
|
})
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 19:50:59 -05:00
|
|
|
fn item_enum_poly(&self, span: Span, name: Ident,
|
2014-01-09 07:05:33 -06:00
|
|
|
enum_definition: ast::EnumDef,
|
2014-09-13 11:06:01 -05:00
|
|
|
generics: Generics) -> P<ast::Item> {
|
2014-02-28 15:09:09 -06:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemEnum(enum_definition, generics))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2013-09-01 19:50:59 -05:00
|
|
|
fn item_enum(&self, span: Span, name: Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
enum_definition: ast::EnumDef) -> P<ast::Item> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.item_enum_poly(span, name, enum_definition,
|
2013-05-17 08:51:25 -05:00
|
|
|
ast_util::empty_generics())
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn item_struct(&self, span: Span, name: Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
struct_def: ast::StructDef) -> P<ast::Item> {
|
2013-05-17 08:51:25 -05:00
|
|
|
self.item_struct_poly(
|
|
|
|
span,
|
2013-05-19 00:53:42 -05:00
|
|
|
name,
|
2013-05-17 08:51:25 -05:00
|
|
|
struct_def,
|
|
|
|
ast_util::empty_generics()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
fn item_struct_poly(&self, span: Span, name: Ident,
|
2014-09-13 11:06:01 -05:00
|
|
|
struct_def: ast::StructDef, generics: Generics) -> P<ast::Item> {
|
|
|
|
self.item(span, name, Vec::new(), ast::ItemStruct(P(struct_def), generics))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-04-26 15:05:45 -05:00
|
|
|
fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
|
2015-01-13 09:30:17 -06:00
|
|
|
attrs: Vec<ast::Attribute>,
|
|
|
|
items: Vec<P<ast::Item>>) -> P<ast::Item> {
|
2013-05-17 08:51:25 -05:00
|
|
|
self.item(
|
|
|
|
span,
|
2013-05-19 00:53:42 -05:00
|
|
|
name,
|
|
|
|
attrs,
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ItemMod(ast::Mod {
|
2014-04-26 15:05:45 -05:00
|
|
|
inner: inner_span,
|
2013-05-17 08:51:25 -05:00
|
|
|
items: items,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-06-10 15:54:13 -05:00
|
|
|
fn item_static(&self,
|
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability,
|
2014-09-13 11:06:01 -05:00
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item> {
|
2014-06-10 15:54:13 -05:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemStatic(ty, mutbl, expr))
|
|
|
|
}
|
|
|
|
|
rustc: Add `const` globals to the language
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
[rfc]: https://github.com/rust-lang/rfcs/pull/246
2014-10-06 10:17:01 -05:00
|
|
|
fn item_const(&self,
|
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item> {
|
|
|
|
self.item(span, name, Vec::new(), ast::ItemConst(ty, expr))
|
|
|
|
}
|
|
|
|
|
2013-11-30 16:00:39 -06:00
|
|
|
fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
|
2014-09-13 11:06:01 -05:00
|
|
|
generics: Generics) -> P<ast::Item> {
|
2014-02-28 15:09:09 -06:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemTy(ty, generics))
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
|
2013-05-19 00:53:42 -05:00
|
|
|
self.item_ty_poly(span, name, ty, ast_util::empty_generics())
|
2013-05-17 08:51:25 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn attribute(&self, sp: Span, mi: P<ast::MetaItem>) -> ast::Attribute {
|
2013-07-19 06:51:37 -05:00
|
|
|
respan(sp, ast::Attribute_ {
|
2014-05-23 10:39:26 -05:00
|
|
|
id: attr::mk_attr_id(),
|
2015-10-01 11:03:34 -05:00
|
|
|
style: ast::AttrStyle::Outer,
|
2013-07-19 06:51:37 -05:00
|
|
|
value: mi,
|
|
|
|
is_sugared_doc: false,
|
|
|
|
})
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
2014-09-13 11:06:01 -05:00
|
|
|
fn meta_word(&self, sp: Span, w: InternedString) -> P<ast::MetaItem> {
|
|
|
|
P(respan(sp, ast::MetaWord(w)))
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
2014-01-08 12:35:15 -06:00
|
|
|
fn meta_list(&self,
|
|
|
|
sp: Span,
|
|
|
|
name: InternedString,
|
2014-09-13 11:06:01 -05:00
|
|
|
mis: Vec<P<ast::MetaItem>> )
|
|
|
|
-> P<ast::MetaItem> {
|
|
|
|
P(respan(sp, ast::MetaList(name, mis)))
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
2014-01-08 12:35:15 -06:00
|
|
|
fn meta_name_value(&self,
|
|
|
|
sp: Span,
|
|
|
|
name: InternedString,
|
|
|
|
value: ast::Lit_)
|
2014-09-13 11:06:01 -05:00
|
|
|
-> P<ast::MetaItem> {
|
|
|
|
P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use(&self, sp: Span,
|
|
|
|
vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
|
|
|
|
P(ast::Item {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
ident: special_idents::invalid,
|
|
|
|
attrs: vec![],
|
|
|
|
node: ast::ItemUse(vp),
|
2013-05-19 00:53:42 -05:00
|
|
|
vis: vis,
|
|
|
|
span: sp
|
2015-01-13 09:30:17 -06:00
|
|
|
})
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
|
2014-02-16 00:10:16 -06:00
|
|
|
let last = path.segments.last().unwrap().identifier;
|
2015-01-13 09:30:17 -06:00
|
|
|
self.item_use_simple_(sp, vis, last, path)
|
2014-02-16 00:10:16 -06:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
|
|
|
|
ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
|
|
|
|
self.item_use(sp, vis,
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp,
|
|
|
|
ast::ViewPathSimple(ident,
|
2015-01-13 09:30:17 -06:00
|
|
|
path))))
|
2014-02-16 00:10:16 -06:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use_list(&self, sp: Span, vis: ast::Visibility,
|
|
|
|
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
|
2014-03-28 14:42:34 -05:00
|
|
|
let imports = imports.iter().map(|id| {
|
2015-08-01 00:20:25 -05:00
|
|
|
respan(sp, ast::PathListIdent { name: *id, rename: None, id: ast::DUMMY_NODE_ID })
|
2014-03-28 14:42:34 -05:00
|
|
|
}).collect();
|
2013-05-17 09:19:28 -05:00
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
self.item_use(sp, vis,
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp,
|
|
|
|
ast::ViewPathList(self.path(sp, path),
|
2015-01-13 09:30:17 -06:00
|
|
|
imports))))
|
2013-05-17 09:19:28 -05:00
|
|
|
}
|
|
|
|
|
2015-01-13 09:30:17 -06:00
|
|
|
fn item_use_glob(&self, sp: Span,
|
|
|
|
vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
|
|
|
|
self.item_use(sp, vis,
|
2014-09-13 11:06:01 -05:00
|
|
|
P(respan(sp,
|
2015-01-13 09:30:17 -06:00
|
|
|
ast::ViewPathGlob(self.path(sp, path)))))
|
2013-08-29 14:10:02 -05:00
|
|
|
}
|
|
|
|
}
|