From 1ec73d70fac58055eb1a2249279fad81b986edc2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 31 Jan 2024 00:57:56 +0100 Subject: Add `Constructor::Never` --- compiler/rustc_pattern_analysis/src/constructor.rs | 15 ++++++++++----- compiler/rustc_pattern_analysis/src/pat.rs | 1 + compiler/rustc_pattern_analysis/src/rustc.rs | 7 ++++--- 3 files changed, 15 insertions(+), 8 deletions(-) (limited to 'compiler/rustc_pattern_analysis/src') diff --git a/compiler/rustc_pattern_analysis/src/constructor.rs b/compiler/rustc_pattern_analysis/src/constructor.rs index 69e294e47a5..66c9e2e1840 100644 --- a/compiler/rustc_pattern_analysis/src/constructor.rs +++ b/compiler/rustc_pattern_analysis/src/constructor.rs @@ -681,15 +681,19 @@ pub enum Constructor { Or, /// Wildcard pattern. Wildcard, + /// Never pattern. Only used in `WitnessPat`. An actual never pattern should be lowered as + /// `Wildcard`. + Never, /// Fake extra constructor for enums that aren't allowed to be matched exhaustively. Also used - /// for those types for which we cannot list constructors explicitly, like `f64` and `str`. + /// for those types for which we cannot list constructors explicitly, like `f64` and `str`. Only + /// used in `WitnessPat`. NonExhaustive, - /// Fake extra constructor for variants that should not be mentioned in diagnostics. - /// We use this for variants behind an unstable gate as well as - /// `#[doc(hidden)]` ones. + /// Fake extra constructor for variants that should not be mentioned in diagnostics. We use this + /// for variants behind an unstable gate as well as `#[doc(hidden)]` ones. Only used in + /// `WitnessPat`. Hidden, /// Fake extra constructor for constructors that are not seen in the matrix, as explained at the - /// top of the file. + /// top of the file. Only used for specialization. Missing, /// Fake extra constructor that indicates and empty field that is private. When we encounter one /// we skip the column entirely so we don't observe its emptiness. Only used for specialization. @@ -711,6 +715,7 @@ impl Clone for Constructor { Constructor::Str(value) => Constructor::Str(value.clone()), Constructor::Opaque(inner) => Constructor::Opaque(inner.clone()), Constructor::Or => Constructor::Or, + Constructor::Never => Constructor::Never, Constructor::Wildcard => Constructor::Wildcard, Constructor::NonExhaustive => Constructor::NonExhaustive, Constructor::Hidden => Constructor::Hidden, diff --git a/compiler/rustc_pattern_analysis/src/pat.rs b/compiler/rustc_pattern_analysis/src/pat.rs index decbfa5c0cf..3395054b7b3 100644 --- a/compiler/rustc_pattern_analysis/src/pat.rs +++ b/compiler/rustc_pattern_analysis/src/pat.rs @@ -189,6 +189,7 @@ impl fmt::Debug for DeconstructedPat { } Ok(()) } + Never => write!(f, "!"), Wildcard | Missing | NonExhaustive | Hidden | PrivateUninhabited => { write!(f, "_ : {:?}", pat.ty()) } diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index 0085f0ab656..e89d67b3575 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -251,7 +251,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> { _ => bug!("bad slice pattern {:?} {:?}", ctor, ty), }, Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) - | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => &[], + | Never | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => &[], Or => { bug!("called `Fields::wildcards` on an `Or` ctor") } @@ -279,7 +279,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> { Ref => 1, Slice(slice) => slice.arity(), Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..) - | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => 0, + | Never | NonExhaustive | Hidden | Missing | PrivateUninhabited | Wildcard => 0, Or => bug!("The `Or` constructor doesn't have a fixed arity"), } } @@ -809,7 +809,8 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> { } } &Str(value) => PatKind::Constant { value }, - Wildcard | NonExhaustive | Hidden | PrivateUninhabited => PatKind::Wild, + Never if self.tcx.features().never_patterns => PatKind::Never, + Never | Wildcard | NonExhaustive | Hidden | PrivateUninhabited => PatKind::Wild, Missing { .. } => bug!( "trying to convert a `Missing` constructor into a `Pat`; this is probably a bug, `Missing` should have been processed in `apply_constructors`" -- cgit 1.4.1-3-g733a5 From 9f2aa5b85a2584568f1ad34f23d029eeafb20d4b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Feb 2024 22:37:11 +0100 Subject: Suggest never pattern instead of `_` for empty types --- compiler/rustc_pattern_analysis/src/constructor.rs | 24 ++++- compiler/rustc_pattern_analysis/src/pat.rs | 12 ++- .../usefulness/empty-types.never_pats.stderr | 106 ++++++++++----------- tests/ui/pattern/usefulness/empty-types.rs | 8 +- tests/ui/rfcs/rfc-0000-never_patterns/check.rs | 4 +- tests/ui/rfcs/rfc-0000-never_patterns/check.stderr | 12 +-- 6 files changed, 97 insertions(+), 69 deletions(-) (limited to 'compiler/rustc_pattern_analysis/src') diff --git a/compiler/rustc_pattern_analysis/src/constructor.rs b/compiler/rustc_pattern_analysis/src/constructor.rs index 66c9e2e1840..b4d32782acf 100644 --- a/compiler/rustc_pattern_analysis/src/constructor.rs +++ b/compiler/rustc_pattern_analysis/src/constructor.rs @@ -1048,10 +1048,32 @@ impl ConstructorSet { // In a `MaybeInvalid` place even an empty pattern may be reachable. We therefore // add a dummy empty constructor here, which will be ignored if the place is // `ValidOnly`. - missing_empty.push(NonExhaustive); + missing_empty.push(Never); } } SplitConstructorSet { present, missing, missing_empty } } + + /// Whether this set only contains empty constructors. + pub(crate) fn all_empty(&self) -> bool { + match self { + ConstructorSet::Bool + | ConstructorSet::Integers { .. } + | ConstructorSet::Ref + | ConstructorSet::Union + | ConstructorSet::Unlistable => false, + ConstructorSet::NoConstructors => true, + ConstructorSet::Struct { empty } => *empty, + ConstructorSet::Variants { variants, non_exhaustive } => { + !*non_exhaustive + && variants + .iter() + .all(|visibility| matches!(visibility, VariantVisibility::Empty)) + } + ConstructorSet::Slice { array_len, subtype_is_empty } => { + *subtype_is_empty && matches!(array_len, Some(1..)) + } + } + } } diff --git a/compiler/rustc_pattern_analysis/src/pat.rs b/compiler/rustc_pattern_analysis/src/pat.rs index 3395054b7b3..780a386fe65 100644 --- a/compiler/rustc_pattern_analysis/src/pat.rs +++ b/compiler/rustc_pattern_analysis/src/pat.rs @@ -292,18 +292,24 @@ impl WitnessPat { pub(crate) fn new(ctor: Constructor, fields: Vec, ty: Cx::Ty) -> Self { Self { ctor, fields, ty } } - pub(crate) fn wildcard(ty: Cx::Ty) -> Self { - Self::new(Wildcard, Vec::new(), ty) + /// Create a wildcard pattern for this type. If the type is empty, we create a `!` pattern. + pub(crate) fn wildcard(cx: &Cx, ty: Cx::Ty) -> Self { + let is_empty = cx.ctors_for_ty(&ty).is_ok_and(|ctors| ctors.all_empty()); + let ctor = if is_empty { Never } else { Wildcard }; + Self::new(ctor, Vec::new(), ty) } /// Construct a pattern that matches everything that starts with this constructor. /// For example, if `ctor` is a `Constructor::Variant` for `Option::Some`, we get the pattern /// `Some(_)`. pub(crate) fn wild_from_ctor(cx: &Cx, ctor: Constructor, ty: Cx::Ty) -> Self { + if matches!(ctor, Wildcard) { + return Self::wildcard(cx, ty); + } let fields = cx .ctor_sub_tys(&ctor, &ty) .filter(|(_, PrivateUninhabitedField(skip))| !skip) - .map(|(ty, _)| Self::wildcard(ty)) + .map(|(ty, _)| Self::wildcard(cx, ty)) .collect(); Self::new(ctor, fields, ty) } diff --git a/tests/ui/pattern/usefulness/empty-types.never_pats.stderr b/tests/ui/pattern/usefulness/empty-types.never_pats.stderr index e429903fc72..70d5b266bda 100644 --- a/tests/ui/pattern/usefulness/empty-types.never_pats.stderr +++ b/tests/ui/pattern/usefulness/empty-types.never_pats.stderr @@ -74,11 +74,11 @@ error: unreachable pattern LL | _ => {} | ^ -error[E0004]: non-exhaustive patterns: `Ok(_)` and `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Ok(_)` and `Err(!)` not covered --> $DIR/empty-types.rs:91:11 | LL | match res_u32_never {} - | ^^^^^^^^^^^^^ patterns `Ok(_)` and `Err(_)` not covered + | ^^^^^^^^^^^^^ patterns `Ok(_)` and `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -92,15 +92,15 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ match res_u32_never { -LL + Ok(_) | Err(_) => todo!(), +LL + Ok(_) | Err(!) => todo!(), LL + } | -error[E0004]: non-exhaustive patterns: `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Err(!)` not covered --> $DIR/empty-types.rs:93:11 | LL | match res_u32_never { - | ^^^^^^^^^^^^^ pattern `Err(_)` not covered + | ^^^^^^^^^^^^^ pattern `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -111,7 +111,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_) => {}, -LL + Err(_) => todo!() +LL + Err(!) => todo!() | error[E0004]: non-exhaustive patterns: `Ok(1_u32..=u32::MAX)` not covered @@ -136,7 +136,7 @@ error[E0005]: refutable pattern in local binding --> $DIR/empty-types.rs:106:9 | LL | let Ok(_x) = res_u32_never; - | ^^^^^^ pattern `Err(_)` not covered + | ^^^^^^ pattern `Err(!)` not covered | = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html @@ -164,7 +164,7 @@ error[E0005]: refutable pattern in local binding --> $DIR/empty-types.rs:112:9 | LL | let Ok(_x) = &res_u32_never; - | ^^^^^^ pattern `&Err(_)` not covered + | ^^^^^^ pattern `&Err(!)` not covered | = note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant = note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html @@ -174,11 +174,11 @@ help: you might want to use `let else` to handle the variant that isn't matched LL | let Ok(_x) = &res_u32_never else { todo!() }; | ++++++++++++++++ -error[E0004]: non-exhaustive patterns: `Ok(_)` and `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Ok(!)` and `Err(!)` not covered --> $DIR/empty-types.rs:116:11 | LL | match result_never {} - | ^^^^^^^^^^^^ patterns `Ok(_)` and `Err(_)` not covered + | ^^^^^^^^^^^^ patterns `Ok(!)` and `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -192,15 +192,15 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ match result_never { -LL + Ok(_) | Err(_) => todo!(), +LL + Ok(!) | Err(!) => todo!(), LL + } | -error[E0004]: non-exhaustive patterns: `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Err(!)` not covered --> $DIR/empty-types.rs:121:11 | LL | match result_never { - | ^^^^^^^^^^^^ pattern `Err(_)` not covered + | ^^^^^^^^^^^^ pattern `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -210,7 +210,7 @@ note: `Result` defined here = note: the matched value is of type `Result` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | -LL | Ok(_) => {}, Err(_) => todo!() +LL | Ok(_) => {}, Err(!) => todo!() | +++++++++++++++++++ error: unreachable pattern @@ -225,11 +225,11 @@ error: unreachable pattern LL | _ if false => {} | ^ -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/empty-types.rs:146:15 | LL | match opt_void { - | ^^^^^^^^ pattern `Some(_)` not covered + | ^^^^^^^^ pattern `Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -240,14 +240,14 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/empty-types.rs:165:15 | LL | match *ref_opt_void { - | ^^^^^^^^^^^^^ pattern `Some(_)` not covered + | ^^^^^^^^^^^^^ pattern `Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -258,7 +258,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | error: unreachable pattern @@ -325,11 +325,11 @@ LL + _ => todo!(), LL ~ } | -error[E0004]: non-exhaustive patterns: `Ok(_)` and `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Ok(!)` and `Err(!)` not covered --> $DIR/empty-types.rs:320:11 | LL | match *x {} - | ^^ patterns `Ok(_)` and `Err(_)` not covered + | ^^ patterns `Ok(!)` and `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -343,7 +343,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ match *x { -LL + Ok(_) | Err(_) => todo!(), +LL + Ok(!) | Err(!) => todo!(), LL ~ } | @@ -375,44 +375,44 @@ LL + _ => todo!(), LL + } | -error[E0004]: non-exhaustive patterns: `&[_, ..]` not covered +error[E0004]: non-exhaustive patterns: `&[!, ..]` not covered --> $DIR/empty-types.rs:329:11 | LL | match slice_never { - | ^^^^^^^^^^^ pattern `&[_, ..]` not covered + | ^^^^^^^^^^^ pattern `&[!, ..]` not covered | = note: the matched value is of type `&[!]` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ [] => {}, -LL + &[_, ..] => todo!() +LL + &[!, ..] => todo!() | -error[E0004]: non-exhaustive patterns: `&[]`, `&[_]` and `&[_, _]` not covered +error[E0004]: non-exhaustive patterns: `&[]`, `&[!]` and `&[!, !]` not covered --> $DIR/empty-types.rs:338:11 | LL | match slice_never { - | ^^^^^^^^^^^ patterns `&[]`, `&[_]` and `&[_, _]` not covered + | ^^^^^^^^^^^ patterns `&[]`, `&[!]` and `&[!, !]` not covered | = note: the matched value is of type `&[!]` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ [_, _, _, ..] => {}, -LL + &[] | &[_] | &[_, _] => todo!() +LL + &[] | &[!] | &[!, !] => todo!() | -error[E0004]: non-exhaustive patterns: `&[]` and `&[_, ..]` not covered +error[E0004]: non-exhaustive patterns: `&[]` and `&[!, ..]` not covered --> $DIR/empty-types.rs:352:11 | LL | match slice_never { - | ^^^^^^^^^^^ patterns `&[]` and `&[_, ..]` not covered + | ^^^^^^^^^^^ patterns `&[]` and `&[!, ..]` not covered | = note: the matched value is of type `&[!]` = note: match arms with guards don't count towards exhaustivity help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ &[..] if false => {}, -LL + &[] | &[_, ..] => todo!() +LL + &[] | &[!, ..] => todo!() | error[E0004]: non-exhaustive patterns: type `[!]` is non-empty @@ -477,11 +477,11 @@ LL ~ [..] if false => {}, LL + [] => todo!() | -error[E0004]: non-exhaustive patterns: `&Some(_)` not covered +error[E0004]: non-exhaustive patterns: `&Some(!)` not covered --> $DIR/empty-types.rs:452:11 | LL | match ref_opt_never { - | ^^^^^^^^^^^^^ pattern `&Some(_)` not covered + | ^^^^^^^^^^^^^ pattern `&Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -492,14 +492,14 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ &None => {}, -LL + &Some(_) => todo!() +LL + &Some(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/empty-types.rs:493:11 | LL | match *ref_opt_never { - | ^^^^^^^^^^^^^^ pattern `Some(_)` not covered + | ^^^^^^^^^^^^^^ pattern `Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -510,14 +510,14 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Err(!)` not covered --> $DIR/empty-types.rs:541:11 | LL | match *ref_res_never { - | ^^^^^^^^^^^^^^ pattern `Err(_)` not covered + | ^^^^^^^^^^^^^^ pattern `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -528,14 +528,14 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_) => {}, -LL + Err(_) => todo!() +LL + Err(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Err(_)` not covered +error[E0004]: non-exhaustive patterns: `Err(!)` not covered --> $DIR/empty-types.rs:552:11 | LL | match *ref_res_never { - | ^^^^^^^^^^^^^^ pattern `Err(_)` not covered + | ^^^^^^^^^^^^^^ pattern `Err(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -546,7 +546,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_a) => {}, -LL + Err(_) => todo!() +LL + Err(!) => todo!() | error[E0004]: non-exhaustive patterns: type `(u32, !)` is non-empty @@ -587,11 +587,11 @@ error: unreachable pattern LL | _x if false => {} | ^^ -error[E0004]: non-exhaustive patterns: `&_` not covered +error[E0004]: non-exhaustive patterns: `&!` not covered --> $DIR/empty-types.rs:638:11 | LL | match ref_never { - | ^^^^^^^^^ pattern `&_` not covered + | ^^^^^^^^^ pattern `&!` not covered | = note: the matched value is of type `&!` = note: references are always considered inhabited @@ -599,14 +599,14 @@ LL | match ref_never { help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ &_a if false => {}, -LL + &_ => todo!() +LL + &! => todo!() | -error[E0004]: non-exhaustive patterns: `Ok(_)` not covered +error[E0004]: non-exhaustive patterns: `Ok(!)` not covered --> $DIR/empty-types.rs:654:11 | LL | match *ref_result_never { - | ^^^^^^^^^^^^^^^^^ pattern `Ok(_)` not covered + | ^^^^^^^^^^^^^^^^^ pattern `Ok(!)` not covered | note: `Result` defined here --> $SRC_DIR/core/src/result.rs:LL:COL @@ -617,14 +617,14 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Err(_) => {}, -LL + Ok(_) => todo!() +LL + Ok(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/empty-types.rs:674:11 | LL | match *x { - | ^^ pattern `Some(_)` not covered + | ^^ pattern `Some(!)` not covered | note: `Option>` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -635,7 +635,7 @@ note: `Option>` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | error: aborting due to 49 previous errors; 1 warning emitted diff --git a/tests/ui/pattern/usefulness/empty-types.rs b/tests/ui/pattern/usefulness/empty-types.rs index 2454955f8a5..cc71f67831d 100644 --- a/tests/ui/pattern/usefulness/empty-types.rs +++ b/tests/ui/pattern/usefulness/empty-types.rs @@ -338,7 +338,7 @@ fn arrays_and_slices(x: NeverBundle) { match slice_never { //[normal,min_exh_pats]~^ ERROR `&[]`, `&[_]` and `&[_, _]` not covered //[exhaustive_patterns]~^^ ERROR `&[]` not covered - //[never_pats]~^^^ ERROR `&[]`, `&[_]` and `&[_, _]` not covered + //[never_pats]~^^^ ERROR `&[]`, `&[!]` and `&[!, !]` not covered [_, _, _, ..] => {} } match slice_never { @@ -352,7 +352,7 @@ fn arrays_and_slices(x: NeverBundle) { match slice_never { //[normal,min_exh_pats]~^ ERROR `&[]` and `&[_, ..]` not covered //[exhaustive_patterns]~^^ ERROR `&[]` not covered - //[never_pats]~^^^ ERROR `&[]` and `&[_, ..]` not covered + //[never_pats]~^^^ ERROR `&[]` and `&[!, ..]` not covered &[..] if false => {} } @@ -653,7 +653,7 @@ fn guards_and_validity(x: NeverBundle) { } match *ref_result_never { //[normal,min_exh_pats]~^ ERROR `Ok(_)` not covered - //[never_pats]~^^ ERROR `Ok(_)` not covered + //[never_pats]~^^ ERROR `Ok(!)` not covered // useful, reachable Ok(_) if false => {} // useful, reachable @@ -673,7 +673,7 @@ fn diagnostics_subtlety(x: NeverBundle) { let x: &Option> = &None; match *x { //[normal,min_exh_pats]~^ ERROR `Some(_)` not covered - //[never_pats]~^^ ERROR `Some(_)` not covered + //[never_pats]~^^ ERROR `Some(!)` not covered None => {} } } diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/check.rs b/tests/ui/rfcs/rfc-0000-never_patterns/check.rs index b6da0c20e07..0831477e749 100644 --- a/tests/ui/rfcs/rfc-0000-never_patterns/check.rs +++ b/tests/ui/rfcs/rfc-0000-never_patterns/check.rs @@ -15,12 +15,12 @@ fn no_arms_or_guards(x: Void) { //~^ ERROR a never pattern is always unreachable None => {} } - match None:: { //~ ERROR: `Some(_)` not covered + match None:: { //~ ERROR: `Some(!)` not covered Some(!) if true, //~^ ERROR guard on a never pattern None => {} } - match None:: { //~ ERROR: `Some(_)` not covered + match None:: { //~ ERROR: `Some(!)` not covered Some(!) if true => {} //~^ ERROR a never pattern is always unreachable None => {} diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr b/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr index 5497252890f..82457f8b805 100644 --- a/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr +++ b/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr @@ -31,11 +31,11 @@ LL | Some(never!()) => {} | this will never be executed | help: remove this expression -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/check.rs:18:11 | LL | match None:: { - | ^^^^^^^^^^^^ pattern `Some(_)` not covered + | ^^^^^^^^^^^^ pattern `Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -46,14 +46,14 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | -error[E0004]: non-exhaustive patterns: `Some(_)` not covered +error[E0004]: non-exhaustive patterns: `Some(!)` not covered --> $DIR/check.rs:23:11 | LL | match None:: { - | ^^^^^^^^^^^^ pattern `Some(_)` not covered + | ^^^^^^^^^^^^ pattern `Some(!)` not covered | note: `Option` defined here --> $SRC_DIR/core/src/option.rs:LL:COL @@ -64,7 +64,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(_) => todo!() +LL + Some(!) => todo!() | error: aborting due to 6 previous errors -- cgit 1.4.1-3-g733a5 From b878ab6a270928fa45850183b82b13eac1e80c39 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 28 Feb 2024 22:26:20 +0100 Subject: Don't suggest an arm when suggesting a never pattern --- .../src/thir/pattern/check_match.rs | 14 +++++++--- compiler/rustc_pattern_analysis/src/pat.rs | 8 ++++++ .../usefulness/empty-types.never_pats.stderr | 30 +++++++++++----------- tests/ui/rfcs/rfc-0000-never_patterns/check.stderr | 4 +-- 4 files changed, 36 insertions(+), 20 deletions(-) (limited to 'compiler/rustc_pattern_analysis/src') diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 2685bae4d09..6a75573dfa0 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -1024,6 +1024,14 @@ fn report_non_exhaustive_match<'p, 'tcx>( let mut suggestion = None; let sm = cx.tcx.sess.source_map(); + let suggested_arm = if witnesses.len() < 4 + && witnesses.iter().all(|p| p.is_never_pattern()) + && cx.tcx.features().never_patterns + { + pattern + } else { + format!("{pattern} => todo!()") + }; match arms { [] if sp.eq_ctxt(expr_span) => { // Get the span for the empty match body `{}`. @@ -1034,7 +1042,7 @@ fn report_non_exhaustive_match<'p, 'tcx>( }; suggestion = Some(( sp.shrink_to_hi().with_hi(expr_span.hi()), - format!(" {{{indentation}{more}{pattern} => todo!(),{indentation}}}",), + format!(" {{{indentation}{more}{suggested_arm},{indentation}}}",), )); } [only] => { @@ -1060,7 +1068,7 @@ fn report_non_exhaustive_match<'p, 'tcx>( }; suggestion = Some(( only.span.shrink_to_hi(), - format!("{comma}{pre_indentation}{pattern} => todo!()"), + format!("{comma}{pre_indentation}{suggested_arm}"), )); } [.., prev, last] => { @@ -1083,7 +1091,7 @@ fn report_non_exhaustive_match<'p, 'tcx>( if let Some(spacing) = spacing { suggestion = Some(( last.span.shrink_to_hi(), - format!("{comma}{spacing}{pattern} => todo!()"), + format!("{comma}{spacing}{suggested_arm}"), )); } } diff --git a/compiler/rustc_pattern_analysis/src/pat.rs b/compiler/rustc_pattern_analysis/src/pat.rs index 780a386fe65..33d2fe89f43 100644 --- a/compiler/rustc_pattern_analysis/src/pat.rs +++ b/compiler/rustc_pattern_analysis/src/pat.rs @@ -321,6 +321,14 @@ impl WitnessPat { &self.ty } + pub fn is_never_pattern(&self) -> bool { + match self.ctor() { + Never => true, + Or => self.fields.iter().all(|p| p.is_never_pattern()), + _ => self.fields.iter().any(|p| p.is_never_pattern()), + } + } + pub fn iter_fields(&self) -> impl Iterator> { self.fields.iter() } diff --git a/tests/ui/pattern/usefulness/empty-types.never_pats.stderr b/tests/ui/pattern/usefulness/empty-types.never_pats.stderr index 70d5b266bda..0ff2472922e 100644 --- a/tests/ui/pattern/usefulness/empty-types.never_pats.stderr +++ b/tests/ui/pattern/usefulness/empty-types.never_pats.stderr @@ -111,7 +111,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_) => {}, -LL + Err(!) => todo!() +LL + Err(!) | error[E0004]: non-exhaustive patterns: `Ok(1_u32..=u32::MAX)` not covered @@ -192,7 +192,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ match result_never { -LL + Ok(!) | Err(!) => todo!(), +LL + Ok(!) | Err(!), LL + } | @@ -210,8 +210,8 @@ note: `Result` defined here = note: the matched value is of type `Result` help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | -LL | Ok(_) => {}, Err(!) => todo!() - | +++++++++++++++++++ +LL | Ok(_) => {}, Err(!) + | ++++++++ error: unreachable pattern --> $DIR/empty-types.rs:140:13 @@ -240,7 +240,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error[E0004]: non-exhaustive patterns: `Some(!)` not covered @@ -258,7 +258,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error: unreachable pattern @@ -343,7 +343,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern, a match arm with multiple or-patterns as shown, or multiple match arms | LL ~ match *x { -LL + Ok(!) | Err(!) => todo!(), +LL + Ok(!) | Err(!), LL ~ } | @@ -385,7 +385,7 @@ LL | match slice_never { help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ [] => {}, -LL + &[!, ..] => todo!() +LL + &[!, ..] | error[E0004]: non-exhaustive patterns: `&[]`, `&[!]` and `&[!, !]` not covered @@ -492,7 +492,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ &None => {}, -LL + &Some(!) => todo!() +LL + &Some(!) | error[E0004]: non-exhaustive patterns: `Some(!)` not covered @@ -510,7 +510,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error[E0004]: non-exhaustive patterns: `Err(!)` not covered @@ -528,7 +528,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_) => {}, -LL + Err(!) => todo!() +LL + Err(!) | error[E0004]: non-exhaustive patterns: `Err(!)` not covered @@ -546,7 +546,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Ok(_a) => {}, -LL + Err(!) => todo!() +LL + Err(!) | error[E0004]: non-exhaustive patterns: type `(u32, !)` is non-empty @@ -599,7 +599,7 @@ LL | match ref_never { help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ &_a if false => {}, -LL + &! => todo!() +LL + &! | error[E0004]: non-exhaustive patterns: `Ok(!)` not covered @@ -617,7 +617,7 @@ note: `Result` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ Err(_) => {}, -LL + Ok(!) => todo!() +LL + Ok(!) | error[E0004]: non-exhaustive patterns: `Some(!)` not covered @@ -635,7 +635,7 @@ note: `Option>` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error: aborting due to 49 previous errors; 1 warning emitted diff --git a/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr b/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr index 82457f8b805..25f7343a8a8 100644 --- a/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr +++ b/tests/ui/rfcs/rfc-0000-never_patterns/check.stderr @@ -46,7 +46,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error[E0004]: non-exhaustive patterns: `Some(!)` not covered @@ -64,7 +64,7 @@ note: `Option` defined here help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown | LL ~ None => {}, -LL + Some(!) => todo!() +LL + Some(!) | error: aborting due to 6 previous errors -- cgit 1.4.1-3-g733a5