Skip to content

Commit 4bfab39

Browse files
committed
Check for missing space between fat arrow and range pattern
1 parent d6f0642 commit 4bfab39

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3168,7 +3168,7 @@ impl<'a> Parser<'a> {
31683168
limits: RangeLimits,
31693169
) -> ExprKind {
31703170
if end.is_none() && limits == RangeLimits::Closed {
3171-
self.inclusive_range_with_incorrect_end(self.prev_token.span);
3171+
self.inclusive_range_with_incorrect_end();
31723172
ExprKind::Err
31733173
} else {
31743174
ExprKind::Range(start, end, limits)

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -746,32 +746,38 @@ impl<'a> Parser<'a> {
746746
// Parsing e.g. `X..`.
747747
if let RangeEnd::Included(_) = re.node {
748748
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
749-
self.inclusive_range_with_incorrect_end(re.span);
749+
self.inclusive_range_with_incorrect_end();
750750
}
751751
None
752752
};
753753
Ok(PatKind::Range(Some(begin), end, re))
754754
}
755755

756-
pub(super) fn inclusive_range_with_incorrect_end(&mut self, span: Span) {
756+
pub(super) fn inclusive_range_with_incorrect_end(&mut self) {
757757
let tok = &self.token;
758-
758+
let span = self.prev_token.span;
759759
// If the user typed "..==" instead of "..=", we want to give them
760760
// a specific error message telling them to use "..=".
761+
// If they typed "..=>", suggest they use ".. =>".
761762
// Otherwise, we assume that they meant to type a half open exclusive
762763
// range and give them an error telling them to do that instead.
763-
if matches!(tok.kind, token::Eq) && tok.span.lo() == span.hi() {
764-
let span_with_eq = span.to(tok.span);
764+
let no_space = tok.span.lo() == span.hi();
765+
match tok.kind {
766+
token::Eq if no_space => {
767+
let span_with_eq = span.to(tok.span);
765768

766-
// Ensure the user doesn't receive unhelpful unexpected token errors
767-
self.bump();
768-
if self.is_pat_range_end_start(0) {
769-
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
770-
}
769+
// Ensure the user doesn't receive unhelpful unexpected token errors
770+
self.bump();
771+
if self.is_pat_range_end_start(0) {
772+
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
773+
}
771774

772-
self.error_inclusive_range_with_extra_equals(span_with_eq);
773-
} else {
774-
self.error_inclusive_range_with_no_end(span);
775+
self.error_inclusive_range_with_extra_equals(span_with_eq);
776+
}
777+
token::Gt if no_space => {
778+
self.error_inclusive_range_match_arrow(span);
779+
}
780+
_ => self.error_inclusive_range_with_no_end(span),
775781
}
776782
}
777783

@@ -782,6 +788,18 @@ impl<'a> Parser<'a> {
782788
.emit();
783789
}
784790

791+
fn error_inclusive_range_match_arrow(&self, span: Span) {
792+
let without_eq = span.with_hi(span.hi() - rustc_span::BytePos(1));
793+
self.struct_span_err(span, "unexpected `=>` after open range")
794+
.span_suggestion_verbose(
795+
without_eq.shrink_to_hi(),
796+
"add a space between the pattern and `=>`",
797+
" ",
798+
Applicability::MachineApplicable,
799+
)
800+
.emit();
801+
}
802+
785803
fn error_inclusive_range_with_no_end(&self, span: Span) {
786804
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
787805
.span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
let x = 42;
3+
match x {
4+
0..=73 => {},
5+
74..=> {}, //~ ERROR unexpected `=>` after open range
6+
//~^ ERROR expected one of `=>`, `if`, or `|`, found `>`
7+
}
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: unexpected `=>` after open range
2+
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:11
3+
|
4+
LL | 74..=> {},
5+
| ^^^
6+
|
7+
help: add a space between the pattern and `=>`
8+
|
9+
LL | 74.. => {},
10+
| +
11+
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
19+

0 commit comments

Comments
 (0)