about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-31 20:04:07 +0000
committerGitHub <noreply@github.com>2021-01-31 20:04:07 +0000
commite6e93b3d1d8af018a6cb1fe3fd6c3f166c8a64f5 (patch)
tree20420c6ff4f67e09395700d4877e797cda865820
parent286d90de2d213b467a092e702edf8b0706c7c1b2 (diff)
parent412f180d71bb942dcda5afaa7d6dc2ad6f463d61 (diff)
downloadrust-e6e93b3d1d8af018a6cb1fe3fd6c3f166c8a64f5.tar.gz
rust-e6e93b3d1d8af018a6cb1fe3fd6c3f166c8a64f5.zip
Merge #7502
7502: Honor #![macro_use] in mod source files r=jonas-schievink a=Veykril

Fixes #7501

Since `ItemTree` builds the `RawAttrs` directly we need the special check here as I don't think we can fix this in `RawAttrs` constructor as its solely AST based and we need to touch two different ASTs here.

This just made me realize that `attrs_query` suffers from a similar problem, for example hovering an outline `mod` decl won't show inner docs, only outer ones, #7503.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
-rw-r--r--crates/hir_def/src/nameres/collector.rs17
-rw-r--r--crates/hir_def/src/nameres/tests/macros.rs14
2 files changed, 23 insertions, 8 deletions
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index ae98fadac29..fcc8e2607ea 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -1273,12 +1273,8 @@ impl ModCollector<'_, '_> {
             // 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 @@ impl ModCollector<'_, '_> {
                             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 @@ impl ModCollector<'_, '_> {
                             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);
                         }
                     }
diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs
index e5e9e8ca16d..36ed5e8cefe 100644
--- a/crates/hir_def/src/nameres/tests/macros.rs
+++ b/crates/hir_def/src/nameres/tests/macros.rs
@@ -391,11 +391,21 @@ foo!(ok_shadow);
 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
         "#]],
     );
 }