diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 131ff1ae6b3da..3cddb27c00710 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -105,7 +105,10 @@ impl<'a> Parser<'a> { let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription }; - let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof + let kind = if delim == token::Brace + || self.token == token::Semi + || self.token == token::Eof + || self.token == token::CloseDelim(token::Brace) { StmtKind::MacCall(P(MacCallStmt { mac, style, attrs })) } else { diff --git a/src/test/ui/macros/bang-macro-stmt.rs b/src/test/ui/macros/bang-macro-stmt.rs new file mode 100644 index 0000000000000..e8b4f7317dd6c --- /dev/null +++ b/src/test/ui/macros/bang-macro-stmt.rs @@ -0,0 +1,12 @@ +// check-pass + +// Tests that we parse a bang macro +// as a statement when it occurs in the trailing expression position, +// which allows it to expand to a statement + +fn main() { + macro_rules! a { + ($e:expr) => { $e; } + } + a!(true) +} diff --git a/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.rs b/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.rs index d78139365549a..d2b87c55e9157 100644 --- a/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.rs +++ b/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.rs @@ -1,8 +1,9 @@ +// check-pass + macro_rules! make_item { ($a:ident) => { struct $a; - }; //~^ ERROR expected expression - //~| ERROR expected expression + }; } fn a() { diff --git a/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.stderr b/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.stderr deleted file mode 100644 index c8d69640071c6..0000000000000 --- a/src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.stderr +++ /dev/null @@ -1,34 +0,0 @@ -error: expected expression, found keyword `struct` - --> $DIR/issue-34421-mac-expr-bad-stmt-good-add-semi.rs:3:9 - | -LL | struct $a; - | ^^^^^^ expected expression -... -LL | make_item!(A) - | ------------- 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 a macro (in Nightly builds, run with -Z macro-backtrace for more info) -help: add `;` to interpret the expansion as a statement - | -LL | make_item!(A); - | ^ - -error: expected expression, found keyword `struct` - --> $DIR/issue-34421-mac-expr-bad-stmt-good-add-semi.rs:3:9 - | -LL | struct $a; - | ^^^^^^ expected expression -... -LL | make_item!(B) - | ------------- 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 a macro (in Nightly builds, run with -Z macro-backtrace for more info) -help: add `;` to interpret the expansion as a statement - | -LL | make_item!(B); - | ^ - -error: aborting due to 2 previous errors - diff --git a/src/test/ui/macros/macro-in-expression-context-2.rs b/src/test/ui/macros/macro-in-expression-context-2.rs index 9423f0a359c69..c14e0a98e60c4 100644 --- a/src/test/ui/macros/macro-in-expression-context-2.rs +++ b/src/test/ui/macros/macro-in-expression-context-2.rs @@ -1,8 +1,9 @@ +// check-pass + macro_rules! empty { () => () } fn main() { match 42 { _ => { empty!() } -//~^ ERROR macro expansion ends with an incomplete expression }; } diff --git a/src/test/ui/macros/macro-in-expression-context-2.stderr b/src/test/ui/macros/macro-in-expression-context-2.stderr deleted file mode 100644 index 8f9660963937f..0000000000000 --- a/src/test/ui/macros/macro-in-expression-context-2.stderr +++ /dev/null @@ -1,17 +0,0 @@ -error: macro expansion ends with an incomplete expression: expected expression - --> $DIR/macro-in-expression-context-2.rs:5:16 - | -LL | macro_rules! empty { () => () } - | -- in this macro arm -... -LL | _ => { empty!() } - | ^^^^^^^^ expected expression - | - = note: the macro call doesn't expand to an expression, but it can expand to a statement -help: add `;` to interpret the expansion as a statement - | -LL | _ => { empty!(); } - | ^ - -error: aborting due to previous error - diff --git a/src/test/ui/macros/macro-in-expression-context.fixed b/src/test/ui/macros/macro-in-expression-context.fixed deleted file mode 100644 index df36db0f49e72..0000000000000 --- a/src/test/ui/macros/macro-in-expression-context.fixed +++ /dev/null @@ -1,15 +0,0 @@ -// run-rustfix - -macro_rules! foo { - () => { - assert_eq!("A", "A"); - assert_eq!("B", "B"); - } - //~^^ ERROR macro expansion ignores token `assert_eq` and any following - //~| NOTE the usage of `foo!` is likely invalid in expression context -} - -fn main() { - foo!(); - //~^ NOTE caused by the macro expansion here -} diff --git a/src/test/ui/macros/macro-in-expression-context.rs b/src/test/ui/macros/macro-in-expression-context.rs index b3f5e568967e8..d2250eae52196 100644 --- a/src/test/ui/macros/macro-in-expression-context.rs +++ b/src/test/ui/macros/macro-in-expression-context.rs @@ -1,15 +1,12 @@ -// run-rustfix +// check-pass macro_rules! foo { () => { assert_eq!("A", "A"); assert_eq!("B", "B"); } - //~^^ ERROR macro expansion ignores token `assert_eq` and any following - //~| NOTE the usage of `foo!` is likely invalid in expression context } fn main() { foo!() - //~^ NOTE caused by the macro expansion here } diff --git a/src/test/ui/macros/macro-in-expression-context.stderr b/src/test/ui/macros/macro-in-expression-context.stderr deleted file mode 100644 index d27d6fbaef7a6..0000000000000 --- a/src/test/ui/macros/macro-in-expression-context.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error: macro expansion ignores token `assert_eq` and any following - --> $DIR/macro-in-expression-context.rs:6:9 - | -LL | assert_eq!("B", "B"); - | ^^^^^^^^^ -... -LL | foo!() - | ------- help: you might be missing a semicolon here: `;` - | | - | caused by the macro expansion here - | - = note: the usage of `foo!` is likely invalid in expression context - -error: aborting due to previous error - diff --git a/src/test/ui/proc-macro/attr-stmt-expr.rs b/src/test/ui/proc-macro/attr-stmt-expr.rs index 14a392db4e118..751a2fc798698 100644 --- a/src/test/ui/proc-macro/attr-stmt-expr.rs +++ b/src/test/ui/proc-macro/attr-stmt-expr.rs @@ -8,8 +8,6 @@ use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_ex fn print_str(string: &'static str) { // macros are handled a bit differently #[expect_print_expr] - //~^ ERROR attributes on expressions are experimental - //~| HELP add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable println!("{}", string) } diff --git a/src/test/ui/proc-macro/attr-stmt-expr.stderr b/src/test/ui/proc-macro/attr-stmt-expr.stderr index 0d6f247cf8359..b253e474d8214 100644 --- a/src/test/ui/proc-macro/attr-stmt-expr.stderr +++ b/src/test/ui/proc-macro/attr-stmt-expr.stderr @@ -1,14 +1,5 @@ error[E0658]: attributes on expressions are experimental - --> $DIR/attr-stmt-expr.rs:10:5 - | -LL | #[expect_print_expr] - | ^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #15701 for more information - = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable - -error[E0658]: attributes on expressions are experimental - --> $DIR/attr-stmt-expr.rs:23:5 + --> $DIR/attr-stmt-expr.rs:21:5 | LL | #[expect_expr] | ^^^^^^^^^^^^^^ @@ -16,6 +7,6 @@ LL | #[expect_expr] = note: see issue #15701 for more information = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable -error: aborting due to 2 previous errors +error: aborting due to previous error For more information about this error, try `rustc --explain E0658`.