diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-06 14:19:58 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-06 14:19:58 +0000 |
commit | 1cc6879576b04850db3dd8aa1df0cf7c8f270503 (patch) | |
tree | 9221e6742315cc0faa8cdec5d44852afcf7d28e1 /crates/ra_hir_def/src | |
parent | 5947c1f8b52deb4fcfd97970ba6eb473f092cb94 (diff) | |
parent | 57da3df99a52ccedd7a684d2053db766e5681a61 (diff) |
Merge #3489
3489: More robust expression lowering r=matklad a=matklad
Closes #2236
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 41 |
2 files changed, 32 insertions, 24 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 010d35e55..57ba45b45 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -149,13 +149,16 @@ 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, Result<PatSource, SyntheticSyntax>>, |
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(Default, Debug, Eq, PartialEq, Clone, Copy)] | ||
160 | pub struct SyntheticSyntax; | ||
161 | |||
159 | impl Body { | 162 | impl Body { |
160 | pub(crate) fn body_with_source_map_query( | 163 | pub(crate) fn body_with_source_map_query( |
161 | db: &impl DefDatabase, | 164 | db: &impl DefDatabase, |
@@ -219,8 +222,8 @@ impl Index<PatId> for Body { | |||
219 | } | 222 | } |
220 | 223 | ||
221 | impl BodySourceMap { | 224 | impl BodySourceMap { |
222 | pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> { | 225 | pub fn expr_syntax(&self, expr: ExprId) -> Result<ExprSource, SyntheticSyntax> { |
223 | self.expr_map_back.get(expr).copied() | 226 | self.expr_map_back[expr] |
224 | } | 227 | } |
225 | 228 | ||
226 | pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { | 229 | pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> { |
@@ -238,8 +241,8 @@ impl BodySourceMap { | |||
238 | self.expr_map.get(&src).cloned() | 241 | self.expr_map.get(&src).cloned() |
239 | } | 242 | } |
240 | 243 | ||
241 | pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> { | 244 | pub fn pat_syntax(&self, pat: PatId) -> Result<PatSource, SyntheticSyntax> { |
242 | self.pat_map_back.get(pat).copied() | 245 | self.pat_map_back[pat] |
243 | } | 246 | } |
244 | 247 | ||
245 | pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> { | 248 | pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> { |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index c18e6879b..ec1b0c2e7 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, PatSource}; | ||
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,44 +103,48 @@ 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); | ||
126 | let src = self.expander.to_source(ptr); | 136 | let src = self.expander.to_source(ptr); |
137 | let id = self.make_pat(pat, Ok(src)); | ||
127 | self.source_map.pat_map.insert(src, id); | 138 | self.source_map.pat_map.insert(src, id); |
128 | self.source_map.pat_map_back.insert(id, src); | ||
129 | id | 139 | id |
130 | } | 140 | } |
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 { | 141 | fn missing_pat(&mut self) -> PatId { |
142 | self.body.pats.alloc(Pat::Missing) | 142 | self.make_pat(Pat::Missing, Err(SyntheticSyntax)) |
143 | } | ||
144 | fn make_pat(&mut self, pat: Pat, src: Result<PatSource, SyntheticSyntax>) -> PatId { | ||
145 | let id = self.body.pats.alloc(pat); | ||
146 | self.source_map.pat_map_back.insert(id, src); | ||
147 | id | ||
143 | } | 148 | } |
144 | 149 | ||
145 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { | 150 | fn collect_expr(&mut self, expr: ast::Expr) -> ExprId { |