Skip to content

Commit 29a229c

Browse files
authored
Rollup merge of #164181 - estebank:issue-57966, r=khyperia
Tweak output on unused/unaccessible local macro In unused macro lint, mention when its enclosing `mod` isn't annotated with `#[macro_use]`: ``` error: unused macro definition: `unused` --> $DIR/unused-macros-decl.rs:17:11 | LL | macro unused { | ^^^^^^ | note: the lint level is defined here --> $DIR/unused-macros-decl.rs:16:12 | LL | #[deny(unused_macros)] | ^^^^^^^^^^^^^ help: consider annotating the enclosing module with `#[macro_use]` | LL + #[macro_use] LL | mod bar { | ``` On resolution error for unexported macro, point at macro of the same name and mention its enclosing scope could be marked with `#[macro_use]`: ``` error: cannot find macro `a_macro` in this scope --> $DIR/unresolved-macro-missing-macro-use.rs:11:5 | LL | a_macro!(); | ^^^^^^^ | help: you might be missing a `#[macro_use]` attribute on a `mod` module or `use` import --> $DIR/unresolved-macro-missing-macro-use.rs:5:5 | LL | / mod has_a_macro { LL | | LL | | macro_rules! a_macro { () => {} } | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ a macro with the same name is defined here ... | LL | | } | |_- consider annotating this `mod` with `#[macro_use]` ``` On ambiguous macro use, suggest removing invalid import: ``` error[E0659]: `a_macro` is ambiguous --> $DIR/unresolved-macro-missing-macro-use-3.rs:15:5 | LL | a_macro!(); | ^^^^^^^ ambiguous name | = note: ambiguous because of a conflict between a `macro_rules` name and a non-`macro_rules` name from another module note: `a_macro` could refer to the macro defined here --> $DIR/unresolved-macro-missing-macro-use-3.rs:5:5 | LL | macro_rules! a_macro { () => {} } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: `a_macro` could also refer to the unresolved item imported here --> $DIR/unresolved-macro-missing-macro-use-3.rs:9:5 | LL | use crate::has_a_macro::a_macro; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: consider removing this ``` Fix #57966.
2 parents 0b41aed + cfcc5ea commit 29a229c

17 files changed

Lines changed: 375 additions & 32 deletions

