Skip to content

Commit f1cb13e

Browse files
committed
Improve diagnostic for missing space in range pattern
1 parent a322848 commit f1cb13e

File tree

6 files changed

+26
-23
lines changed

6 files changed

+26
-23
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
@@ -667,13 +667,10 @@ pub(crate) struct InclusiveRangeExtraEquals {
667667
#[diag(parse_inclusive_range_match_arrow)]
668668
pub(crate) struct InclusiveRangeMatchArrow {
669669
#[primary_span]
670+
pub arrow: Span,
671+
#[label]
670672
pub span: Span,
671-
#[suggestion(
672-
suggestion_add_space,
673-
style = "verbose",
674-
code = " ",
675-
applicability = "machine-applicable"
676-
)]
673+
#[suggestion(style = "verbose", code = " ", applicability = "machine-applicable")]
677674
pub after_pat: Span,
678675
}
679676

compiler/rustc_parse/src/parser/expr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,14 @@ impl<'a> Parser<'a> {
27072707
);
27082708
err.emit();
27092709
this.bump();
2710+
} else if matches!(
2711+
(&this.prev_token.kind, &this.token.kind),
2712+
(token::DotDotEq, token::Gt)
2713+
) {
2714+
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
2715+
// so we supress the error here
2716+
err.delay_as_bug();
2717+
this.bump();
27102718
} else {
27112719
return Err(err);
27122720
}

compiler/rustc_parse/src/parser/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ impl<'a> Parser<'a> {
777777
self.error_inclusive_range_with_extra_equals(span_with_eq);
778778
}
779779
token::Gt if no_space => {
780-
self.error_inclusive_range_match_arrow(span);
780+
self.error_inclusive_range_match_arrow(span, tok.span);
781781
}
782782
_ => self.error_inclusive_range_with_no_end(span),
783783
}
@@ -787,9 +787,9 @@ impl<'a> Parser<'a> {
787787
self.sess.emit_err(InclusiveRangeExtraEquals { span });
788788
}
789789

790-
fn error_inclusive_range_match_arrow(&self, span: Span) {
790+
fn error_inclusive_range_match_arrow(&self, span: Span, arrow: Span) {
791791
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
792-
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
792+
self.sess.emit_err(InclusiveRangeMatchArrow { span, arrow, after_pat });
793793
}
794794

795795
fn error_inclusive_range_with_no_end(&self, span: 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)