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

Parenthesized match guards should have a specific parser error (and suggestion) #100825

Closed
CAD97 opened this issue Aug 20, 2022 · 0 comments · Fixed by #117565
Closed

Parenthesized match guards should have a specific parser error (and suggestion) #100825

CAD97 opened this issue Aug 20, 2022 · 0 comments · Fixed by #117565
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CAD97
Copy link
Contributor

CAD97 commented Aug 20, 2022

Given the following code: [playground]

pub fn test(val: i32) {
    match val {
        (0 if true) => {}
        _ => {}
    }
}

The current output is:

error: expected identifier, found keyword `if`
 --> src/lib.rs:3:12
  |
3 |         (0 if true) => {}
  |            ^^ expected identifier, found keyword

error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `if`
 --> src/lib.rs:3:12
  |
3 |         (0 if true) => {}
  |           -^^ expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
  |           |
  |           help: missing `,`

error: expected one of `)`, `,`, `@`, or `|`, found keyword `true`
 --> src/lib.rs:3:15
  |
3 |         (0 if true) => {}
  |              -^^^^ expected one of `)`, `,`, `@`, or `|`
  |              |
  |              help: missing `,`

error[E0308]: mismatched types
 --> src/lib.rs:3:9
  |
2 |     match val {
  |           --- this expression has type `i32`
3 |         (0 if true) => {}
  |         ^^^^^^^^^^^ expected `i32`, found tuple
  |
  = note: expected type `i32`
            found tuple `(_, _, _)`

Ideally the output should look like:

error: match pattern guard not allowed here
 --> src/lib.rs:3:9
  |
3 |         (0 if true) => {}
  |            ^^^^^^^ not supported
  |
  = note: match pattern guards must appear at the top level
help: move the guard to the top level
  |
3 -         (_ if true) => {}
3 +         (_) if true => {}
  |

As-is, results in unused_parens warning. Alternative help that doesn't:

help: remove these parentheses
  |
3 -         (0 if true) => {}
3 +         0 if true => {}
  |

Bonus points:

  • Stash this error while parsing and tweak it later when types are available; e.g. if the scrutinee actually is (_, _, _), perhaps (_, r#if, true) (what this currently seemingly error-recovers as) actually was meant. With a non-tuple scrutinee, though, clearly the parenthesization was meant.

  • Support parsing parenthesized guards in nested patterns and give the nice parser error there. Note that for the guard lifting to be semantics preserving, it must not exit any alt (|) pattern combinations.

    examples
    Some(0 if true) => Some(_) if true;
    (0, 0 if true) => (0, 0) if true;
    (_ if true) | 0 => {} // cannot be written as a single pattern
    0 | _ if true => 0 | _ if true; // is a valid pattern: (0 | _) if true
    Struct { field: 0 if true } => Struct { field: 0 } if true;

@rustbot label +D-confusing +D-verbose

(Is this D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. ? The help is not moving towards correct code.)

@CAD97 CAD97 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 20, 2022
@rustbot rustbot added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Aug 20, 2022
@estebank estebank added A-parser Area: The parsing of Rust source code to an AST D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 22, 2022
estebank added a commit to estebank/rust that referenced this issue Nov 4, 2023
Tweak recovery of `for (pat in expr) {}` for more accurate spans.

When encountering `match` arm `(pat if expr) => {}`, recover and suggest
removing parentheses. Fix rust-lang#100825.

Move parser recovery tests to subdirectory.
estebank added a commit to estebank/rust that referenced this issue Nov 6, 2023
When encountering match arm (pat if expr) => {}, recover and suggest removing parentheses. Fix rust-lang#100825.
estebank added a commit to estebank/rust that referenced this issue Nov 16, 2023
When encountering match arm (pat if expr) => {}, recover and suggest removing parentheses. Fix rust-lang#100825.
estebank added a commit to estebank/rust that referenced this issue Nov 17, 2023
When encountering match arm (pat if expr) => {}, recover and suggest removing parentheses. Fix rust-lang#100825.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 29, 2023
Tweak parsing recovery of enums, for exprs and match arm patterns

Tweak recovery of `for (pat in expr) {}` for more accurate spans.

When encountering `match` arm `(pat if expr) => {}`, recover and suggest removing parentheses. Fix rust-lang#100825.

When encountering malformed enums, try more localized per-variant parse recovery.

Move parser recovery tests to subdirectory.
estebank added a commit to estebank/rust that referenced this issue Nov 29, 2023
When encountering match arm (pat if expr) => {}, recover and suggest removing parentheses. Fix rust-lang#100825.
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 29, 2023
Tweak parsing recovery of enums, for exprs and match arm patterns

Tweak recovery of `for (pat in expr) {}` for more accurate spans.

When encountering `match` arm `(pat if expr) => {}`, recover and suggest removing parentheses. Fix rust-lang#100825.

When encountering malformed enums, try more localized per-variant parse recovery.

Move parser recovery tests to subdirectory.
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 30, 2023
Tweak parsing recovery of enums, for exprs and match arm patterns

Tweak recovery of `for (pat in expr) {}` for more accurate spans.

When encountering `match` arm `(pat if expr) => {}`, recover and suggest removing parentheses. Fix rust-lang#100825.

When encountering malformed enums, try more localized per-variant parse recovery.

Move parser recovery tests to subdirectory.
@bors bors closed this as completed in c473189 Nov 30, 2023
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 D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler 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