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