Skip to content

Fix the issue of typo of comma in arm parsing #141077

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3311,26 +3311,44 @@ impl<'a> Parser<'a> {
let sm = this.psess.source_map();
if let Ok(expr_lines) = sm.span_to_lines(expr_span)
&& let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span)
&& arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
&& expr_lines.lines.len() == 2
{
// We check whether there's any trailing code in the parse span,
// if there isn't, we very likely have the following:
//
// X | &Y => "y"
// | -- - missing comma
// | |
// | arrow_span
// X | &X => "x"
// | - ^^ self.token.span
// | |
// | parsed until here as `"y" & X`
err.span_suggestion_short(
arm_start_span.shrink_to_hi(),
"missing a comma here to end this `match` arm",
",",
Applicability::MachineApplicable,
);
if arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col {
// We check whether there's any trailing code in the parse span,
// if there isn't, we very likely have the following:
//
// X | &Y => "y"
// | -- - missing comma
// | |
// | arrow_span
// X | &X => "x"
// | - ^^ self.token.span
// | |
// | parsed until here as `"y" & X`
err.span_suggestion_short(
arm_start_span.shrink_to_hi(),
"missing a comma here to end this `match` arm",
",",
Applicability::MachineApplicable,
);
} else if arm_start_lines.lines[0].end_col + rustc_span::CharPos(1)
== expr_lines.lines[0].end_col
{
// similar to the above, but we may typo a `.` or `/` at the end of the line
let comma_span = arm_start_span
.shrink_to_hi()
.with_hi(arm_start_span.hi() + rustc_span::BytePos(1));
if let Ok(res) = sm.span_to_snippet(comma_span)
&& (res == "." || res == "/")
{
err.span_suggestion_short(
comma_span,
"you might have meant to write a `,` to end this `match` arm",
",",
Applicability::MachineApplicable,
);
}
}
}
} else {
err.span_label(
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/parser/match-arm-comma-typo-issue-140991.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@ run-rustfix

pub enum Foo {
X, Y
}

pub fn typo1(foo: Foo) -> Foo {
use Foo::*;
match foo {
X => Y,
Y => X, //~ ERROR expected one of
}
}

pub fn typo2(foo: Foo) -> Foo {
use Foo::*;
match foo {
X => Y,
Y => X, //~ ERROR expected one of
}
}


fn main() { }
24 changes: 24 additions & 0 deletions tests/ui/parser/match-arm-comma-typo-issue-140991.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@ run-rustfix

pub enum Foo {
X, Y
}

pub fn typo1(foo: Foo) -> Foo {
use Foo::*;
match foo {
X => Y.
Y => X, //~ ERROR expected one of
}
}

pub fn typo2(foo: Foo) -> Foo {
use Foo::*;
match foo {
X => Y/
Y => X, //~ ERROR expected one of
}
}


fn main() { }
26 changes: 26 additions & 0 deletions tests/ui/parser/match-arm-comma-typo-issue-140991.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: expected one of `(`, `,`, `.`, `::`, `?`, `}`, or an operator, found `=>`
--> $DIR/match-arm-comma-typo-issue-140991.rs:11:11
|
LL | Y => X,
| ^^ expected one of 7 possible tokens
|
help: you might have meant to write a `,` to end this `match` arm
|
LL - X => Y.
LL + X => Y,
|

error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, `}`, or an operator, found `=>`
--> $DIR/match-arm-comma-typo-issue-140991.rs:19:11
|
LL | Y => X,
| ^^ expected one of 8 possible tokens
|
help: you might have meant to write a `,` to end this `match` arm
|
LL - X => Y/
LL + X => Y,
|

error: aborting due to 2 previous errors

Loading