-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
rustc_expand: improve diagnostics for non-repeatable metavars #152679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ pub(crate) struct ParserAnyMacro<'a> { | |
| arm_span: Span, | ||
| /// Whether or not this macro is defined in the current crate | ||
| is_local: bool, | ||
| bindings: Vec<Ident>, | ||
| matched_rule_bindings: Vec<Ident>, | ||
| } | ||
|
|
||
| impl<'a> ParserAnyMacro<'a> { | ||
|
|
@@ -68,13 +70,22 @@ impl<'a> ParserAnyMacro<'a> { | |
| arm_span, | ||
| is_trailing_mac, | ||
| is_local, | ||
| bindings, | ||
| matched_rule_bindings, | ||
| } = *self; | ||
| let snapshot = &mut parser.create_snapshot_for_diagnostic(); | ||
| let fragment = match parse_ast_fragment(parser, kind) { | ||
| Ok(f) => f, | ||
| Err(err) => { | ||
| let guar = diagnostics::emit_frag_parse_err( | ||
| err, parser, snapshot, site_span, arm_span, kind, | ||
| err, | ||
| parser, | ||
| snapshot, | ||
| site_span, | ||
| arm_span, | ||
| kind, | ||
| bindings, | ||
| matched_rule_bindings, | ||
| ); | ||
| return kind.dummy(site_span, guar); | ||
| } | ||
|
|
@@ -109,6 +120,9 @@ impl<'a> ParserAnyMacro<'a> { | |
| arm_span: Span, | ||
| is_local: bool, | ||
| macro_ident: Ident, | ||
| // bindings and lhs is for diagnostics | ||
| bindings: Vec<Ident>, | ||
| matched_rule_bindings: Vec<Ident>, | ||
| ) -> Self { | ||
| Self { | ||
| parser: Parser::new(&cx.sess.psess, tts, None), | ||
|
|
@@ -122,6 +136,8 @@ impl<'a> ParserAnyMacro<'a> { | |
| is_trailing_mac: cx.current_expansion.is_trailing_mac, | ||
| arm_span, | ||
| is_local, | ||
| bindings, | ||
| matched_rule_bindings, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -360,7 +376,7 @@ fn expand_macro<'cx>( | |
|
|
||
| match try_success_result { | ||
| Ok((rule_index, rule, named_matches)) => { | ||
| let MacroRule::Func { rhs, .. } = rule else { | ||
| let MacroRule::Func { lhs, rhs, .. } = rule else { | ||
| panic!("try_match_macro returned non-func rule"); | ||
| }; | ||
| let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else { | ||
|
|
@@ -388,8 +404,32 @@ fn expand_macro<'cx>( | |
| cx.resolver.record_macro_rule_usage(node_id, rule_index); | ||
| } | ||
|
|
||
| let mut bindings = vec![]; | ||
| for rule in rules { | ||
| let MacroRule::Func { lhs, .. } = rule else { continue }; | ||
| for param in lhs { | ||
| let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; | ||
| bindings.push(*bind); | ||
| } | ||
| } | ||
|
|
||
| let mut matched_rule_bindings = vec![]; | ||
| for param in lhs { | ||
| let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue }; | ||
| matched_rule_bindings.push(*bind); | ||
| } | ||
|
Comment on lines
+407
to
+420
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without closely looking at the lifetimes, it might be that we can pass
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I will take a look. Is there any local way to test for the regression though ?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can try
Comment on lines
+407
to
+420
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be moved to where |
||
|
|
||
| // Let the context choose how to interpret the result. Weird, but useful for X-macros. | ||
| Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name)) | ||
| Box::new(ParserAnyMacro::from_tts( | ||
| cx, | ||
| tts, | ||
| sp, | ||
| arm_span, | ||
| is_local, | ||
| name, | ||
| bindings, | ||
| matched_rule_bindings, | ||
| )) | ||
| } | ||
| Err(CanRetry::No(guar)) => { | ||
| debug!("Will not retry matching as an error was emitted already"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| macro_rules! err { | ||
| (begin $follow:ident end $arg:expr) => { | ||
| [$arg] | ||
| }; | ||
| (begin1 $arg1:ident end $agr2:expr) => { | ||
| [$follow] //~ ERROR: expected expression, found `$` | ||
| //~^ NOTE: there is an macro metavariable with this name in another macro matcher | ||
| //~| NOTE: expected expression | ||
| }; | ||
| } | ||
|
|
||
| macro_rules! err1 { | ||
| (begin $follow:ident end $arg:expr) => { | ||
| [$arg] | ||
| }; | ||
| (begin1 $arg1:ident end) => { | ||
| [$follo] //~ ERROR: expected expression, found `$` | ||
| //~| NOTE: expected expression | ||
| //~| HELP: there is a macro metavariable with a similar name in another macro matcher | ||
| }; | ||
| } | ||
|
|
||
| macro_rules! err2 { | ||
| (begin $follow:ident end $arg:expr) => { | ||
| [$arg] | ||
| }; | ||
| (begin1 $arg1:ident end) => { | ||
| [$xyz] //~ ERROR: expected expression, found `$` | ||
| //~^ NOTE: expected expression | ||
| //~| NOTE available metavariable names are: $arg1 | ||
| //~| NOTE: macro metavariable not found | ||
| }; | ||
| } | ||
|
|
||
| fn main () { | ||
| let _ = err![begin1 x end ig]; //~ NOTE: in this expansion of err! | ||
| let _ = err1![begin1 x end]; //~ NOTE: in this expansion of err1! | ||
| //~| NOTE: in this expansion of err1! | ||
|
|
||
| let _ = err2![begin1 x end]; //~ NOTE: in this expansion of err2! | ||
| //~| NOTE in this expansion of err2! | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| error: expected expression, found `$` | ||
| --> $DIR/typo-in-norepeat-expr-2.rs:6:10 | ||
| | | ||
| LL | [$follow] | ||
| | ^------ | ||
| | || | ||
| | |there is an macro metavariable with this name in another macro matcher | ||
| | expected expression | ||
| ... | ||
| LL | let _ = err![begin1 x end ig]; | ||
| | ---------------------- in this macro invocation | ||
| | | ||
| = note: this error originates in the macro `err` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: expected expression, found `$` | ||
| --> $DIR/typo-in-norepeat-expr-2.rs:17:10 | ||
| | | ||
| LL | [$follo] | ||
| | ^^^^^^ expected expression | ||
| ... | ||
| LL | let _ = err1![begin1 x end]; | ||
| | -------------------- in this macro invocation | ||
| | | ||
| = note: this error originates in the macro `err1` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: there is a macro metavariable with a similar name in another macro matcher | ||
| | | ||
| LL | [$follow] | ||
| | + | ||
|
|
||
| error: expected expression, found `$` | ||
| --> $DIR/typo-in-norepeat-expr-2.rs:28:10 | ||
| | | ||
| LL | [$xyz] | ||
| | ^--- | ||
| | || | ||
| | |macro metavariable not found | ||
| | expected expression | ||
| ... | ||
| LL | let _ = err2![begin1 x end]; | ||
| | -------------------- in this macro invocation | ||
| | | ||
| = note: available metavariable names are: $arg1 | ||
| = note: this error originates in the macro `err2` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| //@ run-rustfix | ||
| macro_rules! m { | ||
| (begin $ard:ident end) => { | ||
| [$ard] //~ ERROR: expected expression, found `$` | ||
| //~^ HELP: there is a macro metavariable with similar name | ||
| }; | ||
| } | ||
|
|
||
| fn main() { | ||
| let x = 1; | ||
| let _ = m![begin x end]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| //@ run-rustfix | ||
| macro_rules! m { | ||
| (begin $ard:ident end) => { | ||
| [$arg] //~ ERROR: expected expression, found `$` | ||
| //~^ HELP: there is a macro metavariable with similar name | ||
| }; | ||
| } | ||
|
|
||
| fn main() { | ||
| let x = 1; | ||
| let _ = m![begin x end]; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| error: expected expression, found `$` | ||
| --> $DIR/typo-in-norepeat-expr.rs:4:10 | ||
| | | ||
| LL | [$arg] | ||
| | ^^^^ expected expression | ||
| ... | ||
| 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 | ||
| | | ||
| LL - [$arg] | ||
| LL + [$ard] | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.