Revert, but without type const. Update symbol for feature err, then update suggestion output, and lastly update tests that change because of those. Update these new tests with the correct syntax, and few existing tests with the new outputs the merge with main added. Fix for tidyfmt and some errors when manually resolving a merge conflicts. Update these tests to use update error messages and type const syntax. Update comments and error message to use new syntax instead of old type_const attribute. Remove the type_const attribute update some more tests to use the new syntax. Update these test cases. update feature gate test Change gate logic for `mgca_type_const_syntax` to work also if `min_generic_const_args` is enabled. Create a new feature gate that checks for the feature before expansion. Make rustfmt handle the `type const` syntax correctly. Add a convience method to check if a RhsKind is type const. Rename `Const` discriminant to `Body` for `ConstItemRhsKind` Give the `TraitItemKind` flag an enum instead of a simple bool to better describe what the flag is for. Update formatting for these match statements. Update clippy test to use type const syntax. Update test to use type const syntax. update rustfmt to match ast items. Update clippy to match ast and hir items. Few more test cases that used old attribute, instead of 'type const' Update to match the output from the feature gate checks. tidyfmt adjustments. Update the is_type_const, so I can constrain record!(..) in encoder.rs Update conditional compilation test. Move the feature gate to after expansion to allow for cfg(...) to work. Update some more tests to use the new syntax. Update type const tests in associated-const-bindings to use new syntax. Don't check based off the attribute, but the item here. Update some tests outside of the const_generics folder that were using #[type_const] update the tests in associated consts that use #[type_const] to use type const Update these mgca tests with the type const syntax. Add a flag to TraitItemKind for detecting type const for now. Maybe later change ItemConstRhs to have optional consts but that touches a lot more lines of code. Don't need into for these now that it's a query. Add is_type_const query to handle foreign def ids. update this test to use type const syntax. Fix logic here, we only want to lower if there is expression in this case. Update built-in macros to use ConstItemRhsKind Update more instance of the old ConstItemRhs. Rename ConstItemKind to ConstItemRhsKind, I noticed there is a typed called ConstantItemKind, so add the Rhs to the name to avoid confusion. Update lower to use ConstItemKind Add an other helper method to check if the rhs kinda has an expr. Update item parse to use ConstItemKind enum. Felt the field name could a be little clear when editing a few other things. Change the ConstItem struct see know if we have a type const or regular const. Make sure this syntax is properly feature gated.
790 lines
25 KiB
Rust
790 lines
25 KiB
Rust
use rustc_ast::token::Delimiter;
|
|
use rustc_ast::tokenstream::TokenStream;
|
|
use rustc_ast::util::literal;
|
|
use rustc_ast::{
|
|
self as ast, AnonConst, AttrItem, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind,
|
|
MgcaDisambiguation, PatKind, UnOp, attr, token, tokenstream,
|
|
};
|
|
use rustc_span::source_map::Spanned;
|
|
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
|
|
use thin_vec::{ThinVec, thin_vec};
|
|
|
|
use crate::base::ExtCtxt;
|
|
|
|
impl<'a> ExtCtxt<'a> {
|
|
pub fn path(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
|
|
self.path_all(span, false, strs, vec![])
|
|
}
|
|
pub fn path_ident(&self, span: Span, id: Ident) -> ast::Path {
|
|
self.path(span, vec![id])
|
|
}
|
|
pub fn path_global(&self, span: Span, strs: Vec<Ident>) -> ast::Path {
|
|
self.path_all(span, true, strs, vec![])
|
|
}
|
|
pub fn path_all(
|
|
&self,
|
|
span: Span,
|
|
global: bool,
|
|
mut idents: Vec<Ident>,
|
|
args: Vec<ast::GenericArg>,
|
|
) -> ast::Path {
|
|
assert!(!idents.is_empty());
|
|
let add_root = global && !idents[0].is_path_segment_keyword();
|
|
let mut segments = ThinVec::with_capacity(idents.len() + add_root as usize);
|
|
if add_root {
|
|
segments.push(ast::PathSegment::path_root(span));
|
|
}
|
|
let last_ident = idents.pop().unwrap();
|
|
segments.extend(
|
|
idents.into_iter().map(|ident| ast::PathSegment::from_ident(ident.with_span_pos(span))),
|
|
);
|
|
let args = if !args.is_empty() {
|
|
let args = args.into_iter().map(ast::AngleBracketedArg::Arg).collect();
|
|
Some(ast::AngleBracketedArgs { args, span }.into())
|
|
} else {
|
|
None
|
|
};
|
|
segments.push(ast::PathSegment {
|
|
ident: last_ident.with_span_pos(span),
|
|
id: ast::DUMMY_NODE_ID,
|
|
args,
|
|
});
|
|
ast::Path { span, segments, tokens: None }
|
|
}
|
|
|
|
pub fn macro_call(
|
|
&self,
|
|
span: Span,
|
|
path: ast::Path,
|
|
delim: Delimiter,
|
|
tokens: TokenStream,
|
|
) -> Box<ast::MacCall> {
|
|
Box::new(ast::MacCall {
|
|
path,
|
|
args: Box::new(ast::DelimArgs {
|
|
dspan: tokenstream::DelimSpan { open: span, close: span },
|
|
delim,
|
|
tokens,
|
|
}),
|
|
})
|
|
}
|
|
|
|
pub fn ty_mt(&self, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
|
|
ast::MutTy { ty, mutbl }
|
|
}
|
|
|
|
pub fn ty(&self, span: Span, kind: ast::TyKind) -> Box<ast::Ty> {
|
|
Box::new(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind, tokens: None })
|
|
}
|
|
|
|
pub fn ty_infer(&self, span: Span) -> Box<ast::Ty> {
|
|
self.ty(span, ast::TyKind::Infer)
|
|
}
|
|
|
|
pub fn ty_path(&self, path: ast::Path) -> Box<ast::Ty> {
|
|
self.ty(path.span, ast::TyKind::Path(None, path))
|
|
}
|
|
|
|
// Might need to take bounds as an argument in the future, if you ever want
|
|
// to generate a bounded existential trait type.
|
|
pub fn ty_ident(&self, span: Span, ident: Ident) -> Box<ast::Ty> {
|
|
self.ty_path(self.path_ident(span, ident))
|
|
}
|
|
|
|
pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
|
|
ast::AnonConst {
|
|
id: ast::DUMMY_NODE_ID,
|
|
value: Box::new(ast::Expr {
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind,
|
|
span,
|
|
attrs: AttrVec::new(),
|
|
tokens: None,
|
|
}),
|
|
mgca_disambiguation: MgcaDisambiguation::Direct,
|
|
}
|
|
}
|
|
|
|
pub fn anon_const_block(&self, b: Box<ast::Block>) -> Box<ast::AnonConst> {
|
|
Box::new(self.anon_const(b.span, ast::ExprKind::Block(b, None)))
|
|
}
|
|
|
|
pub fn const_ident(&self, span: Span, ident: Ident) -> ast::AnonConst {
|
|
self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
|
|
}
|
|
|
|
pub fn ty_ref(
|
|
&self,
|
|
span: Span,
|
|
ty: Box<ast::Ty>,
|
|
lifetime: Option<ast::Lifetime>,
|
|
mutbl: ast::Mutability,
|
|
) -> Box<ast::Ty> {
|
|
self.ty(span, ast::TyKind::Ref(lifetime, self.ty_mt(ty, mutbl)))
|
|
}
|
|
|
|
pub fn ty_ptr(&self, span: Span, ty: Box<ast::Ty>, mutbl: ast::Mutability) -> Box<ast::Ty> {
|
|
self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
|
|
}
|
|
|
|
pub fn typaram(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
bounds: ast::GenericBounds,
|
|
default: Option<Box<ast::Ty>>,
|
|
) -> ast::GenericParam {
|
|
ast::GenericParam {
|
|
ident: ident.with_span_pos(span),
|
|
id: ast::DUMMY_NODE_ID,
|
|
attrs: AttrVec::new(),
|
|
bounds,
|
|
kind: ast::GenericParamKind::Type { default },
|
|
is_placeholder: false,
|
|
colon_span: None,
|
|
}
|
|
}
|
|
|
|
pub fn lifetime_param(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
bounds: ast::GenericBounds,
|
|
) -> ast::GenericParam {
|
|
ast::GenericParam {
|
|
id: ast::DUMMY_NODE_ID,
|
|
ident: ident.with_span_pos(span),
|
|
attrs: AttrVec::new(),
|
|
bounds,
|
|
is_placeholder: false,
|
|
kind: ast::GenericParamKind::Lifetime,
|
|
colon_span: None,
|
|
}
|
|
}
|
|
|
|
pub fn const_param(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
bounds: ast::GenericBounds,
|
|
ty: Box<ast::Ty>,
|
|
default: Option<AnonConst>,
|
|
) -> ast::GenericParam {
|
|
ast::GenericParam {
|
|
id: ast::DUMMY_NODE_ID,
|
|
ident: ident.with_span_pos(span),
|
|
attrs: AttrVec::new(),
|
|
bounds,
|
|
is_placeholder: false,
|
|
kind: ast::GenericParamKind::Const { ty, span: DUMMY_SP, default },
|
|
colon_span: None,
|
|
}
|
|
}
|
|
|
|
pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
|
|
ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
|
|
}
|
|
|
|
pub fn poly_trait_ref(&self, span: Span, path: ast::Path, is_const: bool) -> ast::PolyTraitRef {
|
|
ast::PolyTraitRef {
|
|
bound_generic_params: ThinVec::new(),
|
|
modifiers: ast::TraitBoundModifiers {
|
|
polarity: ast::BoundPolarity::Positive,
|
|
constness: if is_const {
|
|
ast::BoundConstness::Maybe(DUMMY_SP)
|
|
} else {
|
|
ast::BoundConstness::Never
|
|
},
|
|
asyncness: ast::BoundAsyncness::Normal,
|
|
},
|
|
trait_ref: self.trait_ref(path),
|
|
span,
|
|
parens: ast::Parens::No,
|
|
}
|
|
}
|
|
|
|
pub fn trait_bound(&self, path: ast::Path, is_const: bool) -> ast::GenericBound {
|
|
ast::GenericBound::Trait(self.poly_trait_ref(path.span, path, is_const))
|
|
}
|
|
|
|
pub fn lifetime(&self, span: Span, ident: Ident) -> ast::Lifetime {
|
|
ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
|
|
}
|
|
|
|
pub fn lifetime_static(&self, span: Span) -> ast::Lifetime {
|
|
self.lifetime(span, Ident::new(kw::StaticLifetime, span))
|
|
}
|
|
|
|
pub fn stmt_expr(&self, expr: Box<ast::Expr>) -> ast::Stmt {
|
|
ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Expr(expr) }
|
|
}
|
|
|
|
pub fn stmt_let(&self, sp: Span, mutbl: bool, ident: Ident, ex: Box<ast::Expr>) -> ast::Stmt {
|
|
self.stmt_let_ty(sp, mutbl, ident, None, ex)
|
|
}
|
|
|
|
pub fn stmt_let_ty(
|
|
&self,
|
|
sp: Span,
|
|
mutbl: bool,
|
|
ident: Ident,
|
|
ty: Option<Box<ast::Ty>>,
|
|
ex: Box<ast::Expr>,
|
|
) -> ast::Stmt {
|
|
let pat = if mutbl {
|
|
self.pat_ident_binding_mode(sp, ident, ast::BindingMode::MUT)
|
|
} else {
|
|
self.pat_ident(sp, ident)
|
|
};
|
|
let local = Box::new(ast::Local {
|
|
super_: None,
|
|
pat: Box::new(pat),
|
|
ty,
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind: LocalKind::Init(ex),
|
|
span: sp,
|
|
colon_sp: None,
|
|
attrs: AttrVec::new(),
|
|
tokens: None,
|
|
});
|
|
self.stmt_local(local, sp)
|
|
}
|
|
|
|
/// Generates `let _: Type;`, which is usually used for type assertions.
|
|
pub fn stmt_let_type_only(&self, span: Span, ty: Box<ast::Ty>) -> ast::Stmt {
|
|
let local = Box::new(ast::Local {
|
|
super_: None,
|
|
pat: Box::new(self.pat_wild(span)),
|
|
ty: Some(ty),
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind: LocalKind::Decl,
|
|
span,
|
|
colon_sp: None,
|
|
attrs: AttrVec::new(),
|
|
tokens: None,
|
|
});
|
|
self.stmt_local(local, span)
|
|
}
|
|
|
|
pub fn stmt_semi(&self, expr: Box<ast::Expr>) -> ast::Stmt {
|
|
ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Semi(expr) }
|
|
}
|
|
|
|
pub fn stmt_local(&self, local: Box<ast::Local>, span: Span) -> ast::Stmt {
|
|
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Let(local), span }
|
|
}
|
|
|
|
pub fn stmt_item(&self, sp: Span, item: Box<ast::Item>) -> ast::Stmt {
|
|
ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Item(item), span: sp }
|
|
}
|
|
|
|
pub fn block_expr(&self, expr: Box<ast::Expr>) -> Box<ast::Block> {
|
|
self.block(
|
|
expr.span,
|
|
thin_vec![ast::Stmt {
|
|
id: ast::DUMMY_NODE_ID,
|
|
span: expr.span,
|
|
kind: ast::StmtKind::Expr(expr),
|
|
}],
|
|
)
|
|
}
|
|
pub fn block(&self, span: Span, stmts: ThinVec<ast::Stmt>) -> Box<ast::Block> {
|
|
Box::new(ast::Block {
|
|
stmts,
|
|
id: ast::DUMMY_NODE_ID,
|
|
rules: BlockCheckMode::Default,
|
|
span,
|
|
tokens: None,
|
|
})
|
|
}
|
|
|
|
pub fn expr(&self, span: Span, kind: ast::ExprKind) -> Box<ast::Expr> {
|
|
Box::new(ast::Expr {
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind,
|
|
span,
|
|
attrs: AttrVec::new(),
|
|
tokens: None,
|
|
})
|
|
}
|
|
|
|
pub fn expr_path(&self, path: ast::Path) -> Box<ast::Expr> {
|
|
self.expr(path.span, ast::ExprKind::Path(None, path))
|
|
}
|
|
|
|
pub fn expr_ident(&self, span: Span, id: Ident) -> Box<ast::Expr> {
|
|
self.expr_path(self.path_ident(span, id))
|
|
}
|
|
pub fn expr_self(&self, span: Span) -> Box<ast::Expr> {
|
|
self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
|
|
}
|
|
|
|
pub fn expr_macro_call(&self, span: Span, call: Box<ast::MacCall>) -> Box<ast::Expr> {
|
|
self.expr(span, ast::ExprKind::MacCall(call))
|
|
}
|
|
|
|
pub fn expr_binary(
|
|
&self,
|
|
sp: Span,
|
|
op: ast::BinOpKind,
|
|
lhs: Box<ast::Expr>,
|
|
rhs: Box<ast::Expr>,
|
|
) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
|
|
}
|
|
|
|
pub fn expr_deref(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
|
|
}
|
|
|
|
pub fn expr_addr_of(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
|
|
}
|
|
|
|
pub fn expr_paren(&self, sp: Span, e: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Paren(e))
|
|
}
|
|
|
|
pub fn expr_method_call(
|
|
&self,
|
|
span: Span,
|
|
expr: Box<ast::Expr>,
|
|
ident: Ident,
|
|
args: ThinVec<Box<ast::Expr>>,
|
|
) -> Box<ast::Expr> {
|
|
let seg = ast::PathSegment::from_ident(ident);
|
|
self.expr(
|
|
span,
|
|
ast::ExprKind::MethodCall(Box::new(ast::MethodCall {
|
|
seg,
|
|
receiver: expr,
|
|
args,
|
|
span,
|
|
})),
|
|
)
|
|
}
|
|
|
|
pub fn expr_call(
|
|
&self,
|
|
span: Span,
|
|
expr: Box<ast::Expr>,
|
|
args: ThinVec<Box<ast::Expr>>,
|
|
) -> Box<ast::Expr> {
|
|
self.expr(span, ast::ExprKind::Call(expr, args))
|
|
}
|
|
pub fn expr_loop(&self, sp: Span, block: Box<ast::Block>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Loop(block, None, sp))
|
|
}
|
|
pub fn expr_asm(&self, sp: Span, expr: Box<ast::InlineAsm>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::InlineAsm(expr))
|
|
}
|
|
pub fn expr_call_ident(
|
|
&self,
|
|
span: Span,
|
|
id: Ident,
|
|
args: ThinVec<Box<ast::Expr>>,
|
|
) -> Box<ast::Expr> {
|
|
self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
|
|
}
|
|
pub fn expr_call_global(
|
|
&self,
|
|
sp: Span,
|
|
fn_path: Vec<Ident>,
|
|
args: ThinVec<Box<ast::Expr>>,
|
|
) -> Box<ast::Expr> {
|
|
let pathexpr = self.expr_path(self.path_global(sp, fn_path));
|
|
self.expr_call(sp, pathexpr, args)
|
|
}
|
|
pub fn expr_block(&self, b: Box<ast::Block>) -> Box<ast::Expr> {
|
|
self.expr(b.span, ast::ExprKind::Block(b, None))
|
|
}
|
|
pub fn field_imm(&self, span: Span, ident: Ident, e: Box<ast::Expr>) -> ast::ExprField {
|
|
ast::ExprField {
|
|
ident: ident.with_span_pos(span),
|
|
expr: e,
|
|
span,
|
|
is_shorthand: false,
|
|
attrs: AttrVec::new(),
|
|
id: ast::DUMMY_NODE_ID,
|
|
is_placeholder: false,
|
|
}
|
|
}
|
|
pub fn expr_struct(
|
|
&self,
|
|
span: Span,
|
|
path: ast::Path,
|
|
fields: ThinVec<ast::ExprField>,
|
|
) -> Box<ast::Expr> {
|
|
self.expr(
|
|
span,
|
|
ast::ExprKind::Struct(Box::new(ast::StructExpr {
|
|
qself: None,
|
|
path,
|
|
fields,
|
|
rest: ast::StructRest::None,
|
|
})),
|
|
)
|
|
}
|
|
pub fn expr_struct_ident(
|
|
&self,
|
|
span: Span,
|
|
id: Ident,
|
|
fields: ThinVec<ast::ExprField>,
|
|
) -> Box<ast::Expr> {
|
|
self.expr_struct(span, self.path_ident(span, id), fields)
|
|
}
|
|
|
|
pub fn expr_usize(&self, span: Span, n: usize) -> Box<ast::Expr> {
|
|
let suffix = Some(ast::UintTy::Usize.name());
|
|
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
|
}
|
|
|
|
pub fn expr_u32(&self, span: Span, n: u32) -> Box<ast::Expr> {
|
|
let suffix = Some(ast::UintTy::U32.name());
|
|
let lit = token::Lit::new(token::Integer, sym::integer(n), suffix);
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
|
}
|
|
|
|
pub fn expr_bool(&self, span: Span, value: bool) -> Box<ast::Expr> {
|
|
let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None);
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
|
}
|
|
|
|
pub fn expr_str(&self, span: Span, s: Symbol) -> Box<ast::Expr> {
|
|
let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None);
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
|
}
|
|
|
|
pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> Box<ast::Expr> {
|
|
let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None);
|
|
self.expr(span, ast::ExprKind::Lit(lit))
|
|
}
|
|
|
|
/// `[expr1, expr2, ...]`
|
|
pub fn expr_array(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Array(exprs))
|
|
}
|
|
|
|
/// `&[expr1, expr2, ...]`
|
|
pub fn expr_array_ref(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
|
|
self.expr_addr_of(sp, self.expr_array(sp, exprs))
|
|
}
|
|
|
|
pub fn expr_some(&self, sp: Span, expr: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
|
|
self.expr_call_global(sp, some, thin_vec![expr])
|
|
}
|
|
|
|
pub fn expr_none(&self, sp: Span) -> Box<ast::Expr> {
|
|
let none = self.std_path(&[sym::option, sym::Option, sym::None]);
|
|
self.expr_path(self.path_global(sp, none))
|
|
}
|
|
pub fn expr_tuple(&self, sp: Span, exprs: ThinVec<Box<ast::Expr>>) -> Box<ast::Expr> {
|
|
self.expr(sp, ast::ExprKind::Tup(exprs))
|
|
}
|
|
|
|
pub fn expr_unreachable(&self, span: Span) -> Box<ast::Expr> {
|
|
self.expr_macro_call(
|
|
span,
|
|
self.macro_call(
|
|
span,
|
|
self.path_global(
|
|
span,
|
|
[sym::std, sym::unreachable].map(|s| Ident::new(s, span)).to_vec(),
|
|
),
|
|
Delimiter::Parenthesis,
|
|
TokenStream::default(),
|
|
),
|
|
)
|
|
}
|
|
|
|
pub fn expr_ok(&self, sp: Span, expr: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
|
|
self.expr_call_global(sp, ok, thin_vec![expr])
|
|
}
|
|
|
|
pub fn expr_try(&self, sp: Span, head: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
|
|
let ok_path = self.path_global(sp, ok);
|
|
let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
|
|
let err_path = self.path_global(sp, err);
|
|
|
|
let binding_variable = Ident::new(sym::__try_var, sp);
|
|
let binding_pat = self.pat_ident(sp, binding_variable);
|
|
let binding_expr = self.expr_ident(sp, binding_variable);
|
|
|
|
// `Ok(__try_var)` pattern
|
|
let ok_pat = self.pat_tuple_struct(sp, ok_path, thin_vec![binding_pat.clone()]);
|
|
|
|
// `Err(__try_var)` (pattern and expression respectively)
|
|
let err_pat = self.pat_tuple_struct(sp, err_path.clone(), thin_vec![binding_pat]);
|
|
let err_inner_expr =
|
|
self.expr_call(sp, self.expr_path(err_path), thin_vec![binding_expr.clone()]);
|
|
// `return Err(__try_var)`
|
|
let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
|
|
|
|
// `Ok(__try_var) => __try_var`
|
|
let ok_arm = self.arm(sp, ok_pat, binding_expr);
|
|
// `Err(__try_var) => return Err(__try_var)`
|
|
let err_arm = self.arm(sp, err_pat, err_expr);
|
|
|
|
// `match head { Ok() => ..., Err() => ... }`
|
|
self.expr_match(sp, head, thin_vec![ok_arm, err_arm])
|
|
}
|
|
|
|
pub fn pat(&self, span: Span, kind: PatKind) -> ast::Pat {
|
|
ast::Pat { id: ast::DUMMY_NODE_ID, kind, span, tokens: None }
|
|
}
|
|
pub fn pat_wild(&self, span: Span) -> ast::Pat {
|
|
self.pat(span, PatKind::Wild)
|
|
}
|
|
pub fn pat_lit(&self, span: Span, expr: Box<ast::Expr>) -> ast::Pat {
|
|
self.pat(span, PatKind::Expr(expr))
|
|
}
|
|
pub fn pat_ident(&self, span: Span, ident: Ident) -> ast::Pat {
|
|
self.pat_ident_binding_mode(span, ident, ast::BindingMode::NONE)
|
|
}
|
|
|
|
pub fn pat_ident_binding_mode(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
ann: ast::BindingMode,
|
|
) -> ast::Pat {
|
|
let pat = PatKind::Ident(ann, ident.with_span_pos(span), None);
|
|
self.pat(span, pat)
|
|
}
|
|
pub fn pat_path(&self, span: Span, path: ast::Path) -> ast::Pat {
|
|
self.pat(span, PatKind::Path(None, path))
|
|
}
|
|
pub fn pat_tuple_struct(
|
|
&self,
|
|
span: Span,
|
|
path: ast::Path,
|
|
subpats: ThinVec<ast::Pat>,
|
|
) -> ast::Pat {
|
|
self.pat(span, PatKind::TupleStruct(None, path, subpats))
|
|
}
|
|
pub fn pat_struct(
|
|
&self,
|
|
span: Span,
|
|
path: ast::Path,
|
|
field_pats: ThinVec<ast::PatField>,
|
|
) -> ast::Pat {
|
|
self.pat(span, PatKind::Struct(None, path, field_pats, ast::PatFieldsRest::None))
|
|
}
|
|
pub fn pat_tuple(&self, span: Span, pats: ThinVec<ast::Pat>) -> ast::Pat {
|
|
self.pat(span, PatKind::Tuple(pats))
|
|
}
|
|
|
|
pub fn pat_some(&self, span: Span, pat: ast::Pat) -> ast::Pat {
|
|
let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
|
|
let path = self.path_global(span, some);
|
|
self.pat_tuple_struct(span, path, thin_vec![pat])
|
|
}
|
|
|
|
pub fn arm(&self, span: Span, pat: ast::Pat, expr: Box<ast::Expr>) -> ast::Arm {
|
|
ast::Arm {
|
|
attrs: AttrVec::new(),
|
|
pat: Box::new(pat),
|
|
guard: None,
|
|
body: Some(expr),
|
|
span,
|
|
id: ast::DUMMY_NODE_ID,
|
|
is_placeholder: false,
|
|
}
|
|
}
|
|
|
|
pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
|
|
self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
|
|
}
|
|
|
|
pub fn expr_match(
|
|
&self,
|
|
span: Span,
|
|
arg: Box<ast::Expr>,
|
|
arms: ThinVec<ast::Arm>,
|
|
) -> Box<Expr> {
|
|
self.expr(span, ast::ExprKind::Match(arg, arms, MatchKind::Prefix))
|
|
}
|
|
|
|
pub fn expr_if(
|
|
&self,
|
|
span: Span,
|
|
cond: Box<ast::Expr>,
|
|
then: Box<ast::Expr>,
|
|
els: Option<Box<ast::Expr>>,
|
|
) -> Box<ast::Expr> {
|
|
let els = els.map(|x| self.expr_block(self.block_expr(x)));
|
|
self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
|
|
}
|
|
|
|
pub fn lambda(&self, span: Span, ids: Vec<Ident>, body: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
let fn_decl = self.fn_decl(
|
|
ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
|
|
ast::FnRetTy::Default(span),
|
|
);
|
|
|
|
// 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.
|
|
self.expr(
|
|
span,
|
|
ast::ExprKind::Closure(Box::new(ast::Closure {
|
|
binder: ast::ClosureBinder::NotPresent,
|
|
capture_clause: ast::CaptureBy::Ref,
|
|
constness: ast::Const::No,
|
|
coroutine_kind: None,
|
|
movability: ast::Movability::Movable,
|
|
fn_decl,
|
|
body,
|
|
fn_decl_span: span,
|
|
// FIXME(SarthakSingh31): This points to the start of the declaration block and
|
|
// not the span of the argument block.
|
|
fn_arg_span: span,
|
|
})),
|
|
)
|
|
}
|
|
|
|
pub fn lambda0(&self, span: Span, body: Box<ast::Expr>) -> Box<ast::Expr> {
|
|
self.lambda(span, Vec::new(), body)
|
|
}
|
|
|
|
pub fn lambda1(&self, span: Span, body: Box<ast::Expr>, ident: Ident) -> Box<ast::Expr> {
|
|
self.lambda(span, vec![ident], body)
|
|
}
|
|
|
|
pub fn lambda_stmts_1(
|
|
&self,
|
|
span: Span,
|
|
stmts: ThinVec<ast::Stmt>,
|
|
ident: Ident,
|
|
) -> Box<ast::Expr> {
|
|
self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
|
|
}
|
|
|
|
pub fn param(&self, span: Span, ident: Ident, ty: Box<ast::Ty>) -> ast::Param {
|
|
let pat = Box::new(self.pat_ident(span, ident));
|
|
ast::Param {
|
|
attrs: AttrVec::default(),
|
|
id: ast::DUMMY_NODE_ID,
|
|
pat,
|
|
span,
|
|
ty,
|
|
is_placeholder: false,
|
|
}
|
|
}
|
|
|
|
// `self` is unused but keep it as method for the convenience use.
|
|
pub fn fn_decl(&self, inputs: ThinVec<ast::Param>, output: ast::FnRetTy) -> Box<ast::FnDecl> {
|
|
Box::new(ast::FnDecl { inputs, output })
|
|
}
|
|
|
|
pub fn item(&self, span: Span, attrs: ast::AttrVec, kind: ast::ItemKind) -> Box<ast::Item> {
|
|
Box::new(ast::Item {
|
|
attrs,
|
|
id: ast::DUMMY_NODE_ID,
|
|
kind,
|
|
vis: ast::Visibility {
|
|
span: span.shrink_to_lo(),
|
|
kind: ast::VisibilityKind::Inherited,
|
|
tokens: None,
|
|
},
|
|
span,
|
|
tokens: None,
|
|
})
|
|
}
|
|
|
|
pub fn item_static(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
ty: Box<ast::Ty>,
|
|
mutability: ast::Mutability,
|
|
expr: Box<ast::Expr>,
|
|
) -> Box<ast::Item> {
|
|
self.item(
|
|
span,
|
|
AttrVec::new(),
|
|
ast::ItemKind::Static(
|
|
ast::StaticItem {
|
|
ident,
|
|
ty,
|
|
safety: ast::Safety::Default,
|
|
mutability,
|
|
expr: Some(expr),
|
|
define_opaque: None,
|
|
}
|
|
.into(),
|
|
),
|
|
)
|
|
}
|
|
|
|
pub fn item_const(
|
|
&self,
|
|
span: Span,
|
|
ident: Ident,
|
|
ty: Box<ast::Ty>,
|
|
rhs_kind: ast::ConstItemRhsKind,
|
|
) -> Box<ast::Item> {
|
|
let defaultness = ast::Defaultness::Final;
|
|
self.item(
|
|
span,
|
|
AttrVec::new(),
|
|
ast::ItemKind::Const(
|
|
ast::ConstItem {
|
|
defaultness,
|
|
ident,
|
|
// FIXME(generic_const_items): Pass the generics as a parameter.
|
|
generics: ast::Generics::default(),
|
|
ty,
|
|
rhs_kind,
|
|
define_opaque: None,
|
|
}
|
|
.into(),
|
|
),
|
|
)
|
|
}
|
|
|
|
// Builds `#[name]`.
|
|
pub fn attr_word(&self, name: Symbol, span: Span) -> ast::Attribute {
|
|
let g = &self.sess.psess.attr_id_generator;
|
|
attr::mk_attr_word(g, ast::AttrStyle::Outer, ast::Safety::Default, name, span)
|
|
}
|
|
|
|
// Builds `#[name = val]`.
|
|
//
|
|
// Note: `span` is used for both the identifier and the value.
|
|
pub fn attr_name_value_str(&self, name: Symbol, val: Symbol, span: Span) -> ast::Attribute {
|
|
let g = &self.sess.psess.attr_id_generator;
|
|
attr::mk_attr_name_value_str(
|
|
g,
|
|
ast::AttrStyle::Outer,
|
|
ast::Safety::Default,
|
|
name,
|
|
val,
|
|
span,
|
|
)
|
|
}
|
|
|
|
// Builds `#[outer(inner)]`.
|
|
pub fn attr_nested_word(&self, outer: Symbol, inner: Symbol, span: Span) -> ast::Attribute {
|
|
let g = &self.sess.psess.attr_id_generator;
|
|
attr::mk_attr_nested_word(
|
|
g,
|
|
ast::AttrStyle::Outer,
|
|
ast::Safety::Default,
|
|
outer,
|
|
inner,
|
|
span,
|
|
)
|
|
}
|
|
|
|
// Builds an attribute fully manually.
|
|
pub fn attr_nested(&self, inner: AttrItem, span: Span) -> ast::Attribute {
|
|
let g = &self.sess.psess.attr_id_generator;
|
|
attr::mk_attr_from_item(g, inner, None, ast::AttrStyle::Outer, span)
|
|
}
|
|
}
|