Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reduce unnecessary allocations a bit
  • Loading branch information
cyrgani committed Apr 14, 2026
commit e972232f276d8860d8f0abaf54dc3671b0ece46e
43 changes: 20 additions & 23 deletions compiler/rustc_expand/src/mbe/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,50 +288,47 @@ pub(super) fn emit_frag_parse_err(
_ => annotate_err_with_kind(&mut e, kind, site_span),
};

let mut bindings_rules = vec![];
for rule in bindings {
let MacroRule::Func { lhs, .. } = rule else { continue };
for param in lhs {
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
bindings_rules.push(*bind);
}
}

let mut matched_rule_bindings_rules = vec![];
for param in matched_rule_bindings {
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
matched_rule_bindings_rules.push(*bind);
}

let matched_rule_bindings_names: Vec<_> =
matched_rule_bindings_rules.iter().map(|bind| bind.name).collect();
let bindings_name: Vec<_> = bindings_rules.iter().map(|bind| bind.name).collect();
if parser.token.kind == token::Dollar {
parser.bump();
if let token::Ident(name, _) = parser.token.kind {
let mut bindings_names = vec![];
for rule in bindings {
let MacroRule::Func { lhs, .. } = rule else { continue };
for param in lhs {
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
bindings_names.push(bind.name);
}
}

let mut matched_rule_bindings_names = vec![];
for param in matched_rule_bindings {
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
matched_rule_bindings_names.push(bind.name);
}

if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name(
&matched_rule_bindings_names[..],
name,
None,
) {
e.span_suggestion_verbose(
parser.token.span,
"there is a macro metavariable with similar name",
format!("{matched_name}"),
"there is a macro metavariable with a similar name",
matched_name,
Applicability::MaybeIncorrect,
);
} else if bindings_name.contains(&name) {
} else if bindings_names.contains(&name) {
e.span_label(
parser.token.span,
"there is an macro metavariable with this name in another macro matcher",
);
} else if let Some(matched_name) =
rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None)
rustc_span::edit_distance::find_best_match_for_name(&bindings_names[..], name, None)
{
e.span_suggestion_verbose(
parser.token.span,
"there is a macro metavariable with a similar name in another macro matcher",
format!("{matched_name}"),
matched_name,
Applicability::MaybeIncorrect,
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/typo-in-norepeat-expr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
macro_rules! m {
(begin $ard:ident end) => {
[$ard] //~ ERROR: expected expression, found `$`
//~^ HELP: there is a macro metavariable with similar name
//~^ HELP: there is a macro metavariable with a similar name
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/typo-in-norepeat-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
macro_rules! m {
(begin $ard:ident end) => {
[$arg] //~ ERROR: expected expression, found `$`
//~^ HELP: there is a macro metavariable with similar name
//~^ HELP: there is a macro metavariable with a similar name
};
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/macros/typo-in-norepeat-expr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | let _ = m![begin x end];
| --------------- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: there is a macro metavariable with similar name
help: there is a macro metavariable with a similar name
|
LL - [$arg]
LL + [$ard]
Expand Down