diff options
author | Seivan Heidari <[email protected]> | 2019-10-31 08:43:20 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-10-31 08:43:20 +0000 |
commit | 8edda0e7b164009d6c03bb3d4be603fb38ad2e2a (patch) | |
tree | 744cf81075d394e2f9c06afb07642a2601800dda /crates/ra_hir/src/nameres/collector.rs | |
parent | 49562d36b97ddde34cf7585a8c2e8f232519b657 (diff) | |
parent | d067afb064a7fa67b172abf561b7d80740cd6f18 (diff) |
Merge branch 'master' into feature/themes
Diffstat (limited to 'crates/ra_hir/src/nameres/collector.rs')
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index b5fe16bfa..ee0a4c99f 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs | |||
@@ -1,5 +1,10 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_def::{ | ||
4 | attr::Attr, | ||
5 | nameres::{mod_resolution::ModDir, raw}, | ||
6 | }; | ||
7 | use hir_expand::name; | ||
3 | use ra_cfg::CfgOptions; | 8 | use ra_cfg::CfgOptions; |
4 | use ra_db::FileId; | 9 | use ra_db::FileId; |
5 | use ra_syntax::{ast, SmolStr}; | 10 | use ra_syntax::{ast, SmolStr}; |
@@ -7,13 +12,11 @@ use rustc_hash::FxHashMap; | |||
7 | use test_utils::tested_by; | 12 | use test_utils::tested_by; |
8 | 13 | ||
9 | use crate::{ | 14 | use crate::{ |
10 | attr::Attr, | ||
11 | db::DefDatabase, | 15 | db::DefDatabase, |
12 | ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, | 16 | ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, |
13 | name::MACRO_RULES, | ||
14 | nameres::{ | 17 | nameres::{ |
15 | diagnostics::DefDiagnostic, mod_resolution::ModDir, raw, Crate, CrateDefMap, CrateModuleId, | 18 | diagnostics::DefDiagnostic, Crate, CrateDefMap, CrateModuleId, ModuleData, ModuleDef, |
16 | ModuleData, ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode, | 19 | PerNs, ReachedFixedPoint, Resolution, ResolveMode, |
17 | }, | 20 | }, |
18 | Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, | 21 | Adt, AstId, Const, Enum, Function, HirFileId, MacroDef, Module, Name, Path, PathKind, Static, |
19 | Struct, Trait, TypeAlias, Union, | 22 | Struct, Trait, TypeAlias, Union, |
@@ -212,7 +215,7 @@ where | |||
212 | 215 | ||
213 | if let Some(ModuleDef::Module(m)) = res.take_types() { | 216 | if let Some(ModuleDef::Module(m)) = res.take_types() { |
214 | tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); | 217 | tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); |
215 | self.import_all_macros_exported(current_module_id, m.krate); | 218 | self.import_all_macros_exported(current_module_id, m.krate()); |
216 | } | 219 | } |
217 | } | 220 | } |
218 | 221 | ||
@@ -289,11 +292,11 @@ where | |||
289 | if import.is_prelude { | 292 | if import.is_prelude { |
290 | tested_by!(std_prelude); | 293 | tested_by!(std_prelude); |
291 | self.def_map.prelude = Some(m); | 294 | self.def_map.prelude = Some(m); |
292 | } else if m.krate != self.def_map.krate { | 295 | } else if m.krate() != self.def_map.krate { |
293 | tested_by!(glob_across_crates); | 296 | tested_by!(glob_across_crates); |
294 | // glob import from other crate => we can just import everything once | 297 | // glob import from other crate => we can just import everything once |
295 | let item_map = self.db.crate_def_map(m.krate); | 298 | let item_map = self.db.crate_def_map(m.krate()); |
296 | let scope = &item_map[m.module_id].scope; | 299 | let scope = &item_map[m.id.module_id].scope; |
297 | 300 | ||
298 | // Module scoped macros is included | 301 | // Module scoped macros is included |
299 | let items = scope | 302 | let items = scope |
@@ -307,7 +310,7 @@ where | |||
307 | // glob import from same crate => we do an initial | 310 | // glob import from same crate => we do an initial |
308 | // import, and then need to propagate any further | 311 | // import, and then need to propagate any further |
309 | // additions | 312 | // additions |
310 | let scope = &self.def_map[m.module_id].scope; | 313 | let scope = &self.def_map[m.id.module_id].scope; |
311 | 314 | ||
312 | // Module scoped macros is included | 315 | // Module scoped macros is included |
313 | let items = scope | 316 | let items = scope |
@@ -319,7 +322,7 @@ where | |||
319 | self.update(module_id, Some(import_id), &items); | 322 | self.update(module_id, Some(import_id), &items); |
320 | // record the glob import in case we add further items | 323 | // record the glob import in case we add further items |
321 | self.glob_imports | 324 | self.glob_imports |
322 | .entry(m.module_id) | 325 | .entry(m.id.module_id) |
323 | .or_default() | 326 | .or_default() |
324 | .push((module_id, import_id)); | 327 | .push((module_id, import_id)); |
325 | } | 328 | } |
@@ -448,7 +451,7 @@ where | |||
448 | ); | 451 | ); |
449 | 452 | ||
450 | if let Some(def) = resolved_res.resolved_def.get_macros() { | 453 | if let Some(def) = resolved_res.resolved_def.get_macros() { |
451 | let call_id = MacroCallLoc { def: def.id, ast_id: *ast_id }.id(self.db); | 454 | let call_id = self.db.intern_macro(MacroCallLoc { def: def.id, ast_id: *ast_id }); |
452 | resolved.push((*module_id, call_id, def.id)); | 455 | resolved.push((*module_id, call_id, def.id)); |
453 | res = ReachedFixedPoint::No; | 456 | res = ReachedFixedPoint::No; |
454 | return false; | 457 | return false; |
@@ -523,9 +526,10 @@ where | |||
523 | 526 | ||
524 | // Prelude module is always considered to be `#[macro_use]`. | 527 | // Prelude module is always considered to be `#[macro_use]`. |
525 | if let Some(prelude_module) = self.def_collector.def_map.prelude { | 528 | if let Some(prelude_module) = self.def_collector.def_map.prelude { |
526 | if prelude_module.krate != self.def_collector.def_map.krate { | 529 | if prelude_module.krate() != self.def_collector.def_map.krate { |
527 | tested_by!(prelude_is_macro_use); | 530 | tested_by!(prelude_is_macro_use); |
528 | self.def_collector.import_all_macros_exported(self.module_id, prelude_module.krate); | 531 | self.def_collector |
532 | .import_all_macros_exported(self.module_id, prelude_module.krate()); | ||
529 | } | 533 | } |
530 | } | 534 | } |
531 | 535 | ||
@@ -567,7 +571,7 @@ where | |||
567 | // inline module, just recurse | 571 | // inline module, just recurse |
568 | raw::ModuleData::Definition { name, items, ast_id } => { | 572 | raw::ModuleData::Definition { name, items, ast_id } => { |
569 | let module_id = | 573 | let module_id = |
570 | self.push_child_module(name.clone(), ast_id.with_file_id(self.file_id), None); | 574 | self.push_child_module(name.clone(), AstId::new(self.file_id, *ast_id), None); |
571 | 575 | ||
572 | ModCollector { | 576 | ModCollector { |
573 | def_collector: &mut *self.def_collector, | 577 | def_collector: &mut *self.def_collector, |
@@ -583,7 +587,7 @@ where | |||
583 | } | 587 | } |
584 | // out of line module, resolve, parse and recurse | 588 | // out of line module, resolve, parse and recurse |
585 | raw::ModuleData::Declaration { name, ast_id } => { | 589 | raw::ModuleData::Declaration { name, ast_id } => { |
586 | let ast_id = ast_id.with_file_id(self.file_id); | 590 | let ast_id = AstId::new(self.file_id, *ast_id); |
587 | match self.mod_dir.resolve_declaration( | 591 | match self.mod_dir.resolve_declaration( |
588 | self.def_collector.db, | 592 | self.def_collector.db, |
589 | self.file_id, | 593 | self.file_id, |
@@ -631,9 +635,7 @@ where | |||
631 | modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); | 635 | modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); |
632 | modules[self.module_id].children.insert(name.clone(), res); | 636 | modules[self.module_id].children.insert(name.clone(), res); |
633 | let resolution = Resolution { | 637 | let resolution = Resolution { |
634 | def: PerNs::types( | 638 | def: PerNs::types(Module::new(self.def_collector.def_map.krate, res).into()), |
635 | Module { krate: self.def_collector.def_map.krate, module_id: res }.into(), | ||
636 | ), | ||
637 | import: None, | 639 | import: None, |
638 | }; | 640 | }; |
639 | self.def_collector.update(self.module_id, None, &[(name, resolution)]); | 641 | self.def_collector.update(self.module_id, None, &[(name, resolution)]); |
@@ -641,8 +643,8 @@ where | |||
641 | } | 643 | } |
642 | 644 | ||
643 | fn define_def(&mut self, def: &raw::DefData) { | 645 | fn define_def(&mut self, def: &raw::DefData) { |
644 | let module = Module { krate: self.def_collector.def_map.krate, module_id: self.module_id }; | 646 | let module = Module::new(self.def_collector.def_map.krate, self.module_id); |
645 | let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); | 647 | let ctx = LocationCtx::new(self.def_collector.db, module.id, self.file_id); |
646 | 648 | ||
647 | macro_rules! def { | 649 | macro_rules! def { |
648 | ($kind:ident, $ast_id:ident) => { | 650 | ($kind:ident, $ast_id:ident) => { |
@@ -671,28 +673,26 @@ where | |||
671 | } | 673 | } |
672 | 674 | ||
673 | fn collect_macro(&mut self, mac: &raw::MacroData) { | 675 | fn collect_macro(&mut self, mac: &raw::MacroData) { |
676 | let ast_id = AstId::new(self.file_id, mac.ast_id); | ||
677 | |||
674 | // Case 1: macro rules, define a macro in crate-global mutable scope | 678 | // Case 1: macro rules, define a macro in crate-global mutable scope |
675 | if is_macro_rules(&mac.path) { | 679 | if is_macro_rules(&mac.path) { |
676 | if let Some(name) = &mac.name { | 680 | if let Some(name) = &mac.name { |
677 | let macro_id = MacroDefId { | 681 | let macro_id = |
678 | ast_id: mac.ast_id.with_file_id(self.file_id), | 682 | MacroDefId { ast_id, krate: self.def_collector.def_map.krate.crate_id }; |
679 | krate: self.def_collector.def_map.krate, | ||
680 | }; | ||
681 | let macro_ = MacroDef { id: macro_id }; | 683 | let macro_ = MacroDef { id: macro_id }; |
682 | self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export); | 684 | self.def_collector.define_macro(self.module_id, name.clone(), macro_, mac.export); |
683 | } | 685 | } |
684 | return; | 686 | return; |
685 | } | 687 | } |
686 | 688 | ||
687 | let ast_id = mac.ast_id.with_file_id(self.file_id); | ||
688 | |||
689 | // Case 2: try to resolve in legacy scope and expand macro_rules, triggering | 689 | // Case 2: try to resolve in legacy scope and expand macro_rules, triggering |
690 | // recursive item collection. | 690 | // recursive item collection. |
691 | if let Some(macro_def) = mac.path.as_ident().and_then(|name| { | 691 | if let Some(macro_def) = mac.path.as_ident().and_then(|name| { |
692 | self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) | 692 | self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) |
693 | }) { | 693 | }) { |
694 | let def = macro_def.id; | 694 | let def = macro_def.id; |
695 | let macro_call_id = MacroCallLoc { def, ast_id }.id(self.def_collector.db); | 695 | let macro_call_id = self.def_collector.db.intern_macro(MacroCallLoc { def, ast_id }); |
696 | 696 | ||
697 | self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, def); | 697 | self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, def); |
698 | return; | 698 | return; |
@@ -728,7 +728,7 @@ where | |||
728 | } | 728 | } |
729 | 729 | ||
730 | fn is_macro_rules(path: &Path) -> bool { | 730 | fn is_macro_rules(path: &Path) -> bool { |
731 | path.as_ident() == Some(&MACRO_RULES) | 731 | path.as_ident() == Some(&name::MACRO_RULES) |
732 | } | 732 | } |
733 | 733 | ||
734 | #[cfg(test)] | 734 | #[cfg(test)] |