‎compiler/rustc_resolve/src/build_reduced_graph.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,9 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
12941294
// items will be reported as `unused_macros`.
12951295
fn insert_unused_macro(&mut self, ident: Ident, def_id: LocalDefId, node_id: NodeId) {
12961296
if !ident.as_str().starts_with('_') {
1297-
self.r.unused_macros.insert(def_id, (node_id, ident));
1297+
self.r
1298+
.unused_macros
1299+
.insert(def_id, (node_id, ident, self.parent_scope.module.opt_def_id()));
12981300
if let SyntaxExtensionKind::MacroRules(mr) = &self.r.local_macro_map[&def_id].kind {
12991301
let value = (def_id, DenseBitSet::new_filled(mr.nrules()));
13001302
self.r.unused_macro_rules.insert(node_id, value);
@@ -1333,7 +1335,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
13331335

13341336
if macro_rules {
13351337
let ident = IdentKey::new(orig_ident);
1336-
self.r.macro_names.insert(ident);
1338+
self.r.macro_names.insert(ident, def_id);
13371339
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
13381340
let vis = if is_macro_export {
13391341
Visibility::Public
@@ -1430,6 +1432,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
14301432
}
14311433
match item.kind {
14321434
ItemKind::Mod(..) if self.contains_macro_use(&item.attrs) => {
1435+
self.r.macro_use_mods.insert(item.id);
14331436
self.parent_scope.macro_rules
14341437
}
14351438
_ => orig_macro_rules_scope,

‎compiler/rustc_resolve/src/diagnostics/impls.rs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20312031
return;
20322032
}
20332033

2034-
let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident))| {
2034+
let unused_macro = self.unused_macros.iter().find_map(|(def_id, (_, unused_ident, _))| {
20352035
if unused_ident.name == ident.name { Some((def_id, unused_ident)) } else { None }
20362036
});
20372037

@@ -2166,8 +2166,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21662166
}
21672167
}
21682168

2169-
if self.macro_names.contains(&IdentKey::new(ident)) {
2170-
err.subdiagnostic(AddedMacroUse);
2169+
if let Some(def_id) = self.macro_names.get(&IdentKey::new(ident)) {
2170+
let macro_span = self.def_span(def_id.to_def_id());
2171+
let mut span: MultiSpan = macro_span.into();
2172+
span.push_span_label(
2173+
macro_span,
2174+
"a macro with the same name is defined here".to_string(),
2175+
);
2176+
if let Some(def_id) = self.local_macro_def_scopes[def_id].opt_def_id() {
2177+
span.push_span_label(
2178+
self.def_span(def_id),
2179+
"consider annotating this `mod` with `#[macro_use]`".to_string(),
2180+
);
2181+
}
2182+
2183+
err.subdiagnostic(AddedMacroUse { span });
21712184
return;
21722185
}
21732186
}
@@ -2439,7 +2452,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24392452

24402453
if kind != AmbiguityKind::GlobVsGlob {
24412454
if let Scope::ModuleNonGlobs(module, _) | Scope::ModuleGlobs(module, _) = scope {
2442-
if module == self.graph_root.to_module() {
2455+
if b.res() == Res::Err {
2456+
help_msgs.push("consider removing this".to_string());
2457+
} else if module == self.graph_root.to_module() {
24432458
help_msgs.push(format!(
24442459
"use `crate::{ident}` to refer to this {thing} unambiguously"
24452460
));

‎compiler/rustc_resolve/src/diagnostics/mod.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,11 @@ pub(crate) struct MaybeMissingMacroRulesName {
770770
}
771771

772772
#[derive(Subdiagnostic)]
773-
#[help("have you added the `#[macro_use]` on the module/import?")]
774-
pub(crate) struct AddedMacroUse;
773+
#[help("you might be missing a `#[macro_use]` attribute on a `mod` module or `use` import")]
774+
pub(crate) struct AddedMacroUse {
775+
#[primary_span]
776+
pub(crate) span: MultiSpan,
777+
}
775778

776779
#[derive(Subdiagnostic)]
777780
#[suggestion("consider adding a derive", code = "{suggestion}", applicability = "maybe-incorrect")]
@@ -1472,6 +1475,14 @@ pub(crate) struct MacroIsPrivate {
14721475
#[diag("unused macro definition: `{$name}`")]
14731476
pub(crate) struct UnusedMacroDefinition {
14741477
pub name: Symbol,
1478+
#[suggestion(
1479+
"consider annotating the enclosing module with `#[macro_use]`",
1480+
code = "#[macro_use]\n",
1481+
applicability = "machine-applicable"
1482+
)]
1483+
pub unannotated_mod: Option<Span>,
1484+
#[label("the macro is exported by its enclosing module")]
1485+
pub annotated_mod: Option<Span>,
14751486
}
14761487

14771488
#[derive(Diagnostic)]

‎compiler/rustc_resolve/src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ pub struct Resolver<'ra, 'tcx> {
14451445
builtin_type_decls: FxHashMap<Symbol, Decl<'ra>>,
14461446
builtin_attr_decls: FxHashMap<Symbol, Decl<'ra>>,
14471447
registered_attr_tool_decls: FxHashMap<IdentKey, Decl<'ra>>,
1448-
macro_names: FxHashSet<IdentKey> = default::fx_hash_set(),
1448+
macro_names: FxHashMap<IdentKey, LocalDefId> = default::fx_hash_map(),
14491449
builtin_macros: FxHashMap<Symbol, SyntaxExtensionKind> = default::fx_hash_map(),
14501450
registered_attr_tools: &'tcx RegisteredTools,
14511451
registered_lint_tools: &'tcx RegisteredTools,
@@ -1459,7 +1459,8 @@ pub struct Resolver<'ra, 'tcx> {
14591459
non_macro_attr: &'ra Arc<SyntaxExtension>,
14601460
local_macro_def_scopes: FxHashMap<LocalDefId, LocalModule<'ra>> = default::fx_hash_map(),
14611461
ast_transform_scopes: FxHashMap<LocalExpnId, LocalModule<'ra>> = default::fx_hash_map(),
1462-
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident)> = default::fx_index_map(),
1462+
unused_macros: FxIndexMap<LocalDefId, (NodeId, Ident, Option<DefId>)> = default::fx_index_map(),
1463+
macro_use_mods: FxIndexSet<NodeId> = default::fx_index_set(),
14631464
/// A map from the macro to all its potentially unused arms and the `LocalDefId` of the macro itself.
14641465
unused_macro_rules: FxIndexMap<NodeId, (LocalDefId, DenseBitSet<usize>)> = default::fx_index_map(),
14651466
proc_macro_stubs: FxHashSet<LocalDefId> = default::fx_hash_set(),

‎compiler/rustc_resolve/src/macros.rs‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,30 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
366366
}
367367

368368
fn check_unused_macros(&mut self) {
369-
for (_, &(node_id, ident)) in self.unused_macros.iter() {
369+
for (_, &(node_id, ident, parent_def_id)) in self.unused_macros.iter() {
370+
let mut unannotated_mod = None;
371+
let mut annotated_mod = None;
372+
if let Some(def_id) = parent_def_id {
373+
let span = self.def_span(def_id);
374+
let node_id = self.def_id_to_node_id(def_id.expect_local());
375+
if ast::CRATE_NODE_ID != node_id {
376+
if self.macro_use_mods.contains(&node_id) {
377+
annotated_mod = Some(span);
378+
} else {
379+
unannotated_mod = Some(span.shrink_to_lo());
380+
}
381+
}
382+
}
383+
370384
self.lint_buffer.buffer_lint(
371385
UNUSED_MACROS,
372386
node_id,
373387
ident.span,
374-
diagnostics::UnusedMacroDefinition { name: ident.name },
388+
diagnostics::UnusedMacroDefinition {
389+
name: ident.name,
390+
unannotated_mod,
391+
annotated_mod,
392+
},
375393
);
376394
// Do not report unused individual rules if the entire macro is unused
377395
self.unused_macro_rules.swap_remove(&node_id);

0 commit comments

Comments
 (0)