1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
4 use std::{any::type_name, sync::Arc};
10 name::{name, AsName, Name},
11 ExpandError, HirFileId, MacroDefId, MacroDefKind,
13 use rustc_hash::FxHashMap;
16 self, ArgListOwner, ArrayExprKind, AstChildren, LiteralKind, LoopBodyOwner, NameOwner,
19 AstNode, AstPtr, SyntaxNodePtr,
25 body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax},
26 builtin_type::{BuiltinFloat, BuiltinInt},
28 diagnostics::{InactiveCode, MacroError, UnresolvedProcMacro},
30 dummy_expr_id, ArithOp, Array, BinaryOp, BindingAnnotation, CmpOp, Expr, ExprId, Literal,
31 LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
33 item_scope::BuiltinShadowMode,
34 item_tree::{ItemTree, ItemTreeId, ItemTreeNode},
35 path::{GenericArgs, Path},
36 type_ref::{Mutability, Rawness, TypeRef},
37 AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId,
38 StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
41 use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource};
43 pub(crate) struct LowerCtx {
48 pub(crate) fn new(db: &dyn DefDatabase, file_id: HirFileId) -> Self {
49 LowerCtx { hygiene: Hygiene::new(db.upcast(), file_id) }
51 pub(crate) fn with_hygiene(hygiene: &Hygiene) -> Self {
52 LowerCtx { hygiene: hygiene.clone() }
55 pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
56 Path::from_src(ast, &self.hygiene)
64 params: Option<ast::ParamList>,
65 body: Option<ast::Expr>,
66 ) -> (Body, BodySourceMap) {
67 let item_tree = db.item_tree(expander.current_file_id);
71 source_map: BodySourceMap::default(),
73 exprs: Arena::default(),
74 pats: Arena::default(),
76 body_expr: dummy_expr_id(),
77 item_scope: Default::default(),
80 let mut map = FxHashMap::default();
81 map.insert(expander.current_file_id, item_tree);
86 .collect(params, body)
89 struct ExprCollector<'a> {
90 db: &'a dyn DefDatabase,
94 source_map: BodySourceMap,
96 item_trees: FxHashMap<HirFileId, Arc<ItemTree>>,
99 impl ExprCollector<'_> {
102 param_list: Option<ast::ParamList>,
103 body: Option<ast::Expr>,
104 ) -> (Body, BodySourceMap) {
105 if let Some(param_list) = param_list {
106 if let Some(self_param) = param_list.self_param() {
107 let ptr = AstPtr::new(&self_param);
108 let param_pat = self.alloc_pat(
111 mode: BindingAnnotation::new(
112 self_param.mut_token().is_some() && self_param.amp_token().is_none(),
119 self.body.params.push(param_pat);
122 for param in param_list.params() {
123 let pat = match param.pat() {
127 let param_pat = self.collect_pat(pat);
128 self.body.params.push(param_pat);
132 self.body.body_expr = self.collect_expr_opt(body);
133 (self.body, self.source_map)
136 fn ctx(&self) -> LowerCtx {
137 LowerCtx::new(self.db, self.expander.current_file_id)
140 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
141 let src = self.expander.to_source(ptr);
142 let id = self.make_expr(expr, Ok(src.clone()));
143 self.source_map.expr_map.insert(src, id);
146 // desugared exprs don't have ptr, that's wrong and should be fixed
148 fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
149 self.make_expr(expr, Err(SyntheticSyntax))
151 fn empty_block(&mut self) -> ExprId {
152 self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None })
154 fn missing_expr(&mut self) -> ExprId {
155 self.alloc_expr_desugared(Expr::Missing)
157 fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
158 let id = self.body.exprs.alloc(expr);
159 self.source_map.expr_map_back.insert(id, src);
163 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
164 let src = self.expander.to_source(ptr);
165 let id = self.make_pat(pat, Ok(src.clone()));
166 self.source_map.pat_map.insert(src, id);
169 fn missing_pat(&mut self) -> PatId {
170 self.make_pat(Pat::Missing, Err(SyntheticSyntax))
172 fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
173 let id = self.body.pats.alloc(pat);
174 self.source_map.pat_map_back.insert(id, src);
178 fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
179 let syntax_ptr = AstPtr::new(&expr);
180 if self.check_cfg(&expr).is_none() {
181 return self.missing_expr();
185 ast::Expr::IfExpr(e) => {
186 let then_branch = self.collect_block_opt(e.then_branch());
188 let else_branch = e.else_branch().map(|b| match b {
189 ast::ElseBranch::Block(it) => self.collect_block(it),
190 ast::ElseBranch::IfExpr(elif) => {
191 let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
192 self.collect_expr(expr)
196 let condition = match e.condition() {
197 None => self.missing_expr(),
198 Some(condition) => match condition.pat() {
199 None => self.collect_expr_opt(condition.expr()),
200 // if let -- desugar to match
202 let pat = self.collect_pat(pat);
203 let match_expr = self.collect_expr_opt(condition.expr());
204 let placeholder_pat = self.missing_pat();
206 MatchArm { pat, expr: then_branch, guard: None },
208 pat: placeholder_pat,
209 expr: else_branch.unwrap_or_else(|| self.empty_block()),
214 .alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr);
219 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
221 ast::Expr::EffectExpr(e) => match e.effect() {
222 ast::Effect::Try(_) => {
223 let body = self.collect_block_opt(e.block_expr());
224 self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
226 ast::Effect::Unsafe(_) => {
227 let body = self.collect_block_opt(e.block_expr());
228 self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
230 // FIXME: we need to record these effects somewhere...
231 ast::Effect::Label(label) => match e.block_expr() {
233 let res = self.collect_block(block);
234 match &mut self.body.exprs[res] {
235 Expr::Block { label: block_label, .. } => {
236 *block_label = label.lifetime().map(|t| Name::new_lifetime(&t))
242 None => self.missing_expr(),
244 // FIXME: we need to record these effects somewhere...
245 ast::Effect::Async(_) => {
246 let body = self.collect_block_opt(e.block_expr());
247 self.alloc_expr(Expr::Async { body }, syntax_ptr)
250 ast::Expr::BlockExpr(e) => self.collect_block(e),
251 ast::Expr::LoopExpr(e) => {
252 let body = self.collect_block_opt(e.loop_body());
256 label: e.label().and_then(|l| l.lifetime()).map(|l| Name::new_lifetime(&l)),
261 ast::Expr::WhileExpr(e) => {
262 let body = self.collect_block_opt(e.loop_body());
264 let condition = match e.condition() {
265 None => self.missing_expr(),
266 Some(condition) => match condition.pat() {
267 None => self.collect_expr_opt(condition.expr()),
268 // if let -- desugar to match
270 mark::hit!(infer_resolve_while_let);
271 let pat = self.collect_pat(pat);
272 let match_expr = self.collect_expr_opt(condition.expr());
273 let placeholder_pat = self.missing_pat();
275 self.alloc_expr_desugared(Expr::Break { expr: None, label: None });
277 MatchArm { pat, expr: body, guard: None },
278 MatchArm { pat: placeholder_pat, expr: break_, guard: None },
281 self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
282 return self.alloc_expr(
287 .and_then(|l| l.lifetime())
288 .map(|l| Name::new_lifetime(&l)),
300 label: e.label().and_then(|l| l.lifetime()).map(|l| Name::new_lifetime(&l)),
305 ast::Expr::ForExpr(e) => {
306 let iterable = self.collect_expr_opt(e.iterable());
307 let pat = self.collect_pat_opt(e.pat());
308 let body = self.collect_block_opt(e.loop_body());
314 label: e.label().and_then(|l| l.lifetime()).map(|l| Name::new_lifetime(&l)),
319 ast::Expr::CallExpr(e) => {
320 let callee = self.collect_expr_opt(e.expr());
321 let args = if let Some(arg_list) = e.arg_list() {
322 arg_list.args().map(|e| self.collect_expr(e)).collect()
326 self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
328 ast::Expr::MethodCallExpr(e) => {
329 let receiver = self.collect_expr_opt(e.receiver());
330 let args = if let Some(arg_list) = e.arg_list() {
331 arg_list.args().map(|e| self.collect_expr(e)).collect()
335 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
337 e.generic_arg_list().and_then(|it| GenericArgs::from_ast(&self.ctx(), it));
339 Expr::MethodCall { receiver, method_name, args, generic_args },
343 ast::Expr::MatchExpr(e) => {
344 let expr = self.collect_expr_opt(e.expr());
345 let arms = if let Some(match_arm_list) = e.match_arm_list() {
349 self.check_cfg(&arm).map(|()| MatchArm {
350 pat: self.collect_pat_opt(arm.pat()),
351 expr: self.collect_expr_opt(arm.expr()),
354 .and_then(|guard| guard.expr())
355 .map(|e| self.collect_expr(e)),
362 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
364 ast::Expr::PathExpr(e) => {
367 .and_then(|path| self.expander.parse_path(path))
369 .unwrap_or(Expr::Missing);
370 self.alloc_expr(path, syntax_ptr)
372 ast::Expr::ContinueExpr(e) => self.alloc_expr(
373 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
376 ast::Expr::BreakExpr(e) => {
377 let expr = e.expr().map(|e| self.collect_expr(e));
379 Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
383 ast::Expr::ParenExpr(e) => {
384 let inner = self.collect_expr_opt(e.expr());
385 // make the paren expr point to the inner expression as well
386 let src = self.expander.to_source(syntax_ptr);
387 self.source_map.expr_map.insert(src, inner);
390 ast::Expr::ReturnExpr(e) => {
391 let expr = e.expr().map(|e| self.collect_expr(e));
392 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
394 ast::Expr::RecordExpr(e) => {
395 let path = e.path().and_then(|path| self.expander.parse_path(path));
396 let mut field_ptrs = Vec::new();
397 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
400 .inspect(|field| field_ptrs.push(AstPtr::new(field)))
401 .filter_map(|field| {
402 self.check_cfg(&field)?;
404 let name = field.field_name()?.as_name();
406 Some(RecordLitField {
408 expr: match field.expr() {
409 Some(e) => self.collect_expr(e),
410 None => self.missing_expr(),
415 let spread = nfl.spread().map(|s| self.collect_expr(s));
416 Expr::RecordLit { path, fields, spread }
418 Expr::RecordLit { path, fields: Vec::new(), spread: None }
421 let res = self.alloc_expr(record_lit, syntax_ptr);
422 for (i, ptr) in field_ptrs.into_iter().enumerate() {
423 let src = self.expander.to_source(ptr);
424 self.source_map.field_map.insert((res, i), src);
428 ast::Expr::FieldExpr(e) => {
429 let expr = self.collect_expr_opt(e.expr());
430 let name = match e.field_access() {
431 Some(kind) => kind.as_name(),
432 _ => Name::missing(),
434 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
436 ast::Expr::AwaitExpr(e) => {
437 let expr = self.collect_expr_opt(e.expr());
438 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
440 ast::Expr::TryExpr(e) => {
441 let expr = self.collect_expr_opt(e.expr());
442 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
444 ast::Expr::CastExpr(e) => {
445 let expr = self.collect_expr_opt(e.expr());
446 let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
447 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
449 ast::Expr::RefExpr(e) => {
450 let expr = self.collect_expr_opt(e.expr());
451 let raw_tok = e.raw_token().is_some();
452 let mutability = if raw_tok {
453 if e.mut_token().is_some() {
455 } else if e.const_token().is_some() {
458 unreachable!("parser only remaps to raw_token() if matching mutability token follows")
461 Mutability::from_mutable(e.mut_token().is_some())
463 let rawness = Rawness::from_raw(raw_tok);
464 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
466 ast::Expr::PrefixExpr(e) => {
467 let expr = self.collect_expr_opt(e.expr());
468 if let Some(op) = e.op_kind() {
469 self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
471 self.alloc_expr(Expr::Missing, syntax_ptr)
474 ast::Expr::ClosureExpr(e) => {
475 let mut args = Vec::new();
476 let mut arg_types = Vec::new();
477 if let Some(pl) = e.param_list() {
478 for param in pl.params() {
479 let pat = self.collect_pat_opt(param.pat());
480 let type_ref = param.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
482 arg_types.push(type_ref);
486 e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it));
487 let body = self.collect_expr_opt(e.body());
488 self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
490 ast::Expr::BinExpr(e) => {
491 let lhs = self.collect_expr_opt(e.lhs());
492 let rhs = self.collect_expr_opt(e.rhs());
493 let op = e.op_kind().map(BinaryOp::from);
494 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
496 ast::Expr::TupleExpr(e) => {
497 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
498 self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
500 ast::Expr::BoxExpr(e) => {
501 let expr = self.collect_expr_opt(e.expr());
502 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
505 ast::Expr::ArrayExpr(e) => {
509 ArrayExprKind::ElementList(e) => {
510 let exprs = e.map(|expr| self.collect_expr(expr)).collect();
511 self.alloc_expr(Expr::Array(Array::ElementList(exprs)), syntax_ptr)
513 ArrayExprKind::Repeat { initializer, repeat } => {
514 let initializer = self.collect_expr_opt(initializer);
515 let repeat = self.collect_expr_opt(repeat);
517 Expr::Array(Array::Repeat { initializer, repeat }),
524 ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
525 ast::Expr::IndexExpr(e) => {
526 let base = self.collect_expr_opt(e.base());
527 let index = self.collect_expr_opt(e.index());
528 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
530 ast::Expr::RangeExpr(e) => {
531 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
532 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
534 Some(range_type) => {
535 self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
537 None => self.alloc_expr(Expr::Missing, syntax_ptr),
540 ast::Expr::MacroCall(e) => {
541 let mut ids = vec![];
542 self.collect_macro_call(e, syntax_ptr.clone(), |this, expansion| {
543 ids.push(match expansion {
544 Some(it) => this.collect_expr(it),
545 None => this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
553 fn collect_macro_call<F: FnMut(&mut Self, Option<T>), T: ast::AstNode>(
556 syntax_ptr: AstPtr<ast::Expr>,
559 // File containing the macro call. Expansion errors will be attached here.
560 let outer_file = self.expander.current_file_id;
562 let macro_call = self.expander.to_source(AstPtr::new(&e));
563 let res = self.expander.enter_expand(self.db, Some(&self.body.item_scope), e);
566 Some(ExpandError::UnresolvedProcMacro) => {
567 self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro(
568 UnresolvedProcMacro {
570 node: syntax_ptr.into(),
571 precise_location: None,
577 self.source_map.diagnostics.push(BodyDiagnostic::MacroError(MacroError {
579 node: syntax_ptr.into(),
580 message: err.to_string(),
587 Some((mark, expansion)) => {
588 // FIXME: Statements are too complicated to recover from error for now.
589 // It is because we don't have any hygenine for local variable expansion right now.
590 if T::can_cast(syntax::SyntaxKind::MACRO_STMTS) && res.err.is_some() {
591 self.expander.exit(self.db, mark);
592 collector(self, None);
594 self.source_map.expansions.insert(macro_call, self.expander.current_file_id);
596 let item_tree = self.db.item_tree(self.expander.current_file_id);
597 self.item_trees.insert(self.expander.current_file_id, item_tree);
599 let id = collector(self, Some(expansion));
600 self.expander.exit(self.db, mark);
604 None => collector(self, None),
608 fn find_inner_item<N: ItemTreeNode>(&self, ast: &N::Source) -> Option<ItemTreeId<N>> {
609 let id = self.expander.ast_id(ast);
610 let tree = &self.item_trees[&id.file_id];
612 // FIXME: This probably breaks with `use` items, since they produce multiple item tree nodes
614 // Root file (non-macro).
615 let item_tree_id = tree
617 .chain(tree.top_level_items().iter().copied())
618 .filter_map(|mod_item| mod_item.downcast::<N>())
619 .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value.upcast())
622 "couldn't find inner {} item for {:?} (AST: `{}` - {:?})",
631 Some(ItemTreeId::new(id.file_id, item_tree_id))
634 fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
635 if let Some(expr) = expr {
636 self.collect_expr(expr)
642 fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Vec<Statement>> {
645 ast::Stmt::LetStmt(stmt) => {
646 self.check_cfg(&stmt)?;
648 let pat = self.collect_pat_opt(stmt.pat());
649 let type_ref = stmt.ty().map(|it| TypeRef::from_ast(&self.ctx(), it));
650 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
651 vec![Statement::Let { pat, type_ref, initializer }]
653 ast::Stmt::ExprStmt(stmt) => {
654 self.check_cfg(&stmt)?;
656 // Note that macro could be expended to multiple statements
657 if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
658 let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());
659 let mut stmts = vec![];
661 self.collect_macro_call(m, syntax_ptr.clone(), |this, expansion| {
664 let statements: ast::MacroStmts = expansion;
665 this.collect_stmts_items(statements.statements());
667 statements.statements().for_each(|stmt| {
668 if let Some(mut r) = this.collect_stmt(stmt) {
669 stmts.append(&mut r);
672 if let Some(expr) = statements.expr() {
673 stmts.push(Statement::Expr(this.collect_expr(expr)));
677 stmts.push(Statement::Expr(
678 this.alloc_expr(Expr::Missing, syntax_ptr.clone()),
685 vec![Statement::Expr(self.collect_expr_opt(stmt.expr()))]
688 ast::Stmt::Item(item) => {
689 self.check_cfg(&item)?;
698 fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
699 let syntax_node_ptr = AstPtr::new(&block.clone().into());
700 self.collect_stmts_items(block.statements());
702 block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect();
703 let tail = block.expr().map(|e| self.collect_expr(e));
704 self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr)
707 fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) {
708 let container = ContainerId::DefWithBodyId(self.def);
711 .filter_map(|stmt| match stmt {
712 ast::Stmt::Item(it) => Some(it),
713 ast::Stmt::LetStmt(_) | ast::Stmt::ExprStmt(_) => None,
716 let (def, name): (ModuleDefId, Option<ast::Name>) = match item {
717 ast::Item::Fn(def) => {
718 let id = self.find_inner_item(&def)?;
720 FunctionLoc { container: container.into(), id }.intern(self.db).into(),
724 ast::Item::TypeAlias(def) => {
725 let id = self.find_inner_item(&def)?;
727 TypeAliasLoc { container: container.into(), id }.intern(self.db).into(),
731 ast::Item::Const(def) => {
732 let id = self.find_inner_item(&def)?;
734 ConstLoc { container: container.into(), id }.intern(self.db).into(),
738 ast::Item::Static(def) => {
739 let id = self.find_inner_item(&def)?;
740 (StaticLoc { container, id }.intern(self.db).into(), def.name())
742 ast::Item::Struct(def) => {
743 let id = self.find_inner_item(&def)?;
744 (StructLoc { container, id }.intern(self.db).into(), def.name())
746 ast::Item::Enum(def) => {
747 let id = self.find_inner_item(&def)?;
748 (EnumLoc { container, id }.intern(self.db).into(), def.name())
750 ast::Item::Union(def) => {
751 let id = self.find_inner_item(&def)?;
752 (UnionLoc { container, id }.intern(self.db).into(), def.name())
754 ast::Item::Trait(def) => {
755 let id = self.find_inner_item(&def)?;
756 (TraitLoc { container, id }.intern(self.db).into(), def.name())
758 ast::Item::ExternBlock(_) => return None, // FIXME: collect from extern blocks
761 | ast::Item::ExternCrate(_)
762 | ast::Item::Module(_)
763 | ast::Item::MacroCall(_) => return None,
764 ast::Item::MacroRules(def) => {
765 return Some(Either::Right(ast::Macro::from(def)));
767 ast::Item::MacroDef(def) => {
768 return Some(Either::Right(ast::Macro::from(def)));
772 Some(Either::Left((def, name)))
774 .collect::<Vec<_>>();
776 for either in items {
778 Either::Left((def, name)) => {
779 self.body.item_scope.define_def(def);
780 if let Some(name) = name {
781 let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
782 let has_constructor = match def {
783 ModuleDefId::AdtId(AdtId::StructId(s)) => {
784 self.db.struct_data(s).variant_data.kind() != StructKind::Record
788 self.body.item_scope.push_res(
790 crate::per_ns::PerNs::from_def(def, vis, has_constructor),
794 Either::Right(e) => {
795 let mac = MacroDefId {
796 krate: self.expander.module.krate,
797 ast_id: Some(self.expander.ast_id(&e)),
798 kind: MacroDefKind::Declarative,
801 if let Some(name) = e.name() {
802 self.body.item_scope.define_legacy_macro(name.as_name(), mac);
809 fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
810 if let Some(block) = expr {
811 self.collect_block(block)
817 fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
818 let pattern = match &pat {
819 ast::Pat::IdentPat(bp) => {
820 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
822 BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
823 let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
824 if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
825 // This could also be a single-segment path pattern. To
826 // decide that, we need to try resolving the name.
827 let (resolved, _) = self.expander.crate_def_map.resolve_path(
829 self.expander.module.local_id,
830 &name.clone().into(),
831 BuiltinShadowMode::Other,
833 match resolved.take_values() {
834 Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
835 Some(ModuleDefId::EnumVariantId(_)) => {
836 // this is only really valid for unit variants, but
837 // shadowing other enum variants with a pattern is
839 Pat::Path(name.into())
841 Some(ModuleDefId::AdtId(AdtId::StructId(s)))
842 if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
844 // Funnily enough, record structs *can* be shadowed
845 // by pattern bindings (but unit or tuple structs
847 Pat::Path(name.into())
849 // shadowing statics is an error as well, so we just ignore that case here
850 _ => Pat::Bind { name, mode: annotation, subpat },
853 Pat::Bind { name, mode: annotation, subpat }
856 ast::Pat::TupleStructPat(p) => {
857 let path = p.path().and_then(|path| self.expander.parse_path(path));
858 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
859 Pat::TupleStruct { path, args, ellipsis }
861 ast::Pat::RefPat(p) => {
862 let pat = self.collect_pat_opt(p.pat());
863 let mutability = Mutability::from_mutable(p.mut_token().is_some());
864 Pat::Ref { pat, mutability }
866 ast::Pat::PathPat(p) => {
867 let path = p.path().and_then(|path| self.expander.parse_path(path));
868 path.map(Pat::Path).unwrap_or(Pat::Missing)
870 ast::Pat::OrPat(p) => {
871 let pats = p.pats().map(|p| self.collect_pat(p)).collect();
874 ast::Pat::ParenPat(p) => return self.collect_pat_opt(p.pat()),
875 ast::Pat::TuplePat(p) => {
876 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
877 Pat::Tuple { args, ellipsis }
879 ast::Pat::WildcardPat(_) => Pat::Wild,
880 ast::Pat::RecordPat(p) => {
881 let path = p.path().and_then(|path| self.expander.parse_path(path));
883 .record_pat_field_list()
884 .expect("every struct should have a field list")
887 let ast_pat = f.pat()?;
888 let pat = self.collect_pat(ast_pat);
889 let name = f.field_name()?.as_name();
890 Some(RecordFieldPat { name, pat })
895 .record_pat_field_list()
896 .expect("every struct should have a field list")
900 Pat::Record { path, args, ellipsis }
902 ast::Pat::SlicePat(p) => {
903 let SlicePatComponents { prefix, slice, suffix } = p.components();
905 // FIXME properly handle `RestPat`
907 prefix: prefix.into_iter().map(|p| self.collect_pat(p)).collect(),
908 slice: slice.map(|p| self.collect_pat(p)),
909 suffix: suffix.into_iter().map(|p| self.collect_pat(p)).collect(),
912 ast::Pat::LiteralPat(lit) => {
913 if let Some(ast_lit) = lit.literal() {
914 let expr = Expr::Literal(ast_lit.kind().into());
915 let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
916 let expr_id = self.alloc_expr(expr, expr_ptr);
922 ast::Pat::RestPat(_) => {
923 // `RestPat` requires special handling and should not be mapped
924 // to a Pat. Here we are using `Pat::Missing` as a fallback for
925 // when `RestPat` is mapped to `Pat`, which can easily happen
926 // when the source code being analyzed has a malformed pattern
927 // which includes `..` in a place where it isn't valid.
931 ast::Pat::BoxPat(boxpat) => {
932 let inner = self.collect_pat_opt(boxpat.pat());
935 ast::Pat::ConstBlockPat(const_block_pat) => {
936 if let Some(expr) = const_block_pat.block_expr() {
937 let expr_id = self.collect_block(expr);
938 Pat::ConstBlock(expr_id)
944 ast::Pat::RangePat(_) | ast::Pat::MacroPat(_) => Pat::Missing,
946 let ptr = AstPtr::new(&pat);
947 self.alloc_pat(pattern, Either::Left(ptr))
950 fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
951 if let Some(pat) = pat {
952 self.collect_pat(pat)
958 fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
959 // Find the location of the `..`, if there is one. Note that we do not
960 // consider the possiblity of there being multiple `..` here.
961 let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
962 // We want to skip the `..` pattern here, since we account for it above.
964 .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
965 .map(|p| self.collect_pat(p))
971 /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
973 fn check_cfg(&mut self, owner: &dyn ast::AttrsOwner) -> Option<()> {
974 match self.expander.parse_attrs(self.db, owner).cfg() {
976 if self.expander.cfg_options().check(&cfg) != Some(false) {
980 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode(InactiveCode {
981 file: self.expander.current_file_id,
982 node: SyntaxNodePtr::new(owner.syntax()),
984 opts: self.expander.cfg_options().clone(),
994 impl From<ast::BinOp> for BinaryOp {
995 fn from(ast_op: ast::BinOp) -> Self {
997 ast::BinOp::BooleanOr => BinaryOp::LogicOp(LogicOp::Or),
998 ast::BinOp::BooleanAnd => BinaryOp::LogicOp(LogicOp::And),
999 ast::BinOp::EqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: false }),
1000 ast::BinOp::NegatedEqualityTest => BinaryOp::CmpOp(CmpOp::Eq { negated: true }),
1001 ast::BinOp::LesserEqualTest => {
1002 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: false })
1004 ast::BinOp::GreaterEqualTest => {
1005 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: false })
1007 ast::BinOp::LesserTest => {
1008 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Less, strict: true })
1010 ast::BinOp::GreaterTest => {
1011 BinaryOp::CmpOp(CmpOp::Ord { ordering: Ordering::Greater, strict: true })
1013 ast::BinOp::Addition => BinaryOp::ArithOp(ArithOp::Add),
1014 ast::BinOp::Multiplication => BinaryOp::ArithOp(ArithOp::Mul),
1015 ast::BinOp::Subtraction => BinaryOp::ArithOp(ArithOp::Sub),
1016 ast::BinOp::Division => BinaryOp::ArithOp(ArithOp::Div),
1017 ast::BinOp::Remainder => BinaryOp::ArithOp(ArithOp::Rem),
1018 ast::BinOp::LeftShift => BinaryOp::ArithOp(ArithOp::Shl),
1019 ast::BinOp::RightShift => BinaryOp::ArithOp(ArithOp::Shr),
1020 ast::BinOp::BitwiseXor => BinaryOp::ArithOp(ArithOp::BitXor),
1021 ast::BinOp::BitwiseOr => BinaryOp::ArithOp(ArithOp::BitOr),
1022 ast::BinOp::BitwiseAnd => BinaryOp::ArithOp(ArithOp::BitAnd),
1023 ast::BinOp::Assignment => BinaryOp::Assignment { op: None },
1024 ast::BinOp::AddAssign => BinaryOp::Assignment { op: Some(ArithOp::Add) },
1025 ast::BinOp::DivAssign => BinaryOp::Assignment { op: Some(ArithOp::Div) },
1026 ast::BinOp::MulAssign => BinaryOp::Assignment { op: Some(ArithOp::Mul) },
1027 ast::BinOp::RemAssign => BinaryOp::Assignment { op: Some(ArithOp::Rem) },
1028 ast::BinOp::ShlAssign => BinaryOp::Assignment { op: Some(ArithOp::Shl) },
1029 ast::BinOp::ShrAssign => BinaryOp::Assignment { op: Some(ArithOp::Shr) },
1030 ast::BinOp::SubAssign => BinaryOp::Assignment { op: Some(ArithOp::Sub) },
1031 ast::BinOp::BitOrAssign => BinaryOp::Assignment { op: Some(ArithOp::BitOr) },
1032 ast::BinOp::BitAndAssign => BinaryOp::Assignment { op: Some(ArithOp::BitAnd) },
1033 ast::BinOp::BitXorAssign => BinaryOp::Assignment { op: Some(ArithOp::BitXor) },
1038 impl From<ast::LiteralKind> for Literal {
1039 fn from(ast_lit_kind: ast::LiteralKind) -> Self {
1040 match ast_lit_kind {
1041 LiteralKind::IntNumber(lit) => {
1042 if let Some(float_suffix) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
1043 return Literal::Float(Default::default(), Some(float_suffix));
1045 let ty = lit.suffix().and_then(|it| BuiltinInt::from_suffix(&it));
1046 Literal::Int(Default::default(), ty)
1048 LiteralKind::FloatNumber(lit) => {
1049 let ty = lit.suffix().and_then(|it| BuiltinFloat::from_suffix(&it));
1050 Literal::Float(Default::default(), ty)
1052 LiteralKind::ByteString(_) => Literal::ByteString(Default::default()),
1053 LiteralKind::String(_) => Literal::String(Default::default()),
1054 LiteralKind::Byte => Literal::Int(Default::default(), Some(BuiltinInt::U8)),
1055 LiteralKind::Bool(val) => Literal::Bool(val),
1056 LiteralKind::Char => Literal::Char(Default::default()),