From 92c07803cc0ce1d2008cc912f006d1cd66ff3f4a Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Sun, 8 Sep 2019 00:37:54 +0800 Subject: Rename `textual_macro` -> `legacy_macro` Add comments --- crates/ra_hir/src/nameres.rs | 27 ++++++++++++++++------ crates/ra_hir/src/nameres/collector.rs | 37 ++++++++++++++----------------- crates/ra_hir/src/nameres/tests/macros.rs | 2 +- crates/ra_hir/src/ty/tests.rs | 2 +- 4 files changed, 39 insertions(+), 29 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index befbb2a9b..74546e5e2 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -138,8 +138,21 @@ pub(crate) struct ModuleData { #[derive(Debug, Default, PartialEq, Eq, Clone)] pub struct ModuleScope { items: FxHashMap, + /// Macros in current module scoped + /// + /// This scope works exactly the same way that item scoping does. + /// Macro invocation with quantified path will search in it. + /// See details below. macros: FxHashMap, - textual_macros: FxHashMap, + /// Macros visable in current module in legacy textual scope + /// + /// For macros invoked by an unquatified identifier like `bar!()`, `legacy_macros` will be searched in first. + /// If it yields no result, then it turns to module scoped `macros`. + /// It macros with name quatified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped, + /// and only normal scoped `macros` will be searched in. + /// + /// Note that this automatically inherit macros defined textually before the definition of module itself. + legacy_macros: FxHashMap, } static BUILTIN_SCOPE: Lazy> = Lazy::new(|| { @@ -173,8 +186,8 @@ impl ModuleScope { _ => None, } } - fn get_textual_macro(&self, name: &Name) -> Option { - self.textual_macros.get(name).copied() + fn get_legacy_macro(&self, name: &Name) -> Option { + self.legacy_macros.get(name).copied() } } @@ -489,13 +502,13 @@ impl CrateDefMap { name: &Name, ) -> ItemOrMacro { // Resolve in: - // - textual scoped macros + // - legacy scope // - current module / scope // - extern prelude // - std prelude - let from_textual_mcro = self[module] + let from_legacy_macro = self[module] .scope - .get_textual_macro(name) + .get_legacy_macro(name) .map_or_else(|| Either::A(PerNs::none()), Either::B); let from_scope = self[module].scope.get_item_or_macro(name).unwrap_or_else(|| Either::A(PerNs::none())); @@ -503,7 +516,7 @@ impl CrateDefMap { self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); let from_prelude = self.resolve_in_prelude(db, name); - or(from_textual_mcro, or(from_scope, or(Either::A(from_extern_prelude), from_prelude))) + or(from_legacy_macro, or(from_scope, or(Either::A(from_extern_prelude), from_prelude))) } fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 10c32ffa1..09cda7656 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -146,22 +146,19 @@ where self.def_map.exported_macros.insert(name.clone(), macro_id); } self.update(module_id, None, &[(name.clone(), def)]); - self.define_textual_macro(module_id, name.clone(), macro_id); + self.define_legacy_macro(module_id, name.clone(), macro_id); } - /// Define a macro in current textual scope. + /// Define a legacy textual scoped macro in module /// - /// We use a map `textual_macros` to store all textual macros visable per module. - /// It will clone all macros from parent textual scope, whose definition is prior to + /// We use a map `legacy_macros` to store all legacy textual scoped macros visable per module. + /// It will clone all macros from parent legacy scope, whose definition is prior to /// the definition of current module. - /// And also, `macro_use` on a module will import all textual macros visable inside to - /// current textual scope, with possible shadowing. - fn define_textual_macro(&mut self, module_id: CrateModuleId, name: Name, macro_id: MacroDefId) { + /// And also, `macro_use` on a module will import all legacy macros visable inside to + /// current legacy scope, with possible shadowing. + fn define_legacy_macro(&mut self, module_id: CrateModuleId, name: Name, macro_id: MacroDefId) { // Always shadowing - self.def_map.modules[module_id] - .scope - .textual_macros - .insert(name, MacroDef { id: macro_id }); + self.def_map.modules[module_id].scope.legacy_macros.insert(name, MacroDef { id: macro_id }); } /// Import macros from `#[macro_use] extern crate`. @@ -194,7 +191,7 @@ where fn import_all_macros_exported(&mut self, current_module_id: CrateModuleId, module: Module) { let item_map = self.db.crate_def_map(module.krate); for (name, ¯o_id) in &item_map.exported_macros { - self.define_textual_macro(current_module_id, name.clone(), macro_id); + self.define_legacy_macro(current_module_id, name.clone(), macro_id); } } @@ -578,7 +575,7 @@ where } .collect(&*items); if *is_macro_use { - self.import_all_textual_macros(module_id); + self.import_all_legacy_macros(module_id); } } // out of line module, resolve, parse and recurse @@ -605,7 +602,7 @@ where } .collect(raw_items.items()); if *is_macro_use { - self.import_all_textual_macros(module_id); + self.import_all_legacy_macros(module_id); } } Err(candidate) => self.def_collector.def_map.diagnostics.push( @@ -631,7 +628,7 @@ where modules[res].parent = Some(self.module_id); modules[res].declaration = Some(declaration); modules[res].definition = definition; - modules[res].scope.textual_macros = modules[self.module_id].scope.textual_macros.clone(); + modules[res].scope.legacy_macros = modules[self.module_id].scope.legacy_macros.clone(); modules[self.module_id].children.insert(name.clone(), res); let resolution = Resolution { def: PerNs::types( @@ -685,10 +682,10 @@ where let ast_id = mac.ast_id.with_file_id(self.file_id); - // Case 2: try to resolve in textual scope and expand macro_rules, triggering + // Case 2: try to resolve in legacy scope and expand macro_rules, triggering // recursive item collection. if let Some(macro_def) = mac.path.as_ident().and_then(|name| { - self.def_collector.def_map[self.module_id].scope.get_textual_macro(&name) + self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) }) { let def = macro_def.id; let macro_call_id = MacroCallLoc { def, ast_id }.id(self.def_collector.db); @@ -706,10 +703,10 @@ where self.def_collector.unexpanded_macros.push((self.module_id, ast_id, path)); } - fn import_all_textual_macros(&mut self, module_id: CrateModuleId) { - let macros = self.def_collector.def_map[module_id].scope.textual_macros.clone(); + fn import_all_legacy_macros(&mut self, module_id: CrateModuleId) { + let macros = self.def_collector.def_map[module_id].scope.legacy_macros.clone(); for (name, macro_) in macros { - self.def_collector.define_textual_macro(self.module_id, name.clone(), macro_.id); + self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_.id); } } } diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir/src/nameres/tests/macros.rs index a894c6836..21fab53e9 100644 --- a/crates/ra_hir/src/nameres/tests/macros.rs +++ b/crates/ra_hir/src/nameres/tests/macros.rs @@ -279,7 +279,7 @@ fn prelude_cycle() { } #[test] -fn plain_macros_are_textual_scoped() { +fn plain_macros_are_legacy_textual_scoped() { let map = def_map( r#" //- /main.rs diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index f2d5b115e..25716fe8c 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2804,7 +2804,7 @@ fn main() { } #[test] -fn infer_textual_scoped_macros_expanded() { +fn infer_legacy_textual_scoped_macros_expanded() { assert_snapshot!( infer(r#" struct Foo(Vec); -- cgit v1.2.3