diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-14 07:12:09 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-14 07:12:09 +0100 |
commit | c417c77b681f10cc7585507bd874e9fd2cea63b8 (patch) | |
tree | d05cf30716a7791cfc0d8fcfe522a7132b5fff06 /crates/ra_hir/src/expr.rs | |
parent | ee0ab7c00b4b8c5375c14b44e3d7288ebf0d732d (diff) | |
parent | caa8663c08e1724af2abcde11fa937937d76aa14 (diff) |
Merge #1267
1267: Macro expand to r=edwin0cheng a=matklad
closes #1264
The core problem this PR is trying to wrangle is that macros can expand to different stuffs, depending on context.
That is, `foo!()` on the top-level expands to a list of items, but the same `foo!()` in expression position expands to expression.
Our current `hir_parse(HirFileId) -> TreeArc<SourceFile>` does not really support this.
So, the plan is to change `hir_parse` to untyped inreface (`TreeArc<Syntaxnode>`), and add `expands_to` field to `MacroCallLoc`, such that the *target* of macro expansion is selected by the calling code and is part of macro id.
This unfortunately looses some type-safety :(
Moreover, this doesn't really fix #1264 by itself, because we die due to some other error inside macro expansion: expander fails to produce a tree with a single root, which trips assert inside rowan.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/expr.rs')
-rw-r--r-- | crates/ra_hir/src/expr.rs | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index a2b5db1a1..9618236e5 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs | |||
@@ -6,11 +6,11 @@ use rustc_hash::FxHashMap; | |||
6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; | 6 | use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
8 | SyntaxNodePtr, AstPtr, AstNode, | 8 | SyntaxNodePtr, AstPtr, AstNode, |
9 | ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner} | 9 | ast::{self, LoopBodyOwner, ArgListOwner, NameOwner, LiteralKind,ArrayExprKind, TypeAscriptionOwner}, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{ | 12 | use crate::{ |
13 | Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc, | 13 | Path, Name, HirDatabase, Resolver,DefWithBody, Either, HirFileId, MacroCallLoc, MacroFileKind, |
14 | name::AsName, | 14 | name::AsName, |
15 | type_ref::{Mutability, TypeRef}, | 15 | type_ref::{Mutability, TypeRef}, |
16 | }; | 16 | }; |
@@ -830,11 +830,11 @@ where | |||
830 | 830 | ||
831 | if let Some(def) = self.resolver.resolve_macro_call(path) { | 831 | if let Some(def) = self.resolver.resolve_macro_call(path) { |
832 | let call_id = MacroCallLoc { def, ast_id }.id(self.db); | 832 | let call_id = MacroCallLoc { def, ast_id }.id(self.db); |
833 | if let Some(tt) = self.db.macro_expand(call_id).ok() { | 833 | let file_id = call_id.as_file(MacroFileKind::Expr); |
834 | if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() { | 834 | if let Some(node) = self.db.parse_or_expand(file_id) { |
835 | if let Some(expr) = ast::Expr::cast(&*node) { | ||
835 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); | 836 | log::debug!("macro expansion {}", expr.syntax().debug_dump()); |
836 | let old_file_id = | 837 | let old_file_id = std::mem::replace(&mut self.current_file_id, file_id); |
837 | std::mem::replace(&mut self.current_file_id, call_id.into()); | ||
838 | let id = self.collect_expr(&expr); | 838 | let id = self.collect_expr(&expr); |
839 | self.current_file_id = old_file_id; | 839 | self.current_file_id = old_file_id; |
840 | return id; | 840 | return id; |