Skip to content

Commit 6a5caac

Browse files
committed
Parse bang macro as a statement when used in trailing expr position
cc #33953 Currently, the following code produces an error ```rust fn main() { macro_rules! a { ($e:expr) => { $e; } } a!(true) } ``` With this change, it now compiles, since we parse `a!(true)` as a statement.
1 parent 7f5a42b commit 6a5caac

11 files changed

+24
-102
lines changed

compiler/rustc_parse/src/parser/stmt.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ impl<'a> Parser<'a> {
105105

106106
let mac = MacCall { path, args, prior_type_ascription: self.last_type_ascription };
107107

108-
let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof
108+
let kind = if delim == token::Brace
109+
|| self.token == token::Semi
110+
|| self.token == token::Eof
111+
|| self.token == token::CloseDelim(token::Brace)
109112
{
110113
StmtKind::MacCall(P(MacCallStmt { mac, style, attrs }))
111114
} else {

src/test/ui/macros/bang-macro-stmt.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
// Tests that we parse a bang macro
4+
// as a statement when it occurs in the trailing expression position,
5+
// which allows it to expand to a statement
6+
7+
fn main() {
8+
macro_rules! a {
9+
($e:expr) => { $e; }
10+
}
11+
a!(true)
12+
}

src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// check-pass
2+
13
macro_rules! make_item {
24
($a:ident) => {
35
struct $a;
4-
}; //~^ ERROR expected expression
5-
//~| ERROR expected expression
6+
};
67
}
78

89
fn a() {

src/test/ui/macros/issue-34421-mac-expr-bad-stmt-good-add-semi.stderr

-34
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// check-pass
2+
13
macro_rules! empty { () => () }
24

35
fn main() {
46
match 42 {
57
_ => { empty!() }
6-
//~^ ERROR macro expansion ends with an incomplete expression
78
};
89
}

src/test/ui/macros/macro-in-expression-context-2.stderr

-17
This file was deleted.

src/test/ui/macros/macro-in-expression-context.fixed

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// run-rustfix
1+
// check-pass
22

33
macro_rules! foo {
44
() => {
55
assert_eq!("A", "A");
66
assert_eq!("B", "B");
77
}
8-
//~^^ ERROR macro expansion ignores token `assert_eq` and any following
9-
//~| NOTE the usage of `foo!` is likely invalid in expression context
108
}
119

1210
fn main() {
1311
foo!()
14-
//~^ NOTE caused by the macro expansion here
1512
}

src/test/ui/macros/macro-in-expression-context.stderr

-15
This file was deleted.

src/test/ui/proc-macro/attr-stmt-expr.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use attr_stmt_expr::{expect_let, expect_print_stmt, expect_expr, expect_print_ex
88
fn print_str(string: &'static str) {
99
// macros are handled a bit differently
1010
#[expect_print_expr]
11-
//~^ ERROR attributes on expressions are experimental
12-
//~| HELP add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
1311
println!("{}", string)
1412
}
1513

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
error[E0658]: attributes on expressions are experimental
2-
--> $DIR/attr-stmt-expr.rs:10:5
3-
|
4-
LL | #[expect_print_expr]
5-
| ^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
8-
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
9-
10-
error[E0658]: attributes on expressions are experimental
11-
--> $DIR/attr-stmt-expr.rs:23:5
2+
--> $DIR/attr-stmt-expr.rs:21:5
123
|
134
LL | #[expect_expr]
145
| ^^^^^^^^^^^^^^
156
|
167
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information
178
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
189

19-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
2011

2112
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)