Skip to content

Commit fe3b3a4

Browse files
Revert "rustc_expand: improve diagnostics for non-repeatable metavars"
This reverts commit 93b9973.
1 parent 93b9973 commit fe3b3a4

9 files changed

Lines changed: 6 additions & 239 deletions

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,7 @@ impl<'cx> MacroExpanderResult<'cx> {
277277
// Emit the SEMICOLON_IN_EXPRESSIONS_FROM_MACROS deprecation lint.
278278
let is_local = true;
279279

280-
let parser = ParserAnyMacro::from_tts(
281-
cx,
282-
tts,
283-
site_span,
284-
arm_span,
285-
is_local,
286-
macro_ident,
287-
vec![],
288-
vec![],
289-
);
280+
let parser = ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident);
290281
ExpandResult::Ready(Box::new(parser))
291282
}
292283
}

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,11 @@ impl<'dcx> CollectTrackerAndEmitter<'dcx, '_> {
222222

223223
pub(super) fn emit_frag_parse_err(
224224
mut e: Diag<'_>,
225-
parser: &mut Parser<'_>,
225+
parser: &Parser<'_>,
226226
orig_parser: &mut Parser<'_>,
227227
site_span: Span,
228228
arm_span: Span,
229229
kind: AstFragmentKind,
230-
bindings: Vec<Ident>,
231-
matched_rule_bindings: Vec<Ident>,
232230
) -> ErrorGuaranteed {
233231
// FIXME(davidtwco): avoid depending on the error message text
234232
if parser.token == token::Eof
@@ -287,54 +285,6 @@ pub(super) fn emit_frag_parse_err(
287285
},
288286
_ => annotate_err_with_kind(&mut e, kind, site_span),
289287
};
290-
291-
let matched_rule_bindings_names: Vec<_> =
292-
matched_rule_bindings.iter().map(|bind| bind.name).collect();
293-
let bindings_name: Vec<_> = bindings.iter().map(|bind| bind.name).collect();
294-
if parser.token.kind == token::Dollar {
295-
parser.bump();
296-
if let token::Ident(name, _) = parser.token.kind {
297-
if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name(
298-
&matched_rule_bindings_names[..],
299-
name,
300-
None,
301-
) {
302-
e.span_suggestion_verbose(
303-
parser.token.span,
304-
"there is a macro metavariable with similar name",
305-
format!("{matched_name}"),
306-
Applicability::MaybeIncorrect,
307-
);
308-
} else if bindings_name.contains(&name) {
309-
e.span_label(
310-
parser.token.span,
311-
format!(
312-
"there is an macro metavariable with this name in another macro matcher"
313-
),
314-
);
315-
} else if let Some(matched_name) =
316-
rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None)
317-
{
318-
e.span_suggestion_verbose(
319-
parser.token.span,
320-
"there is a macro metavariable with a similar name in another macro matcher",
321-
format!("{matched_name}"),
322-
Applicability::MaybeIncorrect,
323-
);
324-
} else {
325-
let msg = matched_rule_bindings_names
326-
.iter()
327-
.map(|sym| format!("${}", sym))
328-
.collect::<Vec<_>>()
329-
.join(", ");
330-
331-
e.span_label(parser.token.span, format!("macro metavariable not found"));
332-
if !matched_rule_bindings_names.is_empty() {
333-
e.note(format!("available metavariable names are: {msg}"));
334-
}
335-
}
336-
}
337-
}
338288
e.emit()
339289
}
340290

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ pub(crate) struct ParserAnyMacro<'a> {
5656
arm_span: Span,
5757
/// Whether or not this macro is defined in the current crate
5858
is_local: bool,
59-
bindings: Vec<Ident>,
60-
matched_rule_bindings: Vec<Ident>,
6159
}
6260

