aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-14 07:30:30 +0000
committerAleksey Kladov <[email protected]>2019-11-14 07:30:30 +0000
commitda2ca01ebaaaaa47aa09c5848c53b145a68af8fa (patch)
treebed1d8bfef7cd3c42917d30fae2d8002dd00c38c /crates/ra_hir_def/src
parenta73b7bb3f6af134c781cba1126350749c5a91144 (diff)
Handle macro-generated expressions slightly less wrong
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/body.rs28
-rw-r--r--crates/ra_hir_def/src/body/lower.rs24
2 files changed, 20 insertions, 32 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index 65fefd912..75bba31c2 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -21,7 +21,6 @@ use crate::{
21 21
22pub struct Expander { 22pub struct Expander {
23 crate_def_map: Arc<CrateDefMap>, 23 crate_def_map: Arc<CrateDefMap>,
24 original_file_id: HirFileId,
25 current_file_id: HirFileId, 24 current_file_id: HirFileId,
26 hygiene: Hygiene, 25 hygiene: Hygiene,
27 module: ModuleId, 26 module: ModuleId,
@@ -31,13 +30,7 @@ impl Expander {
31 pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander { 30 pub fn new(db: &impl DefDatabase2, current_file_id: HirFileId, module: ModuleId) -> Expander {
32 let crate_def_map = db.crate_def_map(module.krate); 31 let crate_def_map = db.crate_def_map(module.krate);
33 let hygiene = Hygiene::new(db, current_file_id); 32 let hygiene = Hygiene::new(db, current_file_id);
34 Expander { 33 Expander { crate_def_map, current_file_id, hygiene, module }
35 crate_def_map,
36 original_file_id: current_file_id,
37 current_file_id,
38 hygiene,
39 module,
40 }
41 } 34 }
42 35
43 fn expand( 36 fn expand(
@@ -82,11 +75,6 @@ impl Expander {
82 std::mem::forget(mark); 75 std::mem::forget(mark);
83 } 76 }
84 77
85 // FIXME: remove this.
86 fn is_in_expansion(&self) -> bool {
87 self.original_file_id != self.current_file_id
88 }
89
90 fn to_source<T>(&self, ast: T) -> Source<T> { 78 fn to_source<T>(&self, ast: T) -> Source<T> {
91 Source { file_id: self.current_file_id, ast } 79 Source { file_id: self.current_file_id, ast }
92 } 80 }
@@ -147,9 +135,9 @@ pub type PatSource = Source<PatPtr>;
147/// this properly for macros. 135/// this properly for macros.
148#[derive(Default, Debug, Eq, PartialEq)] 136#[derive(Default, Debug, Eq, PartialEq)]
149pub struct BodySourceMap { 137pub struct BodySourceMap {
150 expr_map: FxHashMap<ExprPtr, ExprId>, 138 expr_map: FxHashMap<ExprSource, ExprId>,
151 expr_map_back: ArenaMap<ExprId, ExprSource>, 139 expr_map_back: ArenaMap<ExprId, ExprSource>,
152 pat_map: FxHashMap<PatPtr, PatId>, 140 pat_map: FxHashMap<PatSource, PatId>,
153 pat_map_back: ArenaMap<PatId, PatSource>, 141 pat_map_back: ArenaMap<PatId, PatSource>,
154 field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, 142 field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>,
155} 143}
@@ -202,16 +190,18 @@ impl BodySourceMap {
202 self.expr_map_back.get(expr).copied() 190 self.expr_map_back.get(expr).copied()
203 } 191 }
204 192
205 pub fn node_expr(&self, node: &ast::Expr) -> Option<ExprId> { 193 pub fn node_expr(&self, node: Source<&ast::Expr>) -> Option<ExprId> {
206 self.expr_map.get(&Either::A(AstPtr::new(node))).cloned() 194 let src = node.map(|it| Either::A(AstPtr::new(it)));
195 self.expr_map.get(&src).cloned()
207 } 196 }
208 197
209 pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> { 198 pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
210 self.pat_map_back.get(pat).copied() 199 self.pat_map_back.get(pat).copied()
211 } 200 }
212 201
213 pub fn node_pat(&self, node: &ast::Pat) -> Option<PatId> { 202 pub fn node_pat(&self, node: Source<&ast::Pat>) -> Option<PatId> {
214 self.pat_map.get(&Either::A(AstPtr::new(node))).cloned() 203 let src = node.map(|it| Either::A(AstPtr::new(it)));
204 self.pat_map.get(&src).cloned()
215 } 205 }
216 206
217 pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> { 207 pub fn field_syntax(&self, expr: ExprId, field: usize) -> AstPtr<ast::RecordField> {
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index c45500195..f6d79ddf0 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -94,10 +94,9 @@ where
94 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { 94 fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
95 let ptr = Either::A(ptr); 95 let ptr = Either::A(ptr);
96 let id = self.body.exprs.alloc(expr); 96 let id = self.body.exprs.alloc(expr);
97 if !self.expander.is_in_expansion() { 97 let src = self.expander.to_source(ptr);
98 self.source_map.expr_map.insert(ptr, id); 98 self.source_map.expr_map.insert(src, id);
99 } 99 self.source_map.expr_map_back.insert(id, src);
100 self.source_map.expr_map_back.insert(id, self.expander.to_source(ptr));
101 id 100 id
102 } 101 }
103 // desugared exprs don't have ptr, that's wrong and should be fixed 102 // desugared exprs don't have ptr, that's wrong and should be fixed
@@ -108,18 +107,16 @@ where
108 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId { 107 fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
109 let ptr = Either::B(ptr); 108 let ptr = Either::B(ptr);
110 let id = self.body.exprs.alloc(expr); 109 let id = self.body.exprs.alloc(expr);
111 if !self.expander.is_in_expansion() { 110 let src = self.expander.to_source(ptr);
112 self.source_map.expr_map.insert(ptr, id); 111 self.source_map.expr_map.insert(src, id);
113 } 112 self.source_map.expr_map_back.insert(id, src);
114 self.source_map.expr_map_back.insert(id, self.expander.to_source(ptr));
115 id 113 id
116 } 114 }
117 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { 115 fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId {
118 let id = self.body.pats.alloc(pat); 116 let id = self.body.pats.alloc(pat);
119 if !self.expander.is_in_expansion() { 117 let src = self.expander.to_source(ptr);
120 self.source_map.pat_map.insert(ptr, id); 118 self.source_map.pat_map.insert(src, id);
121 } 119 self.source_map.pat_map_back.insert(id, src);
122 self.source_map.pat_map_back.insert(id, self.expander.to_source(ptr));
123 id 120 id
124 } 121 }
125 122
@@ -277,7 +274,8 @@ where
277 ast::Expr::ParenExpr(e) => { 274 ast::Expr::ParenExpr(e) => {
278 let inner = self.collect_expr_opt(e.expr()); 275 let inner = self.collect_expr_opt(e.expr());
279 // make the paren expr point to the inner expression as well 276 // make the paren expr point to the inner expression as well
280 self.source_map.expr_map.insert(Either::A(syntax_ptr), inner); 277 let src = self.expander.to_source(Either::A(syntax_ptr));
278 self.source_map.expr_map.insert(src, inner);
281 inner 279 inner
282 } 280 }
283 ast::Expr::ReturnExpr(e) => { 281 ast::Expr::ReturnExpr(e) => {