Skip to content

Commit 5f8954b

Browse files
committed
Fix the issue of typo of comma in arm parsing
1 parent c79bbfa commit 5f8954b

File tree

4 files changed

+110
-18
lines changed

4 files changed

+110
-18
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,26 +3311,44 @@ impl<'a> Parser<'a> {
33113311
let sm = this.psess.source_map();
33123312
if let Ok(expr_lines) = sm.span_to_lines(expr_span)
33133313
&& let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span)
3314-
&& arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col
33153314
&& expr_lines.lines.len() == 2
33163315
{
3317-
// We check whether there's any trailing code in the parse span,
3318-
// if there isn't, we very likely have the following:
3319-
//
3320-
// X | &Y => "y"
3321-
// | -- - missing comma
3322-
// | |
3323-
// | arrow_span
3324-
// X | &X => "x"
3325-
// | - ^^ self.token.span
3326-
// | |
3327-
// | parsed until here as `"y" & X`
3328-
err.span_suggestion_short(
3329-
arm_start_span.shrink_to_hi(),
3330-
"missing a comma here to end this `match` arm",
3331-
",",
3332-
Applicability::MachineApplicable,
3333-
);
3316+
if arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col {
3317+
// We check whether there's any trailing code in the parse span,
3318+
// if there isn't, we very likely have the following:
3319+
//
3320+
// X | &Y => "y"
3321+
// | -- - missing comma
3322+
// | |
3323+
// | arrow_span
3324+
// X | &X => "x"
3325+
// | - ^^ self.token.span
3326+
// | |
3327+
// | parsed until here as `"y" & X`
3328+
err.span_suggestion_short(
3329+
arm_start_span.shrink_to_hi(),
3330+
"missing a comma here to end this `match` arm",
3331+
",",
3332+
Applicability::MachineApplicable,
3333+
);
3334+
} else if arm_start_lines.lines[0].end_col + rustc_span::CharPos(1)
3335+
== expr_lines.lines[0].end_col
3336+
{
3337+
// similar to the above, but we may typo a `.` or `/` at the end of the line
3338+
let comma_span = arm_start_span
3339+
.shrink_to_hi()
3340+
.with_hi(arm_start_span.hi() + rustc_span::BytePos(1));
3341+
if let Ok(res) = sm.span_to_snippet(comma_span)
3342+
&& (res == "." || res == "/")
3343+
{
3344+
err.span_suggestion_short(
3345+
comma_span,
3346+
"you might have meant to write a `,` to end this `match` arm",
3347+
",",
3348+
Applicability::MachineApplicable,
3349+
);
3350+
}
3351+
}
33343352
}
33353353
} else {
33363354
err.span_label(
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ run-rustfix
2+
3+
pub enum Foo {
4+
X, Y
5+
}
6+
7+
pub fn typo1(foo: Foo) -> Foo {
8+
use Foo::*;
9+
match foo {
10+
X => Y,
11+
Y => X, //~ ERROR expected one of
12+
}
13+
}
14+
15+
pub fn typo2(foo: Foo) -> Foo {
16+
use Foo::*;
17+
match foo {
18+
X => Y,
19+
Y => X, //~ ERROR expected one of
20+
}
21+
}
22+
23+
24+
fn main() { }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ run-rustfix
2+
3+
pub enum Foo {
4+
X, Y
5+
}
6+
7+
pub fn typo1(foo: Foo) -> Foo {
8+
use Foo::*;
9+
match foo {
10+
X => Y.
11+
Y => X, //~ ERROR expected one of
12+
}
13+
}
14+
15+
pub fn typo2(foo: Foo) -> Foo {
16+
use Foo::*;
17+
match foo {
18+
X => Y/
19+
Y => X, //~ ERROR expected one of
20+
}
21+
}
22+
23+
24+
fn main() { }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected one of `(`, `,`, `.`, `::`, `?`, `}`, or an operator, found `=>`
2+
--> $DIR/match-arm-comma-typo-issue-140991.rs:11:11
3+
|
4+
LL | Y => X,
5+
| ^^ expected one of 7 possible tokens
6+
|
7+
help: you might have meant to write a `,` to end this `match` arm
8+
|
9+
LL - X => Y.
10+
LL + X => Y,
11+
|
12+
13+
error: expected one of `!`, `,`, `.`, `::`, `?`, `{`, `}`, or an operator, found `=>`
14+
--> $DIR/match-arm-comma-typo-issue-140991.rs:19:11
15+
|
16+
LL | Y => X,
17+
| ^^ expected one of 8 possible tokens
18+
|
19+
help: you might have meant to write a `,` to end this `match` arm
20+
|
21+
LL - X => Y/
22+
LL + X => Y,
23+
|
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)