]> git.lizzy.rs Git - rust.git/commitdiff
Honor #![macro_use] in mod source files
authorLukas Wirth <lukastw97@gmail.com>
Sun, 31 Jan 2021 18:26:04 +0000 (19:26 +0100)
committerLukas Wirth <lukastw97@gmail.com>
Sun, 31 Jan 2021 18:33:02 +0000 (19:33 +0100)
crates/hir_def/src/nameres/collector.rs
crates/hir_def/src/nameres/tests/macros.rs

index ae98fadac29785514ffdb72376f97fccb12588d1..fcc8e2607ea517202a3113eb72e7b155f82432ed 100644 (file)
@@ -1273,12 +1273,8 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
             // out of line module, resolve, parse and recurse
             ModKind::Outline {} => {
                 let ast_id = AstId::new(self.file_id, module.ast_id);
-                match self.mod_dir.resolve_declaration(
-                    self.def_collector.db,
-                    self.file_id,
-                    &module.name,
-                    path_attr,
-                ) {
+                let db = self.def_collector.db;
+                match self.mod_dir.resolve_declaration(db, self.file_id, &module.name, path_attr) {
                     Ok((file_id, is_mod_rs, mod_dir)) => {
                         let module_id = self.push_child_module(
                             module.name.clone(),
@@ -1286,7 +1282,7 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
                             Some((file_id, is_mod_rs)),
                             &self.item_tree[module.visibility],
                         );
-                        let item_tree = self.def_collector.db.item_tree(file_id.into());
+                        let item_tree = db.item_tree(file_id.into());
                         ModCollector {
                             def_collector: &mut *self.def_collector,
                             macro_depth: self.macro_depth,
@@ -1296,7 +1292,12 @@ fn collect_module(&mut self, module: &Mod, attrs: &Attrs) {
                             mod_dir,
                         }
                         .collect(item_tree.top_level_items());
-                        if is_macro_use {
+                        if is_macro_use
+                            || item_tree
+                                .top_level_attrs(db, self.def_collector.def_map.krate)
+                                .by_key("macro_use")
+                                .exists()
+                        {
                             self.import_all_legacy_macros(module_id);
                         }
                     }
index e5e9e8ca16d3ac118885168e69a15a959bb629b9..36ed5e8cefe52d06c53bc43fa8f385015b9c46b8 100644 (file)
@@ -391,11 +391,21 @@ macro_rules! foo {
 mod m4;
 bar!(OkMacroUse);
 
+mod m5;
+baz!(OkMacroUseInner);
+
 //- /m3/m4.rs
 foo!(ok_shadow_deep);
 macro_rules! bar {
     ($x:ident) => { struct $x; }
 }
+//- /m3/m5.rs
+#![macro_use]
+macro_rules! baz {
+    ($x:ident) => { struct $x; }
+}
+
+
 "#,
         expect![[r#"
             crate
@@ -423,11 +433,15 @@ macro_rules! bar {
             crate::m3
             OkAfterInside: t v
             OkMacroUse: t v
+            OkMacroUseInner: t v
             m4: t
+            m5: t
             ok_shadow: v
 
             crate::m3::m4
             ok_shadow_deep: v
+
+            crate::m3::m5
         "#]],
     );
 }