Skip to content

Include whitespace in "remove |" suggestion and make it hidden #137872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
.map(|snippet| {
debug_assert!(
!(sp.is_empty() && snippet.is_empty()),
"Span must not be empty and have no suggestion"
"Span `{sp:?}` must not be empty and have no suggestion"
);
Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
})
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,8 @@ parse_too_many_hashes = too many `#` symbols: raw strings may be delimited by up

parse_too_short_hex_escape = numeric character escape is too short

parse_trailing_vert_not_allowed = a trailing `|` is not allowed in an or-pattern
.suggestion = remove the `{$token}`
parse_trailing_vert_not_allowed = a trailing `{$token}` is not allowed in an or-pattern
parse_trailing_vert_not_allowed_suggestion = remove the `{$token}`

parse_trait_alias_cannot_be_auto = trait aliases cannot be `auto`
parse_trait_alias_cannot_be_const = trait aliases cannot be `const`
Expand Down
17 changes: 15 additions & 2 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2620,7 +2620,7 @@ pub(crate) enum TopLevelOrPatternNotAllowedSugg {
parse_sugg_remove_leading_vert_in_pattern,
code = "",
applicability = "machine-applicable",
style = "verbose"
style = "tool-only"
)]
RemoveLeadingVert {
#[primary_span]
Expand Down Expand Up @@ -2653,12 +2653,25 @@ pub(crate) struct UnexpectedVertVertInPattern {
pub start: Option<Span>,
}

#[derive(Subdiagnostic)]
#[suggestion(
parse_trailing_vert_not_allowed,
code = "",
applicability = "machine-applicable",
style = "tool-only"
)]
pub(crate) struct TrailingVertSuggestion {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(parse_trailing_vert_not_allowed)]
pub(crate) struct TrailingVertNotAllowed {
#[primary_span]
#[suggestion(code = "", applicability = "machine-applicable", style = "verbose")]
pub span: Span,
#[subdiagnostic]
pub suggestion: TrailingVertSuggestion,
#[label(parse_label_while_parsing_or_pattern_here)]
pub start: Option<Span>,
pub token: Token,
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ use crate::errors::{
GenericArgsInPatRequireTurbofishSyntax, InclusiveRangeExtraEquals, InclusiveRangeMatchArrow,
InclusiveRangeNoEnd, InvalidMutInPattern, ParenRangeSuggestion, PatternOnWrongSideOfAt,
RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder, TopLevelOrPatternNotAllowed,
TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, UnexpectedExpressionInPattern,
UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat,
UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam,
UnexpectedVertVertInPattern, WrapInParens,
TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed, TrailingVertSuggestion,
UnexpectedExpressionInPattern, UnexpectedExpressionInPatternSugg, UnexpectedLifetimeInPattern,
UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, WrapInParens,
};
use crate::parser::expr::{DestructuredFloat, could_be_unclosed_char_literal};
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
Expand Down Expand Up @@ -268,10 +268,9 @@ impl<'a> Parser<'a> {

if let PatKind::Or(pats) = &pat.kind {
let span = pat.span;
let sub = if pats.len() == 1 {
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert {
span: span.with_hi(span.lo() + BytePos(1)),
})
let sub = if let [_] = &pats[..] {
let span = span.with_hi(span.lo() + BytePos(1));
Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span })
} else {
Some(TopLevelOrPatternNotAllowedSugg::WrapInParens {
span,
Expand Down Expand Up @@ -363,6 +362,9 @@ impl<'a> Parser<'a> {
self.dcx().emit_err(TrailingVertNotAllowed {
span: self.token.span,
start: lo,
suggestion: TrailingVertSuggestion {
span: self.prev_token.span.shrink_to_hi().with_hi(self.token.span.hi()),
},
token: self.token,
note_double_vert: self.token.kind == token::OrOr,
});
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/or-patterns/issue-64879-trailing-before-guard.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// In this regression test we check that a trailing `|` in an or-pattern just
// before the `if` token of a `match` guard will receive parser recovery with
// an appropriate error message.
//@ run-rustfix
#![allow(dead_code)]

enum E { A, B }

fn main() {
match E::A {
E::A |
E::B //~ ERROR a trailing `|` is not allowed in an or-pattern
if true => {
let _recovery_witness: i32 = 0i32; //~ ERROR mismatched types
}
_ => {}
}
}
5 changes: 4 additions & 1 deletion tests/ui/or-patterns/issue-64879-trailing-before-guard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// In this regression test we check that a trailing `|` in an or-pattern just
// before the `if` token of a `match` guard will receive parser recovery with
// an appropriate error message.
//@ run-rustfix
#![allow(dead_code)]

enum E { A, B }

Expand All @@ -9,7 +11,8 @@ fn main() {
E::A |
E::B | //~ ERROR a trailing `|` is not allowed in an or-pattern
if true => {
let recovery_witness: bool = 0; //~ ERROR mismatched types
let _recovery_witness: i32 = 0u32; //~ ERROR mismatched types
}
_ => {}
}
}
22 changes: 11 additions & 11 deletions tests/ui/or-patterns/issue-64879-trailing-before-guard.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
error: a trailing `|` is not allowed in an or-pattern
--> $DIR/issue-64879-trailing-before-guard.rs:10:14
--> $DIR/issue-64879-trailing-before-guard.rs:12:14
|
LL | E::A |
| ---- while parsing this or-pattern starting here
LL | E::B |
| ^

error[E0308]: mismatched types
--> $DIR/issue-64879-trailing-before-guard.rs:14:42
|
help: remove the `|`
LL | let _recovery_witness: i32 = 0u32;
| --- ^^^^ expected `i32`, found `u32`
| |
| expected due to this
|
LL - E::B |
LL + E::B
help: change the type of the numeric literal from `u32` to `i32`
|

error[E0308]: mismatched types
--> $DIR/issue-64879-trailing-before-guard.rs:12:42
LL - let _recovery_witness: i32 = 0u32;
LL + let _recovery_witness: i32 = 0i32;
|
LL | let recovery_witness: bool = 0;
| ---- ^ expected `bool`, found integer
| |
| expected due to this

error: aborting due to 2 previous errors

Expand Down
26 changes: 13 additions & 13 deletions tests/ui/or-patterns/remove-leading-vert.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ fn leading() {

#[cfg(false)]
fn trailing() {
let ( A ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
let (a ,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
let ( A | B ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
let [ A | B ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
let S { f: B }; //~ ERROR a trailing `|` is not allowed in an or-pattern
let ( A | B ): E; //~ ERROR unexpected token `||` in pattern
let ( A ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
let (a,): (E,); //~ ERROR a trailing `|` is not allowed in an or-pattern
let ( A | B ): E; //~ ERROR a trailing `|` is not allowed in an or-pattern
let [ A | B ]: [E; 1]; //~ ERROR a trailing `|` is not allowed in an or-pattern
let S { f: B }; //~ ERROR a trailing `|` is not allowed in an or-pattern
let ( A | B ): E; //~ ERROR unexpected token `||` in pattern
//~^ ERROR a trailing `|` is not allowed in an or-pattern
match A {
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
A | B => {} //~ ERROR unexpected token `||` in pattern
A => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
A => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
A | B => {} //~ ERROR unexpected token `||` in pattern
//~^ ERROR a trailing `|` is not allowed in an or-pattern
| A | B => {}
| A | B => {}
//~^ ERROR a trailing `|` is not allowed in an or-pattern
}

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

let a : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
let a = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
let a ; //~ ERROR a trailing `|` is not allowed in an or-pattern
let a : u8 = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
let a = 0; //~ ERROR a trailing `|` is not allowed in an or-pattern
let a ; //~ ERROR a trailing `|` is not allowed in an or-pattern
}
2 changes: 1 addition & 1 deletion tests/ui/or-patterns/remove-leading-vert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn trailing() {
//~^ ERROR a trailing `|` is not allowed in an or-pattern
match A {
A | => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
A || => {} //~ ERROR a trailing `|` is not allowed in an or-pattern
A || => {} //~ ERROR a trailing `||` is not allowed in an or-pattern
A || B | => {} //~ ERROR unexpected token `||` in pattern
//~^ ERROR a trailing `|` is not allowed in an or-pattern
| A | B | => {}
Expand Down
Loading
Loading