Skip to content

Commit c59f0ea

Browse files
authored
Unrolled build for #137872
Rollup merge of #137872 - estebank:extra-vert, r=compiler-errors Include whitespace in "remove |" suggestion and make it hidden Tweak error rendering of patterns with an extra `|` on either end. Built on #137409. Only last commit is relevant. ? ``@compiler-errors``
2 parents 898aff7 + 2db126d commit c59f0ea

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
@@ -1113,7 +1113,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
11131113
.map(|snippet| {
11141114
debug_assert!(
11151115
!(sp.is_empty() && snippet.is_empty()),
1116-
"Span must not be empty and have no suggestion"
1116+
"Span `{sp:?}` must not be empty and have no suggestion"
11171117
);
11181118
Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
11191119
})

compiler/rustc_parse/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,8 @@ parse_too_many_hashes = too many `#` symbols: raw strings may be delimited by up
862862
863863
parse_too_short_hex_escape = numeric character escape is too short
864864
865-
parse_trailing_vert_not_allowed = a trailing `|` is not allowed in an or-pattern
866-
.suggestion = remove the `{$token}`
865+
parse_trailing_vert_not_allowed = a trailing `{$token}` is not allowed in an or-pattern
866+
parse_trailing_vert_not_allowed_suggestion = remove the `{$token}`
867867
868868
parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
869869
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
@@ -2661,7 +2661,7 @@ pub(crate) enum TopLevelOrPatternNotAllowedSugg {
26612661
parse_sugg_remove_leading_vert_in_pattern,
26622662
code = "",
26632663
applicability = "machine-applicable",
2664-
style = "verbose"
2664+
style = "tool-only"
26652665
)]
26662666
RemoveLeadingVert {
26672667
#[primary_span]
@@ -2694,12 +2694,25 @@ pub(crate) struct UnexpectedVertVertInPattern {
26942694
pub start: Option<Span>,
26952695
}
26962696

2697+
#[derive(Subdiagnostic)]
2698+
#[suggestion(
2699+
parse_trailing_vert_not_allowed,
2700+
code = "",
2701+
applicability = "machine-applicable",
2702+
style = "tool-only"
2703+
)]
2704+
pub(crate) struct TrailingVertSuggestion {
2705+
#[primary_span]
2706+
pub span: Span,
2707+
}
2708+
26972709
#[derive(Diagnostic)]
26982710
#[diag(parse_trailing_vert_not_allowed)]
26992711
pub(crate) struct TrailingVertNotAllowed {
27002712
#[primary_span]
2701-
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
27022713
pub span: Span,
2714+
#[subdiagnostic]
2715+
pub suggestion: TrailingVertSuggestion,
27032716
#[label(parse_label_while_parsing_or_pattern_here)]
27042717
pub start: Option<Span>,
27052718
pub token: Token,

compiler/rustc_parse/src/parser/pat.rs

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

268268
if let PatKind::Or(pats) = &pat.kind {
269269
let span = pat.span;
270-
let sub = if pats.len() == 1 {
271-
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert {
272-
span: span.with_hi(span.lo() + BytePos(1)),
273-
})
270+
let sub = if let [_] = &pats[..] {
271+
let span = span.with_hi(span.lo() + BytePos(1));
272+
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span })
274273
} else {
275274
Some(TopLevelOrPatternNotAllowedSugg::WrapInParens {
276275
span,
@@ -362,6 +361,9 @@ impl<'a> Parser<'a> {
362361
self.dcx().emit_err(TrailingVertNotAllowed {
363362
span: self.token.span,
364363
start: lo,
364+
suggestion: TrailingVertSuggestion {
365+
span: self.prev_token.span.shrink_to_hi().with_hi(self.token.span.hi()),
366+
},
365367
token: self.token,
366368
note_double_vert: self.token.kind == token::OrOr,
367369
});
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)