2019-02-06 17:33:01 +00:00
|
|
|
use crate::ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind};
|
|
|
|
use crate::attr;
|
|
|
|
use crate::source_map::{dummy_spanned, respan, Spanned};
|
|
|
|
use crate::ext::base::ExtCtxt;
|
|
|
|
use crate::ptr::P;
|
2019-05-22 04:41:15 +00:00
|
|
|
use crate::symbol::{kw, sym, Symbol};
|
2019-02-06 17:33:01 +00:00
|
|
|
use crate::ThinVec;
|
|
|
|
|
2018-04-25 16:30:39 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-05-22 04:41:15 +00:00
|
|
|
use syntax_pos::{Pos, Span};
|
2013-02-15 05:50:03 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
// Left so that Cargo tests don't break, this can be removed once those no longer use it
|
|
|
|
pub trait AstBuilder {}
|
2013-05-15 22:55:57 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
impl<'a> ExtCtxt<'a> {
|
|
|
|
pub fn path(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
|
2018-05-16 11:57:45 +00:00
|
|
|
self.path_all(span, false, strs, vec![], vec![])
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
|
2016-10-29 21:54:04 +00:00
|
|
|
self.path(span, vec![id])
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn path_global(&self, span: Span, strs: Vec<ast::Ident> ) -> ast::Path {
|
2018-05-16 11:57:45 +00:00
|
|
|
self.path_all(span, true, strs, vec![], vec![])
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn path_all(&self,
|
2017-07-23 17:50:56 +00:00
|
|
|
span: Span,
|
2013-05-19 05:53:42 +00:00
|
|
|
global: bool,
|
2014-02-28 21:09:09 +00:00
|
|
|
mut idents: Vec<ast::Ident> ,
|
2018-05-27 19:07:09 +00:00
|
|
|
args: Vec<ast::GenericArg>,
|
2019-02-28 22:43:53 +00:00
|
|
|
constraints: Vec<ast::AssocTyConstraint> )
|
2013-08-07 16:47:28 +00:00
|
|
|
-> ast::Path {
|
2018-12-02 12:15:42 +00:00
|
|
|
assert!(!idents.is_empty());
|
|
|
|
let add_root = global && !idents[0].is_path_segment_keyword();
|
|
|
|
let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
|
|
|
|
if add_root {
|
|
|
|
segments.push(ast::PathSegment::path_root(span));
|
|
|
|
}
|
2018-03-18 00:53:41 +00:00
|
|
|
let last_ident = idents.pop().unwrap();
|
2018-03-19 00:54:56 +00:00
|
|
|
segments.extend(idents.into_iter().map(|ident| {
|
|
|
|
ast::PathSegment::from_ident(ident.with_span_pos(span))
|
|
|
|
}));
|
2019-02-28 22:43:53 +00:00
|
|
|
let args = if !args.is_empty() || !constraints.is_empty() {
|
|
|
|
ast::AngleBracketedArgs { args, constraints, span }.into()
|
2016-12-10 06:45:58 +00:00
|
|
|
} else {
|
2017-07-23 16:32:36 +00:00
|
|
|
None
|
2016-12-10 06:45:58 +00:00
|
|
|
};
|
2018-08-31 00:01:26 +00:00
|
|
|
segments.push(ast::PathSegment {
|
|
|
|
ident: last_ident.with_span_pos(span),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
args,
|
|
|
|
});
|
2018-12-02 12:15:42 +00:00
|
|
|
ast::Path { span, segments }
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 19:00:28 +00:00
|
|
|
/// Constructs a qualified path.
|
|
|
|
///
|
2015-01-31 19:20:24 +00:00
|
|
|
/// Constructs a path like `<self_type as trait_path>::ident`.
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn qpath(&self,
|
2015-02-04 19:00:28 +00:00
|
|
|
self_type: P<ast::Ty>,
|
2015-01-31 19:20:24 +00:00
|
|
|
trait_path: ast::Path,
|
2018-03-18 13:47:09 +00:00
|
|
|
ident: ast::Ident)
|
2015-02-17 17:29:13 +00:00
|
|
|
-> (ast::QSelf, ast::Path) {
|
2018-02-08 08:58:13 +00:00
|
|
|
self.qpath_all(self_type, trait_path, ident, vec![], vec![])
|
2015-02-04 19:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a qualified path.
|
|
|
|
///
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Constructs a path like `<self_type as trait_path>::ident<'a, T, A = Bar>`.
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn qpath_all(&self,
|
2015-02-04 19:00:28 +00:00
|
|
|
self_type: P<ast::Ty>,
|
2015-01-31 19:20:24 +00:00
|
|
|
trait_path: ast::Path,
|
2018-03-18 13:47:09 +00:00
|
|
|
ident: ast::Ident,
|
2018-05-27 19:07:09 +00:00
|
|
|
args: Vec<ast::GenericArg>,
|
2019-02-28 22:43:53 +00:00
|
|
|
constraints: Vec<ast::AssocTyConstraint>)
|
2015-02-17 17:29:13 +00:00
|
|
|
-> (ast::QSelf, ast::Path) {
|
2015-01-31 19:20:24 +00:00
|
|
|
let mut path = trait_path;
|
2019-02-28 22:43:53 +00:00
|
|
|
let args = if !args.is_empty() || !constraints.is_empty() {
|
|
|
|
ast::AngleBracketedArgs { args, constraints, span: ident.span }.into()
|
2017-07-23 16:32:36 +00:00
|
|
|
} else {
|
|
|
|
None
|
2016-12-10 06:45:58 +00:00
|
|
|
};
|
2018-08-31 00:01:26 +00:00
|
|
|
path.segments.push(ast::PathSegment { ident, id: ast::DUMMY_NODE_ID, args });
|
2015-02-04 19:00:28 +00:00
|
|
|
|
2015-02-17 17:29:13 +00:00
|
|
|
(ast::QSelf {
|
|
|
|
ty: self_type,
|
2018-05-22 19:26:35 +00:00
|
|
|
path_span: path.span,
|
2015-02-17 17:29:13 +00:00
|
|
|
position: path.segments.len() - 1
|
|
|
|
}, path)
|
2015-02-04 19:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::MutTy {
|
2017-08-07 05:54:09 +00:00
|
|
|
ty,
|
|
|
|
mutbl,
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty> {
|
2013-11-30 22:00:39 +00:00
|
|
|
P(ast::Ty {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2013-05-19 05:53:42 +00:00
|
|
|
node: ty
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
|
2016-02-08 15:53:21 +00:00
|
|
|
self.ty(path.span, ast::TyKind::Path(None, path))
|
2014-11-20 20:08:48 +00:00
|
|
|
}
|
|
|
|
|
2013-06-17 19:16:30 +00:00
|
|
|
// Might need to take bounds as an argument in the future, if you ever want
|
|
|
|
// to generate a bounded existential trait type.
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_ident(&self, span: Span, ident: ast::Ident)
|
2013-11-30 22:00:39 +00:00
|
|
|
-> P<ast::Ty> {
|
2014-11-20 20:08:48 +00:00
|
|
|
self.ty_path(self.path_ident(span, ident))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
|
2019-02-05 15:50:00 +00:00
|
|
|
ast::AnonConst {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
value: P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: expr,
|
|
|
|
span,
|
|
|
|
attrs: ThinVec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
|
2019-02-05 15:50:00 +00:00
|
|
|
self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_rptr(&self,
|
2013-08-31 16:13:04 +00:00
|
|
|
span: Span,
|
2013-11-30 22:00:39 +00:00
|
|
|
ty: P<ast::Ty>,
|
2013-07-05 12:33:52 +00:00
|
|
|
lifetime: Option<ast::Lifetime>,
|
2013-09-02 01:45:37 +00:00
|
|
|
mutbl: ast::Mutability)
|
2013-11-30 22:00:39 +00:00
|
|
|
-> P<ast::Ty> {
|
2013-05-19 05:53:42 +00:00
|
|
|
self.ty(span,
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2013-12-31 20:55:39 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_ptr(&self,
|
2014-08-27 13:19:17 +00:00
|
|
|
span: Span,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability)
|
|
|
|
-> P<ast::Ty> {
|
|
|
|
self.ty(span,
|
2016-02-08 15:53:21 +00:00
|
|
|
ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
|
2014-08-27 13:19:17 +00:00
|
|
|
}
|
2013-12-31 20:55:39 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn ty_infer(&self, span: Span) -> P<ast::Ty> {
|
2016-02-08 15:53:21 +00:00
|
|
|
self.ty(span, ast::TyKind::Infer)
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn typaram(&self,
|
2014-04-03 00:53:57 +00:00
|
|
|
span: Span,
|
2018-03-19 00:54:56 +00:00
|
|
|
ident: ast::Ident,
|
2016-05-17 16:51:45 +00:00
|
|
|
attrs: Vec<ast::Attribute>,
|
2018-06-14 11:08:58 +00:00
|
|
|
bounds: ast::GenericBounds,
|
2018-05-27 19:07:09 +00:00
|
|
|
default: Option<P<ast::Ty>>) -> ast::GenericParam {
|
|
|
|
ast::GenericParam {
|
2018-03-19 00:54:56 +00:00
|
|
|
ident: ident.with_span_pos(span),
|
2014-01-30 17:28:02 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-05-17 16:51:45 +00:00
|
|
|
attrs: attrs.into(),
|
2018-05-28 12:33:28 +00:00
|
|
|
bounds,
|
2018-05-27 19:07:09 +00:00
|
|
|
kind: ast::GenericParamKind::Type {
|
2018-05-26 18:16:21 +00:00
|
|
|
default,
|
|
|
|
}
|
2014-01-30 17:28:02 +00:00
|
|
|
}
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
|
2014-01-09 13:05:33 +00:00
|
|
|
ast::TraitRef {
|
2017-08-07 05:54:09 +00:00
|
|
|
path,
|
2014-09-05 19:21:02 +00:00
|
|
|
ref_id: ast::DUMMY_NODE_ID,
|
2014-11-07 11:53:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
|
2014-11-07 11:53:45 +00:00
|
|
|
ast::PolyTraitRef {
|
2017-10-16 19:07:26 +00:00
|
|
|
bound_generic_params: Vec::new(),
|
2015-02-05 08:46:01 +00:00
|
|
|
trait_ref: self.trait_ref(path),
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
|
2018-06-14 11:23:46 +00:00
|
|
|
ast::GenericBound::Trait(self.poly_trait_ref(path.span, path),
|
|
|
|
ast::TraitBoundModifier::None)
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
|
2018-03-19 00:54:56 +00:00
|
|
|
ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lifetime_def(&self,
|
2014-08-06 02:59:24 +00:00
|
|
|
span: Span,
|
2017-03-25 21:14:18 +00:00
|
|
|
ident: ast::Ident,
|
2016-05-17 16:51:45 +00:00
|
|
|
attrs: Vec<ast::Attribute>,
|
2018-06-14 11:08:58 +00:00
|
|
|
bounds: ast::GenericBounds)
|
2018-05-27 19:07:09 +00:00
|
|
|
-> ast::GenericParam {
|
2018-05-26 18:16:21 +00:00
|
|
|
let lifetime = self.lifetime(span, ident);
|
2018-05-27 19:07:09 +00:00
|
|
|
ast::GenericParam {
|
2018-05-26 18:16:21 +00:00
|
|
|
ident: lifetime.ident,
|
|
|
|
id: lifetime.id,
|
2016-05-17 16:51:45 +00:00
|
|
|
attrs: attrs.into(),
|
2018-05-28 12:33:28 +00:00
|
|
|
bounds,
|
2018-05-30 15:49:39 +00:00
|
|
|
kind: ast::GenericParamKind::Lifetime,
|
2014-08-06 02:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
|
2016-06-26 02:19:34 +00:00
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: expr.span,
|
|
|
|
node: ast::StmtKind::Expr(expr),
|
|
|
|
}
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_semi(&self, expr: P<ast::Expr>) -> ast::Stmt {
|
2016-06-17 02:30:01 +00:00
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
span: expr.span,
|
|
|
|
node: ast::StmtKind::Semi(expr),
|
|
|
|
}
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: ast::Ident,
|
2016-02-11 20:33:09 +00:00
|
|
|
ex: P<ast::Expr>) -> ast::Stmt {
|
2013-10-20 12:31:23 +00:00
|
|
|
let pat = if mutbl {
|
2016-02-09 16:44:47 +00:00
|
|
|
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
|
|
|
|
self.pat_ident_binding_mode(sp, ident, binding_mode)
|
2013-10-20 12:31:23 +00:00
|
|
|
} else {
|
|
|
|
self.pat_ident(sp, ident)
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
let local = P(ast::Local {
|
2017-08-07 05:54:09 +00:00
|
|
|
pat,
|
2015-01-02 11:55:31 +00:00
|
|
|
ty: None,
|
2013-07-19 05:38:55 +00:00
|
|
|
init: Some(ex),
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-07-19 05:38:55 +00:00
|
|
|
span: sp,
|
2018-08-05 10:04:56 +00:00
|
|
|
attrs: ThinVec::new(),
|
2014-09-13 16:06:01 +00:00
|
|
|
});
|
2016-06-17 02:30:01 +00:00
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Local(local),
|
|
|
|
span: sp,
|
|
|
|
}
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_let_typed(&self,
|
2013-08-31 16:13:04 +00:00
|
|
|
sp: Span,
|
2013-08-08 18:38:10 +00:00
|
|
|
mutbl: bool,
|
2013-09-02 00:50:59 +00:00
|
|
|
ident: ast::Ident,
|
2013-11-30 22:00:39 +00:00
|
|
|
typ: P<ast::Ty>,
|
2014-09-13 16:06:01 +00:00
|
|
|
ex: P<ast::Expr>)
|
2016-08-21 16:57:43 +00:00
|
|
|
-> ast::Stmt {
|
2013-10-20 12:31:23 +00:00
|
|
|
let pat = if mutbl {
|
2016-02-09 16:44:47 +00:00
|
|
|
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mutable);
|
|
|
|
self.pat_ident_binding_mode(sp, ident, binding_mode)
|
2013-10-20 12:31:23 +00:00
|
|
|
} else {
|
|
|
|
self.pat_ident(sp, ident)
|
|
|
|
};
|
2014-09-13 16:06:01 +00:00
|
|
|
let local = P(ast::Local {
|
2017-08-07 05:54:09 +00:00
|
|
|
pat,
|
2015-01-02 11:55:31 +00:00
|
|
|
ty: Some(typ),
|
2013-08-08 18:38:10 +00:00
|
|
|
init: Some(ex),
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-08-08 18:38:10 +00:00
|
|
|
span: sp,
|
2018-08-05 10:04:56 +00:00
|
|
|
attrs: ThinVec::new(),
|
2014-09-13 16:06:01 +00:00
|
|
|
});
|
2016-08-21 16:57:43 +00:00
|
|
|
ast::Stmt {
|
2016-06-17 02:30:01 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Local(local),
|
|
|
|
span: sp,
|
2016-08-21 16:57:43 +00:00
|
|
|
}
|
2013-08-08 18:38:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// Generates `let _: Type;`, which is usually used for type assertions.
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
|
2016-08-26 16:23:42 +00:00
|
|
|
let local = P(ast::Local {
|
|
|
|
pat: self.pat_wild(span),
|
|
|
|
ty: Some(ty),
|
|
|
|
init: None,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2018-08-05 10:04:56 +00:00
|
|
|
attrs: ThinVec::new(),
|
2016-08-26 16:23:42 +00:00
|
|
|
});
|
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Local(local),
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2016-08-26 16:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
|
2016-06-17 02:30:01 +00:00
|
|
|
ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
node: ast::StmtKind::Item(item),
|
|
|
|
span: sp,
|
|
|
|
}
|
2014-06-10 20:54:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
|
2016-06-26 02:19:34 +00:00
|
|
|
self.block(expr.span, vec![ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-06-23 09:51:18 +00:00
|
|
|
span: expr.span,
|
2016-06-26 02:19:34 +00:00
|
|
|
node: ast::StmtKind::Expr(expr),
|
2016-06-23 09:51:18 +00:00
|
|
|
}])
|
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
|
2016-06-23 09:51:18 +00:00
|
|
|
P(ast::Block {
|
2017-08-07 05:54:09 +00:00
|
|
|
stmts,
|
2016-06-23 09:51:18 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: BlockCheckMode::Default,
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2016-06-23 09:51:18 +00:00
|
|
|
})
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr(&self, span: Span, node: ast::ExprKind) -> P<ast::Expr> {
|
2014-09-13 16:06:01 +00:00
|
|
|
P(ast::Expr {
|
2013-09-07 02:11:55 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-08-07 05:54:09 +00:00
|
|
|
node,
|
|
|
|
span,
|
2018-08-05 10:04:56 +00:00
|
|
|
attrs: ThinVec::new(),
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(path.span, ast::ExprKind::Path(None, path))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Constructs a `QPath` expression.
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_qpath(&self, span: Span, qself: ast::QSelf, path: ast::Path) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Path(Some(qself), path))
|
2015-02-04 19:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
|
2013-05-19 05:53:42 +00:00
|
|
|
self.expr_path(self.path_ident(span, id))
|
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_self(&self, span: Span) -> P<ast::Expr> {
|
2019-08-10 23:20:18 +00:00
|
|
|
self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_binary(&self, sp: Span, op: ast::BinOpKind,
|
2014-09-13 16:06:01 +00:00
|
|
|
lhs: P<ast::Expr>, rhs: P<ast::Expr>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2016-02-08 12:21:29 +00:00
|
|
|
self.expr_unary(sp, UnOp::Deref, e)
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_unary(&self, sp: Span, op: ast::UnOp, e: P<ast::Expr>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Unary(op, e))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_field_access(
|
|
|
|
&self, sp: Span, expr: P<ast::Expr>, ident: ast::Ident,
|
|
|
|
) -> P<ast::Expr> {
|
2018-03-18 13:47:09 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Field(expr, ident.with_span_pos(sp)))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_tup_field_access(&self, sp: Span, expr: P<ast::Expr>, idx: usize) -> P<ast::Expr> {
|
2018-04-05 00:20:21 +00:00
|
|
|
let ident = Ident::from_str(&idx.to_string()).with_span_pos(sp);
|
|
|
|
self.expr(sp, ast::ExprKind::Field(expr, ident))
|
2014-08-10 03:54:33 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2016-02-09 16:44:47 +00:00
|
|
|
self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Immutable, e))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_mut_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
|
2016-02-09 16:44:47 +00:00
|
|
|
self.expr(sp, ast::ExprKind::AddrOf(ast::Mutability::Mutable, e))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_call(
|
|
|
|
&self, span: Span, expr: P<ast::Expr>, args: Vec<P<ast::Expr>>,
|
|
|
|
) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Call(expr, args))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_call_ident(&self, span: Span, id: ast::Ident,
|
2014-09-13 16:06:01 +00:00
|
|
|
args: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_call_global(&self, sp: Span, fn_path: Vec<ast::Ident> ,
|
2014-09-13 16:06:01 +00:00
|
|
|
args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
|
2013-05-19 05:53:42 +00:00
|
|
|
let pathexpr = self.expr_path(self.path_global(sp, fn_path));
|
|
|
|
self.expr_call(sp, pathexpr, args)
|
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_method_call(&self, span: Span,
|
2014-09-13 16:06:01 +00:00
|
|
|
expr: P<ast::Expr>,
|
2013-09-02 00:50:59 +00:00
|
|
|
ident: ast::Ident,
|
2014-09-13 16:06:01 +00:00
|
|
|
mut args: Vec<P<ast::Expr>> ) -> P<ast::Expr> {
|
2014-10-15 06:05:01 +00:00
|
|
|
args.insert(0, expr);
|
2018-03-19 00:54:56 +00:00
|
|
|
let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
|
|
|
|
self.expr(span, ast::ExprKind::MethodCall(segment, args))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
|
2018-04-16 03:44:39 +00:00
|
|
|
self.expr(b.span, ast::ExprKind::Block(b, None))
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
|
2017-01-04 03:13:01 +00:00
|
|
|
ast::Field {
|
2018-03-18 13:47:09 +00:00
|
|
|
ident: ident.with_span_pos(span),
|
2017-01-04 03:13:01 +00:00
|
|
|
expr: e,
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2017-01-04 03:13:01 +00:00
|
|
|
is_shorthand: false,
|
2018-08-05 10:04:56 +00:00
|
|
|
attrs: ThinVec::new(),
|
2019-08-14 01:22:51 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-01-04 03:13:01 +00:00
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_struct(
|
|
|
|
&self, span: Span, path: ast::Path, fields: Vec<ast::Field>
|
|
|
|
) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Struct(path, fields, None))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_struct_ident(&self, span: Span,
|
2014-09-13 16:06:01 +00:00
|
|
|
id: ast::Ident, fields: Vec<ast::Field>) -> P<ast::Expr> {
|
2013-05-19 05:53:42 +00:00
|
|
|
self.expr_struct(span, self.path_ident(span, id), fields)
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
|
2019-05-10 00:00:51 +00:00
|
|
|
let lit = ast::Lit::from_lit_kind(lit_kind, span);
|
|
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
|
2016-08-23 00:56:52 +00:00
|
|
|
self.expr_lit(span, ast::LitKind::Int(i as u128,
|
2018-01-04 01:12:04 +00:00
|
|
|
ast::LitIntType::Unsigned(ast::UintTy::Usize)))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_isize(&self, sp: Span, i: isize) -> P<ast::Expr> {
|
2016-02-11 08:52:55 +00:00
|
|
|
if i < 0 {
|
2016-08-23 00:56:52 +00:00
|
|
|
let i = (-i) as u128;
|
2018-01-04 01:12:04 +00:00
|
|
|
let lit_ty = ast::LitIntType::Signed(ast::IntTy::Isize);
|
2016-02-08 16:06:20 +00:00
|
|
|
let lit = self.expr_lit(sp, ast::LitKind::Int(i, lit_ty));
|
2016-02-11 08:52:55 +00:00
|
|
|
self.expr_unary(sp, ast::UnOp::Neg, lit)
|
|
|
|
} else {
|
2016-08-23 00:56:52 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Int(i as u128,
|
2018-01-04 01:12:04 +00:00
|
|
|
ast::LitIntType::Signed(ast::IntTy::Isize)))
|
2016-02-11 08:52:55 +00:00
|
|
|
}
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
|
2016-08-23 00:56:52 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Int(u as u128,
|
|
|
|
ast::LitIntType::Unsigned(ast::UintTy::U32)))
|
2015-02-23 03:07:38 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_u16(&self, sp: Span, u: u16) -> P<ast::Expr> {
|
2018-04-12 09:50:53 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Int(u as u128,
|
|
|
|
ast::LitIntType::Unsigned(ast::UintTy::U16)))
|
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_u8(&self, sp: Span, u: u8) -> P<ast::Expr> {
|
2016-08-23 00:56:52 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U8)))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
|
2016-02-08 16:06:20 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Bool(value))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2017-01-16 07:36:10 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Array(exprs))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
self.expr_call_global(sp, self.std_path(&[sym::vec, sym::Vec, sym::new]),
|
2014-02-28 20:54:01 +00:00
|
|
|
Vec::new())
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub 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 12:20:11 +00:00
|
|
|
self.expr_addr_of(sp, self.expr_vec(sp, exprs))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
|
2016-02-09 17:01:08 +00:00
|
|
|
self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Cast(expr, ty))
|
2013-09-17 04:12:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
|
2016-10-29 21:54:04 +00:00
|
|
|
self.expr_call_global(sp, some, vec![expr])
|
2013-09-17 04:12:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_none(&self, sp: Span) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let none = self.std_path(&[sym::option, sym::Option, sym::None]);
|
2015-07-30 00:01:14 +00:00
|
|
|
let none = self.path_global(sp, none);
|
2013-09-17 04:12:18 +00:00
|
|
|
self.expr_path(none)
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_break(&self, sp: Span) -> P<ast::Expr> {
|
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, `loop` loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop. `while` loops are unchanged.
- `break` respects contexts in which expressions may not end with braced
blocks. That is, `while break { break-value } { while-body }` is
illegal; this preserves backwards compatibility.
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
2016-10-29 22:15:06 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Break(None, None))
|
2014-10-03 03:28:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(sp, ast::ExprKind::Tup(exprs))
|
2014-07-01 16:39:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
|
2018-08-18 10:14:09 +00:00
|
|
|
let loc = self.source_map().lookup_char_pos(span.lo());
|
2017-12-14 07:09:19 +00:00
|
|
|
let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
|
2015-04-11 09:46:57 +00:00
|
|
|
let expr_line = self.expr_u32(span, loc.line as u32);
|
2017-06-27 02:26:52 +00:00
|
|
|
let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
|
|
|
|
let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
|
|
|
|
let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
|
2013-05-19 05:53:42 +00:00
|
|
|
self.expr_call_global(
|
|
|
|
span,
|
2019-08-10 15:38:27 +00:00
|
|
|
[sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
|
2016-10-29 21:54:04 +00:00
|
|
|
vec![
|
2013-09-17 14:45:49 +00:00
|
|
|
self.expr_str(span, msg),
|
2017-06-27 02:26:52 +00:00
|
|
|
expr_loc_ptr])
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
|
2016-11-16 10:52:37 +00:00
|
|
|
self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
|
2013-09-17 14:45:49 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
|
2016-10-29 21:54:04 +00:00
|
|
|
self.expr_call_global(sp, ok, vec![expr])
|
2014-03-18 17:58:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
2016-10-29 21:54:04 +00:00
|
|
|
self.expr_call_global(sp, err, vec![expr])
|
2014-03-18 17:58:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
|
2014-09-07 21:57:26 +00:00
|
|
|
let ok_path = self.path_global(sp, ok);
|
2019-05-22 04:41:15 +00:00
|
|
|
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
2014-09-07 21:57:26 +00:00
|
|
|
let err_path = self.path_global(sp, err);
|
2014-03-18 17:58:26 +00: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);
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// `Ok(__try_var)` pattern
|
2016-08-15 18:28:17 +00:00
|
|
|
let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
|
2014-03-18 17:58:26 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// `Err(__try_var)` (pattern and expression respectively)
|
2016-08-15 18:28:17 +00:00
|
|
|
let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
|
2014-09-07 21:57:26 +00:00
|
|
|
let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
|
2016-10-29 21:54:04 +00:00
|
|
|
vec![binding_expr.clone()]);
|
2019-02-28 22:43:53 +00:00
|
|
|
// `return Err(__try_var)`
|
2016-02-08 15:05:05 +00:00
|
|
|
let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
|
2014-03-18 17:58:26 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// `Ok(__try_var) => __try_var`
|
2019-08-27 23:16:35 +00:00
|
|
|
let ok_arm = self.arm(sp, ok_pat, binding_expr);
|
2019-02-28 22:43:53 +00:00
|
|
|
// `Err(__try_var) => return Err(__try_var)`
|
2019-08-27 23:16:35 +00:00
|
|
|
let err_arm = self.arm(sp, err_pat, err_expr);
|
2014-03-18 17:58:26 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// `match head { Ok() => ..., Err() => ... }`
|
2016-10-29 21:54:04 +00:00
|
|
|
self.expr_match(sp, head, vec![ok_arm, err_arm])
|
2014-03-18 17:58:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 05:53:42 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat(&self, span: Span, pat: PatKind) -> P<ast::Pat> {
|
2019-06-25 21:22:45 +00:00
|
|
|
P(ast::Pat { id: ast::DUMMY_NODE_ID, node: pat, span })
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
|
2016-02-11 18:16:33 +00:00
|
|
|
self.pat(span, PatKind::Wild)
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
|
2016-02-11 18:16:33 +00:00
|
|
|
self.pat(span, PatKind::Lit(expr))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
|
2016-02-09 16:44:47 +00:00
|
|
|
let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Immutable);
|
|
|
|
self.pat_ident_binding_mode(span, ident, binding_mode)
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_ident_binding_mode(&self,
|
2013-08-31 16:13:04 +00:00
|
|
|
span: Span,
|
2013-09-02 00:50:59 +00:00
|
|
|
ident: ast::Ident,
|
2014-09-13 16:06:01 +00:00
|
|
|
bm: ast::BindingMode) -> P<ast::Pat> {
|
2018-03-18 13:47:09 +00:00
|
|
|
let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
|
2013-05-19 05:53:42 +00:00
|
|
|
self.pat(span, pat)
|
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat(span, PatKind::Path(None, path))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_tuple_struct(&self, span: Span, path: ast::Path,
|
2016-08-15 18:28:17 +00:00
|
|
|
subpats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
|
2019-07-09 07:25:18 +00:00
|
|
|
self.pat(span, PatKind::TupleStruct(path, subpats))
|
2016-08-15 18:28:17 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_struct(&self, span: Span, path: ast::Path,
|
2019-08-14 23:35:36 +00:00
|
|
|
field_pats: Vec<ast::FieldPat>) -> P<ast::Pat> {
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat(span, PatKind::Struct(path, field_pats, false))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
|
2019-07-09 07:25:18 +00:00
|
|
|
self.pat(span, PatKind::Tuple(pats))
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
|
2014-07-27 22:11:02 +00:00
|
|
|
let path = self.path_global(span, some);
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat_tuple_struct(span, path, vec![pat])
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_none(&self, span: Span) -> P<ast::Pat> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let some = self.std_path(&[sym::option, sym::Option, sym::None]);
|
2014-07-27 22:11:02 +00:00
|
|
|
let path = self.path_global(span, some);
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat_path(span, path)
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
|
2014-07-27 22:11:02 +00:00
|
|
|
let path = self.path_global(span, some);
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat_tuple_struct(span, path, vec![pat])
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
|
2019-05-22 04:41:15 +00:00
|
|
|
let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
2014-07-27 22:11:02 +00:00
|
|
|
let path = self.path_global(span, some);
|
2016-08-15 18:28:17 +00:00
|
|
|
self.pat_tuple_struct(span, path, vec![pat])
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
2013-05-19 05:53:42 +00:00
|
|
|
|
2019-08-27 23:16:35 +00:00
|
|
|
pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
|
2013-09-02 01:45:37 +00:00
|
|
|
ast::Arm {
|
2016-10-29 21:54:04 +00:00
|
|
|
attrs: vec![],
|
2019-08-27 23:16:35 +00:00
|
|
|
pat,
|
2013-05-19 05:53:42 +00:00
|
|
|
guard: None,
|
2017-08-26 22:09:31 +00:00
|
|
|
body: expr,
|
2019-03-30 22:54:29 +00:00
|
|
|
span,
|
2019-08-14 01:22:51 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
|
2019-08-27 23:16:35 +00:00
|
|
|
self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Match(arg, arms))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_if(&self, span: Span, cond: P<ast::Expr>,
|
2014-09-13 16:06:01 +00:00
|
|
|
then: P<ast::Expr>, els: Option<P<ast::Expr>>) -> P<ast::Expr> {
|
2013-09-20 06:08:47 +00:00
|
|
|
let els = els.map(|x| self.expr_block(self.block_expr(x)));
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn expr_loop(&self, span: Span, block: P<ast::Block>) -> P<ast::Expr> {
|
2016-02-08 15:05:05 +00:00
|
|
|
self.expr(span, ast::ExprKind::Loop(block, None))
|
2014-07-27 22:11:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda_fn_decl(&self,
|
2016-04-20 18:44:07 +00:00
|
|
|
span: Span,
|
|
|
|
fn_decl: P<ast::FnDecl>,
|
2016-10-25 23:17:29 +00:00
|
|
|
body: P<ast::Expr>,
|
2016-04-20 18:44:07 +00:00
|
|
|
fn_decl_span: Span) // span of the `|...|` part
|
|
|
|
-> P<ast::Expr> {
|
|
|
|
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
|
2018-06-06 22:50:59 +00:00
|
|
|
ast::IsAsync::NotAsync,
|
2017-10-07 14:36:28 +00:00
|
|
|
ast::Movability::Movable,
|
2016-04-20 18:44:07 +00:00
|
|
|
fn_decl,
|
2016-10-25 23:17:29 +00:00
|
|
|
body,
|
2016-04-20 18:44:07 +00:00
|
|
|
fn_decl_span))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2016-04-20 18:44:07 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda(&self,
|
2016-04-20 18:44:07 +00:00
|
|
|
span: Span,
|
|
|
|
ids: Vec<ast::Ident>,
|
2016-10-25 23:17:29 +00:00
|
|
|
body: P<ast::Expr>)
|
2016-04-20 18:44:07 +00:00
|
|
|
-> P<ast::Expr> {
|
2013-05-19 05:53:42 +00:00
|
|
|
let fn_decl = self.fn_decl(
|
2019-08-27 11:24:32 +00:00
|
|
|
ids.iter().map(|id| self.param(span, *id, self.ty_infer(span))).collect(),
|
2018-03-22 15:55:57 +00:00
|
|
|
ast::FunctionRetTy::Default(span));
|
2013-05-19 05:53:42 +00:00
|
|
|
|
2016-04-20 18:44:07 +00:00
|
|
|
// FIXME -- We are using `span` as the span of the `|...|`
|
|
|
|
// part of the lambda, but it probably (maybe?) corresponds to
|
|
|
|
// the entire lambda body. Probably we should extend the API
|
|
|
|
// here, but that's not entirely clear.
|
2017-10-07 14:36:28 +00:00
|
|
|
self.expr(span, ast::ExprKind::Closure(ast::CaptureBy::Ref,
|
2018-06-06 22:50:59 +00:00
|
|
|
ast::IsAsync::NotAsync,
|
2017-10-07 14:36:28 +00:00
|
|
|
ast::Movability::Movable,
|
|
|
|
fn_decl,
|
|
|
|
body,
|
|
|
|
span))
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
|
2016-10-25 23:17:29 +00:00
|
|
|
self.lambda(span, Vec::new(), body)
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2013-05-15 22:55:57 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
|
2016-10-25 23:17:29 +00:00
|
|
|
self.lambda(span, vec![ident], body)
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda_stmts(&self,
|
2014-02-28 20:54:01 +00:00
|
|
|
span: Span,
|
|
|
|
ids: Vec<ast::Ident>,
|
2016-02-11 20:33:09 +00:00
|
|
|
stmts: Vec<ast::Stmt>)
|
2014-09-13 16:06:01 +00:00
|
|
|
-> P<ast::Expr> {
|
2016-10-25 23:17:29 +00:00
|
|
|
self.lambda(span, ids, self.expr_block(self.block(span, stmts)))
|
2013-05-19 05:53:42 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda_stmts_0(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Expr> {
|
2016-10-25 23:17:29 +00:00
|
|
|
self.lambda0(span, self.expr_block(self.block(span, stmts)))
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn lambda_stmts_1(&self, span: Span, stmts: Vec<ast::Stmt>,
|
2014-09-13 16:06:01 +00:00
|
|
|
ident: ast::Ident) -> P<ast::Expr> {
|
2016-10-25 23:17:29 +00:00
|
|
|
self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:24:32 +00:00
|
|
|
pub fn param(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Param {
|
2013-05-19 05:53:42 +00:00
|
|
|
let arg_pat = self.pat_ident(span, ident);
|
2019-08-27 11:24:32 +00:00
|
|
|
ast::Param {
|
2019-06-09 10:58:40 +00:00
|
|
|
attrs: ThinVec::default(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2013-05-19 05:53:42 +00:00
|
|
|
pat: arg_pat,
|
2019-07-26 22:52:37 +00:00
|
|
|
span,
|
2019-06-09 10:58:40 +00:00
|
|
|
ty,
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
2013-05-15 22:55:57 +00:00
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
// FIXME: unused `self`
|
2019-08-27 11:24:32 +00:00
|
|
|
pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
|
2014-01-09 13:05:33 +00:00
|
|
|
P(ast::FnDecl {
|
2017-08-07 05:54:09 +00:00
|
|
|
inputs,
|
2018-03-22 15:55:57 +00:00
|
|
|
output,
|
2019-02-08 17:30:42 +00:00
|
|
|
c_variadic: false
|
2013-11-30 22:00:39 +00:00
|
|
|
})
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item(&self, span: Span, name: Ident,
|
2016-02-09 10:36:51 +00:00
|
|
|
attrs: Vec<ast::Attribute>, node: ast::ItemKind) -> P<ast::Item> {
|
2014-01-26 08:43:42 +00:00
|
|
|
// FIXME: Would be nice if our generated code didn't violate
|
2013-05-17 13:51:25 +00:00
|
|
|
// Rust coding conventions
|
2014-09-13 16:06:01 +00:00
|
|
|
P(ast::Item {
|
|
|
|
ident: name,
|
2017-08-07 05:54:09 +00:00
|
|
|
attrs,
|
2014-09-13 16:06:01 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2017-08-07 05:54:09 +00:00
|
|
|
node,
|
2018-03-10 14:45:47 +00:00
|
|
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2017-07-11 00:44:46 +00:00
|
|
|
tokens: None,
|
2014-09-13 16:06:01 +00:00
|
|
|
})
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_fn_poly(&self,
|
2013-08-31 16:13:04 +00:00
|
|
|
span: Span,
|
2013-09-02 00:50:59 +00:00
|
|
|
name: Ident,
|
2019-08-27 11:24:32 +00:00
|
|
|
inputs: Vec<ast::Param> ,
|
2013-11-30 22:00:39 +00:00
|
|
|
output: P<ast::Ty>,
|
2013-05-17 13:51:25 +00:00
|
|
|
generics: Generics,
|
2014-09-13 16:06:01 +00:00
|
|
|
body: P<ast::Block>) -> P<ast::Item> {
|
2013-05-19 05:53:42 +00:00
|
|
|
self.item(span,
|
|
|
|
name,
|
2014-02-28 21:09:09 +00:00
|
|
|
Vec::new(),
|
2018-03-22 15:55:57 +00:00
|
|
|
ast::ItemKind::Fn(self.fn_decl(inputs, ast::FunctionRetTy::Ty(output)),
|
2018-05-17 05:55:18 +00:00
|
|
|
ast::FnHeader {
|
|
|
|
unsafety: ast::Unsafety::Normal,
|
2019-02-23 18:39:27 +00:00
|
|
|
asyncness: dummy_spanned(ast::IsAsync::NotAsync),
|
2018-05-17 05:55:18 +00:00
|
|
|
constness: dummy_spanned(ast::Constness::NotConst),
|
|
|
|
abi: Abi::Rust,
|
|
|
|
},
|
2014-01-09 13:05:33 +00:00
|
|
|
generics,
|
|
|
|
body))
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_fn(&self,
|
2013-08-31 16:13:04 +00:00
|
|
|
span: Span,
|
2013-09-02 00:50:59 +00:00
|
|
|
name: Ident,
|
2019-08-27 11:24:32 +00:00
|
|
|
inputs: Vec<ast::Param> ,
|
2013-11-30 22:00:39 +00:00
|
|
|
output: P<ast::Ty>,
|
|
|
|
body: P<ast::Block>
|
2014-09-13 16:06:01 +00:00
|
|
|
) -> P<ast::Item> {
|
2013-05-17 13:51:25 +00:00
|
|
|
self.item_fn_poly(
|
2013-05-19 05:53:42 +00:00
|
|
|
span,
|
2013-05-17 13:51:25 +00:00
|
|
|
name,
|
|
|
|
inputs,
|
|
|
|
output,
|
2015-11-28 19:02:07 +00:00
|
|
|
Generics::default(),
|
2013-05-19 05:53:42 +00:00
|
|
|
body)
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>> ) -> ast::Variant {
|
2015-10-02 00:53:28 +00:00
|
|
|
let fields: Vec<_> = tys.into_iter().map(|ty| {
|
2016-04-06 08:19:10 +00:00
|
|
|
ast::StructField {
|
|
|
|
span: ty.span,
|
2017-08-07 05:54:09 +00:00
|
|
|
ty,
|
2016-04-02 13:47:53 +00:00
|
|
|
ident: None,
|
2018-03-10 14:45:47 +00:00
|
|
|
vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
2015-10-01 15:47:27 +00:00
|
|
|
attrs: Vec::new(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-04-06 08:19:10 +00:00
|
|
|
}
|
2013-07-06 04:57:11 +00:00
|
|
|
}).collect();
|
2013-05-17 13:51:25 +00:00
|
|
|
|
2015-10-10 00:28:40 +00:00
|
|
|
let vdata = if fields.is_empty() {
|
|
|
|
ast::VariantData::Unit(ast::DUMMY_NODE_ID)
|
2015-10-08 20:45:46 +00:00
|
|
|
} else {
|
2015-10-10 00:28:40 +00:00
|
|
|
ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
|
2015-10-08 20:45:46 +00:00
|
|
|
};
|
2015-10-02 00:53:28 +00:00
|
|
|
|
2019-08-14 00:40:21 +00:00
|
|
|
ast::Variant {
|
|
|
|
attrs: Vec::new(),
|
|
|
|
data: vdata,
|
|
|
|
disr_expr: None,
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
ident,
|
|
|
|
span,
|
|
|
|
}
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_enum_poly(&self, span: Span, name: Ident,
|
2014-01-09 13:05:33 +00:00
|
|
|
enum_definition: ast::EnumDef,
|
2014-09-13 16:06:01 +00:00
|
|
|
generics: Generics) -> P<ast::Item> {
|
2016-02-09 10:36:51 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::Enum(enum_definition, generics))
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_enum(&self, span: Span, name: Ident,
|
2014-09-13 16:06:01 +00:00
|
|
|
enum_definition: ast::EnumDef) -> P<ast::Item> {
|
2013-05-19 05:53:42 +00:00
|
|
|
self.item_enum_poly(span, name, enum_definition,
|
2015-11-28 19:02:07 +00:00
|
|
|
Generics::default())
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_struct(&self, span: Span, name: Ident,
|
2015-10-08 00:20:57 +00:00
|
|
|
struct_def: ast::VariantData) -> P<ast::Item> {
|
2013-05-17 13:51:25 +00:00
|
|
|
self.item_struct_poly(
|
|
|
|
span,
|
2013-05-19 05:53:42 +00:00
|
|
|
name,
|
2013-05-17 13:51:25 +00:00
|
|
|
struct_def,
|
2015-11-28 19:02:07 +00:00
|
|
|
Generics::default()
|
2013-05-17 13:51:25 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_struct_poly(&self, span: Span, name: Ident,
|
2015-10-08 00:20:57 +00:00
|
|
|
struct_def: ast::VariantData, generics: Generics) -> P<ast::Item> {
|
2016-02-09 10:36:51 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::Struct(struct_def, generics))
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
|
2015-01-13 15:30:17 +00:00
|
|
|
attrs: Vec<ast::Attribute>,
|
|
|
|
items: Vec<P<ast::Item>>) -> P<ast::Item> {
|
2013-05-17 13:51:25 +00:00
|
|
|
self.item(
|
|
|
|
span,
|
2013-05-19 05:53:42 +00:00
|
|
|
name,
|
|
|
|
attrs,
|
2016-02-09 10:36:51 +00:00
|
|
|
ast::ItemKind::Mod(ast::Mod {
|
2014-04-26 20:05:45 +00:00
|
|
|
inner: inner_span,
|
2017-08-07 05:54:09 +00:00
|
|
|
items,
|
2018-07-11 13:19:32 +00:00
|
|
|
inline: true
|
2013-05-17 13:51:25 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_extern_crate(&self, span: Span, name: Ident) -> P<ast::Item> {
|
2017-06-03 21:54:08 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::ExternCrate(None))
|
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_static(&self,
|
2014-06-10 20:54:13 +00:00
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
mutbl: ast::Mutability,
|
2014-09-13 16:06:01 +00:00
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item> {
|
2016-02-09 10:36:51 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
|
2014-06-10 20:54:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_const(&self,
|
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 15:17:01 +00:00
|
|
|
span: Span,
|
|
|
|
name: Ident,
|
|
|
|
ty: P<ast::Ty>,
|
|
|
|
expr: P<ast::Expr>)
|
|
|
|
-> P<ast::Item> {
|
2016-02-09 10:36:51 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, 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 15:17:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_ty_poly(&self, span: Span, name: Ident, ty: P<ast::Ty>,
|
2014-09-13 16:06:01 +00:00
|
|
|
generics: Generics) -> P<ast::Item> {
|
2019-08-02 10:02:08 +00:00
|
|
|
self.item(span, name, Vec::new(), ast::ItemKind::TyAlias(ty, generics))
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_ty(&self, span: Span, name: Ident, ty: P<ast::Ty>) -> P<ast::Item> {
|
2015-11-28 19:02:07 +00:00
|
|
|
self.item_ty_poly(span, name, ty, Generics::default())
|
2013-05-17 13:51:25 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {
|
2019-07-30 18:18:19 +00:00
|
|
|
attr::mk_attr_outer(mi)
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
|
2019-07-31 12:01:36 +00:00
|
|
|
attr::mk_word_item(Ident::new(w, sp))
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn meta_list_item_word(&self, sp: Span, w: ast::Name) -> ast::NestedMetaItem {
|
2019-07-31 12:01:36 +00:00
|
|
|
attr::mk_nested_word_item(Ident::new(w, sp))
|
2016-08-20 01:58:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn meta_list(&self, sp: Span, name: ast::Name, mis: Vec<ast::NestedMetaItem>)
|
2016-11-15 10:17:24 +00:00
|
|
|
-> ast::MetaItem {
|
2019-08-04 21:59:06 +00:00
|
|
|
attr::mk_list_item(Ident::new(name, sp), mis)
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn meta_name_value(&self, span: Span, name: ast::Name, lit_kind: ast::LitKind)
|
2016-11-15 10:17:24 +00:00
|
|
|
-> ast::MetaItem {
|
2019-08-04 22:03:34 +00:00
|
|
|
attr::mk_name_value_item(Ident::new(name, span), lit_kind, span)
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_use(&self, sp: Span,
|
2017-09-26 21:04:00 +00:00
|
|
|
vis: ast::Visibility, vp: P<ast::UseTree>) -> P<ast::Item> {
|
2015-01-13 15:30:17 +00:00
|
|
|
P(ast::Item {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-05-11 16:08:09 +00:00
|
|
|
ident: Ident::invalid(),
|
2015-01-13 15:30:17 +00:00
|
|
|
attrs: vec![],
|
2016-02-09 10:36:51 +00:00
|
|
|
node: ast::ItemKind::Use(vp),
|
2017-08-07 05:54:09 +00:00
|
|
|
vis,
|
2017-07-11 00:44:46 +00:00
|
|
|
span: sp,
|
|
|
|
tokens: None,
|
2015-01-13 15:30:17 +00:00
|
|
|
})
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
|
2018-03-09 15:58:44 +00:00
|
|
|
self.item_use_simple_(sp, vis, None, path)
|
2014-02-16 06:10:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
|
2018-03-09 15:58:44 +00:00
|
|
|
rename: Option<ast::Ident>, path: ast::Path) -> P<ast::Item> {
|
2017-09-26 21:04:00 +00:00
|
|
|
self.item_use(sp, vis, P(ast::UseTree {
|
|
|
|
span: sp,
|
|
|
|
prefix: path,
|
2018-06-13 16:44:06 +00:00
|
|
|
kind: ast::UseTreeKind::Simple(rename, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
|
2017-09-26 21:04:00 +00:00
|
|
|
}))
|
2014-02-16 06:10:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_use_list(&self, sp: Span, vis: ast::Visibility,
|
2015-01-13 15:30:17 +00:00
|
|
|
path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
|
2014-03-28 19:42:34 +00:00
|
|
|
let imports = imports.iter().map(|id| {
|
2017-09-26 21:04:00 +00:00
|
|
|
(ast::UseTree {
|
|
|
|
span: sp,
|
|
|
|
prefix: self.path(sp, vec![*id]),
|
2018-06-13 16:44:06 +00:00
|
|
|
kind: ast::UseTreeKind::Simple(None, ast::DUMMY_NODE_ID, ast::DUMMY_NODE_ID),
|
2017-09-26 21:04:00 +00:00
|
|
|
}, ast::DUMMY_NODE_ID)
|
2014-03-28 19:42:34 +00:00
|
|
|
}).collect();
|
2013-05-17 14:19:28 +00:00
|
|
|
|
2017-09-26 21:04:00 +00:00
|
|
|
self.item_use(sp, vis, P(ast::UseTree {
|
|
|
|
span: sp,
|
|
|
|
prefix: self.path(sp, path),
|
|
|
|
kind: ast::UseTreeKind::Nested(imports),
|
|
|
|
}))
|
2013-05-17 14:19:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 11:40:32 +00:00
|
|
|
pub fn item_use_glob(&self, sp: Span,
|
2015-01-13 15:30:17 +00:00
|
|
|
vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
|
2017-09-26 21:04:00 +00:00
|
|
|
self.item_use(sp, vis, P(ast::UseTree {
|
|
|
|
span: sp,
|
|
|
|
prefix: self.path(sp, path),
|
|
|
|
kind: ast::UseTreeKind::Glob,
|
|
|
|
}))
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
}
|