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