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