Skip to content

Commit 2db126d

Browse files
committed
Include whitespace in "remove |" suggestion and make it hidden
1 parent e5e79f8 commit 2db126d

File tree

11 files changed

+76
-129
lines changed

11 files changed

+76
-129
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
11121112
.map(|snippet| {
11131113
debug_assert!(
11141114
!(sp.is_empty() && snippet.is_empty()),
1115-
"Span must not be empty and have no suggestion"
1115+
"Span `{sp:?}` must not be empty and have no suggestion"
11161116
);
11171117
Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
11181118
})

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,8 @@ parse_too_many_hashes = too many `#` symbols: raw strings may be delimited by up
851851
852852
parse_too_short_hex_escape = numeric character escape is too short
853853
854-
parse_trailing_vert_not_allowed = a trailing `|` is not allowed in an or-pattern
855-
.suggestion = remove the `{$token}`
854+
parse_trailing_vert_not_allowed = a trailing `{$token}` is not allowed in an or-pattern
855+
parse_trailing_vert_not_allowed_suggestion = remove the `{$token}`
856856
857857
parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
858858
parse_trait_alias_cannot_be_const = trait aliases cannot be `const`

compiler/rustc_parse/src/errors.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,7 @@ pub(crate) enum TopLevelOrPatternNotAllowedSugg {
26202620
parse_sugg_remove_leading_vert_in_pattern,
26212621
code = "",
26222622
applicability = "machine-applicable",
2623-
style = "verbose"
2623+
style = "tool-only"
26242624
)]
26252625
RemoveLeadingVert {
26262626
#[primary_span]
@@ -2653,12 +2653,25 @@ pub(crate) struct UnexpectedVertVertInPattern {
26532653
pub start: Option<Span>,
26542654
}
26552655

2656+
#[derive(Subdiagnostic)]
2657+
#[suggestion(
2658+
parse_trailing_vert_not_allowed,
2659+
code = "",
2660+
applicability = "machine-applicable",
2661+
style = "tool-only"
2662+
)]
2663+
pub(crate) struct TrailingVertSuggestion {
2664+
#[primary_span]
2665+
pub span: Span,
2666+
}
2667+
26562668
#[derive(Diagnostic)]
26572669
#[diag(parse_trailing_vert_not_allowed)]
26582670
pub(crate) struct TrailingVertNotAllowed {
26592671
#[primary_span]
2660-
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
26612672
pub span: Span,
2673+
#[subdiagnostic]
2674+
pub suggestion: TrailingVertSuggestion,
26622675
#[label(parse_label_while_parsing_or_pattern_here)]
26632676
pub start: Option<Span>,
26642677
pub token: Token,

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ use crate::errors::{
2525
GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow,
2626
InclusiveRangeNoEnd, InvalidMutInPattern, ParenRangeSuggestion, PatternOnWrongSideOfAt,
2727
RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, TopLevelOrPatternNotAllowed,
28-
TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, UnexpectedExpressionInPattern,
29-
UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat,
30-
UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam,
31-
UnexpectedVertVertInPattern, WrapInParens,
28+
TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, TrailingVertSuggestion,
29+
UnexpectedExpressionInPattern, UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern,
30+
UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
31+
UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens,
3232
};
3333
use crate::parser::expr::{DestructuredFloat, could_be_unclosed_char_literal};
3434
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
@@ -268,10 +268,9 @@ impl<'a> Parser<'a> {
268268

269269
if let PatKind::Or(pats) = &pat.kind {
270270
let span = pat.span;
271-
let sub = if pats.len() == 1 {
272-
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert {
273-
span: span.with_hi(span.lo() + BytePos(1)),
274-
})
271+
let sub = if let [_] = &pats[..] {
272+
let span = span.with_hi(span.lo() + BytePos(1));
273+
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span })
275274
} else {
276275
Some(TopLevelOrPatternNotAllowedSugg::WrapInParens {
277276
span,
@@ -363,6 +362,9 @@ impl<'a> Parser<'a> {
363362
self.dcx().emit_err(TrailingVertNotAllowed {
364363
span: self.token.span,
365364
start: lo,
365+
suggestion: TrailingVertSuggestion {
366+
span: self.prev_token.span.shrink_to_hi().with_hi(self.token.span.hi()),
367+
},
366368
token: self.token,
367369
note_double_vert: self.token.kind == token::OrOr,
368370
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// In this regression test we check that a trailing `|` in an or-pattern just
2+
// before the `if` token of a `match` guard will receive parser recovery with
3+
// an appropriate error message.
4+
//@ run-rustfix
5+
#![allow(dead_code)]
6+
7+
enum E { A, B }
8+
9+
fn main() {
10+
match E::A {
11+
E::A |
12+
E::B //~ ERROR a trailing `|` is not allowed in an or-pattern
13+
if true => {
14+
let _recovery_witness: i32 = 0i32; //~ ERROR mismatched types
15+
}
16+
_ => {}
17+
}
18+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// In this regression test we check that a trailing `|` in an or-pattern just
22
// before the `if` token of a `match` guard will receive parser recovery with
33
// an appropriate error message.
4+
//@ run-rustfix
5+
#![allow(dead_code)]
46

57
enum E { A, B }
68

@@ -9,7 +11,8 @@ fn main() {
911
E::A |
1012
E::B | //~ ERROR a trailing `|` is not allowed in an or-pattern
1113
if true => {
12-
let recovery_witness: bool = 0; //~ ERROR mismatched types
14+
let _recovery_witness: i32 = 0u32; //~ ERROR mismatched types
1315
}
16+
_ => {}
1417
}
1518
}

tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error: a trailing `|` is not allowed in an or-pattern
2-
--> $DIR/issue-64879-trailing-before-guard.rs:10:14
2+
--> $DIR/issue-64879-trailing-before-guard.rs:12:14
33
|
44
LL | E::A |
55
| ---- while parsing this or-pattern starting here
66
LL | E::B |
77
| ^
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/issue-64879-trailing-before-guard.rs:14:42
811
|
9-
help: remove the `|`
12+
LL | let _recovery_witness: i32 = 0u32;
13+
| --- ^^^^ expected `i32`, found `u32`
14+
| |
15+
| expected due to this
1016
|
11-
LL - E::B |
12-
LL + E::B
17+
help: change the type of the numeric literal from `u32` to `i32`
1318
|
14-
15-
error[E0308]: mismatched types
16-
--> $DIR/issue-64879-trailing-before-guard.rs:12:42
19+
LL - let _recovery_witness: i32 = 0u32;
20+
LL + let _recovery_witness: i32 = 0i32;
1721
|
18-
LL | let recovery_witness: bool = 0;
19-
| ---- ^ expected `bool`, found integer
20-
| |
21-
| expected due to this
2222

2323
error: aborting due to 2 previous errors
2424

tests/ui/or-patterns/remove-leading-vert.fixed

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ fn leading() {
2323

2424
#[cfg(false)]
2525
fn trailing() {
26-
let ( A ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
27-
let (a ,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
28-
let ( A | B ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
29-
let [ A | B ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
30-
let S { f: B }; //~ ERROR a trailing `|` is not allowed in an or-pattern
31-
let ( A | B ): E; //~ ERROR unexpected token `||` in pattern
26+
let ( A ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
27+
let (a,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
28+
let ( A | B ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
29+
let [ A | B ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
30+
let S { f: B }; //~ ERROR a trailing `|` is not allowed in an or-pattern
31+
let ( A | B ): E; //~ ERROR unexpected token `||` in pattern
3232
//~^ ERROR a trailing `|` is not allowed in an or-pattern
3333
match A {
34-
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
35-
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
36-
A | B => {} //~ ERROR unexpected token `||` in pattern
34+
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
35+
A => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
36+
A | B => {} //~ ERROR unexpected token `||` in pattern
3737
//~^ ERROR a trailing `|` is not allowed in an or-pattern
38-
| A | B => {}
38+
| A | B => {}
3939
//~^ ERROR a trailing `|` is not allowed in an or-pattern
4040
}
4141

4242
// These test trailing-vert in `let` bindings, but they also test that we don't emit a
4343
// duplicate suggestion that would confuse rustfix.
4444

45-
let a : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
46-
let a = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
47-
let a ; //~ ERROR a trailing `|` is not allowed in an or-pattern
45+
let a : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
46+
let a = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
47+
let a ; //~ ERROR a trailing `|` is not allowed in an or-pattern
4848
}

tests/ui/or-patterns/remove-leading-vert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn trailing() {
3232
//~^ ERROR a trailing `|` is not allowed in an or-pattern
3333
match A {
3434
A | => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
35-
A || => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
35+
A || => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
3636
A || B | => {} //~ ERROR unexpected token `||` in pattern
3737
//~^ ERROR a trailing `|` is not allowed in an or-pattern
3838
| A | B | => {}

0 commit comments

Comments
 (0)