diff options
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 31 |
2 files changed, 19 insertions, 18 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index cd9e22c42..e60adc146 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -149,14 +149,14 @@ pub type PatSource = InFile<PatPtr>; | |||
149 | #[derive(Default, Debug, Eq, PartialEq)] | 149 | #[derive(Default, Debug, Eq, PartialEq)] |
150 | pub struct BodySourceMap { | 150 | pub struct BodySourceMap { |
151 | expr_map: FxHashMap<ExprSource, ExprId>, | 151 | expr_map: FxHashMap<ExprSource, ExprId>, |
152 | expr_map_back: ArenaMap<ExprId, ExprSource>, | 152 | expr_map_back: ArenaMap<ExprId, Result<ExprSource, SyntheticSyntax>>, |
153 | pat_map: FxHashMap<PatSource, PatId>, | 153 | pat_map: FxHashMap<PatSource, PatId>, |
154 | pat_map_back: ArenaMap<PatId, PatSource>, | 154 | pat_map_back: ArenaMap<PatId, PatSource>, |
155 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, | 155 | field_map: FxHashMap<(ExprId, usize), AstPtr<ast::RecordField>>, |
156 | expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>, | 156 | expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>, |
157 | } | 157 | } |
158 | 158 | ||
159 | #[derive(Debug)] | 159 | #[derive(Default, Debug, Eq, PartialEq, Clone, Copy)] |
160 | pub struct SyntheticSyntax; | 160 | pub struct SyntheticSyntax; |
161 | 161 | ||
162 | impl Body { | 162 | impl Body { |
@@ -223,7 +223,7 @@ impl Index<PatId> for Body { | |||
223 | 223 | ||
224 | impl BodySourceMap { | 224 | impl BodySourceMap { |
225 | pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> { | 225 | pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> { |
226 | self.expr_map_back.get(expr).copied().ok_or(SyntheticSyntax) | 226 | self.expr_map_back[expr] |
227 | } | 227 | } |
228 | 228 | ||
229 | pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { | 229 | pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index c18e6879b..209907700 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -14,9 +14,10 @@ use ra_syntax::{ | |||
14 | }; | 14 | }; |
15 | use test_utils::tested_by; | 15 | use test_utils::tested_by; |
16 | 16 | ||
17 | use super::ExprSource; | ||
17 | use crate::{ | 18 | use crate::{ |
18 | adt::StructKind, | 19 | adt::StructKind, |
19 | body::{Body, BodySourceMap, Expander, PatPtr}, | 20 | body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, |
20 | builtin_type::{BuiltinFloat, BuiltinInt}, | 21 | builtin_type::{BuiltinFloat, BuiltinInt}, |
21 | db::DefDatabase, | 22 | db::DefDatabase, |
22 | expr::{ | 23 | expr::{ |
@@ -102,25 +103,35 @@ where | |||
102 | 103 | ||
103 | fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { | 104 | fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId { |
104 | let ptr = Either::Left(ptr); | 105 | let ptr = Either::Left(ptr); |
105 | let id = self.body.exprs.alloc(expr); | ||
106 | let src = self.expander.to_source(ptr); | 106 | let src = self.expander.to_source(ptr); |
107 | let id = self.make_expr(expr, Ok(src)); | ||
107 | self.source_map.expr_map.insert(src, id); | 108 | self.source_map.expr_map.insert(src, id); |
108 | self.source_map.expr_map_back.insert(id, src); | ||
109 | id | 109 | id |
110 | } | 110 | } |
111 | // 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 |
112 | // somehow. | 112 | // somehow. |
113 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { | 113 | fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId { |
114 | self.body.exprs.alloc(expr) | 114 | self.make_expr(expr, Err(SyntheticSyntax)) |
115 | } | 115 | } |
116 | 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 { |
117 | let ptr = Either::Right(ptr); | 117 | let ptr = Either::Right(ptr); |
118 | let id = self.body.exprs.alloc(expr); | ||
119 | let src = self.expander.to_source(ptr); | 118 | let src = self.expander.to_source(ptr); |
119 | let id = self.make_expr(expr, Ok(src)); | ||
120 | self.source_map.expr_map.insert(src, id); | 120 | self.source_map.expr_map.insert(src, id); |
121 | id | ||
122 | } | ||
123 | fn empty_block(&mut self) -> ExprId { | ||
124 | self.alloc_expr_desugared(Expr::Block { statements: Vec::new(), tail: None }) | ||
125 | } | ||
126 | fn missing_expr(&mut self) -> ExprId { | ||
127 | self.alloc_expr_desugared(Expr::Missing) | ||
128 | } | ||
129 | fn make_expr(&mut self, expr: Expr, src: Result<ExprSource, SyntheticSyntax>) -> ExprId { | ||
130 | let id = self.body.exprs.alloc(expr); | ||
121 | self.source_map.expr_map_back.insert(id, src); | 131 | self.source_map.expr_map_back.insert(id, src); |
122 | id | 132 | id |
123 | } | 133 | } |
134 | |||
124 | fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { | 135 | fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { |
125 | let id = self.body.pats.alloc(pat); | 136 | let id = self.body.pats.alloc(pat); |
126 | let src = self.expander.to_source(ptr); | 137 | let src = self.expander.to_source(ptr); |
@@ -128,16 +139,6 @@ where | |||
128 | self.source_map.pat_map_back.insert(id, src); | 139 | self.source_map.pat_map_back.insert(id, src); |
129 | id | 140 | id |
130 | } | 141 | } |
131 | |||
132 | fn empty_block(&mut self) -> ExprId { | ||
133 | let block = Expr::Block { statements: Vec::new(), tail: None }; | ||
134 | self.body.exprs.alloc(block) | ||
135 | } | ||
136 | |||
137 | fn missing_expr(&mut self) -> ExprId { | ||
138 | self.body.exprs.alloc(Expr::Missing) | ||
139 | } | ||
140 | |||
141 | fn missing_pat(&mut self) -> PatId { | 142 | fn missing_pat(&mut self) -> PatId { |
142 | self.body.pats.alloc(Pat::Missing) | 143 | self.body.pats.alloc(Pat::Missing) |
143 | } | 144 | } |