aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body.rs13
-rw-r--r--crates/ra_hir_def/src/body/lower.rs31
2 files changed, 25 insertions, 19 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 269501221..7e84e9113 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -18,12 +18,18 @@ use crate::{
18 18
19pub struct MacroResolver { 19pub struct MacroResolver {
20 crate_def_map: Arc<CrateDefMap>, 20 crate_def_map: Arc<CrateDefMap>,
21 current_file_id: HirFileId,
21 module: ModuleId, 22 module: ModuleId,
22} 23}
23 24
24impl MacroResolver { 25impl MacroResolver {
25 pub fn new(db: &impl DefDatabase2, module: ModuleId) -> MacroResolver { 26 pub fn new(
26 MacroResolver { crate_def_map: db.crate_def_map(module.krate), module } 27 db: &impl DefDatabase2,
28 current_file_id: HirFileId,
29 module: ModuleId,
30 ) -> MacroResolver {
31 let crate_def_map = db.crate_def_map(module.krate);
32 MacroResolver { crate_def_map, current_file_id, module }
27 } 33 }
28 34
29 fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> { 35 fn resolve_path_as_macro(&self, db: &impl DefDatabase2, path: &Path) -> Option<MacroDefId> {
@@ -77,11 +83,10 @@ impl Body {
77 pub fn new( 83 pub fn new(
78 db: &impl DefDatabase2, 84 db: &impl DefDatabase2,
79 resolver: MacroResolver, 85 resolver: MacroResolver,
80 file_id: HirFileId,
81 params: Option<ast::ParamList>, 86 params: Option<ast::ParamList>,
82 body: Option<ast::Expr>, 87 body: Option<ast::Expr>,
83 ) -> (Body, BodySourceMap) { 88 ) -> (Body, BodySourceMap) {
84 lower::lower(db, resolver, file_id, params, body) 89 lower::lower(db, resolver, params, body)
85 } 90 }
86 91
87 pub fn params(&self) -> &[PatId] { 92 pub fn params(&self) -> &[PatId] {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 2aa863c9e..1a990101f 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -31,15 +31,15 @@ use crate::{
31pub(super) fn lower( 31pub(super) fn lower(
32 db: &impl DefDatabase2, 32 db: &impl DefDatabase2,
33 resolver: MacroResolver, 33 resolver: MacroResolver,
34 file_id: HirFileId,
35 params: Option<ast::ParamList>, 34 params: Option<ast::ParamList>,
36 body: Option<ast::Expr>, 35 body: Option<ast::Expr>,
37) -> (Body, BodySourceMap) { 36) -> (Body, BodySourceMap) {
37 let original_file_id = resolver.current_file_id;
38
38 ExprCollector { 39 ExprCollector {
39 resolver, 40 resolver,
40 db, 41 db,
41 original_file_id: file_id, 42 original_file_id,
42 current_file_id: file_id,
43 source_map: BodySourceMap::default(), 43 source_map: BodySourceMap::default(),
44 body: Body { 44 body: Body {
45 exprs: Arena::default(), 45 exprs: Arena::default(),
@@ -55,7 +55,6 @@ struct ExprCollector<DB> {
55 db: DB, 55 db: DB,
56 resolver: MacroResolver, 56 resolver: MacroResolver,
57 original_file_id: HirFileId, 57 original_file_id: HirFileId,
58 current_file_id: HirFileId,
59 58
60 body: Body, 59 body: Body,
61 source_map: BodySourceMap, 60 source_map: BodySourceMap,
@@ -101,12 +100,12 @@ where
101 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { 100 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
102 let ptr = Either::A(ptr); 101 let ptr = Either::A(ptr);
103 let id = self.body.exprs.alloc(expr); 102 let id = self.body.exprs.alloc(expr);
104 if self.current_file_id == self.original_file_id { 103 if self.resolver.current_file_id == self.original_file_id {
105 self.source_map.expr_map.insert(ptr, id); 104 self.source_map.expr_map.insert(ptr, id);
106 } 105 }
107 self.source_map 106 self.source_map
108 .expr_map_back 107 .expr_map_back
109 .insert(id, Source { file_id: self.current_file_id, ast: ptr }); 108 .insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
110 id 109 id
111 } 110 }
112 // desugared exprs don't have ptr, that's wrong and should be fixed 111 // desugared exprs don't have ptr, that's wrong and should be fixed
@@ -117,20 +116,22 @@ where
117 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId { 116 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
118 let ptr = Either::B(ptr); 117 let ptr = Either::B(ptr);
119 let id = self.body.exprs.alloc(expr); 118 let id = self.body.exprs.alloc(expr);
120 if self.current_file_id == self.original_file_id { 119 if self.resolver.current_file_id == self.original_file_id {
121 self.source_map.expr_map.insert(ptr, id); 120 self.source_map.expr_map.insert(ptr, id);
122 } 121 }
123 self.source_map 122 self.source_map
124 .expr_map_back 123 .expr_map_back
125 .insert(id, Source { file_id: self.current_file_id, ast: ptr }); 124 .insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
126 id 125 id
127 } 126 }
128 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { 127 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
129 let id = self.body.pats.alloc(pat); 128 let id = self.body.pats.alloc(pat);
130 if self.current_file_id == self.original_file_id { 129 if self.resolver.current_file_id == self.original_file_id {
131 self.source_map.pat_map.insert(ptr, id); 130 self.source_map.pat_map.insert(ptr, id);
132 } 131 }
133 self.source_map.pat_map_back.insert(id, Source { file_id: self.current_file_id, ast: ptr }); 132 self.source_map
133 .pat_map_back
134 .insert(id, Source { file_id: self.resolver.current_file_id, ast: ptr });
134 id 135 id
135 } 136 }
136 137
@@ -445,8 +446,8 @@ where
445 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr), 446 ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
446 ast::Expr::MacroCall(e) => { 447 ast::Expr::MacroCall(e) => {
447 let ast_id = AstId::new( 448 let ast_id = AstId::new(
448 self.current_file_id, 449 self.resolver.current_file_id,
449 self.db.ast_id_map(self.current_file_id).ast_id(&e), 450 self.db.ast_id_map(self.resolver.current_file_id).ast_id(&e),
450 ); 451 );
451 452
452 if let Some(path) = e.path().and_then(|path| self.parse_path(path)) { 453 if let Some(path) = e.path().and_then(|path| self.parse_path(path)) {
@@ -457,9 +458,9 @@ where
457 if let Some(expr) = ast::Expr::cast(node) { 458 if let Some(expr) = ast::Expr::cast(node) {
458 log::debug!("macro expansion {:#?}", expr.syntax()); 459 log::debug!("macro expansion {:#?}", expr.syntax());
459 let old_file_id = 460 let old_file_id =
460 std::mem::replace(&mut self.current_file_id, file_id); 461 std::mem::replace(&mut self.resolver.current_file_id, file_id);
461 let id = self.collect_expr(expr); 462 let id = self.collect_expr(expr);
462 self.current_file_id = old_file_id; 463 self.resolver.current_file_id = old_file_id;
463 return id; 464 return id;
464 } 465 }
465 } 466 }
@@ -581,7 +582,7 @@ where
581 } 582 }
582 583
583 fn parse_path(&mut self, path: ast::Path) -> Option<Path> { 584 fn parse_path(&mut self, path: ast::Path) -> Option<Path> {
584 let hygiene = Hygiene::new(self.db, self.current_file_id); 585 let hygiene = Hygiene::new(self.db, self.resolver.current_file_id);
585 Path::from_src(path, &hygiene) 586 Path::from_src(path, &hygiene)
586 } 587 }
587} 588}