Skip to content

Commit 83b4329

Browse files
bors[bot]Jonas Schievink
and
Jonas Schievink
authored
Merge #11800
11800: fix: fix `#[macro_use]` no longer importing non-`macro_rules!` macros r=jonas-schievink a=jonas-schievink #11663 introduced a regression tracked in #11781, this fixes it. Fixes #11781 bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 0b3daef + 18ad750 commit 83b4329

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

crates/hir/src/symbols.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use base_db::FileRange;
44
use hir_def::{
55
item_tree::ItemTreeNode, src::HasSource, AdtId, AssocItemId, AssocItemLoc, DefWithBodyId,
6-
ImplId, ItemContainerId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId,
6+
HasModule, ImplId, ItemContainerId, Lookup, MacroId, ModuleDefId, ModuleId, TraitId,
77
};
88
use hir_expand::{HirFileId, InFile};
99
use hir_ty::db::HirDatabase;
@@ -176,9 +176,12 @@ impl<'a> SymbolCollector<'a> {
176176
}
177177

178178
for (_, id) in scope.legacy_macros() {
179-
let loc = id.lookup(self.db.upcast());
180-
if loc.container == module_id {
181-
self.push_decl(id, FileSymbolKind::Macro);
179+
if id.module(self.db.upcast()) == module_id {
180+
match id {
181+
MacroId::Macro2Id(id) => self.push_decl(id, FileSymbolKind::Macro),
182+
MacroId::MacroRulesId(id) => self.push_decl(id, FileSymbolKind::Macro),
183+
MacroId::ProcMacroId(id) => self.push_decl(id, FileSymbolKind::Macro),
184+
}
182185
}
183186
}
184187
}

crates/hir_def/src/child_by_source.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ impl ChildBySource for ItemScope {
103103
},
104104
);
105105
self.legacy_macros().for_each(|(_, id)| {
106-
let loc = id.lookup(db);
107-
if loc.id.file_id() == file_id {
108-
res[keys::MACRO_RULES].insert(loc.source(db).value, id);
106+
if let MacroId::MacroRulesId(id) = id {
107+
let loc = id.lookup(db);
108+
if loc.id.file_id() == file_id {
109+
res[keys::MACRO_RULES].insert(loc.source(db).value, id);
110+
}
109111
}
110112
});
111113
self.derive_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(

crates/hir_def/src/item_scope.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use syntax::ast;
1414

1515
use crate::{
1616
attr::AttrId, db::DefDatabase, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType,
17-
ConstId, HasModule, ImplId, LocalModuleId, MacroId, MacroRulesId, ModuleDefId, ModuleId,
18-
TraitId,
17+
ConstId, HasModule, ImplId, LocalModuleId, MacroId, ModuleDefId, ModuleId, TraitId,
1918
};
2019

2120
#[derive(Copy, Clone)]
@@ -62,7 +61,7 @@ pub struct ItemScope {
6261
/// Module scoped macros will be inserted into `items` instead of here.
6362
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
6463
// be all resolved to the last one defined if shadowing happens.
65-
legacy_macros: FxHashMap<Name, MacroRulesId>,
64+
legacy_macros: FxHashMap<Name, MacroId>,
6665
/// The derive macro invocations in this scope.
6766
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
6867
/// The derive macro invocations in this scope, keyed by the owner item over the actual derive attributes
@@ -135,7 +134,7 @@ impl ItemScope {
135134
}
136135

137136
/// Iterate over all legacy textual scoped macros visible at the end of the module
138-
pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroRulesId)> + 'a {
137+
pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroId)> + 'a {
139138
self.legacy_macros.iter().map(|(name, def)| (name, *def))
140139
}
141140

@@ -181,7 +180,7 @@ impl ItemScope {
181180
self.declarations.push(def)
182181
}
183182

184-
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroRulesId> {
183+
pub(crate) fn get_legacy_macro(&self, name: &Name) -> Option<MacroId> {
185184
self.legacy_macros.get(name).copied()
186185
}
187186

@@ -193,7 +192,7 @@ impl ItemScope {
193192
self.unnamed_consts.push(konst);
194193
}
195194

196-
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroRulesId) {
195+
pub(crate) fn define_legacy_macro(&mut self, name: Name, mac: MacroId) {
197196
self.legacy_macros.insert(name, mac);
198197
}
199198

@@ -323,7 +322,7 @@ impl ItemScope {
323322
)
324323
}
325324

326-
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroRulesId> {
325+
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, MacroId> {
327326
self.legacy_macros.clone()
328327
}
329328

crates/hir_def/src/nameres/collector.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl DefCollector<'_> {
610610
export: bool,
611611
) {
612612
// Textual scoping
613-
self.define_legacy_macro(module_id, name.clone(), macro_);
613+
self.define_legacy_macro(module_id, name.clone(), macro_.into());
614614

615615
// Module scoping
616616
// In Rust, `#[macro_export]` macros are unconditionally visible at the
@@ -634,7 +634,7 @@ impl DefCollector<'_> {
634634
/// the definition of current module.
635635
/// And also, `macro_use` on a module will import all legacy macros visible inside to
636636
/// current legacy scope, with possible shadowing.
637-
fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroRulesId) {
637+
fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, mac: MacroId) {
638638
// Always shadowing
639639
self.def_map.modules[module_id].scope.define_legacy_macro(name, mac);
640640
}
@@ -706,10 +706,8 @@ impl DefCollector<'_> {
706706
fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) {
707707
let def_map = self.db.crate_def_map(krate);
708708
for (name, def) in def_map[def_map.root].scope.macros() {
709-
if let MacroId::MacroRulesId(def) = def {
710-
// `macro_use` only bring things into legacy scope.
711-
self.define_legacy_macro(current_module_id, name.clone(), def);
712-
}
709+
// `#[macro_use]` brings macros into legacy scope. Yes, even non-`macro_rules!` macros.
710+
self.define_legacy_macro(current_module_id, name.clone(), def);
713711
}
714712
}
715713

crates/hir_def/src/nameres/tests/macros.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,40 @@ struct A;
11431143
"#]],
11441144
);
11451145
}
1146+
1147+
#[test]
1148+
fn macro_use_imports_all_macro_types() {
1149+
let def_map = compute_crate_def_map(
1150+
r#"
1151+
//- /main.rs crate:main deps:lib
1152+
#[macro_use]
1153+
extern crate lib;
1154+
1155+
//- /lib.rs crate:lib deps:proc
1156+
pub use proc::*;
1157+
1158+
#[macro_export]
1159+
macro_rules! legacy { () => () }
1160+
1161+
pub macro macro20 {}
1162+
1163+
//- /proc.rs crate:proc
1164+
#![crate_type="proc-macro"]
1165+
1166+
struct TokenStream;
1167+
1168+
#[proc_macro_attribute]
1169+
fn proc_attr(a: TokenStream, b: TokenStream) -> TokenStream { a }
1170+
"#,
1171+
);
1172+
1173+
let root = &def_map[def_map.root()].scope;
1174+
let actual = root.legacy_macros().map(|(name, _)| format!("{name}\n")).collect::<String>();
1175+
1176+
expect![[r#"
1177+
macro20
1178+
legacy
1179+
proc_attr
1180+
"#]]
1181+
.assert_eq(&actual);
1182+
}

0 commit comments

Comments
 (0)