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

diagnostic could be better whe using "..." instead of ".." when destructuring enum #70388

Closed
matthiaskrgr opened this issue Mar 25, 2020 · 4 comments · Fixed by #70417
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 25, 2020

enum MyEnum {
    A(i32),
    B(u32),
}

fn use_enum(e: MyEnum) {
    if let MyEnum::A(...) = e {
        println!("got enum")
    };
}

The problem here is that we have if let MyEnum::A(...) = e instead of if let MyEnum::A(..) = e

The compiler however points towards ) in the error message instead of the superfluous .:

 error: unexpected token: `)`
 --> src/main.rs:7:25
  |
7 |     if let MyEnum::A(...) = e {
  |                         ^

error: aborting due to previous error

Fun fact: when using four dots MyEnum::A(....), it correctly points at the last dot :)

error: unexpected token: `.`
 --> src/main.rs:7:25
  |
7 |     if let MyEnum::A(....) = e {
  |                         ^

error: aborting due to previous error

This issue has been assigned to @rakshith-ravi via this comment.

@rakshith-ravi
Copy link
Contributor

@matthiaskrgr seems like a simple issue to fix. If I were to take a shot at fixing this, where would I start?

@Centril
Copy link
Contributor

Centril commented Mar 25, 2020

Mentoring instructions:

Add a branch after this one...

} else if self.check(&token::DotDot) && !self.is_pat_range_end_start(1) {
// A rest pattern `..`.
self.bump(); // `..`
PatKind::Rest

...but instead of self.check(&token::DotDot), check that self.token.kind == token::DotDotDot.

This should be interpreted as a ::Rest as in the previous branch, but an error should also be emitted. (Add the code for error emission in a separate method, with an appropriate .span_suggestion(...), where the span of the ... is replaced with ... See fn recover_intersection_pat for an example on emitting an error with a suggestion.)

@Centril Centril added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-papercut Diagnostics: An error or lint that needs small tweaks. A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST labels Mar 25, 2020
@rakshith-ravi
Copy link
Contributor

rakshith-ravi commented Mar 25, 2020

Sure thing! Will get to it right after work. Thanks for the pointers.

@Centril
Copy link
Contributor

Centril commented Mar 25, 2020

@rustbot assign @rakshith-ravi

@rustbot rustbot self-assigned this Mar 25, 2020
Centril added a commit to Centril/rust that referenced this issue Mar 26, 2020
parser: recover on `...` as a pattern, suggesting `..`

Fixes rust-lang#70388

My first PR to rust. So please let me know if I'm doing something wrong.
@bors bors closed this as completed in 37e1860 Mar 26, 2020
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-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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.

4 participants