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

Ambiguity in the syntax for if and if let #50090

Closed
yeah-boi opened this issue Apr 19, 2018 · 1 comment
Closed

Ambiguity in the syntax for if and if let #50090

yeah-boi opened this issue Apr 19, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST

Comments

@yeah-boi
Copy link

There seems to be an ambiguity in the syntax for both if and if let. This is due to the fact that expressions can contain { ... } and this looks like a branches of the if-statement. For example, the rust compiler fails to parse the following two examples:

fn main() {
    struct S { x: i32, y: i32 }
    
    #[derive(PartialEq)]
    struct T {}
    
    // Example 1:
    if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
    // Example 2:
    if T {} == T {} { println!("Ok"); }

    println!("Hello, world!");
}

@varkor
Copy link
Member

varkor commented Apr 20, 2018

This is similar to the situation in #47360. The diagnostics could be similarly improved. Note that you can still write these conditions, with extra parentheses:

// Example 1:
if let S { x: _x, y: 2 } = (S { x: 1, y: 2 }) { println!("Ok"); }
// Example 2:
if (T {}) == (T {}) { println!("Ok"); }

@Mark-Simulacrum Mark-Simulacrum added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST labels May 29, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 8, 2018
…cts, r=nikomatsakis

Suggest parentheses when a struct literal needs them

When writing a struct literal in an expression that expects a block to
be started afterwards (like an `if` statement), do not suggest using the
same struct literal:

```
did you mean `S { /* fields * /}`?
```

Instead, suggest surrounding the expression with parentheses:

```
did you mean `(S { /* fields * /})`?
```

Fix rust-lang#47360, rust-lang#50090. Leaving rust-lang#42982 open to come back to this problem with a better solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST
Projects
None yet
Development

No branches or pull requests

4 participants