6361
impl<'a> ParserAnyMacro<'a> {
@@ -70,22 +68,13 @@ impl<'a> ParserAnyMacro<'a> {
7068
arm_span,
7169
is_trailing_mac,
7270
is_local,
73-
bindings,
74-
matched_rule_bindings,
7571
} = *self;
7672
let snapshot = &mut parser.create_snapshot_for_diagnostic();
7773
let fragment = match parse_ast_fragment(parser, kind) {
7874
Ok(f) => f,
7975
Err(err) => {
8076
let guar = diagnostics::emit_frag_parse_err(
81-
err,
82-
parser,
83-
snapshot,
84-
site_span,
85-
arm_span,
86-
kind,
87-
bindings,
88-
matched_rule_bindings,
77+
err, parser, snapshot, site_span, arm_span, kind,
8978
);
9079
return kind.dummy(site_span, guar);
9180
}
@@ -120,9 +109,6 @@ impl<'a> ParserAnyMacro<'a> {
120109
arm_span: Span,
121110
is_local: bool,
122111
macro_ident: Ident,
123-
// bindings and lhs is for diagnostics
124-
bindings: Vec<Ident>,
125-
matched_rule_bindings: Vec<Ident>,
126112
) -> Self {
127113
Self {
128114
parser: Parser::new(&cx.sess.psess, tts, None),
@@ -136,8 +122,6 @@ impl<'a> ParserAnyMacro<'a> {
136122
is_trailing_mac: cx.current_expansion.is_trailing_mac,
137123
arm_span,
138124
is_local,
139-
bindings,
140-
matched_rule_bindings,
141125
}
142126
}
143127
}
@@ -376,7 +360,7 @@ fn expand_macro<'cx>(
376360

377361
match try_success_result {
378362
Ok((rule_index, rule, named_matches)) => {
379-
let MacroRule::Func { lhs, rhs, .. } = rule else {
363+
let MacroRule::Func { rhs, .. } = rule else {
380364
panic!("try_match_macro returned non-func rule");
381365
};
382366
let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
@@ -404,32 +388,8 @@ fn expand_macro<'cx>(
404388
cx.resolver.record_macro_rule_usage(node_id, rule_index);
405389
}
406390

407-
let mut bindings = vec![];
408-
for rule in rules {
409-
let MacroRule::Func { lhs, .. } = rule else { continue };
410-
for param in lhs {
411-
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
412-
bindings.push(*bind);
413-
}
414-
}
415-
416-
let mut matched_rule_bindings = vec![];
417-
for param in lhs {
418-
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
419-
matched_rule_bindings.push(*bind);
420-
}
421-
422391
// Let the context choose how to interpret the result. Weird, but useful for X-macros.
423-
Box::new(ParserAnyMacro::from_tts(
424-
cx,
425-
tts,
426-
sp,
427-
arm_span,
428-
is_local,
429-
name,
430-
bindings,
431-
matched_rule_bindings,
432-
))
392+
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name))
433393
}
434394
Err(CanRetry::No(guar)) => {
435395
debug!("Will not retry matching as an error was emitted already");

tests/ui/macros/issue-6596-1.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ error: expected expression, found `$`
22
--> $DIR/issue-6596-1.rs:3:9
33
|
44
LL | $nonexistent
5-
| ^-----------
6-
| ||
7-
| |macro metavariable not found
8-
| expected expression
5+
| ^^^^^^^^^^^^ expected expression
96
...
107
LL | e!(foo);
118
| ------- in this macro invocation
129
|
13-
= note: available metavariable names are: $inp
1410
= note: this error originates in the macro `e` (in Nightly builds, run with -Z macro-backtrace for more info)
1511

1612
error: aborting due to 1 previous error

tests/ui/macros/typo-in-norepeat-expr-2.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/ui/macros/typo-in-norepeat-expr-2.stderr

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ui/macros/typo-in-norepeat-expr.fixed

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/ui/macros/typo-in-norepeat-expr.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/ui/macros/typo-in-norepeat-expr.stderr

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)