Skip to content

Commit 4ab75de

Browse files
committed
Improve diagnostic for missing space in range pattern
1 parent a9985cf commit 4ab75de

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

compiler/rustc_error_messages/locales/en-US/parse.ftl

+3-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,9 @@ parse_inclusive_range_extra_equals = unexpected `=` after inclusive range
203203
.suggestion_remove_eq = use `..=` instead
204204
.note = inclusive ranges end with a single equals sign (`..=`)
205205
206-
parse_inclusive_range_match_arrow = unexpected `=>` after open range
207-
.suggestion_add_space = add a space between the pattern and `=>`
206+
parse_inclusive_range_match_arrow = unexpected `>` after inclusive range
207+
.label = this is parsed as an inclusive range `..=`
208+
.suggestion = add a space between the pattern and `=>`
208209
209210
parse_inclusive_range_no_end = inclusive range with no end
210211
.suggestion_open_range = use `..` instead

compiler/rustc_parse/src/errors.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,10 @@ pub(crate) struct InclusiveRangeExtraEquals {
668668
#[diag(parse_inclusive_range_match_arrow)]
669669
pub(crate) struct InclusiveRangeMatchArrow {
670670
#[primary_span]
671+
pub arrow: Span,
672+
#[label]
671673
pub span: Span,
672-
#[suggestion(
673-
suggestion_add_space,
674-
style = "verbose",
675-
code = " ",
676-
applicability = "machine-applicable"
677-
)]
674+
#[suggestion(style = "verbose", code = " ", applicability = "machine-applicable")]
678675
pub after_pat: Span,
679676
}
680677

compiler/rustc_parse/src/parser/expr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,14 @@ impl<'a> Parser<'a> {
27172717
);
27182718
err.emit();
27192719
this.bump();
2720+
} else if matches!(
2721+
(&this.prev_token.kind, &this.token.kind),
2722+
(token::DotDotEq, token::Gt)
2723+
) {
2724+
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
2725+
// so we supress the error here
2726+
err.delay_as_bug();
2727+
this.bump();
27202728
} else {
27212729
return Err(err);
27222730
}

compiler/rustc_parse/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl<'a> Parser<'a> {
744744
}
745745
token::Gt if no_space => {
746746
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
747-
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
747+
self.sess.emit_err(InclusiveRangeMatchArrow { span, arrow: tok.span, after_pat });
748748
}
749749
_ => {
750750
self.sess.emit_err(InclusiveRangeNoEnd { span });

tests/ui/half-open-range-patterns/half-open-range-pats-inclusive-match-arrow.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ fn main() {
22
let x = 42;
33
match x {
44
0..=73 => {},
5-
74..=> {}, //~ ERROR unexpected `=>` after open range
6-
//~^ ERROR expected one of `=>`, `if`, or `|`, found `>`
5+
74..=> {},
6+
//~^ ERROR unexpected `>` after inclusive range
7+
//~| NOTE this is parsed as an inclusive range `..=`
78
}
89
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
error: unexpected `=>` after open range
2-
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:11
1+
error: unexpected `>` after inclusive range
2+
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
33
|
44
LL | 74..=> {},
5-
| ^^^
5+
| ---^
6+
| |
7+
| this is parsed as an inclusive range `..=`
68
|
79
help: add a space between the pattern and `=>`
810
|
911
LL | 74.. => {},
1012
| +
1113

12-
error: expected one of `=>`, `if`, or `|`, found `>`
13-
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
14-
|
15-
LL | 74..=> {},
16-
| ^ expected one of `=>`, `if`, or `|`
17-
18-
error: aborting due to 2 previous errors
14+
error: aborting due to previous error
1915

0 commit comments

Comments
 (0)