Skip to content

Commit d13aefd

Browse files
Rollup merge of #101357 - compiler-errors:variant-sugg-tweak, r=oli-obk
Include enum path in variant suggestion (except for `Result` and `Option`, which we should have via the prelude) Fixes #101356
2 parents 3c72788 + be53bd8 commit d13aefd

File tree

54 files changed

+260
-244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+260
-244
lines changed

compiler/rustc_middle/src/thir.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::ty::subst::SubstsRef;
2323
use rustc_middle::ty::{self, AdtDef, Ty, UpvarSubsts};
2424
use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation};
2525
use rustc_span::def_id::LocalDefId;
26-
use rustc_span::{Span, Symbol, DUMMY_SP};
26+
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
2727
use rustc_target::abi::VariantIdx;
2828
use rustc_target::asm::InlineAsmRegOrRegClass;
2929
use std::fmt;
@@ -695,17 +695,32 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
695695
Ok(())
696696
}
697697
PatKind::Variant { ref subpatterns, .. } | PatKind::Leaf { ref subpatterns } => {
698-
let variant = match self.kind {
699-
PatKind::Variant { adt_def, variant_index, .. } => {
700-
Some(adt_def.variant(variant_index))
701-
}
702-
_ => self.ty.ty_adt_def().and_then(|adt| {
703-
if !adt.is_enum() { Some(adt.non_enum_variant()) } else { None }
698+
let variant_and_name = match self.kind {
699+
PatKind::Variant { adt_def, variant_index, .. } => ty::tls::with(|tcx| {
700+
let variant = adt_def.variant(variant_index);
701+
let adt_did = adt_def.did();
702+
let name = if tcx.get_diagnostic_item(sym::Option) == Some(adt_did)
703+
|| tcx.get_diagnostic_item(sym::Result) == Some(adt_did)
704+
{
705+
variant.name.to_string()
706+
} else {
707+
format!("{}::{}", tcx.def_path_str(adt_def.did()), variant.name)
708+
};
709+
Some((variant, name))
710+
}),
711+
_ => self.ty.ty_adt_def().and_then(|adt_def| {
712+
if !adt_def.is_enum() {
713+
ty::tls::with(|tcx| {
714+
Some((adt_def.non_enum_variant(), tcx.def_path_str(adt_def.did())))
715+
})
716+
} else {
717+
None
718+
}
704719
}),
705720
};
706721

707-
if let Some(variant) = variant {
708-
write!(f, "{}", variant.name)?;
722+
if let Some((variant, name)) = &variant_and_name {
723+
write!(f, "{}", name)?;
709724

710725
// Only for Adt we can have `S {...}`,
711726
// which we handle separately here.
@@ -730,8 +745,9 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
730745
}
731746
}
732747

733-
let num_fields = variant.map_or(subpatterns.len(), |v| v.fields.len());
734-
if num_fields != 0 || variant.is_none() {
748+
let num_fields =
749+
variant_and_name.as_ref().map_or(subpatterns.len(), |(v, _)| v.fields.len());
750+
if num_fields != 0 || variant_and_name.is_none() {
735751
write!(f, "(")?;
736752
for i in 0..num_fields {
737753
write!(f, "{}", start_or_comma())?;

compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ fn lint_non_exhaustive_omitted_patterns<'p, 'tcx>(
754754
hir_id: HirId,
755755
witnesses: Vec<DeconstructedPat<'p, 'tcx>>,
756756
) {
757-
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
758757
cx.tcx.struct_span_lint_hir(NON_EXHAUSTIVE_OMITTED_PATTERNS, hir_id, sp, |build| {
758+
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
759759
let mut lint = build.build("some variants are not matched explicitly");
760760
lint.span_label(sp, pattern_not_covered_label(&witnesses, &joined_patterns));
761761
lint.help(

src/test/ui/closures/2229_closure_analysis/match/non-exhaustive-match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() {
2424
let _a = || { match l1 { L1::A => (), L1::B => () } };
2525
// (except if the match is already non-exhaustive)
2626
let _b = || { match l1 { L1::A => () } };
27-
//~^ ERROR: non-exhaustive patterns: `B` not covered [E0004]
27+
//~^ ERROR: non-exhaustive patterns: `L1::B` not covered [E0004]
2828

2929
// l2 should not be captured as it is a non-exhaustive SingleVariant
3030
// defined in this crate

src/test/ui/closures/2229_closure_analysis/match/non-exhaustive-match.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `B` not covered
1+
error[E0004]: non-exhaustive patterns: `L1::B` not covered
22
--> $DIR/non-exhaustive-match.rs:26:25
33
|
44
LL | let _b = || { match l1 { L1::A => () } };
5-
| ^^ pattern `B` not covered
5+
| ^^ pattern `L1::B` not covered
66
|
77
note: `L1` defined here
88
--> $DIR/non-exhaustive-match.rs:12:14
@@ -12,8 +12,8 @@ LL | enum L1 { A, B }
1212
= note: the matched value is of type `L1`
1313
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
15-
LL | let _b = || { match l1 { L1::A => (), B => todo!() } };
16-
| ++++++++++++++
15+
LL | let _b = || { match l1 { L1::A => (), L1::B => todo!() } };
16+
| ++++++++++++++++++
1717

1818
error[E0004]: non-exhaustive patterns: type `E1` is non-empty
1919
--> $DIR/non-exhaustive-match.rs:37:25

src/test/ui/empty/empty-never-array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum Helper<T, U> {
88

99
fn transmute<T, U>(t: T) -> U {
1010
let Helper::U(u) = Helper::T(t, []);
11-
//~^ ERROR refutable pattern in local binding: `T(_, _)` not covered
11+
//~^ ERROR refutable pattern in local binding: `Helper::T(_, _)` not covered
1212
u
1313
}
1414

src/test/ui/empty/empty-never-array.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in local binding: `T(_, _)` not covered
1+
error[E0005]: refutable pattern in local binding: `Helper::T(_, _)` not covered
22
--> $DIR/empty-never-array.rs:10:9
33
|
44
LL | let Helper::U(u) = Helper::T(t, []);
5-
| ^^^^^^^^^^^^ pattern `T(_, _)` not covered
5+
| ^^^^^^^^^^^^ pattern `Helper::T(_, _)` not covered
66
|
77
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
88
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html

src/test/ui/error-codes/E0004.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `HastaLaVistaBaby` not covered
1+
error[E0004]: non-exhaustive patterns: `Terminator::HastaLaVistaBaby` not covered
22
--> $DIR/E0004.rs:9:11
33
|
44
LL | match x {
5-
| ^ pattern `HastaLaVistaBaby` not covered
5+
| ^ pattern `Terminator::HastaLaVistaBaby` not covered
66
|
77
note: `Terminator` defined here
88
--> $DIR/E0004.rs:2:5
@@ -15,7 +15,7 @@ LL | HastaLaVistaBaby,
1515
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1616
|
1717
LL ~ Terminator::TalkToMyHand => {}
18-
LL + HastaLaVistaBaby => todo!()
18+
LL + Terminator::HastaLaVistaBaby => todo!()
1919
|
2020

2121
error: aborting due to previous error

src/test/ui/feature-gates/feature-gate-non_exhaustive_omitted_patterns_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
Foo::A => {}
2222
Foo::B => {}
2323
}
24-
//~^^^^ ERROR non-exhaustive patterns: `C` not covered
24+
//~^^^^ ERROR non-exhaustive patterns: `Foo::C` not covered
2525

2626
match Foo::A {
2727
Foo::A => {}

src/test/ui/feature-gates/feature-gate-non_exhaustive_omitted_patterns_lint.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ LL | #[warn(non_exhaustive_omitted_patterns)]
9999
= note: see issue #89554 <https://github.com/rust-lang/rust/issues/89554> for more information
100100
= help: add `#![feature(non_exhaustive_omitted_patterns_lint)]` to the crate attributes to enable
101101

102-
error[E0004]: non-exhaustive patterns: `C` not covered
102+
error[E0004]: non-exhaustive patterns: `Foo::C` not covered
103103
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:20:11
104104
|
105105
LL | match Foo::A {
106-
| ^^^^^^ pattern `C` not covered
106+
| ^^^^^^ pattern `Foo::C` not covered
107107
|
108108
note: `Foo` defined here
109109
--> $DIR/feature-gate-non_exhaustive_omitted_patterns_lint.rs:12:15
@@ -116,7 +116,7 @@ LL | A, B, C,
116116
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
117117
|
118118
LL ~ Foo::B => {}
119-
LL + C => todo!()
119+
LL + Foo::C => todo!()
120120
|
121121

122122
error: aborting due to previous error; 10 warnings emitted

src/test/ui/issue-94866.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `B` not covered
1+
error[E0004]: non-exhaustive patterns: `Enum::B` not covered
22
--> $DIR/issue-94866.rs:10:11
33
|
44
LL | match Enum::A {
5-
| ^^^^^^^ pattern `B` not covered
5+
| ^^^^^^^ pattern `Enum::B` not covered
66
|
77
note: `Enum` defined here
88
--> $DIR/issue-94866.rs:7:16
@@ -13,7 +13,7 @@ LL | enum Enum { A, B }
1313
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
1515
LL ~ Enum::A => m!(),
16-
LL + B => todo!()
16+
LL + Enum::B => todo!()
1717
|
1818

1919
error: aborting due to previous error

src/test/ui/match/match_non_exhaustive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
match l { L::A => (), L::B => () };
2222
// (except if the match is already non-exhaustive)
2323
match l { L::A => () };
24-
//~^ ERROR: non-exhaustive patterns: `B` not covered [E0004]
24+
//~^ ERROR: non-exhaustive patterns: `L::B` not covered [E0004]
2525

2626
// E1 is not visibly uninhabited from here
2727
let (e1, e2) = bar();

src/test/ui/match/match_non_exhaustive.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `B` not covered
1+
error[E0004]: non-exhaustive patterns: `L::B` not covered
22
--> $DIR/match_non_exhaustive.rs:23:11
33
|
44
LL | match l { L::A => () };
5-
| ^ pattern `B` not covered
5+
| ^ pattern `L::B` not covered
66
|
77
note: `L` defined here
88
--> $DIR/match_non_exhaustive.rs:10:13
@@ -12,8 +12,8 @@ LL | enum L { A, B }
1212
= note: the matched value is of type `L`
1313
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
1414
|
15-
LL | match l { L::A => (), B => todo!() };
16-
| ++++++++++++++
15+
LL | match l { L::A => (), L::B => todo!() };
16+
| +++++++++++++++++
1717

1818
error[E0004]: non-exhaustive patterns: type `E1` is non-empty
1919
--> $DIR/match_non_exhaustive.rs:28:11

src/test/ui/pattern/usefulness/doc-hidden-non-exhaustive.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ fn main() {
2222
HiddenEnum::A => {}
2323
HiddenEnum::C => {}
2424
}
25-
//~^^^^ non-exhaustive patterns: `B` not covered
25+
//~^^^^ non-exhaustive patterns: `HiddenEnum::B` not covered
2626

2727
match HiddenEnum::A {
2828
HiddenEnum::A => {}
2929
}
30-
//~^^^ non-exhaustive patterns: `B` and `_` not covered
30+
//~^^^ non-exhaustive patterns: `HiddenEnum::B` and `_` not covered
3131

3232
match None {
3333
None => {}
3434
Some(HiddenEnum::A) => {}
3535
}
36-
//~^^^^ non-exhaustive patterns: `Some(B)` and `Some(_)` not covered
36+
//~^^^^ non-exhaustive patterns: `Some(HiddenEnum::B)` and `Some(_)` not covered
3737

3838
match InCrate::A {
3939
InCrate::A => {}
4040
InCrate::B => {}
4141
}
42-
//~^^^^ non-exhaustive patterns: `C` not covered
42+
//~^^^^ non-exhaustive patterns: `InCrate::C` not covered
4343
}

src/test/ui/pattern/usefulness/doc-hidden-non-exhaustive.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ LL ~ HiddenEnum::B => {}
1616
LL + _ => todo!()
1717
|
1818

19-
error[E0004]: non-exhaustive patterns: `B` not covered
19+
error[E0004]: non-exhaustive patterns: `HiddenEnum::B` not covered
2020
--> $DIR/doc-hidden-non-exhaustive.rs:21:11
2121
|
2222
LL | match HiddenEnum::A {
23-
| ^^^^^^^^^^^^^ pattern `B` not covered
23+
| ^^^^^^^^^^^^^ pattern `HiddenEnum::B` not covered
2424
|
2525
note: `HiddenEnum` defined here
2626
--> $DIR/auxiliary/hidden.rs:3:5
@@ -34,14 +34,14 @@ LL | B,
3434
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
3535
|
3636
LL ~ HiddenEnum::C => {}
37-
LL + B => todo!()
37+
LL + HiddenEnum::B => todo!()
3838
|
3939

40-
error[E0004]: non-exhaustive patterns: `B` and `_` not covered
40+
error[E0004]: non-exhaustive patterns: `HiddenEnum::B` and `_` not covered
4141
--> $DIR/doc-hidden-non-exhaustive.rs:27:11
4242
|
4343
LL | match HiddenEnum::A {
44-
| ^^^^^^^^^^^^^ patterns `B` and `_` not covered
44+
| ^^^^^^^^^^^^^ patterns `HiddenEnum::B` and `_` not covered
4545
|
4646
note: `HiddenEnum` defined here
4747
--> $DIR/auxiliary/hidden.rs:3:5
@@ -55,14 +55,14 @@ LL | B,
5555
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
5656
|
5757
LL ~ HiddenEnum::A => {}
58-
LL + B | _ => todo!()
58+
LL + HiddenEnum::B | _ => todo!()
5959
|
6060

61-
error[E0004]: non-exhaustive patterns: `Some(B)` and `Some(_)` not covered
61+
error[E0004]: non-exhaustive patterns: `Some(HiddenEnum::B)` and `Some(_)` not covered
6262
--> $DIR/doc-hidden-non-exhaustive.rs:32:11
6363
|
6464
LL | match None {
65-
| ^^^^ patterns `Some(B)` and `Some(_)` not covered
65+
| ^^^^ patterns `Some(HiddenEnum::B)` and `Some(_)` not covered
6666
|
6767
note: `Option<HiddenEnum>` defined here
6868
--> $SRC_DIR/core/src/option.rs:LL:COL
@@ -76,14 +76,14 @@ LL | Some(#[stable(feature = "rust1", since = "1.0.0")] T),
7676
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
7777
|
7878
LL ~ Some(HiddenEnum::A) => {}
79-
LL + Some(B) | Some(_) => todo!()
79+
LL + Some(HiddenEnum::B) | Some(_) => todo!()
8080
|
8181

82-
error[E0004]: non-exhaustive patterns: `C` not covered
82+
error[E0004]: non-exhaustive patterns: `InCrate::C` not covered
8383
--> $DIR/doc-hidden-non-exhaustive.rs:38:11
8484
|
8585
LL | match InCrate::A {
86-
| ^^^^^^^^^^ pattern `C` not covered
86+
| ^^^^^^^^^^ pattern `InCrate::C` not covered
8787
|
8888
note: `InCrate` defined here
8989
--> $DIR/doc-hidden-non-exhaustive.rs:11:5
@@ -97,7 +97,7 @@ LL | C,
9797
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
9898
|
9999
LL ~ InCrate::B => {}
100-
LL + C => todo!()
100+
LL + InCrate::C => todo!()
101101
|
102102

103103
error: aborting due to 5 previous errors

0 commit comments

Comments
 (0)