From 8208f8885513c0401b6275fb2f7179ba5e01ad0a Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Thu, 17 Oct 2024 19:51:18 +0200 Subject: [PATCH] Stop relying on hashmap iteration for unused macro rules arms --- .../rustc_resolve/src/build_reduced_graph.rs | 6 +++- compiler/rustc_resolve/src/lib.rs | 3 +- compiler/rustc_resolve/src/macros.rs | 32 ++++++++++++------- .../unused-macro-rules-compile-error.stderr | 12 +++---- .../unused/unused-macro-rules-decl.stderr | 16 +++++----- .../ui/lint/unused/unused-macro-rules.stderr | 16 +++++----- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 0145ffd3a4b7c..0ca6bb8c07d9d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1193,7 +1193,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { if !ident.as_str().starts_with('_') { self.r.unused_macros.insert(def_id, (node_id, ident)); for (rule_i, rule_span) in &self.r.macro_map[&def_id.to_def_id()].rule_spans { - self.r.unused_macro_rules.insert((def_id, *rule_i), (ident, *rule_span)); + self.r + .unused_macro_rules + .entry(def_id) + .or_default() + .insert(*rule_i, (ident, *rule_span)); } } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 24a0c252e55ad..35d491cfc18e7 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1122,7 +1122,8 @@ pub struct Resolver<'ra, 'tcx> { local_macro_def_scopes: FxHashMap>, ast_transform_scopes: FxHashMap>, unused_macros: FxHashMap, - unused_macro_rules: FxHashMap<(LocalDefId, usize), (Ident, Span)>, + /// A map from the macro to all its potentially unused arms. + unused_macro_rules: FxIndexMap>, proc_macro_stubs: FxHashSet, /// Traces collected during macro resolution and validated when it's complete. single_segment_macro_resolutions: diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index fa2be8216c73f..e43c871866581 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -340,7 +340,9 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { fn record_macro_rule_usage(&mut self, id: NodeId, rule_i: usize) { let did = self.local_def_id(id); - self.unused_macro_rules.remove(&(did, rule_i)); + if let Some(rules) = self.unused_macro_rules.get_mut(&did) { + rules.remove(&rule_i); + } } fn check_unused_macros(&mut self) { @@ -352,18 +354,24 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> { BuiltinLintDiag::UnusedMacroDefinition(ident.name), ); } - for (&(def_id, arm_i), &(ident, rule_span)) in self.unused_macro_rules.iter() { - if self.unused_macros.contains_key(&def_id) { - // We already lint the entire macro as unused - continue; + + for (&def_id, unused_arms) in self.unused_macro_rules.iter() { + let mut unused_arms = unused_arms.iter().collect::>(); + unused_arms.sort_by_key(|&(&arm_i, _)| arm_i); + + for (&arm_i, &(ident, rule_span)) in unused_arms { + if self.unused_macros.contains_key(&def_id) { + // We already lint the entire macro as unused + continue; + } + let node_id = self.def_id_to_node_id[def_id]; + self.lint_buffer.buffer_lint( + UNUSED_MACRO_RULES, + node_id, + rule_span, + BuiltinLintDiag::MacroRuleNeverUsed(arm_i, ident.name), + ); } - let node_id = self.def_id_to_node_id[def_id]; - self.lint_buffer.buffer_lint( - UNUSED_MACRO_RULES, - node_id, - rule_span, - BuiltinLintDiag::MacroRuleNeverUsed(arm_i, ident.name), - ); } } diff --git a/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr b/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr index 936428f6a1ca6..d91af762e6830 100644 --- a/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr +++ b/tests/ui/lint/unused/unused-macro-rules-compile-error.stderr @@ -10,17 +10,17 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: rule #3 of macro `num2` is never used - --> $DIR/unused-macro-rules-compile-error.rs:22:5 - | -LL | (two_) => { compile_error! }; - | ^^^^^^ - error: rule #2 of macro `num2` is never used --> $DIR/unused-macro-rules-compile-error.rs:20:5 | LL | (two) => { fn compile_error() {} }; | ^^^^^ +error: rule #3 of macro `num2` is never used + --> $DIR/unused-macro-rules-compile-error.rs:22:5 + | +LL | (two_) => { compile_error! }; + | ^^^^^^ + error: aborting due to 3 previous errors diff --git a/tests/ui/lint/unused/unused-macro-rules-decl.stderr b/tests/ui/lint/unused/unused-macro-rules-decl.stderr index 10ceb3921f3b5..98f146c5b5b52 100644 --- a/tests/ui/lint/unused/unused-macro-rules-decl.stderr +++ b/tests/ui/lint/unused/unused-macro-rules-decl.stderr @@ -1,8 +1,8 @@ -error: rule #4 of macro `num` is never used - --> $DIR/unused-macro-rules-decl.rs:11:5 +error: rule #2 of macro `num` is never used + --> $DIR/unused-macro-rules-decl.rs:9:5 | -LL | (four) => { 4 }, - | ^^^^^^ +LL | (two) => { 2 }, + | ^^^^^ | note: the lint level is defined here --> $DIR/unused-macro-rules-decl.rs:2:9 @@ -10,11 +10,11 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: rule #2 of macro `num` is never used - --> $DIR/unused-macro-rules-decl.rs:9:5 +error: rule #4 of macro `num` is never used + --> $DIR/unused-macro-rules-decl.rs:11:5 | -LL | (two) => { 2 }, - | ^^^^^ +LL | (four) => { 4 }, + | ^^^^^^ error: rule #3 of macro `num_rec` is never used --> $DIR/unused-macro-rules-decl.rs:31:5 diff --git a/tests/ui/lint/unused/unused-macro-rules.stderr b/tests/ui/lint/unused/unused-macro-rules.stderr index b9258e778053d..e50a8a63cea47 100644 --- a/tests/ui/lint/unused/unused-macro-rules.stderr +++ b/tests/ui/lint/unused/unused-macro-rules.stderr @@ -1,8 +1,8 @@ -error: rule #4 of macro `num` is never used - --> $DIR/unused-macro-rules.rs:10:5 +error: rule #2 of macro `num` is never used + --> $DIR/unused-macro-rules.rs:8:5 | -LL | (four) => { 4 }; - | ^^^^^^ +LL | (two) => { 2 }; + | ^^^^^ | note: the lint level is defined here --> $DIR/unused-macro-rules.rs:1:9 @@ -10,11 +10,11 @@ note: the lint level is defined here LL | #![deny(unused_macro_rules)] | ^^^^^^^^^^^^^^^^^^ -error: rule #2 of macro `num` is never used - --> $DIR/unused-macro-rules.rs:8:5 +error: rule #4 of macro `num` is never used + --> $DIR/unused-macro-rules.rs:10:5 | -LL | (two) => { 2 }; - | ^^^^^ +LL | (four) => { 4 }; + | ^^^^^^ error: rule #3 of macro `num_rec` is never used --> $DIR/unused-macro-rules.rs:30:5