Skip to content

Commit 4e8f7e4

Browse files
authored
Rollup merge of rust-lang#107425 - clubby789:match-range-missing-space, r=compiler-errors
Check for missing space between fat arrow and range pattern Fixes rust-lang#107420 Ideally we wouldn't emit an error about expecting `=>` etc., but I'm not sure how to recover from this. `@rustbot` label +A-diagnostics
2 parents 5ff6cdc + c568879 commit 4e8f7e4

File tree

6 files changed

+111
-24
lines changed

6 files changed

+111
-24
lines changed

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

+11
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ parse_match_arm_body_without_braces = `match` arm body without braces
199199
} with a body
200200
.suggestion_use_comma_not_semicolon = use a comma to end a `match` arm expression
201201
202+
parse_inclusive_range_extra_equals = unexpected `=` after inclusive range
203+
.suggestion_remove_eq = use `..=` instead
204+
.note = inclusive ranges end with a single equals sign (`..=`)
205+
206+
parse_inclusive_range_match_arrow = unexpected `=>` after open range
207+
.suggestion_add_space = add a space between the pattern and `=>`
208+
209+
parse_inclusive_range_no_end = inclusive range with no end
210+
.suggestion_open_range = use `..` instead
211+
.note = inclusive ranges must be bounded at the end (`..=b` or `a..=b`)
212+
202213
parse_struct_literal_not_allowed_here = struct literals are not allowed here
203214
.suggestion = surround the struct literal with parentheses
204215

compiler/rustc_parse/src/errors.rs

+42
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,48 @@ pub(crate) struct MatchArmBodyWithoutBraces {
649649
pub sub: MatchArmBodyWithoutBracesSugg,
650650
}
651651

652+
#[derive(Diagnostic)]
653+
#[diag(parse_inclusive_range_extra_equals)]
654+
#[note]
655+
pub(crate) struct InclusiveRangeExtraEquals {
656+
#[primary_span]
657+
#[suggestion(
658+
suggestion_remove_eq,
659+
style = "short",
660+
code = "..=",
661+
applicability = "maybe-incorrect"
662+
)]
663+
pub span: Span,
664+
}
665+
666+
#[derive(Diagnostic)]
667+
#[diag(parse_inclusive_range_match_arrow)]
668+
pub(crate) struct InclusiveRangeMatchArrow {
669+
#[primary_span]
670+
pub span: Span,
671+
#[suggestion(
672+
suggestion_add_space,
673+
style = "verbose",
674+
code = " ",
675+
applicability = "machine-applicable"
676+
)]
677+
pub after_pat: Span,
678+
}
679+
680+
#[derive(Diagnostic)]
681+
#[diag(parse_inclusive_range_no_end, code = "E0586")]
682+
#[note]
683+
pub(crate) struct InclusiveRangeNoEnd {
684+
#[primary_span]
685+
#[suggestion(
686+
suggestion_open_range,
687+
code = "..",
688+
applicability = "machine-applicable",
689+
style = "short"
690+
)]
691+
pub span: Span,
692+
}
693+
652694
#[derive(Subdiagnostic)]
653695
pub(crate) enum MatchArmBodyWithoutBracesSugg {
654696
#[multipart_suggestion(suggestion_add_braces, applicability = "machine-applicable")]

compiler/rustc_parse/src/parser/expr.rs

+1-1
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

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::{ForceCollect, Parser, PathStyle, TrailingToken};
2-
use crate::errors::RemoveLet;
2+
use crate::errors::{
3+
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, RemoveLet,
4+
};
35
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
46
use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor};
57
use rustc_ast::ptr::P;
@@ -9,7 +11,7 @@ use rustc_ast::{
911
PatField, PatKind, Path, QSelf, RangeEnd, RangeSyntax,
1012
};
1113
use rustc_ast_pretty::pprust;
12-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
14+
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult};
1315
use rustc_session::errors::ExprParenthesesNeeded;
1416
use rustc_span::source_map::{respan, Span, Spanned};
1517
use rustc_span::symbol::{kw, sym, Ident};
@@ -746,47 +748,52 @@ impl<'a> Parser<'a> {
746748
// Parsing e.g. `X..`.
747749
if let RangeEnd::Included(_) = re.node {
748750
// FIXME(Centril): Consider semantic errors instead in `ast_validation`.
749-
self.inclusive_range_with_incorrect_end(re.span);
751+
self.inclusive_range_with_incorrect_end();
750752
}
751753
None
752754
};
753755
Ok(PatKind::Range(Some(begin), end, re))
754756
}
755757

756-
pub(super) fn inclusive_range_with_incorrect_end(&mut self, span: Span) {
758+
pub(super) fn inclusive_range_with_incorrect_end(&mut self) {
757759
let tok = &self.token;
758-
760+
let span = self.prev_token.span;
759761
// If the user typed "..==" instead of "..=", we want to give them
760762
// a specific error message telling them to use "..=".
763+
// If they typed "..=>", suggest they use ".. =>".
761764
// Otherwise, we assume that they meant to type a half open exclusive
762765
// 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);
766+
let no_space = tok.span.lo() == span.hi();
767+
match tok.kind {
768+
token::Eq if no_space => {
769+
let span_with_eq = span.to(tok.span);
765770

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-
}
771+
// Ensure the user doesn't receive unhelpful unexpected token errors
772+
self.bump();
773+
if self.is_pat_range_end_start(0) {
774+
let _ = self.parse_pat_range_end().map_err(|e| e.cancel());
775+
}
771776

772-
self.error_inclusive_range_with_extra_equals(span_with_eq);
773-
} else {
774-
self.error_inclusive_range_with_no_end(span);
777+
self.error_inclusive_range_with_extra_equals(span_with_eq);
778+
}
779+
token::Gt if no_space => {
780+
self.error_inclusive_range_match_arrow(span);
781+
}
782+
_ => self.error_inclusive_range_with_no_end(span),
775783
}
776784
}
777785

778786
fn error_inclusive_range_with_extra_equals(&self, span: Span) {
779-
self.struct_span_err(span, "unexpected `=` after inclusive range")
780-
.span_suggestion_short(span, "use `..=` instead", "..=", Applicability::MaybeIncorrect)
781-
.note("inclusive ranges end with a single equals sign (`..=`)")
782-
.emit();
787+
self.sess.emit_err(InclusiveRangeExtraEquals { span });
788+
}
789+
790+
fn error_inclusive_range_match_arrow(&self, span: Span) {
791+
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
792+
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
783793
}
784794

785795
fn error_inclusive_range_with_no_end(&self, span: Span) {
786-
struct_span_err!(self.sess.span_diagnostic, span, E0586, "inclusive range with no end")
787-
.span_suggestion_short(span, "use `..` instead", "..", Applicability::MachineApplicable)
788-
.note("inclusive ranges must be bounded at the end (`..=b` or `a..=b`)")
789-
.emit();
796+
self.sess.emit_err(InclusiveRangeNoEnd { span });
790797
}
791798

792799
/// Parse a range-to pattern, `..X` or `..=X` where `X` remains to be parsed.
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+
}
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)