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

Trailing semicolon silently ignored in expr macro #33953

Open
mahkoh opened this issue May 29, 2016 · 3 comments
Open

Trailing semicolon silently ignored in expr macro #33953

mahkoh opened this issue May 29, 2016 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.

Comments

@mahkoh
Copy link
Contributor

mahkoh commented May 29, 2016

fn main() {
    macro_rules! a {
        ($e:expr) => { $e; }
    }
    a!(true)
}

Error: Expected (), found bool

But:

fn main() {
    macro_rules! a {
        ($e:expr) => { $e; }
    }
    a!(true);  // <-- semicolon
}

does not warn that the ; is ignored.

So either

  • there should be a warning that the ; has no effect in the macro, or
  • the ; inside the macro should turn the expression into ().
@Aatch Aatch added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label May 30, 2016
@rozbb
Copy link

rozbb commented May 30, 2016

I'm not sure there should be a warning for use of a semicolon in an expression expansion. The problem is fixed when you write ($e: expr) => {{ $e; }} which indicates a block expansion.

So the semicolon is silently ignored, but putting something after it throws a helpful error: ($e:expr) => { $e; blahblah } gives the error "macro expansion ignores token blahblah and any following".

@jseyfried

This comment has been minimized.

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
Aaron1011 added a commit to Aaron1011/stdarch that referenced this issue Oct 27, 2020
The x86 code contains several macros that following this pattern:

```rust
macro_rules! expr {
    () => { true; }
}

fn bar(_val: bool) {}

fn main() {
    bar(expr!());
}
```

Here, we have a macro `expr!` that expands to tokens sequence with
a trailing semicolon.

Currently, the trailing semicolon is ignored when the macro is invoked
in expression position, due to rust-lang/rust#33953
If this behavior is changed, then a large number of macro invocations in
`stdarch` will stop compiling.

Regardless of whether nor not this change is made, removing the
semicolon more clearly expresses the intent of the code - these macros
are designed to expand to the result of a function call, not ignore its
results (as the `;` would suggest).
Aaron1011 added a commit to Aaron1011/stdarch that referenced this issue Oct 27, 2020
The x86 code contains several macros that following this pattern:

```rust
macro_rules! expr {
    () => { true; }
}

fn bar(_val: bool) {}

fn main() {
    bar(expr!());
}
```

Here, we have a macro `expr!` that expands to tokens sequence with
a trailing semicolon.

Currently, the trailing semicolon is ignored when the macro is invoked
in expression position, due to rust-lang/rust#33953
If this behavior is changed, then a large number of macro invocations in
`stdarch` will stop compiling.

Regardless of whether nor not this change is made, removing the
semicolon more clearly expresses the intent of the code - these macros
are designed to expand to the result of a function call, not ignore its
results (as the `;` would suggest).
Aaron1011 added a commit to Aaron1011/rustfmt that referenced this issue Oct 27, 2020
We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
Aaron1011 added a commit to Aaron1011/rustfmt that referenced this issue Nov 1, 2020
Currently, rustfmt inserts semicolons for certain trailing expressions
(`return`, `continue`, and `break`). However, this should not be done in
the body of a `macro_rules!` arm, since the macro might be used in
expression position (where a trailing semicolon will be invalid).

Currently, this rewriting has no effect due to rust-lang/rust#33953
If this issue is fixed, then this rewriting will prevent some macros
from being used in expression position.

This commit prevents rustfmt from inserting semicolons for trailing
expressions in `macro_rules!` arms. The user is free to either include
or omit the semicolon - rustfmt will not modify either case.
Amanieu pushed a commit to rust-lang/stdarch that referenced this issue Nov 2, 2020
The x86 code contains several macros that following this pattern:

```rust
macro_rules! expr {
    () => { true; }
}

fn bar(_val: bool) {}

fn main() {
    bar(expr!());
}
```

Here, we have a macro `expr!` that expands to tokens sequence with
a trailing semicolon.

Currently, the trailing semicolon is ignored when the macro is invoked
in expression position, due to rust-lang/rust#33953
If this behavior is changed, then a large number of macro invocations in
`stdarch` will stop compiling.

Regardless of whether nor not this change is made, removing the
semicolon more clearly expresses the intent of the code - these macros
are designed to expand to the result of a function call, not ignore its
results (as the `;` would suggest).
Aaron1011 added a commit to Aaron1011/rustfmt that referenced this issue Nov 2, 2020
Currently, rustfmt inserts semicolons for certain trailing expressions
(`return`, `continue`, and `break`). However, this should not be done in
the body of a `macro_rules!` arm, since the macro might be used in
expression position (where a trailing semicolon will be invalid).

