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