Skip to content

Commit

Permalink
Rollup merge of rust-lang#109251 - MU001999:master, r=Nilstrieb
Browse files Browse the repository at this point in the history
Suggest surrounding the macro with `{}` to interpret as a statement

Fixes rust-lang#109237
  • Loading branch information
matthiaskrgr authored Mar 17, 2023
2 parents b83b925 + 550e308 commit 4debfbc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
24 changes: 18 additions & 6 deletions compiler/rustc_expand/src/mbe/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/macros/issue-109237.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
macro_rules! statement {
() => {;}; //~ ERROR expected expression
}

fn main() {
let _ = statement!();
}
18 changes: 18 additions & 0 deletions tests/ui/macros/issue-109237.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4debfbc

Please sign in to comment.