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

Error for incorrect identifier patterns could be improved #65400

Closed
tesuji opened this issue Oct 14, 2019 · 2 comments · Fixed by #65410
Closed

Error for incorrect identifier patterns could be improved #65400

tesuji opened this issue Oct 14, 2019 · 2 comments · Fixed by #65410
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-feature-request Category: A feature request, i.e: not implemented / a PR. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tesuji
Copy link
Contributor

tesuji commented Oct 14, 2019

The compiler could suggest that e identifier must precede the pattern in the following code:

playpen link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=ca2dc7094a56b0478edefe0faefefe0e

fn main() {
    let x = 2;

    match x {
        1 ..= 5 @ e => {}
        _ => {}
    }
}

Current error message:

error: expected one of `=>`, `if`, or `|`, found `@`
 --> src/main.rs:5:17
  |
5 |         1 ..= 5 @ e => {}
  |                 ^ expected one of `=>`, `if`, or `|` here

error: aborting due to previous error

It could be improved as:

error: expected one of `=>`, `if`, or `|`, found `@`
 --> src/main.rs:5:17
  |
5 |         1 ..= 5 @ e => {}
  |                 ^ expected one of `=>`, `if`, or `|` here
                    | help: try `e @ 1 ..= 5`


error: aborting due to previous error
@tesuji tesuji changed the title Errror for mismatch identifier patterns could be improved Errror for incorrect identifier patterns could be improved Oct 14, 2019
@tesuji

This comment has been minimized.

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 14, 2019
@tesuji tesuji changed the title Errror for incorrect identifier patterns could be improved Error for incorrect identifier patterns could be improved Oct 14, 2019
@Centril
Copy link
Contributor

Centril commented Oct 14, 2019

So what happens after 1 ..= 5 is that we've parsed a PatKind::Range and then @ follows which is not a valid token after a parsed $pat. To fix this we can, after having parsed let pat = ...; in parse_pat_with_range_pat, check if @ follows and then emit appropriate suggestions. We will need to use some heuristics to figure out what the user wants based on the structure of pat: PatKind. Some of the suggestions would be MachineApplicable and some not.

cc @estebank

@Centril Centril added A-parser Area: The parsing of Rust source code to an AST D-papercut Diagnostics: An error or lint that needs small tweaks. labels Oct 14, 2019
@Centril Centril self-assigned this Oct 14, 2019
@bors bors closed this as completed in 1b18237 Oct 15, 2019
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 C-feature-request Category: A feature request, i.e: not implemented / a PR. D-papercut Diagnostics: An error or lint that needs small tweaks. 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