Skip to content

Commit

Permalink
Rollup merge of rust-lang#107493 - clubby789:range-fat-arrow-followup…
Browse files Browse the repository at this point in the history
…, 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`
  • Loading branch information
matthiaskrgr authored Feb 2, 2023
2 parents 6e7cb8f + 4ab75de commit 0b4f828
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_error_messages/locales/en-US/parse.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ parse_inclusive_range_extra_equals = unexpected `=` after inclusive range
.suggestion_remove_eq = use `..=` instead
.note = inclusive ranges end with a single equals sign (`..=`)
parse_inclusive_range_match_arrow = unexpected `=>` after open range
.suggestion_add_space = add a space between the pattern and `=>`
parse_inclusive_range_match_arrow = unexpected `>` after inclusive range
.label = this is parsed as an inclusive range `..=`
.suggestion = add a space between the pattern and `=>`
parse_inclusive_range_no_end = inclusive range with no end
.suggestion_open_range = use `..` instead
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,10 @@ pub(crate) struct InclusiveRangeExtraEquals {
#[diag(parse_inclusive_range_match_arrow)]
pub(crate) struct InclusiveRangeMatchArrow {
#[primary_span]
pub arrow: Span,
#[label]
pub span: Span,
#[suggestion(
suggestion_add_space,
style = "verbose",
code = " ",
applicability = "machine-applicable"
)]
#[suggestion(style = "verbose", code = " ", applicability = "machine-applicable")]
pub after_pat: Span,
}

Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,14 @@ impl<'a> Parser<'a> {
);
err.emit();
this.bump();
} else if matches!(
(&this.prev_token.kind, &this.token.kind),
(token::DotDotEq, token::Gt)
) {
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
// so we supress the error here
err.delay_as_bug();
this.bump();
} else {
return Err(err);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ impl<'a> Parser<'a> {
}
token::Gt if no_space => {
let after_pat = span.with_hi(span.hi() - rustc_span::BytePos(1)).shrink_to_hi();
self.sess.emit_err(InclusiveRangeMatchArrow { span, after_pat });
self.sess.emit_err(InclusiveRangeMatchArrow { span, arrow: tok.span, after_pat });
}
_ => {
self.sess.emit_err(InclusiveRangeNoEnd { span });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ fn main() {
let x = 42;
match x {
0..=73 => {},
74..=> {}, //~ ERROR unexpected `=>` after open range
//~^ ERROR expected one of `=>`, `if`, or `|`, found `>`
74..=> {},
//~^ ERROR unexpected `>` after inclusive range
//~| NOTE this is parsed as an inclusive range `..=`
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
error: unexpected `=>` after open range
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:11
error: unexpected `>` after inclusive range
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
|
LL | 74..=> {},
| ^^^
| ---^
| |
| this is parsed as an inclusive range `..=`
|
help: add a space between the pattern and `=>`
|
LL | 74.. => {},
| +

error: expected one of `=>`, `if`, or `|`, found `>`
--> $DIR/half-open-range-pats-inclusive-match-arrow.rs:5:14
|
LL | 74..=> {},
| ^ expected one of `=>`, `if`, or `|`

error: aborting due to 2 previous errors
error: aborting due to previous error

0 comments on commit 0b4f828

Please sign in to comment.