diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-29 15:02:27 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-29 15:02:27 +0000 |
commit | 523b4cbc602447b14202dd2520f84241bb07c4e2 (patch) | |
tree | b145912b63ff25ab6130853867698e622ef50fcc /crates/ra_hir/src | |
parent | ada9e16537c22b490d13cdd54b9e1e4885856a4c (diff) | |
parent | 12cc2ed5572e4df8cfbdaef79ed3c865fda0764c (diff) |
Merge #2650
2650: Add macro call support for SourceAnalyzer::type_of r=matklad a=edwin0cheng
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 85b378483..2c422af8b 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -215,8 +215,32 @@ impl SourceAnalyzer { | |||
215 | self.body_source_map.as_ref()?.node_pat(src) | 215 | self.body_source_map.as_ref()?.node_pat(src) |
216 | } | 216 | } |
217 | 217 | ||
218 | fn expand_expr( | ||
219 | &self, | ||
220 | db: &impl HirDatabase, | ||
221 | expr: InFile<&ast::Expr>, | ||
222 | ) -> Option<InFile<ast::Expr>> { | ||
223 | let macro_call = ast::MacroCall::cast(expr.value.syntax().clone())?; | ||
224 | let macro_file = | ||
225 | self.body_source_map.as_ref()?.node_macro_file(expr.with_value(¯o_call))?; | ||
226 | let expanded = db.parse_or_expand(macro_file)?; | ||
227 | let kind = expanded.kind(); | ||
228 | let expr = InFile::new(macro_file, ast::Expr::cast(expanded)?); | ||
229 | |||
230 | if ast::MacroCall::can_cast(kind) { | ||
231 | self.expand_expr(db, expr.as_ref()) | ||
232 | } else { | ||
233 | Some(expr) | ||
234 | } | ||
235 | } | ||
236 | |||
218 | pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { | 237 | pub fn type_of(&self, db: &impl HirDatabase, expr: &ast::Expr) -> Option<Type> { |
219 | let expr_id = self.expr_id(expr)?; | 238 | let expr_id = if let Some(expr) = self.expand_expr(db, InFile::new(self.file_id, expr)) { |
239 | self.body_source_map.as_ref()?.node_expr(expr.as_ref())? | ||
240 | } else { | ||
241 | self.expr_id(expr)? | ||
242 | }; | ||
243 | |||
220 | let ty = self.infer.as_ref()?[expr_id].clone(); | 244 | let ty = self.infer.as_ref()?[expr_id].clone(); |
221 | let environment = TraitEnvironment::lower(db, &self.resolver); | 245 | let environment = TraitEnvironment::lower(db, &self.resolver); |
222 | Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) | 246 | Some(Type { krate: self.resolver.krate()?, ty: InEnvironment { value: ty, environment } }) |