diff options
author | uHOOCCOOHu <[email protected]> | 2019-09-02 07:36:20 +0100 |
---|---|---|
committer | uHOOCCOOHu <[email protected]> | 2019-09-02 07:36:20 +0100 |
commit | a66214b34effe1ad7f4351a1b920cf3a8f98d3c0 (patch) | |
tree | 21c38c3888edbf0d17b59a4c09494788cea61d1c /crates/ra_hir | |
parent | dfa758f6a9146fbcb5109e7294d0a8e561c77913 (diff) |
Fix import strategy of `macro_use` and its test
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/marks.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/collector.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests/macros.rs | 10 |
4 files changed, 26 insertions, 9 deletions
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!( | |||
11 | match_ergonomics_ref | 11 | match_ergonomics_ref |
12 | trait_resolution_on_fn_type | 12 | trait_resolution_on_fn_type |
13 | infer_while_let | 13 | infer_while_let |
14 | macro_rules_from_other_crates_are_visible_with_macro_use | ||
14 | ); | 15 | ); |
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 { | |||
101 | /// However, do we want to put it as a global variable? | 101 | /// However, do we want to put it as a global variable? |
102 | poison_macros: FxHashSet<MacroDefId>, | 102 | poison_macros: FxHashSet<MacroDefId>, |
103 | 103 | ||
104 | exported_macros: FxHashMap<Name, MacroDefId>, | ||
105 | |||
104 | diagnostics: Vec<DefDiagnostic>, | 106 | diagnostics: Vec<DefDiagnostic>, |
105 | } | 107 | } |
106 | 108 | ||
@@ -245,6 +247,7 @@ impl CrateDefMap { | |||
245 | root, | 247 | root, |
246 | modules, | 248 | modules, |
247 | poison_macros: FxHashSet::default(), | 249 | poison_macros: FxHashSet::default(), |
250 | exported_macros: FxHashMap::default(), | ||
248 | diagnostics: Vec::new(), | 251 | diagnostics: Vec::new(), |
249 | } | 252 | } |
250 | }; | 253 | }; |
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 | |||
157 | // crate root, even if the parent modules is **not** visible. | 157 | // crate root, even if the parent modules is **not** visible. |
158 | if export { | 158 | if export { |
159 | self.update(self.def_map.root, None, &[(name.clone(), def.clone())]); | 159 | self.update(self.def_map.root, None, &[(name.clone(), def.clone())]); |
160 | |||
161 | // Exported macros are collected in crate level ready for | ||
162 | // glob import with `#[macro_use]`. | ||
163 | self.def_map.exported_macros.insert(name.clone(), macro_id); | ||
160 | } | 164 | } |
161 | self.update(module_id, None, &[(name.clone(), def)]); | 165 | self.update(module_id, None, &[(name.clone(), def)]); |
162 | self.global_macro_scope.insert(name, macro_id); | 166 | self.global_macro_scope.insert(name, macro_id); |
@@ -295,20 +299,18 @@ where | |||
295 | } | 299 | } |
296 | } | 300 | } |
297 | 301 | ||
298 | // `#[macro_use] extern crate` glob import macros | 302 | // `#[macro_use] extern crate` glob imports all macros exported, |
303 | // ignoring their scopes | ||
299 | if import.is_extern_crate && import.is_macro_use { | 304 | if import.is_extern_crate && import.is_macro_use { |
300 | if let Some(ModuleDef::Module(m)) = | 305 | if let Some(ModuleDef::Module(m)) = |
301 | def.a().and_then(|item| item.take_types()) | 306 | def.a().and_then(|item| item.take_types()) |
302 | { | 307 | { |
308 | tested_by!(macro_rules_from_other_crates_are_visible_with_macro_use); | ||
309 | |||
303 | let item_map = self.db.crate_def_map(m.krate); | 310 | let item_map = self.db.crate_def_map(m.krate); |
304 | let scope = &item_map[m.module_id].scope; | 311 | for (name, ¯o_id) in &item_map.exported_macros { |
305 | let macros = scope | 312 | self.define_macro(module_id, name.clone(), macro_id, false); |
306 | .macros | 313 | } |
307 | .iter() | ||
308 | .map(|(name, res)| (name.clone(), Either::B(*res))) | ||
309 | .collect::<Vec<_>>(); | ||
310 | |||
311 | self.update(module_id, Some(import_id), ¯os); | ||
312 | } | 314 | } |
313 | } | 315 | } |
314 | 316 | ||
@@ -877,6 +879,7 @@ mod tests { | |||
877 | root, | 879 | root, |
878 | modules, | 880 | modules, |
879 | poison_macros: FxHashSet::default(), | 881 | poison_macros: FxHashSet::default(), |
882 | exported_macros: FxHashMap::default(), | ||
880 | diagnostics: Vec::new(), | 883 | diagnostics: Vec::new(), |
881 | } | 884 | } |
882 | }; | 885 | }; |
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() { | |||
140 | 140 | ||
141 | #[test] | 141 | #[test] |
142 | fn macro_rules_from_other_crates_are_visible_with_macro_use() { | 142 | fn macro_rules_from_other_crates_are_visible_with_macro_use() { |
143 | covers!(macro_rules_from_other_crates_are_visible_with_macro_use); | ||
143 | let map = def_map_with_crate_graph( | 144 | let map = def_map_with_crate_graph( |
144 | " | 145 | " |
145 | //- /main.rs | 146 | //- /main.rs |
@@ -160,6 +161,13 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { | |||
160 | $(struct $i { field: u32 } )* | 161 | $(struct $i { field: u32 } )* |
161 | } | 162 | } |
162 | } | 163 | } |
164 | |||
165 | mod priv_mod { | ||
166 | #[macro_export] | ||
167 | macro_rules! baz { | ||
168 | () => {}; | ||
169 | } | ||
170 | } | ||
163 | ", | 171 | ", |
164 | crate_graph! { | 172 | crate_graph! { |
165 | "main": ("/main.rs", ["foo"]), | 173 | "main": ("/main.rs", ["foo"]), |
@@ -171,6 +179,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { | |||
171 | ⋮Bar: t v | 179 | ⋮Bar: t v |
172 | ⋮Foo: t v | 180 | ⋮Foo: t v |
173 | ⋮bar: t | 181 | ⋮bar: t |
182 | ⋮baz: m | ||
174 | ⋮foo: t | 183 | ⋮foo: t |
175 | ⋮structs: m | 184 | ⋮structs: m |
176 | ⋮ | 185 | ⋮ |
@@ -178,6 +187,7 @@ fn macro_rules_from_other_crates_are_visible_with_macro_use() { | |||
178 | ⋮Bar: t v | 187 | ⋮Bar: t v |
179 | ⋮Foo: t v | 188 | ⋮Foo: t v |
180 | ⋮bar: t | 189 | ⋮bar: t |
190 | ⋮baz: m | ||
181 | ⋮foo: t | 191 | ⋮foo: t |
182 | ⋮structs: m | 192 | ⋮structs: m |
183 | "###); | 193 | "###); |