Skip to content

Commit 2024cd2

Browse files
committed
mbe: Report attr rules using a MultiSpan
This avoids emitting a single span that includes both the argument matcher and body matcher.
1 parent 4c3a353 commit 2024cd2

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,6 +3714,7 @@ dependencies = [
37143714
"rustc_attr_data_structures",
37153715
"rustc_attr_parsing",
37163716
"rustc_data_structures",
3717+
"rustc_error_messages",
37173718
"rustc_errors",
37183719
"rustc_feature",
37193720
"rustc_fluent_macro",

compiler/rustc_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1515
rustc_attr_data_structures = { path = "../rustc_attr_data_structures" }
1616
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
1717
rustc_data_structures = { path = "../rustc_data_structures" }
18+
rustc_error_messages = { path = "../rustc_error_messages" }
1819
rustc_errors = { path = "../rustc_errors" }
1920
rustc_feature = { path = "../rustc_feature" }
2021
rustc_fluent_macro = { path = "../rustc_fluent_macro" }

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_ast::{self as ast, DUMMY_NODE_ID, NodeId};
1212
use rustc_ast_pretty::pprust;
1313
use rustc_attr_data_structures::{AttributeKind, find_attr};
1414
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
15+
use rustc_error_messages::MultiSpan;
1516
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
1617
use rustc_feature::Features;
1718
use rustc_hir as hir;
@@ -129,7 +130,13 @@ pub(super) enum MacroRule {
129130
/// A function-style rule, for use with `m!()`
130131
Func { lhs: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
131132
/// An attr rule, for use with `#[m]`
132-
Attr { args: Vec<MatcherLoc>, body: Vec<MatcherLoc>, lhs_span: Span, rhs: mbe::TokenTree },
133+
Attr {
134+
args: Vec<MatcherLoc>,
135+
args_span: Span,
136+
body: Vec<MatcherLoc>,
137+
body_span: Span,
138+
rhs: mbe::TokenTree,
139+
},
133140
}
134141

135142
pub struct MacroRulesMacroExpander {
@@ -141,11 +148,15 @@ pub struct MacroRulesMacroExpander {
141148
}
142149

143150
impl MacroRulesMacroExpander {
144-
pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, Span)> {
151+
pub fn get_unused_rule(&self, rule_i: usize) -> Option<(&Ident, MultiSpan)> {
145152
// If the rhs contains an invocation like `compile_error!`, don't report it as unused.
146-
let (MacroRule::Func { lhs_span, ref rhs, .. } | MacroRule::Attr { lhs_span, ref rhs, .. }) =
147-
self.rules[rule_i];
148-
if has_compile_error_macro(rhs) { None } else { Some((&self.name, lhs_span)) }
153+
let (span, rhs) = match self.rules[rule_i] {
154+
MacroRule::Func { lhs_span, ref rhs, .. } => (MultiSpan::from_span(lhs_span), rhs),
155+
MacroRule::Attr { args_span, body_span, ref rhs, .. } => {
156+
(MultiSpan::from_spans(vec![args_span, body_span]), rhs)
157+
}
158+
};
159+
if has_compile_error_macro(rhs) { None } else { Some((&self.name, span)) }
149160
}
150161
}
151162

@@ -594,12 +605,13 @@ pub fn compile_declarative_macro(
594605
return dummy_syn_ext(guar.unwrap());
595606
};
596607
if let Some(args) = args {
597-
let lhs_span = args.span().to(lhs_span);
608+
let args_span = args.span();
598609
let mbe::TokenTree::Delimited(.., delimited) = args else {
599610
return dummy_syn_ext(guar.unwrap());
600611
};
601612
let args = mbe::macro_parser::compute_locs(&delimited.tts);
602-
rules.push(MacroRule::Attr { args, body: lhs, lhs_span, rhs: rhs_tt });
613+
let body_span = lhs_span;
614+
rules.push(MacroRule::Attr { args, args_span, body: lhs, body_span, rhs: rhs_tt });
603615
} else {
604616
rules.push(MacroRule::Func { lhs, lhs_span, rhs: rhs_tt });
605617
}

tests/ui/macros/macro-rules-attr.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: rule #3 of macro `local_attr` is never used
22
--> $DIR/macro-rules-attr.rs:43:9
33
|
44
LL | attr() {} => {
5-
| ^^^^^
5+
| ^^ ^^
66
|
77
note: the lint level is defined here
88
--> $DIR/macro-rules-attr.rs:4:9

0 commit comments

Comments
 (0)