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