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

Redundant braces lead to compilation failure: expected expression, found as #59975

Closed
RalfJung opened this issue Apr 14, 2019 · 2 comments · Fixed by #60188
Closed

Redundant braces lead to compilation failure: expected expression, found as #59975

RalfJung opened this issue Apr 14, 2019 · 2 comments · Fixed by #60188
Labels
A-grammar Area: The grammar of Rust T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@RalfJung
Copy link
Member

The following program surprisingly does not compile:

fn main() {
    let x = &mut 2;
    let _val = {{x} as *mut i32};
}

The error is rather confusing, somehow it expects something to be ():

error: expected expression, found keyword `as`
 --> src/main.rs:3:21
  |
3 |     let _val = {{x} as *mut i32};
  |                     ^^ expected expression

error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
1 | fn main() {
  |           - expected `()` because of default return type
2 |     let x = &mut 2;
3 |     let _val = {{x} as *mut i32};
  |                  ^ expected (), found mutable reference
  |
  = note: expected type `()`
             found type `&mut {integer}`

If I remove either of the pair of braces in the last line, it works as expected.

@jonas-schievink jonas-schievink added A-grammar Area: The grammar of Rust T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 14, 2019
@varkor
Copy link
Member

varkor commented Apr 15, 2019

This might be related to #54482.

@RalfJung
Copy link
Member Author

Indeed adding parentheses as suggested there helps:

fn main() {
    let x = &mut 2;
    let _val = {({x}) as *mut i32};
}

Centril added a commit to Centril/rust that referenced this issue May 9, 2019
Identify when a stmt could have been parsed as an expr

There are some expressions that can be parsed as a statement without
a trailing semicolon depending on the context, which can lead to
confusing errors due to the same looking code being accepted in some
places and not others. Identify these cases and suggest enclosing in
parenthesis making the parse non-ambiguous without changing the
accepted grammar.

Fix rust-lang#54186, cc rust-lang#54482, fix rust-lang#59975, fix rust-lang#47287.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-grammar Area: The grammar of Rust T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants