From a66214b34effe1ad7f4351a1b920cf3a8f98d3c0 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Mon, 2 Sep 2019 14:36:20 +0800 Subject: Fix import strategy of `macro_use` and its test --- crates/ra_hir/src/marks.rs | 1 + crates/ra_hir/src/nameres.rs | 3 +++ crates/ra_hir/src/nameres/collector.rs | 21 ++++++++++++--------- crates/ra_hir/src/nameres/tests/macros.rs | 10 ++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 5b15eee90..2e1d35c8c 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -11,4 +11,5 @@ test_utils::marks!( match_ergonomics_ref trait_resolution_on_fn_type infer_while_let + macro_rules_from_other_crates_are_visible_with_macro_use ); diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index bbdc606cd..f69179bf6 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -101,6 +101,8 @@ pub struct CrateDefMap { /// However, do we want to put it as a global variable? poison_macros: FxHashSet, + exported_macros: FxHashMap, + diagnostics: Vec, } @@ -245,6 +247,7 @@ impl CrateDefMap { root, modules, poison_macros: FxHashSet::default(), + exported_macros: FxHashMap::default(), diagnostics: Vec::new(), } }; diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 26158b5c3..dbd687236 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -157,6 +157,10 @@ where // crate root, even if the parent modules is **not** visible. if export { self.update(self.def_map.root, None, &[(name.clone(), def.clone())]); + + // Exported macros are collected in crate level ready for + // glob import with `#[macro_use]`. + self.def_map.exported_macros.insert(name.clone(), macro_id); } self.update(module_id, None, &[(name.clone(), def)]); self.global_macro_scope.insert(name, macro_id); @@ -295,20 +299,18 @@ where } } - // `#[macro_use] extern crate` glob import macros + // `#[macro_use] extern crate` glob imports all macros exported, + // ignoring their scopes if import.is_extern_crate && import.is_macro_use { if let Some(ModuleDef::Module(m)) = def.a().and_then(|item| item.take_types()) { + tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); + let item_map = self.db.crate_def_map(m.krate); - let scope = &item_map[m.module_id].scope; - let macros = scope - .macros - .iter() - .map(|(name, res)| (name.clone(), Either::B(*res))) - .collect::>(); - - self.update(module_id, Some(import_id), ¯os); + for (name, ¯o_id) in &item_map.exported_macros { + self.define_macro(module_id, name.clone(), macro_id, false); + } } } @@ -877,6 +879,7 @@ mod tests { root, modules, poison_macros: FxHashSet::default(), + exported_macros: FxHashMap::default(), diagnostics: Vec::new(), } }; diff --git a/crates/ra_hir/src/nameres/tests/macros.rs b/crates/ra_hir/src/nameres/tests/macros.rs index a8ee0ba28..cfddf3029 100644 --- a/crates/ra_hir/src/nameres/tests/macros.rs +++ b/crates/ra_hir/src/nameres/tests/macros.rs @@ -140,6 +140,7 @@ fn unexpanded_macro_should_expand_by_fixedpoint_loop() { #[test] fn macro_rules_from_other_crates_are_visible_with_macro_use() { + covers!(macro_rules_from_other_crates_are_visible_with_macro_use); let map = def_map_with_crate_graph( " //- /main.rs @@ -160,6 +161,13 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { $(struct $i { field: u32 } )* } } + + mod priv_mod { + #[macro_export] + macro_rules! baz { + () => {}; + } + } ", crate_graph! { "main": ("/main.rs", ["foo"]), @@ -171,6 +179,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { ⋮Bar: t v ⋮Foo: t v ⋮bar: t + ⋮baz: m ⋮foo: t ⋮structs: m ⋮ @@ -178,6 +187,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { ⋮Bar: t v ⋮Foo: t v ⋮bar: t + ⋮baz: m ⋮foo: t ⋮structs: m "###); -- cgit v1.2.3