Skip to content
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

Parse bang macro as a statement when used in trailing expr position #78991

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/macros/bang-macro-stmt.rs
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// check-pass

macro_rules! make_item {
($a:ident) => {
struct $a;
}; //~^ ERROR expected expression
//~| ERROR expected expression
};
}

fn a() {
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion src/test/ui/macros/macro-in-expression-context-2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// check-pass

macro_rules! empty { () => () }

fn main() {
match 42 {
_ => { empty!() }
//~^ ERROR macro expansion ends with an incomplete expression
};
}
17 changes: 0 additions & 17 deletions src/test/ui/macros/macro-in-expression-context-2.stderr

This file was deleted.

15 changes: 0 additions & 15 deletions src/test/ui/macros/macro-in-expression-context.fixed

This file was deleted.

5 changes: 1 addition & 4 deletions src/test/ui/macros/macro-in-expression-context.rs
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 0 additions & 15 deletions src/test/ui/macros/macro-in-expression-context.stderr

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/proc-macro/attr-stmt-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
13 changes: 2 additions & 11 deletions src/test/ui/proc-macro/attr-stmt-expr.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
error[E0658]: attributes on expressions are experimental
--> $DIR/attr-stmt-expr.rs:10:5
|
LL | #[expect_print_expr]
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/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]
| ^^^^^^^^^^^^^^
|
= note: see issue #15701 <https://github.com/rust-lang/rust/issues/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`.