diff options
Diffstat (limited to 'crates/hir_def/src/body')
-rw-r--r-- | crates/hir_def/src/body/lower.rs | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/crates/hir_def/src/body/lower.rs b/crates/hir_def/src/body/lower.rs index 209965fca..bc61730a7 100644 --- a/crates/hir_def/src/body/lower.rs +++ b/crates/hir_def/src/body/lower.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` | 1 | //! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr` |
2 | //! representation. | 2 | //! representation. |
3 | 3 | ||
4 | use std::{any::type_name, sync::Arc}; | 4 | use std::{any::type_name, mem, sync::Arc}; |
5 | 5 | ||
6 | use either::Either; | 6 | use either::Either; |
7 | use hir_expand::{ | 7 | use hir_expand::{ |
@@ -36,8 +36,8 @@ use crate::{ | |||
36 | item_tree::{ItemTree, ItemTreeId, ItemTreeNode}, | 36 | item_tree::{ItemTree, ItemTreeId, ItemTreeNode}, |
37 | path::{GenericArgs, Path}, | 37 | path::{GenericArgs, Path}, |
38 | type_ref::{Mutability, Rawness, TypeRef}, | 38 | type_ref::{Mutability, Rawness, TypeRef}, |
39 | AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, | 39 | AdtId, BlockLoc, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, |
40 | StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, | 40 | ModuleDefId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
41 | }; | 41 | }; |
42 | 42 | ||
43 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; | 43 | use super::{diagnostics::BodyDiagnostic, ExprSource, PatSource}; |
@@ -152,8 +152,8 @@ impl ExprCollector<'_> { | |||
152 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { | 152 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { |
153 | self.make_expr(expr, Err(SyntheticSyntax)) | 153 | self.make_expr(expr, Err(SyntheticSyntax)) |
154 | } | 154 | } |
155 | fn empty_block(&mut self) -> ExprId { | 155 | fn unit(&mut self) -> ExprId { |
156 | self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None, label: None }) | 156 | self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() }) |
157 | } | 157 | } |
158 | fn missing_expr(&mut self) -> ExprId { | 158 | fn missing_expr(&mut self) -> ExprId { |
159 | self.alloc_expr_desugared(Expr::Missing) | 159 | self.alloc_expr_desugared(Expr::Missing) |
@@ -222,7 +222,7 @@ impl ExprCollector<'_> { | |||
222 | MatchArm { pat, expr: then_branch, guard: None }, | 222 | MatchArm { pat, expr: then_branch, guard: None }, |
223 | MatchArm { | 223 | MatchArm { |
224 | pat: placeholder_pat, | 224 | pat: placeholder_pat, |
225 | expr: else_branch.unwrap_or_else(|| self.empty_block()), | 225 | expr: else_branch.unwrap_or_else(|| self.unit()), |
226 | guard: None, | 226 | guard: None, |
227 | }, | 227 | }, |
228 | ]; | 228 | ]; |
@@ -561,7 +561,7 @@ impl ExprCollector<'_> { | |||
561 | let outer_file = self.expander.current_file_id; | 561 | let outer_file = self.expander.current_file_id; |
562 | 562 | ||
563 | let macro_call = self.expander.to_source(AstPtr::new(&e)); | 563 | let macro_call = self.expander.to_source(AstPtr::new(&e)); |
564 | let res = self.expander.enter_expand(self.db, Some(&self.body.item_scope), e); | 564 | let res = self.expander.enter_expand(self.db, e); |
565 | 565 | ||
566 | match &res.err { | 566 | match &res.err { |
567 | Some(ExpandError::UnresolvedProcMacro) => { | 567 | Some(ExpandError::UnresolvedProcMacro) => { |
@@ -697,12 +697,27 @@ impl ExprCollector<'_> { | |||
697 | } | 697 | } |
698 | 698 | ||
699 | fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { | 699 | fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId { |
700 | let syntax_node_ptr = AstPtr::new(&block.clone().into()); | 700 | let ast_id = self.expander.ast_id(&block); |
701 | let block_loc = BlockLoc { ast_id, module: self.expander.module }; | ||
702 | let block_id = self.db.intern_block(block_loc); | ||
703 | let def_map = self.db.block_def_map(block_id); | ||
704 | let root = def_map.module_id(def_map.root()); | ||
705 | let prev_def_map = mem::replace(&mut self.expander.def_map, def_map); | ||
706 | let prev_module = mem::replace(&mut self.expander.module, root); | ||
707 | |||
701 | self.collect_stmts_items(block.statements()); | 708 | self.collect_stmts_items(block.statements()); |
702 | let statements = | 709 | let statements = |
703 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); | 710 | block.statements().filter_map(|s| self.collect_stmt(s)).flatten().collect(); |
704 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); | 711 | let tail = block.tail_expr().map(|e| self.collect_expr(e)); |
705 | self.alloc_expr(Expr::Block { statements, tail, label: None }, syntax_node_ptr) | 712 | let syntax_node_ptr = AstPtr::new(&block.clone().into()); |
713 | let expr_id = self.alloc_expr( | ||
714 | Expr::Block { id: block_id, statements, tail, label: None }, | ||
715 | syntax_node_ptr, | ||
716 | ); | ||
717 | |||
718 | self.expander.def_map = prev_def_map; | ||
719 | self.expander.module = prev_module; | ||
720 | expr_id | ||
706 | } | 721 | } |
707 | 722 | ||
708 | fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { | 723 | fn collect_stmts_items(&mut self, stmts: ast::AstChildren<ast::Stmt>) { |
@@ -832,7 +847,7 @@ impl ExprCollector<'_> { | |||
832 | if annotation == BindingAnnotation::Unannotated && subpat.is_none() { | 847 | if annotation == BindingAnnotation::Unannotated && subpat.is_none() { |
833 | // This could also be a single-segment path pattern. To | 848 | // This could also be a single-segment path pattern. To |
834 | // decide that, we need to try resolving the name. | 849 | // decide that, we need to try resolving the name. |
835 | let (resolved, _) = self.expander.crate_def_map.resolve_path( | 850 | let (resolved, _) = self.expander.def_map.resolve_path( |
836 | self.db, | 851 | self.db, |
837 | self.expander.module.local_id, | 852 | self.expander.module.local_id, |
838 | &name.clone().into(), | 853 | &name.clone().into(), |