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

Failure parsing | followed by if in tail expressions #93536

Closed
j-browne opened this issue Feb 1, 2022 · 2 comments · Fixed by #94344
Closed

Failure parsing | followed by if in tail expressions #93536

j-browne opened this issue Feb 1, 2022 · 2 comments · Fixed by #94344
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug.

Comments

@j-browne
Copy link
Contributor

j-browne commented Feb 1, 2022

There seems to be a problem with parsing | followed by if, but only for tail expressions. If I bind it using let, the code compiles. Here is an example (playground):

// Fails to compile
/*
fn or_vals(conds: (bool, bool), vals: (u8, u8)) -> u8 {
    if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 }
}
*/

fn or_vals(conds: (bool, bool), vals: (u8, u8)) -> u8 {
    let x = if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 };
    x
}

fn main() {
    assert_eq!(or_vals((false, false), (1, 2)), 0);
    assert_eq!(or_vals((true, false), (1, 2)), 1);
    assert_eq!(or_vals((false, true), (1, 2)), 2);
    assert_eq!(or_vals((true, true), (1, 2)), 3);
    
    println!("tests pass!");
}

The first version fails to compile with this error:

error: expected identifier, found keyword `if`
 --> src/main.rs:5:40
  |
5 |     if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 }
  |                                        ^^ expected identifier, found keyword

error: expected one of `,`, `:`, or `@`, found `conds`
 --> src/main.rs:5:43
  |
5 |     if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 }
  |                                          -^^^^^ expected one of `,`, `:`, or `@`
  |                                          |
  |                                          help: missing `,`

error: expected one of `,`, `:`, or `@`, found `.`
 --> src/main.rs:5:48
  |
5 |     if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 }
  |                                                ^
  |                                                |
  |                                                expected one of `,`, `:`, or `@`
  |                                                help: missing `,`

error: expected one of `,`, `...`, `..=`, `..`, or `:`, found `{`
 --> src/main.rs:5:51
  |
5 |     if conds.0 { vals.0 } else { 0 } | if conds.1 { vals.1 } else { 0 }
  |                                                   ^ expected one of `,`, `...`, `..=`, `..`, or `:`

error: could not compile `playground` due to 4 previous errors

I expect the first version to compile, and I also expect whether an expression is accepted or rejected by the compiler to not depend on if it is part of a let statement or a tail expression.

Additionally, clippy suggests the first version via the clippy::let_and_return lint.

This happens in stable, beta, and nightly.

@j-browne j-browne added the C-bug Category: This is a bug. label Feb 1, 2022
@compiler-errors
Copy link
Member

In the first case, if conds.0 { vals.0 } else { 0 } is considered to be its own statement, so the subsequent | is parsed as the beginning of a closure. That's why we see ^^ expected identifier, found keyword.

The subsequent errors are due to parser recovery taking if to be the first closure argument name, and then expecting a comma (since it's parsing a parameter list for the closure). Eventually the parser just gives up though.

@BoxyUwU BoxyUwU added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 8, 2022
@notriddle
Copy link
Contributor

Is this the same issue as #91999 ?

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 C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants