Skip to content

Typoing "." instead of "," in a match produces confusing error messages #140991

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

Closed
ssbr opened this issue May 14, 2025 · 4 comments · Fixed by #141077
Closed

Typoing "." instead of "," in a match produces confusing error messages #140991

ssbr opened this issue May 14, 2025 · 4 comments · Fixed by #141077
Assignees
Labels
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.

Comments

@ssbr
Copy link
Contributor

ssbr commented May 14, 2025

Code

pub enum Foo {
    X, Y
}

pub fn invert(foo: Foo) -> Foo {
    use Foo::*;
    match foo {
        X => Y.
        Y => X,
    }
}

Current output

error: expected one of `(`, `,`, `.`, `::`, `?`, `}`, or an operator, found `=>`
  --> src/lib.rs:12:11
   |
12 |         Y => X,
   |           ^^ expected one of 7 possible tokens

warning: unused import: `Foo::*`

Desired output

I wish it would have tried error recovery by substituting "," where it saw a ".", which is a reasonable enough typo, so that it could give some kind of hint that maybe the previous line had been intended to be Y, not Y..

Rationale and extra context

No response

Other cases

Rust Version

I don't have rust installed locally, this was 1.86 in the rust playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1eb7427edcf893496f899d59f78ec1ff

Anything else?

No response

@ssbr ssbr 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 May 14, 2025
@Kivooeo
Copy link
Contributor

Kivooeo commented May 14, 2025

Hm, it's interesting because it have recovery from ";"

11 |         X => Y;
   |           -- ^ this statement is not surrounded by a body
   |           |
   |           while parsing the `match` arm starting here
   |
help: replace `;` with `,` to end a `match` arm expression
   |
11 -         X => Y;
11 +         X => Y,

I will look into it

@chenyukang chenyukang self-assigned this May 16, 2025
@chenyukang
Copy link
Member

Hm, it's interesting because it have recovery from ";"

11 | X => Y;
| -- ^ this statement is not surrounded by a body
| |
| while parsing the match arm starting here
|
help: replace ; with , to end a match arm expression
|
11 - X => Y;
11 + X => Y,
I will look into it

emm, so sorry that I didn't notice you had a comment to take it.
I gave it a shot and came out with a fix.

@Kivooeo
Copy link
Contributor

Kivooeo commented May 16, 2025

@chenyukang

No worries at all — glad you took a shot at it!

Just for context: the tricky part here is that . is a valid token, unlike ;, which makes typo recovery harder. In cases like this, . could also mean field access or method call, so detecting whether it was actually a typo is not straightforward.

Oh, you already fixed it and it seems more easy than I thought, heh

@chenyukang
Copy link
Member

I think we might don't need to recover here, parser sometimes don't have enough context for a perfect recovery. giving proper suggestion will be good for users.

workingjubilee added a commit to workingjubilee/rustc that referenced this issue May 30, 2025
…, r=wesleywiser

Fix the issue of typo of comma in arm parsing

Fixes rust-lang#140991

I also checked is it a '/', since it's near from ',' from keyboard.
@bors bors closed this as completed in 5e139db May 31, 2025
rust-timer added a commit that referenced this issue May 31, 2025
Rollup merge of #141077 - chenyukang:yukang-fix-140991-comma, r=wesleywiser

Fix the issue of typo of comma in arm parsing

Fixes #140991

I also checked is it a '/', since it's near from ',' from keyboard.
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 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