Currently, this rewriting has no effect due to rust-lang/rust#33953
If this issue is fixed, then this rewriting will prevent some macros
from being used in expression position.

This commit prevents rustfmt from inserting semicolons for trailing
expressions in `macro_rules!` arms. The user is free to either include
or omit the semicolon - rustfmt will not modify either case.
Aaron1011 added a commit to Aaron1011/anyhow that referenced this issue Nov 2, 2020
This will allow `bail!` to continue to be used in expression position if
rust-lang/rust#33953 is fixed
Aaron1011 added a commit to Aaron1011/cargo that referenced this issue Nov 2, 2020
bors added a commit to rust-lang/cargo that referenced this issue Nov 2, 2020
Bump `anyhow` dependency to `1.0.34` in `crates-io` crate

This will keep `crates-io` compiling if rust-lang/rust#33953
is fixed. See dtolnay/anyhow#120
Aaron1011 added a commit to Aaron1011/git2-rs that referenced this issue Nov 3, 2020
This will allow this crate to continue to compile if
rust-lang/rust#33953 is fixed
calebcartwright pushed a commit to rust-lang/rustfmt that referenced this issue Nov 3, 2020
We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
Aaron1011 added a commit to Aaron1011/rustfmt that referenced this issue Nov 3, 2020
Currently, rustfmt inserts semicolons for certain trailing expressions
(`return`, `continue`, and `break`). However, this should not be done in
the body of a `macro_rules!` arm, since the macro might be used in
expression position (where a trailing semicolon will be invalid).

Currently, this rewriting has no effect due to rust-lang/rust#33953
If this issue is fixed, then this rewriting will prevent some macros
from being used in expression position.

This commit prevents rustfmt from inserting semicolons for trailing
expressions in `macro_rules!` arms. The user is free to either include
or omit the semicolon - rustfmt will not modify either case.
calebcartwright pushed a commit to calebcartwright/rustfmt that referenced this issue Nov 6, 2020
We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
calebcartwright pushed a commit to rust-lang/rustfmt that referenced this issue Nov 6, 2020
We no longer flatten a block that looks like this:

```rust
match val {
    pat => { macro_call!() }
}
```

Currently, rust ignores trailing semicolons in macro expansion in
expression position (see rust-lang/rust#33953)

If this is changed, flattening a block with a macro call may break the
user's code - the trailing semicolon will no longer parse if the macro
call occurs immediately on the right-hand side of the match arm
(e.g. `pat => macro_call!()`)
calebcartwright pushed a commit to rust-lang/rustfmt that referenced this issue Nov 7, 2020
Currently, rustfmt inserts semicolons for certain trailing expressions
(`return`, `continue`, and `break`). However, this should not be done in
the body of a `macro_rules!` arm, since the macro might be used in
expression position (where a trailing semicolon will be invalid).

Currently, this rewriting has no effect due to rust-lang/rust#33953
If this issue is fixed, then this rewriting will prevent some macros
from being used in expression position.

This commit prevents rustfmt from inserting semicolons for trailing
expressions in `macro_rules!` arms. The user is free to either include
or omit the semicolon - rustfmt will not modify either case.
Aaron1011 added a commit to Aaron1011/log that referenced this issue Nov 9, 2020
This will allow these macros to continue to be used in expression
position if rust-lang/rust#33953 is fixed.
Aaron1011 added a commit to Aaron1011/rust that referenced this issue Nov 10, 2020
cc rust-lang#33953

Currently, the following code produces an error

```rust
fn main() {
    macro_rules! a {
        ($e:expr) => { $e; }
    }
    a!(true)
}
```
Aaron1011 added a commit to Aaron1011/rust that referenced this issue Nov 12, 2020
cc rust-lang#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.
Aaron1011 added a commit to Aaron1011/rust that referenced this issue Nov 19, 2020
This macro is used in expression position (a match arm), and only
compiles because of rust-lang#33953

Regardless of what happens with that issue, this makes the
usage of the macro less confusing at the call site.
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 19, 2020
Remove semicolon from internal `err` macro

This macro is used in expression position (a match arm), and only
compiles because of rust-lang#33953

Regardless of what happens with that issue, this makes the
usage of the macro less confusing at the call site.
@Aaron1011
Copy link
Member

Progress towards fixing this issue is tracked in #79813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

6 participants