Skip to content

Commit

Permalink
Parse bang macro as a statement when used in trailing expr position
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Aaron1011 committed Nov 12, 2020
1 parent 7f5a42b commit 6a5caac
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 102 deletions.
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`.

0 comments on commit 6a5caac

Please sign in to comment.