Skip to content

Commit fd406a8

Browse files
Give a helpful error for the mistake ..==
1 parent 7256855 commit fd406a8

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

compiler/rustc_parse/src/parser/expr.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ impl<'a> Parser<'a> {
431431
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
432432
let limits =
433433
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
434-
Ok(self.mk_expr(span, self.mk_range(Some(lhs), rhs, limits), AttrVec::new()))
434+
let range = self.mk_range(Some(lhs), rhs, limits);
435+
Ok(self.mk_expr(span, range, AttrVec::new()))
435436
}
436437

437438
fn is_at_start_of_range_notation_rhs(&self) -> bool {
@@ -479,7 +480,8 @@ impl<'a> Parser<'a> {
479480
} else {
480481
(lo, None)
481482
};
482-
Ok(this.mk_expr(span, this.mk_range(None, opt_end, limits), attrs.into()))
483+
let range = this.mk_range(None, opt_end, limits);
484+
Ok(this.mk_expr(span, range, attrs.into()))
483485
})
484486
}
485487

@@ -2517,13 +2519,13 @@ impl<'a> Parser<'a> {
25172519
}
25182520

25192521
fn mk_range(
2520-
&self,
2522+
&mut self,
25212523
start: Option<P<Expr>>,
25222524
end: Option<P<Expr>>,
25232525
limits: RangeLimits,
25242526
) -> ExprKind {
25252527
if end.is_none() && limits == RangeLimits::Closed {
2526-
self.error_inclusive_range_with_no_end(self.prev_token.span);
2528+
self.inclusive_range_with_incorrect_end(self.prev_token.span);
25272529
ExprKind::Err
25282530
} else {
25292531
ExprKind::Range(start, end, limits)

compiler/rustc_parse/src/parser/pat.rs

+36-3
Original file line numberDiff line numberDiff line change
@@ -736,15 +736,48 @@ impl<'a> Parser<'a> {
736736
// Parsing e.g. `X..`.
737737
if let RangeEnd::Included(_) = re.node {
738738
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
739-
// Possibly also do this for `X..=` in *expression* contexts.
740-
self.error_inclusive_range_with_no_end(re.span);
739+
self.inclusive_range_with_incorrect_end(re.span);
741740
}
742741
None
743742
};
744743
Ok(PatKind::Range(Some(begin), end, re))
745744
}
746745

747-
pub(super) fn error_inclusive_range_with_no_end(&self, span: Span) {
746+
pub(super) fn inclusive_range_with_incorrect_end(&mut self, span: Span) {
747+
let tok = &self.token;
748+
749+
// If the user typed "..==" instead of "..=", we want to give them
750+
// a specific error message telling them to use "..=".
751+
// Otherwise, we assume that they meant to type a half open exclusive
752+
// range and give them an error telling them to do that instead.
753+
if matches!(tok.kind, token::Eq) && tok.span.lo() == span.hi() {
754+
let span_with_eq = span.to(tok.span);
755+
756+
// Ensure the user doesn't receive unhelpful unexpected token errors
757+
self.bump();
758+
if self.is_pat_range_end_start(0) {
759+
let _ = self.parse_pat_range_end();
760+
}
761+
762+
self.error_inclusive_range_with_extra_equals(span_with_eq);
763+
} else {
764+
self.error_inclusive_range_with_no_end(span);
765+
}
766+
}
767+
768+
fn error_inclusive_range_with_extra_equals(&self, span: Span) {
769+
self.struct_span_err(span, "unexpected `=` after inclusive range")
770+
.span_suggestion_short(
771+
span,
772+
"use `..=` instead",
773+
"..=".to_string(),
774+
Applicability::MaybeIncorrect,
775+
)
776+
.note("inclusive ranges end with a single equals sign (`..=`)")
777+
.emit();
778+
}
779+
780+
fn error_inclusive_range_with_no_end(&self, span: Span) {
748781
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
749782
.span_suggestion_short(
750783
span,

0 commit comments

Comments
 (0)