]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs
Rollup merge of #100092 - compiler-errors:issue-100075, r=oli-obk
[rust.git] / src / tools / rust-analyzer / crates / hir-def / src / body / lower.rs
1 //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
2 //! representation.
3
4 use std::{mem, sync::Arc};
5
6 use either::Either;
7 use hir_expand::{
8     ast_id_map::AstIdMap,
9     hygiene::Hygiene,
10     name::{name, AsName, Name},
11     AstId, ExpandError, HirFileId, InFile,
12 };
13 use la_arena::Arena;
14 use once_cell::unsync::OnceCell;
15 use profile::Count;
16 use rustc_hash::FxHashMap;
17 use syntax::{
18     ast::{
19         self, ArrayExprKind, AstChildren, HasArgList, HasLoopBody, HasName, LiteralKind,
20         SlicePatComponents,
21     },
22     AstNode, AstPtr, SyntaxNodePtr,
23 };
24
25 use crate::{
26     adt::StructKind,
27     body::{Body, BodySourceMap, Expander, LabelSource, PatPtr, SyntheticSyntax},
28     body::{BodyDiagnostic, ExprSource, PatSource},
29     builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
30     db::DefDatabase,
31     expr::{
32         dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId,
33         Literal, MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
34     },
35     intern::Interned,
36     item_scope::BuiltinShadowMode,
37     path::{GenericArgs, Path},
38     type_ref::{Mutability, Rawness, TypeRef},
39     AdtId, BlockLoc, ModuleDefId, UnresolvedMacro,
40 };
41
42 pub struct LowerCtx<'a> {
43     pub db: &'a dyn DefDatabase,
44     hygiene: Hygiene,
45     ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
46 }
47
48 impl<'a> LowerCtx<'a> {
49     pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
50         LowerCtx {
51             db,
52             hygiene: Hygiene::new(db.upcast(), file_id),
53             ast_id_map: Some((file_id, OnceCell::new())),
54         }
55     }
56
57     pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
58         LowerCtx { db, hygiene: hygiene.clone(), ast_id_map: None }
59     }
60
61     pub(crate) fn hygiene(&self) -> &Hygiene {
62         &self.hygiene
63     }
64
65     pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
66         Path::from_src(ast, self)
67     }
68
69     pub(crate) fn ast_id<N: AstNode>(&self, db: &dyn DefDatabase, item: &N) -> Option<AstId<N>> {
70         let &(file_id, ref ast_id_map) = self.ast_id_map.as_ref()?;
71         let ast_id_map = ast_id_map.get_or_init(|| db.ast_id_map(file_id));
72         Some(InFile::new(file_id, ast_id_map.ast_id(item)))
73     }
74 }
75
76 pub(super) fn lower(
77     db: &dyn DefDatabase,
78     expander: Expander,
79     params: Option<ast::ParamList>,
80     body: Option<ast::Expr>,
81 ) -> (Body, BodySourceMap) {
82     ExprCollector {
83         db,
84         source_map: BodySourceMap::default(),
85         ast_id_map: db.ast_id_map(expander.current_file_id),
86         body: Body {
87             exprs: Arena::default(),
88             pats: Arena::default(),
89             labels: Arena::default(),
90             params: Vec::new(),
91             body_expr: dummy_expr_id(),
92             block_scopes: Vec::new(),
93             _c: Count::new(),
94             or_pats: Default::default(),
95         },
96         expander,
97         name_to_pat_grouping: Default::default(),
98         is_lowering_inside_or_pat: false,
99         is_lowering_assignee_expr: false,
100     }
101     .collect(params, body)
102 }
103
104 struct ExprCollector<'a> {
105     db: &'a dyn DefDatabase,
106     expander: Expander,
107     ast_id_map: Arc<AstIdMap>,
108     body: Body,
109     source_map: BodySourceMap,
110     // a poor-mans union-find?
111     name_to_pat_grouping: FxHashMap<Name, Vec<PatId>>,
112     is_lowering_inside_or_pat: bool,
113     is_lowering_assignee_expr: bool,
114 }
115
116 impl ExprCollector<'_> {
117     fn collect(
118         mut self,
119         param_list: Option<ast::ParamList>,
120         body: Option<ast::Expr>,
121     ) -> (Body, BodySourceMap) {
122         if let Some(param_list) = param_list {
123             if let Some(self_param) = param_list.self_param() {
124                 let ptr = AstPtr::new(&self_param);
125                 let param_pat = self.alloc_pat(
126                     Pat::Bind {
127                         name: name![self],
128                         mode: BindingAnnotation::new(
129                             self_param.mut_token().is_some() && self_param.amp_token().is_none(),
130                             false,
131                         ),
132                         subpat: None,
133                     },
134                     Either::Right(ptr),
135                 );
136                 self.body.params.push(param_pat);
137             }
138
139             for pat in param_list.params().filter_map(|param| param.pat()) {
140                 let param_pat = self.collect_pat(pat);
141                 self.body.params.push(param_pat);
142             }
143         };
144
145         self.body.body_expr = self.collect_expr_opt(body);
146         (self.body, self.source_map)
147     }
148
149     fn ctx(&self) -> LowerCtx<'_> {
150         LowerCtx::new(self.db, self.expander.current_file_id)
151     }
152
153     fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
154         let src = self.expander.to_source(ptr);
155         let id = self.make_expr(expr, Ok(src.clone()));
156         self.source_map.expr_map.insert(src, id);
157         id
158     }
159     // desugared exprs don't have ptr, that's wrong and should be fixed
160     // somehow.
161     fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
162         self.make_expr(expr, Err(SyntheticSyntax))
163     }
164     fn missing_expr(&mut self) -> ExprId {
165         self.alloc_expr_desugared(Expr::Missing)
166     }
167     fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId {
168         let id = self.body.exprs.alloc(expr);
169         self.source_map.expr_map_back.insert(id, src);
170         id
171     }
172
173     fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
174         let src = self.expander.to_source(ptr);
175         let id = self.make_pat(pat, Ok(src.clone()));
176         self.source_map.pat_map.insert(src, id);
177         id
178     }
179     fn missing_pat(&mut self) -> PatId {
180         self.make_pat(Pat::Missing, Err(SyntheticSyntax))
181     }
182     fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId {
183         let id = self.body.pats.alloc(pat);
184         self.source_map.pat_map_back.insert(id, src);
185         id
186     }
187
188     fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
189         let src = self.expander.to_source(ptr);
190         let id = self.make_label(label, src.clone());
191         self.source_map.label_map.insert(src, id);
192         id
193     }
194     fn make_label(&mut self, label: Label, src: LabelSource) -> LabelId {
195         let id = self.body.labels.alloc(label);
196         self.source_map.label_map_back.insert(id, src);
197         id
198     }
199
200     fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
201         self.maybe_collect_expr(expr).unwrap_or_else(|| self.missing_expr())
202     }
203
204     /// Returns `None` if and only if the expression is `#[cfg]`d out.
205     fn maybe_collect_expr(&mut self, expr: ast::Expr) -> Option<ExprId> {
206         let syntax_ptr = AstPtr::new(&expr);
207         self.check_cfg(&expr)?;
208
209         Some(match expr {
210             ast::Expr::IfExpr(e) => {
211                 let then_branch = self.collect_block_opt(e.then_branch());
212
213                 let else_branch = e.else_branch().map(|b| match b {
214                     ast::ElseBranch::Block(it) => self.collect_block(it),
215                     ast::ElseBranch::IfExpr(elif) => {
216                         let expr: ast::Expr = ast::Expr::cast(elif.syntax().clone()).unwrap();
217                         self.collect_expr(expr)
218                     }
219                 });
220
221                 let condition = self.collect_expr_opt(e.condition());
222
223                 self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
224             }
225             ast::Expr::LetExpr(e) => {
226                 let pat = self.collect_pat_opt(e.pat());
227                 let expr = self.collect_expr_opt(e.expr());
228                 self.alloc_expr(Expr::Let { pat, expr }, syntax_ptr)
229             }
230             ast::Expr::BlockExpr(e) => match e.modifier() {
231                 Some(ast::BlockModifier::Try(_)) => {
232                     let body = self.collect_block(e);
233                     self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
234                 }
235                 Some(ast::BlockModifier::Unsafe(_)) => {
236                     let body = self.collect_block(e);
237                     self.alloc_expr(Expr::Unsafe { body }, syntax_ptr)
238                 }
239                 // FIXME: we need to record these effects somewhere...
240                 Some(ast::BlockModifier::Label(label)) => {
241                     let label = self.collect_label(label);
242                     let res = self.collect_block(e);
243                     match &mut self.body.exprs[res] {
244                         Expr::Block { label: block_label, .. } => {
245                             *block_label = Some(label);
246                         }
247                         _ => unreachable!(),
248                     }
249                     res
250                 }
251                 Some(ast::BlockModifier::Async(_)) => {
252                     let body = self.collect_block(e);
253                     self.alloc_expr(Expr::Async { body }, syntax_ptr)
254                 }
255                 Some(ast::BlockModifier::Const(_)) => {
256                     let body = self.collect_block(e);
257                     self.alloc_expr(Expr::Const { body }, syntax_ptr)
258                 }
259                 None => self.collect_block(e),
260             },
261             ast::Expr::LoopExpr(e) => {
262                 let label = e.label().map(|label| self.collect_label(label));
263                 let body = self.collect_block_opt(e.loop_body());
264                 self.alloc_expr(Expr::Loop { body, label }, syntax_ptr)
265             }
266             ast::Expr::WhileExpr(e) => {
267                 let label = e.label().map(|label| self.collect_label(label));
268                 let body = self.collect_block_opt(e.loop_body());
269
270                 let condition = self.collect_expr_opt(e.condition());
271
272                 self.alloc_expr(Expr::While { condition, body, label }, syntax_ptr)
273             }
274             ast::Expr::ForExpr(e) => {
275                 let label = e.label().map(|label| self.collect_label(label));
276                 let iterable = self.collect_expr_opt(e.iterable());
277                 let pat = self.collect_pat_opt(e.pat());
278                 let body = self.collect_block_opt(e.loop_body());
279                 self.alloc_expr(Expr::For { iterable, pat, body, label }, syntax_ptr)
280             }
281             ast::Expr::CallExpr(e) => {
282                 let callee = self.collect_expr_opt(e.expr());
283                 let args = if let Some(arg_list) = e.arg_list() {
284                     arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
285                 } else {
286                     Box::default()
287                 };
288                 self.alloc_expr(
289                     Expr::Call { callee, args, is_assignee_expr: self.is_lowering_assignee_expr },
290                     syntax_ptr,
291                 )
292             }
293             ast::Expr::MethodCallExpr(e) => {
294                 let receiver = self.collect_expr_opt(e.receiver());
295                 let args = if let Some(arg_list) = e.arg_list() {
296                     arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
297                 } else {
298                     Box::default()
299                 };
300                 let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
301                 let generic_args = e
302                     .generic_arg_list()
303                     .and_then(|it| GenericArgs::from_ast(&self.ctx(), it))
304                     .map(Box::new);
305                 self.alloc_expr(
306                     Expr::MethodCall { receiver, method_name, args, generic_args },
307                     syntax_ptr,
308                 )
309             }
310             ast::Expr::MatchExpr(e) => {
311                 let expr = self.collect_expr_opt(e.expr());
312                 let arms = if let Some(match_arm_list) = e.match_arm_list() {
313                     match_arm_list
314                         .arms()
315                         .filter_map(|arm| {
316                             self.check_cfg(&arm).map(|()| MatchArm {
317                                 pat: self.collect_pat_opt(arm.pat()),
318                                 expr: self.collect_expr_opt(arm.expr()),
319                                 guard: arm
320                                     .guard()
321                                     .map(|guard| self.collect_expr_opt(guard.condition())),
322                             })
323                         })
324                         .collect()
325                 } else {
326                     Box::default()
327                 };
328                 self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
329             }
330             ast::Expr::PathExpr(e) => {
331                 let path = e
332                     .path()
333                     .and_then(|path| self.expander.parse_path(self.db, path))
334                     .map(Expr::Path)
335                     .unwrap_or(Expr::Missing);
336                 self.alloc_expr(path, syntax_ptr)
337             }
338             ast::Expr::ContinueExpr(e) => self.alloc_expr(
339                 Expr::Continue { label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
340                 syntax_ptr,
341             ),
342             ast::Expr::BreakExpr(e) => {
343                 let expr = e.expr().map(|e| self.collect_expr(e));
344                 self.alloc_expr(
345                     Expr::Break { expr, label: e.lifetime().map(|l| Name::new_lifetime(&l)) },
346                     syntax_ptr,
347                 )
348             }
349             ast::Expr::ParenExpr(e) => {
350                 let inner = self.collect_expr_opt(e.expr());
351                 // make the paren expr point to the inner expression as well
352                 let src = self.expander.to_source(syntax_ptr);
353                 self.source_map.expr_map.insert(src, inner);
354                 inner
355             }
356             ast::Expr::ReturnExpr(e) => {
357                 let expr = e.expr().map(|e| self.collect_expr(e));
358                 self.alloc_expr(Expr::Return { expr }, syntax_ptr)
359             }
360             ast::Expr::YieldExpr(e) => {
361                 let expr = e.expr().map(|e| self.collect_expr(e));
362                 self.alloc_expr(Expr::Yield { expr }, syntax_ptr)
363             }
364             ast::Expr::RecordExpr(e) => {
365                 let path =
366                     e.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
367                 let is_assignee_expr = self.is_lowering_assignee_expr;
368                 let record_lit = if let Some(nfl) = e.record_expr_field_list() {
369                     let fields = nfl
370                         .fields()
371                         .filter_map(|field| {
372                             self.check_cfg(&field)?;
373
374                             let name = field.field_name()?.as_name();
375
376                             let expr = match field.expr() {
377                                 Some(e) => self.collect_expr(e),
378                                 None => self.missing_expr(),
379                             };
380                             let src = self.expander.to_source(AstPtr::new(&field));
381                             self.source_map.field_map.insert(src.clone(), expr);
382                             self.source_map.field_map_back.insert(expr, src);
383                             Some(RecordLitField { name, expr })
384                         })
385                         .collect();
386                     let spread = nfl.spread().map(|s| self.collect_expr(s));
387                     let ellipsis = nfl.dotdot_token().is_some();
388                     Expr::RecordLit { path, fields, spread, ellipsis, is_assignee_expr }
389                 } else {
390                     Expr::RecordLit {
391                         path,
392                         fields: Box::default(),
393                         spread: None,
394                         ellipsis: false,
395                         is_assignee_expr,
396                     }
397                 };
398
399                 self.alloc_expr(record_lit, syntax_ptr)
400             }
401             ast::Expr::FieldExpr(e) => {
402                 let expr = self.collect_expr_opt(e.expr());
403                 let name = match e.field_access() {
404                     Some(kind) => kind.as_name(),
405                     _ => Name::missing(),
406                 };
407                 self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
408             }
409             ast::Expr::AwaitExpr(e) => {
410                 let expr = self.collect_expr_opt(e.expr());
411                 self.alloc_expr(Expr::Await { expr }, syntax_ptr)
412             }
413             ast::Expr::TryExpr(e) => {
414                 let expr = self.collect_expr_opt(e.expr());
415                 self.alloc_expr(Expr::Try { expr }, syntax_ptr)
416             }
417             ast::Expr::CastExpr(e) => {
418                 let expr = self.collect_expr_opt(e.expr());
419                 let type_ref = Interned::new(TypeRef::from_ast_opt(&self.ctx(), e.ty()));
420                 self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
421             }
422             ast::Expr::RefExpr(e) => {
423                 let expr = self.collect_expr_opt(e.expr());
424                 let raw_tok = e.raw_token().is_some();
425                 let mutability = if raw_tok {
426                     if e.mut_token().is_some() {
427                         Mutability::Mut
428                     } else if e.const_token().is_some() {
429                         Mutability::Shared
430                     } else {
431                         unreachable!("parser only remaps to raw_token() if matching mutability token follows")
432                     }
433                 } else {
434                     Mutability::from_mutable(e.mut_token().is_some())
435                 };
436                 let rawness = Rawness::from_raw(raw_tok);
437                 self.alloc_expr(Expr::Ref { expr, rawness, mutability }, syntax_ptr)
438             }
439             ast::Expr::PrefixExpr(e) => {
440                 let expr = self.collect_expr_opt(e.expr());
441                 match e.op_kind() {
442                     Some(op) => self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr),
443                     None => self.alloc_expr(Expr::Missing, syntax_ptr),
444                 }
445             }
446             ast::Expr::ClosureExpr(e) => {
447                 let mut args = Vec::new();
448                 let mut arg_types = Vec::new();
449                 if let Some(pl) = e.param_list() {
450                     for param in pl.params() {
451                         let pat = self.collect_pat_opt(param.pat());
452                         let type_ref =
453                             param.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
454                         args.push(pat);
455                         arg_types.push(type_ref);
456                     }
457                 }
458                 let ret_type = e
459                     .ret_type()
460                     .and_then(|r| r.ty())
461                     .map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
462                 let body = self.collect_expr_opt(e.body());
463                 self.alloc_expr(
464                     Expr::Closure {
465                         args: args.into(),
466                         arg_types: arg_types.into(),
467                         ret_type,
468                         body,
469                     },
470                     syntax_ptr,
471                 )
472             }
473             ast::Expr::BinExpr(e) => {
474                 let op = e.op_kind();
475                 if let Some(ast::BinaryOp::Assignment { op: None }) = op {
476                     self.is_lowering_assignee_expr = true;
477                 }
478                 let lhs = self.collect_expr_opt(e.lhs());
479                 self.is_lowering_assignee_expr = false;
480                 let rhs = self.collect_expr_opt(e.rhs());
481                 self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
482             }
483             ast::Expr::TupleExpr(e) => {
484                 let exprs = e.fields().map(|expr| self.collect_expr(expr)).collect();
485                 self.alloc_expr(
486                     Expr::Tuple { exprs, is_assignee_expr: self.is_lowering_assignee_expr },
487                     syntax_ptr,
488                 )
489             }
490             ast::Expr::BoxExpr(e) => {
491                 let expr = self.collect_expr_opt(e.expr());
492                 self.alloc_expr(Expr::Box { expr }, syntax_ptr)
493             }
494
495             ast::Expr::ArrayExpr(e) => {
496                 let kind = e.kind();
497
498                 match kind {
499                     ArrayExprKind::ElementList(e) => {
500                         let elements = e.map(|expr| self.collect_expr(expr)).collect();
501                         self.alloc_expr(
502                             Expr::Array(Array::ElementList {
503                                 elements,
504                                 is_assignee_expr: self.is_lowering_assignee_expr,
505                             }),
506                             syntax_ptr,
507                         )
508                     }
509                     ArrayExprKind::Repeat { initializer, repeat } => {
510                         let initializer = self.collect_expr_opt(initializer);
511                         let repeat = self.collect_expr_opt(repeat);
512                         self.alloc_expr(
513                             Expr::Array(Array::Repeat { initializer, repeat }),
514                             syntax_ptr,
515                         )
516                     }
517                 }
518             }
519
520             ast::Expr::Literal(e) => self.alloc_expr(Expr::Literal(e.kind().into()), syntax_ptr),
521             ast::Expr::IndexExpr(e) => {
522                 let base = self.collect_expr_opt(e.base());
523                 let index = self.collect_expr_opt(e.index());
524                 self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
525             }
526             ast::Expr::RangeExpr(e) => {
527                 let lhs = e.start().map(|lhs| self.collect_expr(lhs));
528                 let rhs = e.end().map(|rhs| self.collect_expr(rhs));
529                 match e.op_kind() {
530                     Some(range_type) => {
531                         self.alloc_expr(Expr::Range { lhs, rhs, range_type }, syntax_ptr)
532                     }
533                     None => self.alloc_expr(Expr::Missing, syntax_ptr),
534                 }
535             }
536             ast::Expr::MacroExpr(e) => {
537                 let e = e.macro_call()?;
538                 let macro_ptr = AstPtr::new(&e);
539                 let id = self.collect_macro_call(e, macro_ptr, true, |this, expansion| {
540                     expansion.map(|it| this.collect_expr(it))
541                 });
542                 match id {
543                     Some(id) => {
544                         // Make the macro-call point to its expanded expression so we can query
545                         // semantics on syntax pointers to the macro
546                         let src = self.expander.to_source(syntax_ptr);
547                         self.source_map.expr_map.insert(src, id);
548                         id
549                     }
550                     None => self.alloc_expr(Expr::Missing, syntax_ptr),
551                 }
552             }
553             ast::Expr::MacroStmts(e) => {
554                 let statements: Box<[_]> =
555                     e.statements().filter_map(|s| self.collect_stmt(s)).collect();
556                 let tail = e.expr().map(|e| self.collect_expr(e));
557
558                 if e.syntax().children().next().is_none() {
559                     // HACK: make sure that macros that expand to nothing aren't treated as a `()`
560                     // expression when used in block tail position.
561                     cov_mark::hit!(empty_macro_in_trailing_position_is_removed);
562                     return None;
563                 }
564
565                 self.alloc_expr(Expr::MacroStmts { tail, statements }, syntax_ptr)
566             }
567             ast::Expr::UnderscoreExpr(_) => self.alloc_expr(Expr::Underscore, syntax_ptr),
568         })
569     }
570
571     fn collect_macro_call<F, T, U>(
572         &mut self,
573         mcall: ast::MacroCall,
574         syntax_ptr: AstPtr<ast::MacroCall>,
575         record_diagnostics: bool,
576         collector: F,
577     ) -> U
578     where
579         F: FnOnce(&mut Self, Option<T>) -> U,
580         T: ast::AstNode,
581     {
582         // File containing the macro call. Expansion errors will be attached here.
583         let outer_file = self.expander.current_file_id;
584
585         let macro_call_ptr = self.expander.to_source(AstPtr::new(&mcall));
586         let res = self.expander.enter_expand(self.db, mcall);
587
588         let res = match res {
589             Ok(res) => res,
590             Err(UnresolvedMacro { path }) => {
591                 if record_diagnostics {
592                     self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedMacroCall {
593                         node: InFile::new(outer_file, syntax_ptr),
594                         path,
595                     });
596                 }
597                 return collector(self, None);
598             }
599         };
600
601         if record_diagnostics {
602             match &res.err {
603                 Some(ExpandError::UnresolvedProcMacro(krate)) => {
604                     self.source_map.diagnostics.push(BodyDiagnostic::UnresolvedProcMacro {
605                         node: InFile::new(outer_file, syntax_ptr),
606                         krate: *krate,
607                     });
608                 }
609                 Some(err) => {
610                     self.source_map.diagnostics.push(BodyDiagnostic::MacroError {
611                         node: InFile::new(outer_file, syntax_ptr),
612                         message: err.to_string(),
613                     });
614                 }
615                 None => {}
616             }
617         }
618
619         match res.value {
620             Some((mark, expansion)) => {
621                 self.source_map.expansions.insert(macro_call_ptr, self.expander.current_file_id);
622                 let prev_ast_id_map = mem::replace(
623                     &mut self.ast_id_map,
624                     self.db.ast_id_map(self.expander.current_file_id),
625                 );
626
627                 let id = collector(self, Some(expansion));
628                 self.ast_id_map = prev_ast_id_map;
629                 self.expander.exit(self.db, mark);
630                 id
631             }
632             None => collector(self, None),
633         }
634     }
635
636     fn collect_expr_opt(&mut self, expr: Option<ast::Expr>) -> ExprId {
637         match expr {
638             Some(expr) => self.collect_expr(expr),
639             None => self.missing_expr(),
640         }
641     }
642
643     fn collect_stmt(&mut self, s: ast::Stmt) -> Option<Statement> {
644         match s {
645             ast::Stmt::LetStmt(stmt) => {
646                 if self.check_cfg(&stmt).is_none() {
647                     return None;
648                 }
649                 let pat = self.collect_pat_opt(stmt.pat());
650                 let type_ref =
651                     stmt.ty().map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
652                 let initializer = stmt.initializer().map(|e| self.collect_expr(e));
653                 let else_branch = stmt
654                     .let_else()
655                     .and_then(|let_else| let_else.block_expr())
656                     .map(|block| self.collect_block(block));
657                 Some(Statement::Let { pat, type_ref, initializer, else_branch })
658             }
659             ast::Stmt::ExprStmt(stmt) => {
660                 let expr = stmt.expr();
661                 if let Some(expr) = &expr {
662                     if self.check_cfg(expr).is_none() {
663                         return None;
664                     }
665                 }
666                 let has_semi = stmt.semicolon_token().is_some();
667                 // Note that macro could be expanded to multiple statements
668                 if let Some(expr @ ast::Expr::MacroExpr(mac)) = &expr {
669                     let mac_call = mac.macro_call()?;
670                     let syntax_ptr = AstPtr::new(expr);
671                     let macro_ptr = AstPtr::new(&mac_call);
672                     let stmt = self.collect_macro_call(
673                         mac_call,
674                         macro_ptr,
675                         false,
676                         |this, expansion: Option<ast::MacroStmts>| match expansion {
677                             Some(expansion) => {
678                                 let statements = expansion
679                                     .statements()
680                                     .filter_map(|stmt| this.collect_stmt(stmt))
681                                     .collect();
682                                 let tail = expansion.expr().map(|expr| this.collect_expr(expr));
683
684                                 let mac_stmts = this.alloc_expr(
685                                     Expr::MacroStmts { tail, statements },
686                                     AstPtr::new(&ast::Expr::MacroStmts(expansion)),
687                                 );
688
689                                 Some(mac_stmts)
690                             }
691                             None => None,
692                         },
693                     );
694
695                     let expr = match stmt {
696                         Some(expr) => {
697                             // Make the macro-call point to its expanded expression so we can query
698                             // semantics on syntax pointers to the macro
699                             let src = self.expander.to_source(syntax_ptr);
700                             self.source_map.expr_map.insert(src, expr);
701                             expr
702                         }
703                         None => self.alloc_expr(Expr::Missing, syntax_ptr),
704                     };
705                     Some(Statement::Expr { expr, has_semi })
706                 } else {
707                     let expr = self.collect_expr_opt(expr);
708                     Some(Statement::Expr { expr, has_semi })
709                 }
710             }
711             ast::Stmt::Item(_item) => None,
712         }
713     }
714
715     fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
716         let file_local_id = self.ast_id_map.ast_id(&block);
717         let ast_id = AstId::new(self.expander.current_file_id, file_local_id);
718         let block_loc =
719             BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
720         let block_id = self.db.intern_block(block_loc);
721
722         let (module, def_map) = match self.db.block_def_map(block_id) {
723             Some(def_map) => {
724                 self.body.block_scopes.push(block_id);
725                 (def_map.root(), def_map)
726             }
727             None => (self.expander.module, self.expander.def_map.clone()),
728         };
729         let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
730         let prev_local_module = mem::replace(&mut self.expander.module, module);
731
732         let mut statements: Vec<_> =
733             block.statements().filter_map(|s| self.collect_stmt(s)).collect();
734         let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));
735         let tail = tail.or_else(|| {
736             let stmt = statements.pop()?;
737             if let Statement::Expr { expr, has_semi: false } = stmt {
738                 return Some(expr);
739             }
740             statements.push(stmt);
741             None
742         });
743
744         let syntax_node_ptr = AstPtr::new(&block.into());
745         let expr_id = self.alloc_expr(
746             Expr::Block {
747                 id: block_id,
748                 statements: statements.into_boxed_slice(),
749                 tail,
750                 label: None,
751             },
752             syntax_node_ptr,
753         );
754
755         self.expander.def_map = prev_def_map;
756         self.expander.module = prev_local_module;
757         expr_id
758     }
759
760     fn collect_block_opt(&mut self, expr: Option<ast::BlockExpr>) -> ExprId {
761         match expr {
762             Some(block) => self.collect_block(block),
763             None => self.missing_expr(),
764         }
765     }
766
767     fn collect_label(&mut self, ast_label: ast::Label) -> LabelId {
768         let label = Label {
769             name: ast_label.lifetime().as_ref().map_or_else(Name::missing, Name::new_lifetime),
770         };
771         self.alloc_label(label, AstPtr::new(&ast_label))
772     }
773
774     fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
775         let pat_id = self.collect_pat_(pat);
776         for (_, pats) in self.name_to_pat_grouping.drain() {
777             let pats = Arc::<[_]>::from(pats);
778             self.body.or_pats.extend(pats.iter().map(|&pat| (pat, pats.clone())));
779         }
780         self.is_lowering_inside_or_pat = false;
781         pat_id
782     }
783
784     fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {
785         match pat {
786             Some(pat) => self.collect_pat(pat),
787             None => self.missing_pat(),
788         }
789     }
790
791     fn collect_pat_(&mut self, pat: ast::Pat) -> PatId {
792         let pattern = match &pat {
793             ast::Pat::IdentPat(bp) => {
794                 let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
795
796                 let key = self.is_lowering_inside_or_pat.then(|| name.clone());
797                 let annotation =
798                     BindingAnnotation::new(bp.mut_token().is_some(), bp.ref_token().is_some());
799                 let subpat = bp.pat().map(|subpat| self.collect_pat_(subpat));
800                 let pattern = if annotation == BindingAnnotation::Unannotated && subpat.is_none() {
801                     // This could also be a single-segment path pattern. To
802                     // decide that, we need to try resolving the name.
803                     let (resolved, _) = self.expander.def_map.resolve_path(
804                         self.db,
805                         self.expander.module,
806                         &name.clone().into(),
807                         BuiltinShadowMode::Other,
808                     );
809                     match resolved.take_values() {
810                         Some(ModuleDefId::ConstId(_)) => Pat::Path(name.into()),
811                         Some(ModuleDefId::EnumVariantId(_)) => {
812                             // this is only really valid for unit variants, but
813                             // shadowing other enum variants with a pattern is
814                             // an error anyway
815                             Pat::Path(name.into())
816                         }
817                         Some(ModuleDefId::AdtId(AdtId::StructId(s)))
818                             if self.db.struct_data(s).variant_data.kind() != StructKind::Record =>
819                         {
820                             // Funnily enough, record structs *can* be shadowed
821                             // by pattern bindings (but unit or tuple structs
822                             // can't).
823                             Pat::Path(name.into())
824                         }
825                         // shadowing statics is an error as well, so we just ignore that case here
826                         _ => Pat::Bind { name, mode: annotation, subpat },
827                     }
828                 } else {
829                     Pat::Bind { name, mode: annotation, subpat }
830                 };
831
832                 let ptr = AstPtr::new(&pat);
833                 let pat = self.alloc_pat(pattern, Either::Left(ptr));
834                 if let Some(key) = key {
835                     self.name_to_pat_grouping.entry(key).or_default().push(pat);
836                 }
837                 return pat;
838             }
839             ast::Pat::TupleStructPat(p) => {
840                 let path =
841                     p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
842                 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
843                 Pat::TupleStruct { path, args, ellipsis }
844             }
845             ast::Pat::RefPat(p) => {
846                 let pat = self.collect_pat_opt(p.pat());
847                 let mutability = Mutability::from_mutable(p.mut_token().is_some());
848                 Pat::Ref { pat, mutability }
849             }
850             ast::Pat::PathPat(p) => {
851                 let path =
852                     p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
853                 path.map(Pat::Path).unwrap_or(Pat::Missing)
854             }
855             ast::Pat::OrPat(p) => {
856                 self.is_lowering_inside_or_pat = true;
857                 let pats = p.pats().map(|p| self.collect_pat_(p)).collect();
858                 Pat::Or(pats)
859             }
860             ast::Pat::ParenPat(p) => return self.collect_pat_opt_(p.pat()),
861             ast::Pat::TuplePat(p) => {
862                 let (args, ellipsis) = self.collect_tuple_pat(p.fields());
863                 Pat::Tuple { args, ellipsis }
864             }
865             ast::Pat::WildcardPat(_) => Pat::Wild,
866             ast::Pat::RecordPat(p) => {
867                 let path =
868                     p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
869                 let args = p
870                     .record_pat_field_list()
871                     .expect("every struct should have a field list")
872                     .fields()
873                     .filter_map(|f| {
874                         let ast_pat = f.pat()?;
875                         let pat = self.collect_pat_(ast_pat);
876                         let name = f.field_name()?.as_name();
877                         Some(RecordFieldPat { name, pat })
878                     })
879                     .collect();
880
881                 let ellipsis = p
882                     .record_pat_field_list()
883                     .expect("every struct should have a field list")
884                     .rest_pat()
885                     .is_some();
886
887                 Pat::Record { path, args, ellipsis }
888             }
889             ast::Pat::SlicePat(p) => {
890                 let SlicePatComponents { prefix, slice, suffix } = p.components();
891
892                 // FIXME properly handle `RestPat`
893                 Pat::Slice {
894                     prefix: prefix.into_iter().map(|p| self.collect_pat_(p)).collect(),
895                     slice: slice.map(|p| self.collect_pat_(p)),
896                     suffix: suffix.into_iter().map(|p| self.collect_pat_(p)).collect(),
897                 }
898             }
899             ast::Pat::LiteralPat(lit) => {
900                 if let Some(ast_lit) = lit.literal() {
901                     let expr = Expr::Literal(ast_lit.kind().into());
902                     let expr_ptr = AstPtr::new(&ast::Expr::Literal(ast_lit));
903                     let expr_id = self.alloc_expr(expr, expr_ptr);
904                     Pat::Lit(expr_id)
905                 } else {
906                     Pat::Missing
907                 }
908             }
909             ast::Pat::RestPat(_) => {
910                 // `RestPat` requires special handling and should not be mapped
911                 // to a Pat. Here we are using `Pat::Missing` as a fallback for
912                 // when `RestPat` is mapped to `Pat`, which can easily happen
913                 // when the source code being analyzed has a malformed pattern
914                 // which includes `..` in a place where it isn't valid.
915
916                 Pat::Missing
917             }
918             ast::Pat::BoxPat(boxpat) => {
919                 let inner = self.collect_pat_opt_(boxpat.pat());
920                 Pat::Box { inner }
921             }
922             ast::Pat::ConstBlockPat(const_block_pat) => {
923                 if let Some(expr) = const_block_pat.block_expr() {
924                     let expr_id = self.collect_block(expr);
925                     Pat::ConstBlock(expr_id)
926                 } else {
927                     Pat::Missing
928                 }
929             }
930             ast::Pat::MacroPat(mac) => match mac.macro_call() {
931                 Some(call) => {
932                     let macro_ptr = AstPtr::new(&call);
933                     let src = self.expander.to_source(Either::Left(AstPtr::new(&pat)));
934                     let pat =
935                         self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
936                             this.collect_pat_opt_(expanded_pat)
937                         });
938                     self.source_map.pat_map.insert(src, pat);
939                     return pat;
940                 }
941                 None => Pat::Missing,
942             },
943             // FIXME: implement
944             ast::Pat::RangePat(_) => Pat::Missing,
945         };
946         let ptr = AstPtr::new(&pat);
947         self.alloc_pat(pattern, Either::Left(ptr))
948     }
949
950     fn collect_pat_opt_(&mut self, pat: Option<ast::Pat>) -> PatId {
951         match pat {
952             Some(pat) => self.collect_pat_(pat),
953             None => self.missing_pat(),
954         }
955     }
956
957     fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Box<[PatId]>, Option<usize>) {
958         // Find the location of the `..`, if there is one. Note that we do not
959         // consider the possibility of there being multiple `..` here.
960         let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
961         // We want to skip the `..` pattern here, since we account for it above.
962         let args = args
963             .filter(|p| !matches!(p, ast::Pat::RestPat(_)))
964             .map(|p| self.collect_pat_(p))
965             .collect();
966
967         (args, ellipsis)
968     }
969
970     /// Returns `None` (and emits diagnostics) when `owner` if `#[cfg]`d out, and `Some(())` when
971     /// not.
972     fn check_cfg(&mut self, owner: &dyn ast::HasAttrs) -> Option<()> {
973         match self.expander.parse_attrs(self.db, owner).cfg() {
974             Some(cfg) => {
975                 if self.expander.cfg_options().check(&cfg) != Some(false) {
976                     return Some(());
977                 }
978
979                 self.source_map.diagnostics.push(BodyDiagnostic::InactiveCode {
980                     node: InFile::new(
981                         self.expander.current_file_id,
982                         SyntaxNodePtr::new(owner.syntax()),
983                     ),
984                     cfg,
985                     opts: self.expander.cfg_options().clone(),
986                 });
987
988                 None
989             }
990             None => Some(()),
991         }
992     }
993 }
994
995 impl From<ast::LiteralKind> for Literal {
996     fn from(ast_lit_kind: ast::LiteralKind) -> Self {
997         match ast_lit_kind {
998             // FIXME: these should have actual values filled in, but unsure on perf impact
999             LiteralKind::IntNumber(lit) => {
1000                 if let builtin @ Some(_) = lit.suffix().and_then(BuiltinFloat::from_suffix) {
1001                     Literal::Float(
1002                         FloatTypeWrapper::new(lit.float_value().unwrap_or(Default::default())),
1003                         builtin,
1004                     )
1005                 } else if let builtin @ Some(_) = lit.suffix().and_then(BuiltinInt::from_suffix) {
1006                     Literal::Int(lit.value().unwrap_or(0) as i128, builtin)
1007                 } else {
1008                     let builtin = lit.suffix().and_then(BuiltinUint::from_suffix);
1009                     Literal::Uint(lit.value().unwrap_or(0), builtin)
1010                 }
1011             }
1012             LiteralKind::FloatNumber(lit) => {
1013                 let ty = lit.suffix().and_then(BuiltinFloat::from_suffix);
1014                 Literal::Float(FloatTypeWrapper::new(lit.value().unwrap_or(Default::default())), ty)
1015             }
1016             LiteralKind::ByteString(bs) => {
1017                 let text = bs.value().map(Box::from).unwrap_or_else(Default::default);
1018                 Literal::ByteString(text)
1019             }
1020             LiteralKind::String(s) => {
1021                 let text = s.value().map(Box::from).unwrap_or_else(Default::default);
1022                 Literal::String(text)
1023             }
1024             LiteralKind::Byte(b) => {
1025                 Literal::Uint(b.value().unwrap_or_default() as u128, Some(BuiltinUint::U8))
1026             }
1027             LiteralKind::Char(c) => Literal::Char(c.value().unwrap_or_default()),
1028             LiteralKind::Bool(val) => Literal::Bool(val),
1029         }
1030     }
1031 }