aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-26 11:19:38 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-26 11:19:38 +0000
commit8254244e4970b085809d42a34282649b4a4e16a2 (patch)
tree380b2c4835bd7ad75a66d26cab8e25e0be801182
parent71f8619b4e614aa9c61be6b2851498050e7f63ae (diff)
parente231277ab2f1ebb3040457e89b92540e599dbb3e (diff)
Merge #1055
1055: store macro def inside macro id r=matklad a=matklad This makes macro expansion resilient to changes to code inside the macro Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs2
-rw-r--r--crates/ra_hir/src/db.rs14
-rw-r--r--crates/ra_hir/src/ids.rs24
-rw-r--r--crates/ra_hir/src/lib.rs2
-rw-r--r--crates/ra_hir/src/nameres.rs11
-rw-r--r--crates/ra_hir/src/nameres/collector.rs93
-rw-r--r--crates/ra_hir/src/nameres/raw.rs80
-rw-r--r--crates/ra_hir/src/nameres/tests/incremental.rs23
8 files changed, 105 insertions, 144 deletions
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index 14237060c..790e2b80f 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -76,7 +76,7 @@ impl Module {
76 import: ImportId, 76 import: ImportId,
77 ) -> TreeArc<ast::PathSegment> { 77 ) -> TreeArc<ast::PathSegment> {
78 let (file_id, source) = self.definition_source(db); 78 let (file_id, source) = self.definition_source(db);
79 let (_, source_map) = db.raw_items_with_source_map(file_id.original_file(db)); 79 let (_, source_map) = db.raw_items_with_source_map(file_id);
80 source_map.get(&source, import) 80 source_map.get(&source, import)
81 } 81 }
82 82
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs
index 143919cdc..492814cbb 100644
--- a/crates/ra_hir/src/db.rs
+++ b/crates/ra_hir/src/db.rs
@@ -1,10 +1,10 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use ra_syntax::{SyntaxNode, TreeArc, SourceFile}; 3use ra_syntax::{SyntaxNode, TreeArc, SourceFile};
4use ra_db::{SourceDatabase, salsa, FileId}; 4use ra_db::{SourceDatabase, salsa};
5 5
6use crate::{ 6use crate::{
7 HirFileId, SourceFileItems, SourceItemId, Crate, Module, HirInterner, 7 HirFileId, MacroDefId, SourceFileItems, SourceItemId, Crate, Module, HirInterner,
8 Function, FnSignature, ExprScopes, TypeAlias, 8 Function, FnSignature, ExprScopes, TypeAlias,
9 Struct, Enum, StructField, 9 Struct, Enum, StructField,
10 Const, ConstSignature, Static, 10 Const, ConstSignature, Static,
@@ -19,6 +19,9 @@ use crate::{
19 19
20#[salsa::query_group(DefDatabaseStorage)] 20#[salsa::query_group(DefDatabaseStorage)]
21pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> { 21pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
22 #[salsa::invoke(crate::ids::macro_def_query)]
23 fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
24
22 #[salsa::invoke(HirFileId::hir_parse)] 25 #[salsa::invoke(HirFileId::hir_parse)]
23 fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>; 26 fn hir_parse(&self, file_id: HirFileId) -> TreeArc<SourceFile>;
24 27
@@ -38,10 +41,13 @@ pub trait DefDatabase: SourceDatabase + AsRef<HirInterner> {
38 fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>; 41 fn file_item(&self, source_item_id: SourceItemId) -> TreeArc<SyntaxNode>;
39 42
40 #[salsa::invoke(RawItems::raw_items_query)] 43 #[salsa::invoke(RawItems::raw_items_query)]
41 fn raw_items(&self, file_id: FileId) -> Arc<RawItems>; 44 fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>;
42 45
43 #[salsa::invoke(RawItems::raw_items_with_source_map_query)] 46 #[salsa::invoke(RawItems::raw_items_with_source_map_query)]
44 fn raw_items_with_source_map(&self, file_id: FileId) -> (Arc<RawItems>, Arc<ImportSourceMap>); 47 fn raw_items_with_source_map(
48 &self,
49 file_id: HirFileId,
50 ) -> (Arc<RawItems>, Arc<ImportSourceMap>);
45 51
46 #[salsa::invoke(CrateDefMap::crate_def_map_query)] 52 #[salsa::invoke(CrateDefMap::crate_def_map_query)]
47 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>; 53 fn crate_def_map(&self, krate: Crate) -> Arc<CrateDefMap>;
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 18401f865..bac7b9e46 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -7,6 +7,7 @@ use std::{
7use ra_db::{LocationInterner, FileId}; 7use ra_db::{LocationInterner, FileId};
8use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast}; 8use ra_syntax::{TreeArc, SyntaxNode, SourceFile, AstNode, SyntaxNodePtr, ast};
9use ra_arena::{Arena, RawId, ArenaId, impl_arena_id}; 9use ra_arena::{Arena, RawId, ArenaId, impl_arena_id};
10use mbe::MacroRules;
10 11
11use crate::{ 12use crate::{
12 Module, 13 Module,
@@ -100,10 +101,7 @@ fn parse_macro(db: &impl DefDatabase, macro_call_id: MacroCallId) -> Option<Tree
100 let macro_call = ast::MacroCall::cast(&syntax).unwrap(); 101 let macro_call = ast::MacroCall::cast(&syntax).unwrap();
101 let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?; 102 let (macro_arg, _) = macro_call.token_tree().and_then(mbe::ast_to_token_tree)?;
102 103
103 let def_map = db.crate_def_map(loc.module.krate); 104 let macro_rules = db.macro_def(loc.def)?;
104 let (krate, macro_id) = def_map.resolve_macro(macro_call_id)?;
105 let def_map = db.crate_def_map(krate);
106 let macro_rules = &def_map[macro_id];
107 let tt = macro_rules.expand(&macro_arg).ok()?; 105 let tt = macro_rules.expand(&macro_arg).ok()?;
108 Some(mbe::token_tree_to_ast_item_list(&tt)) 106 Some(mbe::token_tree_to_ast_item_list(&tt))
109} 107}
@@ -126,6 +124,22 @@ impl From<MacroCallId> for HirFileId {
126 } 124 }
127} 125}
128 126
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
128pub enum MacroDefId {
129 MacroByExample { source_item_id: SourceItemId },
130}
131
132pub(crate) fn macro_def_query(db: &impl DefDatabase, id: MacroDefId) -> Option<Arc<MacroRules>> {
133 let syntax_node = match id {
134 MacroDefId::MacroByExample { source_item_id } => db.file_item(source_item_id),
135 };
136 let macro_call = ast::MacroCall::cast(&syntax_node).unwrap();
137 let arg = macro_call.token_tree()?;
138 let (tt, _) = mbe::ast_to_token_tree(arg)?;
139 let rules = MacroRules::parse(&tt).ok()?;
140 Some(Arc::new(rules))
141}
142
129/// `MacroCallId` identifies a particular macro invocation, like 143/// `MacroCallId` identifies a particular macro invocation, like
130/// `println!("Hello, {}", world)`. 144/// `println!("Hello, {}", world)`.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 145#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -134,7 +148,7 @@ impl_arena_id!(MacroCallId);
134 148
135#[derive(Debug, Clone, PartialEq, Eq, Hash)] 149#[derive(Debug, Clone, PartialEq, Eq, Hash)]
136pub struct MacroCallLoc { 150pub struct MacroCallLoc {
137 pub(crate) module: Module, 151 pub(crate) def: MacroDefId,
138 pub(crate) source_item_id: SourceItemId, 152 pub(crate) source_item_id: SourceItemId,
139} 153}
140 154
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 974ebd831..87bc8009d 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -53,7 +53,7 @@ use crate::{
53pub use self::{ 53pub use self::{
54 path::{Path, PathKind}, 54 path::{Path, PathKind},
55 name::Name, 55 name::Name,
56 ids::{HirFileId, MacroCallId, MacroCallLoc, HirInterner}, 56 ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, HirInterner},
57 nameres::{PerNs, Namespace}, 57 nameres::{PerNs, Namespace},
58 ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, 58 ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
59 impl_block::{ImplBlock, ImplItem}, 59 impl_block::{ImplBlock, ImplItem},
diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs
index c34aa4b50..93c11f271 100644
--- a/crates/ra_hir/src/nameres.rs
+++ b/crates/ra_hir/src/nameres.rs
@@ -63,7 +63,7 @@ use test_utils::tested_by;
63use crate::{ 63use crate::{
64 ModuleDef, Name, Crate, Module, 64 ModuleDef, Name, Crate, Module,
65 DefDatabase, Path, PathKind, HirFileId, Trait, 65 DefDatabase, Path, PathKind, HirFileId, Trait,
66 ids::{SourceItemId, SourceFileItemId, MacroCallId}, 66 ids::{SourceItemId, SourceFileItemId, MacroCallId, MacroDefId},
67 diagnostics::DiagnosticSink, 67 diagnostics::DiagnosticSink,
68 nameres::diagnostics::DefDiagnostic, 68 nameres::diagnostics::DefDiagnostic,
69}; 69};
@@ -85,7 +85,7 @@ pub struct CrateDefMap {
85 root: CrateModuleId, 85 root: CrateModuleId,
86 modules: Arena<CrateModuleId, ModuleData>, 86 modules: Arena<CrateModuleId, ModuleData>,
87 macros: Arena<CrateMacroId, mbe::MacroRules>, 87 macros: Arena<CrateMacroId, mbe::MacroRules>,
88 public_macros: FxHashMap<Name, CrateMacroId>, 88 public_macros: FxHashMap<Name, MacroDefId>,
89 macro_resolutions: FxHashMap<MacroCallId, (Crate, CrateMacroId)>, 89 macro_resolutions: FxHashMap<MacroCallId, (Crate, CrateMacroId)>,
90 diagnostics: Vec<DefDiagnostic>, 90 diagnostics: Vec<DefDiagnostic>,
91} 91}
@@ -238,13 +238,6 @@ impl CrateDefMap {
238 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) 238 self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink))
239 } 239 }
240 240
241 pub(crate) fn resolve_macro(
242 &self,
243 macro_call_id: MacroCallId,
244 ) -> Option<(Crate, CrateMacroId)> {
245 self.macro_resolutions.get(&macro_call_id).map(|&it| it)
246 }
247
248 pub(crate) fn find_module_by_source( 241 pub(crate) fn find_module_by_source(
249 &self, 242 &self,
250 file_id: HirFileId, 243 file_id: HirFileId,
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs
index 8830b4624..89300d2ec 100644
--- a/crates/ra_hir/src/nameres/collector.rs
+++ b/crates/ra_hir/src/nameres/collector.rs
@@ -6,15 +6,15 @@ use ra_db::FileId;
6 6
7use crate::{ 7use crate::{
8 Function, Module, Struct, Enum, Const, Static, Trait, TypeAlias, 8 Function, Module, Struct, Enum, Const, Static, Trait, TypeAlias,
9 DefDatabase, HirFileId, Name, Path, Crate, 9 DefDatabase, HirFileId, Name, Path,
10 KnownName, 10 KnownName,
11 nameres::{ 11 nameres::{
12 Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode, 12 Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode,
13 CrateDefMap, CrateModuleId, ModuleData, CrateMacroId, 13 CrateDefMap, CrateModuleId, ModuleData,
14 diagnostics::DefDiagnostic, 14 diagnostics::DefDiagnostic,
15 raw, 15 raw,
16 }, 16 },
17 ids::{AstItemDef, LocationCtx, MacroCallLoc, SourceItemId, MacroCallId}, 17 ids::{AstItemDef, LocationCtx, MacroCallLoc, SourceItemId, MacroCallId, MacroDefId},
18}; 18};
19 19
20pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 20pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -51,8 +51,8 @@ struct DefCollector<DB> {
51 def_map: CrateDefMap, 51 def_map: CrateDefMap,
52 glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>, 52 glob_imports: FxHashMap<CrateModuleId, Vec<(CrateModuleId, raw::ImportId)>>,
53 unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, 53 unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>,
54 unexpanded_macros: Vec<(CrateModuleId, MacroCallId, Path, tt::Subtree)>, 54 unexpanded_macros: Vec<(CrateModuleId, SourceItemId, Path)>,
55 global_macro_scope: FxHashMap<Name, CrateMacroId>, 55 global_macro_scope: FxHashMap<Name, MacroDefId>,
56} 56}
57 57
58impl<'a, DB> DefCollector<&'a DB> 58impl<'a, DB> DefCollector<&'a DB>
@@ -62,7 +62,7 @@ where
62 fn collect(&mut self) { 62 fn collect(&mut self) {
63 let crate_graph = self.db.crate_graph(); 63 let crate_graph = self.db.crate_graph();
64 let file_id = crate_graph.crate_root(self.def_map.krate.crate_id()); 64 let file_id = crate_graph.crate_root(self.def_map.krate.crate_id());
65 let raw_items = self.db.raw_items(file_id); 65 let raw_items = self.db.raw_items(file_id.into());
66 let module_id = self.def_map.root; 66 let module_id = self.def_map.root;
67 self.def_map.modules[module_id].definition = Some(file_id); 67 self.def_map.modules[module_id].definition = Some(file_id);
68 ModCollector { 68 ModCollector {
@@ -93,14 +93,11 @@ where
93 } 93 }
94 } 94 }
95 95
96 fn define_macro(&mut self, name: Name, tt: &tt::Subtree, export: bool) { 96 fn define_macro(&mut self, name: Name, macro_id: MacroDefId, export: bool) {
97 if let Ok(rules) = mbe::MacroRules::parse(tt) { 97 if export {
98 let macro_id = self.def_map.macros.alloc(rules); 98 self.def_map.public_macros.insert(name.clone(), macro_id);
99 if export {
100 self.def_map.public_macros.insert(name.clone(), macro_id);
101 }
102 self.global_macro_scope.insert(name, macro_id);
103 } 99 }
100 self.global_macro_scope.insert(name, macro_id);
104 } 101 }
105 102
106 fn resolve_imports(&mut self) -> ReachedFixedPoint { 103 fn resolve_imports(&mut self) -> ReachedFixedPoint {
@@ -296,7 +293,7 @@ where
296 let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new()); 293 let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new());
297 let mut resolved = Vec::new(); 294 let mut resolved = Vec::new();
298 let mut res = ReachedFixedPoint::Yes; 295 let mut res = ReachedFixedPoint::Yes;
299 macros.retain(|(module_id, call_id, path, tt)| { 296 macros.retain(|(module_id, source_item_id, path)| {
300 if path.segments.len() != 2 { 297 if path.segments.len() != 2 {
301 return true; 298 return true;
302 } 299 }
@@ -312,47 +309,24 @@ where
312 res = ReachedFixedPoint::No; 309 res = ReachedFixedPoint::No;
313 let def_map = self.db.crate_def_map(krate); 310 let def_map = self.db.crate_def_map(krate);
314 if let Some(macro_id) = def_map.public_macros.get(&path.segments[1].name).cloned() { 311 if let Some(macro_id) = def_map.public_macros.get(&path.segments[1].name).cloned() {
315 resolved.push((*module_id, *call_id, (krate, macro_id), tt.clone())); 312 let call_id =
313 MacroCallLoc { def: macro_id, source_item_id: *source_item_id }.id(self.db);
314 resolved.push((*module_id, call_id));
316 } 315 }
317 false 316 false
318 }); 317 });
319 318
320 for (module_id, macro_call_id, macro_def_id, arg) in resolved { 319 for (module_id, macro_call_id) in resolved {
321 self.collect_macro_expansion(module_id, macro_call_id, macro_def_id, arg); 320 self.collect_macro_expansion(module_id, macro_call_id);
322 } 321 }
323 res 322 res
324 } 323 }
325 324
326 fn collect_macro_expansion( 325 fn collect_macro_expansion(&mut self, module_id: CrateModuleId, macro_call_id: MacroCallId) {
327 &mut self, 326 let file_id: HirFileId = macro_call_id.into();
328 module_id: CrateModuleId, 327 let raw_items = self.db.raw_items(file_id);
329 macro_call_id: MacroCallId, 328 ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
330 macro_def_id: (Crate, CrateMacroId), 329 .collect(raw_items.items())
331 macro_arg: tt::Subtree,
332 ) {
333 let (macro_krate, macro_id) = macro_def_id;
334 let dm;
335 let rules = if macro_krate == self.def_map.krate {
336 &self.def_map[macro_id]
337 } else {
338 dm = self.db.crate_def_map(macro_krate);
339 &dm[macro_id]
340 };
341 if let Ok(expansion) = rules.expand(&macro_arg) {
342 self.def_map.macro_resolutions.insert(macro_call_id, macro_def_id);
343 // XXX: this **does not** go through a database, because we can't
344 // identify macro_call without adding the whole state of name resolution
345 // as a parameter to the query.
346 //
347 // So, we run the queries "manually" and we must ensure that
348 // `db.hir_parse(macro_call_id)` returns the same source_file.
349 let file_id: HirFileId = macro_call_id.into();
350 let source_file = mbe::token_tree_to_ast_item_list(&expansion);
351
352 let raw_items = raw::RawItems::from_source_file(&source_file, file_id);
353 ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items }
354 .collect(raw_items.items())
355 }
356 } 330 }
357 331
358 fn finish(self) -> CrateDefMap { 332 fn finish(self) -> CrateDefMap {
@@ -412,7 +386,7 @@ where
412 Ok(file_id) => { 386 Ok(file_id) => {
413 let module_id = 387 let module_id =
414 self.push_child_module(name.clone(), source_item_id, Some(file_id)); 388 self.push_child_module(name.clone(), source_item_id, Some(file_id));
415 let raw_items = self.def_collector.db.raw_items(file_id); 389 let raw_items = self.def_collector.db.raw_items(file_id.into());
416 ModCollector { 390 ModCollector {
417 def_collector: &mut *self.def_collector, 391 def_collector: &mut *self.def_collector,
418 module_id, 392 module_id,
@@ -484,38 +458,33 @@ where
484 // Case 1: macro rules, define a macro in crate-global mutable scope 458 // Case 1: macro rules, define a macro in crate-global mutable scope
485 if is_macro_rules(&mac.path) { 459 if is_macro_rules(&mac.path) {
486 if let Some(name) = &mac.name { 460 if let Some(name) = &mac.name {
487 self.def_collector.define_macro(name.clone(), &mac.arg, mac.export) 461 let macro_id = MacroDefId::MacroByExample {
462 source_item_id: mac.source_item_id.with_file_id(self.file_id),
463 };
464 self.def_collector.define_macro(name.clone(), macro_id, mac.export)
488 } 465 }
489 return; 466 return;
490 } 467 }
491 468
492 let source_item_id = SourceItemId { file_id: self.file_id, item_id: mac.source_item_id }; 469 let source_item_id = SourceItemId { file_id: self.file_id, item_id: mac.source_item_id };
493 let macro_call_id = MacroCallLoc {
494 module: Module { krate: self.def_collector.def_map.krate, module_id: self.module_id },
495 source_item_id,
496 }
497 .id(self.def_collector.db);
498 470
499 // Case 2: try to expand macro_rules from this crate, triggering 471 // Case 2: try to expand macro_rules from this crate, triggering
500 // recursive item collection. 472 // recursive item collection.
501 if let Some(&macro_id) = 473 if let Some(&macro_id) =
502 mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(name)) 474 mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(name))
503 { 475 {
504 self.def_collector.collect_macro_expansion( 476 let macro_call_id =
505 self.module_id, 477 MacroCallLoc { def: macro_id, source_item_id }.id(self.def_collector.db);
506 macro_call_id, 478
507 (self.def_collector.def_map.krate, macro_id), 479 self.def_collector.collect_macro_expansion(self.module_id, macro_call_id);
508 mac.arg.clone(),
509 );
510 return; 480 return;
511 } 481 }
512 482
513 // Case 3: path to a macro from another crate, expand during name resolution 483 // Case 3: path to a macro from another crate, expand during name resolution
514 self.def_collector.unexpanded_macros.push(( 484 self.def_collector.unexpanded_macros.push((
515 self.module_id, 485 self.module_id,
516 macro_call_id, 486 source_item_id,
517 mac.path.clone(), 487 mac.path.clone(),
518 mac.arg.clone(),
519 )) 488 ))
520 } 489 }
521} 490}
diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs
index f8ba398ec..ee0719ee0 100644
--- a/crates/ra_hir/src/nameres/raw.rs
+++ b/crates/ra_hir/src/nameres/raw.rs
@@ -4,7 +4,6 @@ use std::{
4}; 4};
5 5
6use test_utils::tested_by; 6use test_utils::tested_by;
7use ra_db::FileId;
8use ra_arena::{Arena, impl_arena_id, RawId, map::ArenaMap}; 7use ra_arena::{Arena, impl_arena_id, RawId, map::ArenaMap};
9use ra_syntax::{ 8use ra_syntax::{
10 AstNode, SourceFile, AstPtr, TreeArc, 9 AstNode, SourceFile, AstPtr, TreeArc,
@@ -16,6 +15,10 @@ use crate::{
16 ids::{SourceFileItemId, SourceFileItems}, 15 ids::{SourceFileItemId, SourceFileItems},
17}; 16};
18 17
18/// `RawItems` is a set of top-level items in a file (except for impls).
19///
20/// It is the input to name resolution algorithm. `RawItems` are not invalidated
21/// on most edits.
19#[derive(Debug, Default, PartialEq, Eq)] 22#[derive(Debug, Default, PartialEq, Eq)]
20pub struct RawItems { 23pub struct RawItems {
21 modules: Arena<Module, ModuleData>, 24 modules: Arena<Module, ModuleData>,
@@ -32,11 +35,11 @@ pub struct ImportSourceMap {
32} 35}
33 36
34impl ImportSourceMap { 37impl ImportSourceMap {
35 pub(crate) fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) { 38 fn insert(&mut self, import: ImportId, segment: &ast::PathSegment) {
36 self.map.insert(import, AstPtr::new(segment)) 39 self.map.insert(import, AstPtr::new(segment))
37 } 40 }
38 41
39 pub fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> { 42 pub(crate) fn get(&self, source: &ModuleSource, import: ImportId) -> TreeArc<ast::PathSegment> {
40 let file = match source { 43 let file = match source {
41 ModuleSource::SourceFile(file) => &*file, 44 ModuleSource::SourceFile(file) => &*file,
42 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), 45 ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
@@ -47,40 +50,27 @@ impl ImportSourceMap {
47} 50}
48 51
49impl RawItems { 52impl RawItems {
50 pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: FileId) -> Arc<RawItems> { 53 pub(crate) fn raw_items_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<RawItems> {
51 db.raw_items_with_source_map(file_id).0 54 db.raw_items_with_source_map(file_id).0
52 } 55 }
53 56
54 pub(crate) fn raw_items_with_source_map_query( 57 pub(crate) fn raw_items_with_source_map_query(
55 db: &impl DefDatabase, 58 db: &impl DefDatabase,
56 file_id: FileId, 59 file_id: HirFileId,
57 ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { 60 ) -> (Arc<RawItems>, Arc<ImportSourceMap>) {
58 let mut collector = RawItemsCollector { 61 let mut collector = RawItemsCollector {
59 raw_items: RawItems::default(), 62 raw_items: RawItems::default(),
60 source_file_items: db.file_items(file_id.into()), 63 source_file_items: db.file_items(file_id.into()),
61 source_map: ImportSourceMap::default(), 64 source_map: ImportSourceMap::default(),
62 }; 65 };
63 let source_file = db.parse(file_id); 66 let source_file = db.hir_parse(file_id);
64 collector.process_module(None, &*source_file); 67 collector.process_module(None, &*source_file);
65 (Arc::new(collector.raw_items), Arc::new(collector.source_map)) 68 (Arc::new(collector.raw_items), Arc::new(collector.source_map))
66 } 69 }
67 70
68 pub(crate) fn items(&self) -> &[RawItem] { 71 pub(super) fn items(&self) -> &[RawItem] {
69 &self.items 72 &self.items
70 } 73 }
71
72 // We can't use queries during name resolution for fear of cycles, so this
73 // is a query-less variant of the above function.
74 pub(crate) fn from_source_file(source_file: &SourceFile, file_id: HirFileId) -> RawItems {
75 let source_file_items = SourceFileItems::from_source_file(source_file, file_id);
76 let mut collector = RawItemsCollector {
77 raw_items: RawItems::default(),
78 source_file_items: Arc::new(source_file_items),
79 source_map: ImportSourceMap::default(),
80 };
81 collector.process_module(None, &*source_file);
82 collector.raw_items
83 }
84} 74}
85 75
86impl Index<Module> for RawItems { 76impl Index<Module> for RawItems {
@@ -112,7 +102,7 @@ impl Index<Macro> for RawItems {
112} 102}
113 103
114#[derive(Debug, PartialEq, Eq, Clone, Copy)] 104#[derive(Debug, PartialEq, Eq, Clone, Copy)]
115pub(crate) enum RawItem { 105pub(super) enum RawItem {
116 Module(Module), 106 Module(Module),
117 Import(ImportId), 107 Import(ImportId),
118 Def(Def), 108 Def(Def),
@@ -120,11 +110,11 @@ pub(crate) enum RawItem {
120} 110}
121 111
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
123pub(crate) struct Module(RawId); 113pub(super) struct Module(RawId);
124impl_arena_id!(Module); 114impl_arena_id!(Module);
125 115
126#[derive(Debug, PartialEq, Eq)] 116#[derive(Debug, PartialEq, Eq)]
127pub(crate) enum ModuleData { 117pub(super) enum ModuleData {
128 Declaration { name: Name, source_item_id: SourceFileItemId }, 118 Declaration { name: Name, source_item_id: SourceFileItemId },
129 Definition { name: Name, source_item_id: SourceFileItemId, items: Vec<RawItem> }, 119 Definition { name: Name, source_item_id: SourceFileItemId, items: Vec<RawItem> },
130} 120}
@@ -135,26 +125,26 @@ impl_arena_id!(ImportId);
135 125
136#[derive(Debug, Clone, PartialEq, Eq)] 126#[derive(Debug, Clone, PartialEq, Eq)]
137pub struct ImportData { 127pub struct ImportData {
138 pub(crate) path: Path, 128 pub(super) path: Path,
139 pub(crate) alias: Option<Name>, 129 pub(super) alias: Option<Name>,
140 pub(crate) is_glob: bool, 130 pub(super) is_glob: bool,
141 pub(crate) is_prelude: bool, 131 pub(super) is_prelude: bool,
142 pub(crate) is_extern_crate: bool, 132 pub(super) is_extern_crate: bool,
143} 133}
144 134
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
146pub(crate) struct Def(RawId); 136pub(super) struct Def(RawId);
147impl_arena_id!(Def); 137impl_arena_id!(Def);
148 138
149#[derive(Debug, PartialEq, Eq)] 139#[derive(Debug, PartialEq, Eq)]
150pub(crate) struct DefData { 140pub(super) struct DefData {
151 pub(crate) source_item_id: SourceFileItemId, 141 pub(super) source_item_id: SourceFileItemId,
152 pub(crate) name: Name, 142 pub(super) name: Name,
153 pub(crate) kind: DefKind, 143 pub(super) kind: DefKind,
154} 144}
155 145
156#[derive(Debug, PartialEq, Eq, Clone, Copy)] 146#[derive(Debug, PartialEq, Eq, Clone, Copy)]
157pub(crate) enum DefKind { 147pub(super) enum DefKind {
158 Function, 148 Function,
159 Struct, 149 Struct,
160 Enum, 150 Enum,
@@ -165,16 +155,15 @@ pub(crate) enum DefKind {
165} 155}
166 156
167#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 157#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
168pub(crate) struct Macro(RawId); 158pub(super) struct Macro(RawId);
169impl_arena_id!(Macro); 159impl_arena_id!(Macro);
170 160
171#[derive(Debug, PartialEq, Eq)] 161#[derive(Debug, PartialEq, Eq)]
172pub(crate) struct MacroData { 162pub(super) struct MacroData {
173 pub(crate) source_item_id: SourceFileItemId, 163 pub(super) source_item_id: SourceFileItemId,
174 pub(crate) path: Path, 164 pub(super) path: Path,
175 pub(crate) name: Option<Name>, 165 pub(super) name: Option<Name>,
176 pub(crate) arg: tt::Subtree, 166 pub(super) export: bool,
177 pub(crate) export: bool,
178} 167}
179 168
180struct RawItemsCollector { 169struct RawItemsCollector {
@@ -291,18 +280,15 @@ impl RawItemsCollector {
291 } 280 }
292 281
293 fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) { 282 fn add_macro(&mut self, current_module: Option<Module>, m: &ast::MacroCall) {
294 let (path, arg) = match ( 283 let path = match m.path().and_then(Path::from_ast) {
295 m.path().and_then(Path::from_ast), 284 Some(it) => it,
296 m.token_tree().and_then(mbe::ast_to_token_tree),
297 ) {
298 (Some(path), Some((token_tree, _token_map))) => (path, token_tree),
299 _ => return, 285 _ => return,
300 }; 286 };
301 287
302 let name = m.name().map(|it| it.as_name()); 288 let name = m.name().map(|it| it.as_name());
303 let source_item_id = self.source_file_items.id_of_unchecked(m.syntax()); 289 let source_item_id = self.source_file_items.id_of_unchecked(m.syntax());
304 let export = m.has_atom_attr("macro_export"); 290 let export = m.has_atom_attr("macro_export");
305 let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, arg, name, export }); 291 let m = self.raw_items.macros.alloc(MacroData { source_item_id, path, name, export });
306 self.push_item(current_module, RawItem::Macro(m)); 292 self.push_item(current_module, RawItem::Macro(m));
307 } 293 }
308 294
diff --git a/crates/ra_hir/src/nameres/tests/incremental.rs b/crates/ra_hir/src/nameres/tests/incremental.rs
index 698781923..a059634e2 100644
--- a/crates/ra_hir/src/nameres/tests/incremental.rs
+++ b/crates/ra_hir/src/nameres/tests/incremental.rs
@@ -90,34 +90,27 @@ fn adding_inner_items_should_not_invalidate_def_map() {
90 ); 90 );
91} 91}
92 92
93// It would be awesome to make this work, but it's unclear how
94#[test] 93#[test]
95#[ignore] 94fn typing_inside_a_macro_should_not_invalidate_def_map() {
96fn typing_inside_a_function_inside_a_macro_should_not_invalidate_def_map() {
97 check_def_map_is_not_recomputed( 95 check_def_map_is_not_recomputed(
98 " 96 "
99 //- /lib.rs 97 //- /lib.rs
98 macro_rules! m {
99 ($ident:ident) => {
100 struct Foo;
101 }
102 }
100 mod foo; 103 mod foo;
101 104
102 use crate::foo::bar::Baz;
103
104 //- /foo/mod.rs 105 //- /foo/mod.rs
105 pub mod bar; 106 pub mod bar;
106 107
107 //- /foo/bar.rs 108 //- /foo/bar.rs
108 <|> 109 <|>
109 salsa::query_group! { 110 m!(X);
110 trait Baz {
111 fn foo() -> i32 { 1 + 1 }
112 }
113 }
114 ", 111 ",
115 " 112 "
116 salsa::query_group! { 113 m!(Y);
117 trait Baz {
118 fn foo() -> i32 { 92 }
119 }
120 }
121 ", 114 ",
122 ); 115 );
123} 116}