Skip to content

Commit beb3f5d

Browse files
Rollup merge of rust-lang#107493 - clubby789:range-fat-arrow-followup, r=estebank
Improve diagnostic for missing space in range pattern Improves the diagnostic in rust-lang#107425 by turning it into a note explaining the parsing issue. r? `@compiler-errors`
2 parents 191ee56 + f1cb13e commit beb3f5d

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
@@ -2716,6 +2716,14 @@ impl<'a> Parser<'a> {
27162716
);
27172717
err.emit();
27182718
this.bump();
2719+
} else if matches!(
2720+
(&this.prev_token.kind, &this.token.kind),
2721+
(token::DotDotEq, token::Gt)
2722+
) {
2723+
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
2724+
// so we supress the error here
2725+
err.delay_as_bug();
2726+
this.bump();
27192727
} else {
27202728
return Err(err);
27212729
}

compiler/rustc_parse/src/parser/pat.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ impl<'a> Parser<'a> {
776776
self.error_inclusive_range_with_extra_equals(span_with_eq);
777777
}
778778
token::Gt if no_space => {
779-
self.error_inclusive_range_match_arrow(span);
779+
self.error_inclusive_range_match_arrow(span, tok.span);
780780
}
781781
_ => self.error_inclusive_range_with_no_end(span),
782782
}
@@ -786,9 +786,9 @@ impl<'a> Parser<'a> {
786786
self.sess.emit_err(InclusiveRangeExtraEquals { span });
787787
}
788788

789-
fn error_inclusive_range_match_arrow(&self, span: Span) {
789+
fn error_inclusive_range_match_arrow(&self, span: Span, arrow: Span) {
790790
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
791-
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
791+
self.sess.emit_err(InclusiveRangeMatchArrow { span, arrow, after_pat });
792792
}
793793

794794
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)