aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/expr.rs11
-rw-r--r--crates/ra_hir/src/ids.rs43
-rw-r--r--crates/ra_hir/src/lib.rs1
-rw-r--r--crates/ra_hir/src/nameres/collector.rs4
4 files changed, 38 insertions, 21 deletions
diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs
index a2b5db1a1..288f85b01 100644
--- a/crates/ra_hir/src/expr.rs
+++ b/crates/ra_hir/src/expr.rs
@@ -6,11 +6,11 @@ use rustc_hash::FxHashMap;
6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap}; 6use ra_arena::{Arena, RawId, impl_arena_id, map::ArenaMap};
7use ra_syntax::{ 7use 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
12use crate::{ 12use 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};
@@ -833,8 +833,11 @@ where
833 if let Some(tt) = self.db.macro_expand(call_id).ok() { 833 if let Some(tt) = self.db.macro_expand(call_id).ok() {
834 if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() { 834 if let Some(expr) = mbe::token_tree_to_expr(&tt).ok() {
835 log::debug!("macro expansion {}", expr.syntax().debug_dump()); 835 log::debug!("macro expansion {}", expr.syntax().debug_dump());
836 let old_file_id = 836 let old_file_id = std::mem::replace(
837 std::mem::replace(&mut self.current_file_id, call_id.into()); 837 &mut self.current_file_id,
838 //BUG
839 call_id.as_file(MacroFileKind::Items),
840 );
838 let id = self.collect_expr(&expr); 841 let id = self.collect_expr(&expr);
839 self.current_file_id = old_file_id; 842 self.current_file_id = old_file_id;
840 return id; 843 return id;
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index ff4a81e59..357ef2a80 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -39,8 +39,8 @@ impl HirFileId {
39 pub fn original_file(self, db: &impl DefDatabase) -> FileId { 39 pub fn original_file(self, db: &impl DefDatabase) -> FileId {
40 match self.0 { 40 match self.0 {
41 HirFileIdRepr::File(file_id) => file_id, 41 HirFileIdRepr::File(file_id) => file_id,
42 HirFileIdRepr::Macro(macro_call_id) => { 42 HirFileIdRepr::Macro(macro_file) => {
43 let loc = macro_call_id.loc(db); 43 let loc = macro_file.macro_call_id.loc(db);
44 loc.ast_id.file_id().original_file(db) 44 loc.ast_id.file_id().original_file(db)
45 } 45 }
46 } 46 }
@@ -62,9 +62,10 @@ impl HirFileId {
62 ) -> TreeArc<SourceFile> { 62 ) -> TreeArc<SourceFile> {
63 match file_id.0 { 63 match file_id.0 {
64 HirFileIdRepr::File(file_id) => db.parse(file_id), 64 HirFileIdRepr::File(file_id) => db.parse(file_id),
65 HirFileIdRepr::Macro(macro_call_id) => { 65 HirFileIdRepr::Macro(macro_file) => {
66 match db.macro_expand(macro_call_id) { 66 let macro_call_id = macro_file.macro_call_id;
67 Ok(tt) => mbe::token_tree_to_ast_item_list(&tt), 67 let tt = match db.macro_expand(macro_call_id) {
68 Ok(it) => it,
68 Err(err) => { 69 Err(err) => {
69 // Note: 70 // Note:
70 // The final goal we would like to make all parse_macro success, 71 // The final goal we would like to make all parse_macro success,
@@ -74,10 +75,12 @@ impl HirFileId {
74 err, 75 err,
75 macro_call_id.debug_dump(db) 76 macro_call_id.debug_dump(db)
76 ); 77 );
77
78 // returning an empty string looks fishy... 78 // returning an empty string looks fishy...
79 SourceFile::parse("") 79 return SourceFile::parse("");
80 } 80 }
81 };
82 match macro_file.macro_file_kind {
83 MacroFileKind::Items => mbe::token_tree_to_ast_item_list(&tt),
81 } 84 }
82 } 85 }
83 } 86 }
@@ -87,7 +90,18 @@ impl HirFileId {
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 90#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88enum HirFileIdRepr { 91enum HirFileIdRepr {
89 File(FileId), 92 File(FileId),
90 Macro(MacroCallId), 93 Macro(MacroFile),
94}
95
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
97struct MacroFile {
98 macro_call_id: MacroCallId,
99 macro_file_kind: MacroFileKind,
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103pub(crate) enum MacroFileKind {
104 Items,
91} 105}
92 106
93impl From<FileId> for HirFileId { 107impl From<FileId> for HirFileId {
@@ -96,12 +110,6 @@ impl From<FileId> for HirFileId {
96 } 110 }
97} 111}
98 112
99impl From<MacroCallId> for HirFileId {
100 fn from(macro_call_id: MacroCallId) -> HirFileId {
101 HirFileId(HirFileIdRepr::Macro(macro_call_id))
102 }
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
106pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>); 114pub struct MacroDefId(pub(crate) AstId<ast::MacroCall>);
107 115
@@ -173,6 +181,11 @@ impl MacroCallId {
173 pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc { 181 pub(crate) fn loc(self, db: &impl DefDatabase) -> MacroCallLoc {
174 db.lookup_intern_macro(self) 182 db.lookup_intern_macro(self)
175 } 183 }
184
185 pub(crate) fn as_file(self, kind: MacroFileKind) -> HirFileId {
186 let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
187 HirFileId(HirFileIdRepr::Macro(macro_file))
188 }
176} 189}
177 190
178impl MacroCallLoc { 191impl MacroCallLoc {
@@ -342,7 +355,7 @@ impl MacroCallId {
342 let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" "); 355 let syntax_str = node.syntax().text().chunks().collect::<Vec<_>>().join(" ");
343 356
344 // dump the file name 357 // dump the file name
345 let file_id: HirFileId = self.clone().into(); 358 let file_id: HirFileId = self.loc(db).ast_id.file_id();
346 let original = file_id.original_file(db); 359 let original = file_id.original_file(db);
347 let macro_rules = db.macro_def(loc.def); 360 let macro_rules = db.macro_def(loc.def);
348 361
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 03b1063b6..0c6d7c2b7 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -53,6 +53,7 @@ use crate::{
53 name::{AsName, KnownName}, 53 name::{AsName, KnownName},
54 source_id::{FileAstId, AstId}, 54 source_id::{FileAstId, AstId},
55 resolve::Resolver, 55 resolve::Resolver,
56 ids::MacroFileKind,
56}; 57};
57 58
58pub use self::{ 59pub use self::{
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index 4640b3b74..c615d80c3 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -15,7 +15,7 @@ use crate::{
15 diagnostics::DefDiagnostic, 15 diagnostics::DefDiagnostic,
16 raw, 16 raw,
17 }, 17 },
18 ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId}, 18 ids::{AstItemDef, LocationCtx, MacroCallLoc, MacroCallId, MacroDefId, MacroFileKind},
19 AstId, 19 AstId,
20}; 20};
21 21
@@ -371,7 +371,7 @@ where
371 self.macro_stack_monitor.increase(macro_def_id); 371 self.macro_stack_monitor.increase(macro_def_id);
372 372
373 if !self.macro_stack_monitor.is_poison(macro_def_id) { 373 if !self.macro_stack_monitor.is_poison(macro_def_id) {
374 let file_id: HirFileId = macro_call_id.into(); 374 let file_id: HirFileId = macro_call_id.as_file(MacroFileKind::Items);
375 let raw_items = self.db.raw_items(file_id); 375 let raw_items = self.db.raw_items(file_id);
376 ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items } 376 ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
377 .collect(raw_items.items()); 377 .collect(raw_items.items());