Rollup merge of #109251 - MU001999:master, r=Nilstrieb

Suggest surrounding the macro with `{}` to interpret as a statement

Fixes #109237
This commit is contained in:
Matthias Krüger 2023-03-18 00:05:54 +01:00 committed by GitHub
commit 7e5705e5c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 6 deletions

View File

@ -245,12 +245,24 @@ pub(super) fn emit_frag_parse_err(
e.note(
"the macro call doesn't expand to an expression, but it can expand to a statement",
);
e.span_suggestion_verbose(
site_span.shrink_to_hi(),
"add `;` to interpret the expansion as a statement",
";",
Applicability::MaybeIncorrect,
);
if parser.token == token::Semi {
if let Ok(snippet) = parser.sess.source_map().span_to_snippet(site_span) {
e.span_suggestion_verbose(
site_span,
"surround the macro invocation with `{}` to interpret the expansion as a statement",
format!("{{ {}; }}", snippet),
Applicability::MaybeIncorrect,
);
}
} else {
e.span_suggestion_verbose(
site_span.shrink_to_hi(),
"add `;` to interpret the expansion as a statement",
";",
Applicability::MaybeIncorrect,
);
}
}
},
_ => annotate_err_with_kind(&mut e, kind, site_span),

View File

@ -0,0 +1,7 @@
macro_rules! statement {
() => {;}; //~ ERROR expected expression
}
fn main() {
let _ = statement!();
}

View File

@ -0,0 +1,18 @@
error: expected expression, found `;`
--> $DIR/issue-109237.rs:2:12
|
LL | () => {;};
| ^ expected expression
...
LL | let _ = statement!();
| ------------ in this macro invocation
|
= note: the macro call doesn't expand to an expression, but it can expand to a statement
= note: this error originates in the macro `statement` (in Nightly builds, run with -Z macro-backtrace for more info)
help: surround the macro invocation with `{}` to interpret the expansion as a statement
|
LL | let _ = { statement!(); };
| ~~~~~~~~~~~~~~~~~
error: aborting due to previous error