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