Skip to content

Commit 73c34cb

Browse files
committed
Add notes to non-structural const in pattern error message
1 parent 553ecbe commit 73c34cb

27 files changed

+168
-10
lines changed

compiler/rustc_mir_build/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ mir_build_indirect_structural_match =
331331
mir_build_nontrivial_structural_match =
332332
to use a constant of type `{$non_sm_ty}` in a pattern, the constant's initializer must be trivial or `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
333333
334+
mir_build_type_not_structural_tip = the traits must be derived, manual `impl`s are not sufficient
335+
336+
mir_build_type_not_structural_more_info = see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
337+
334338
mir_build_overlapping_range_endpoints = multiple patterns overlap on their endpoints
335339
.range = ... with this range
336340
.note = you likely meant to write mutually exclusive ranges

compiler/rustc_mir_build/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,8 @@ pub struct UnionPattern {
663663

664664
#[derive(Diagnostic)]
665665
#[diag(mir_build_type_not_structural)]
666+
#[note(mir_build_type_not_structural_tip)]
667+
#[note(mir_build_type_not_structural_more_info)]
666668
pub struct TypeNotStructural<'tcx> {
667669
#[primary_span]
668670
pub span: Span,
@@ -695,12 +697,16 @@ pub struct PointerPattern;
695697

696698
#[derive(LintDiagnostic)]
697699
#[diag(mir_build_indirect_structural_match)]
700+
#[note(mir_build_type_not_structural_tip)]
701+
#[note(mir_build_type_not_structural_more_info)]
698702
pub struct IndirectStructuralMatch<'tcx> {
699703
pub non_sm_ty: Ty<'tcx>,
700704
}
701705

702706
#[derive(LintDiagnostic)]
703707
#[diag(mir_build_nontrivial_structural_match)]
708+
#[note(mir_build_type_not_structural_tip)]
709+
#[note(mir_build_type_not_structural_more_info)]
704710
pub struct NontrivialStructuralMatch<'tcx> {
705711
pub non_sm_ty: Ty<'tcx>,
706712
}

src/tools/clippy/tests/ui/crashes/ice-6254.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | FOO_REF_REF => {},
66
|
77
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
88
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
9+
= note: the traits must be derived, manual `impl`s are not sufficient
10+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
911
= note: `-D indirect-structural-match` implied by `-D warnings`
1012

1113
error: aborting due to previous error

tests/ui/consts/const_in_pattern/cross-crate-fail.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be ann
33
|
44
LL | consts::SOME => panic!(),
55
| ^^^^^^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
811
--> $DIR/cross-crate-fail.rs:20:9
912
|
1013
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
1114
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: the traits must be derived, manual `impl`s are not sufficient
17+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
1218

1319
error: aborting due to 2 previous errors
1420

tests/ui/consts/const_in_pattern/custom-eq-branch-warn.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ fn main() {
2828
match Foo::Qux(CustomEq) {
2929
BAR_BAZ => panic!(),
3030
//~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
31+
//~| NOTE the traits must be derived
32+
//~| NOTE StructuralEq.html for details
3133
//~| WARN this was previously accepted
3234
//~| NOTE see issue #73448
3335
//~| NOTE `#[warn(nontrivial_structural_match)]` on by default

tests/ui/consts/const_in_pattern/custom-eq-branch-warn.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | BAR_BAZ => panic!(),
66
|
77
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
88
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
9+
= note: the traits must be derived, manual `impl`s are not sufficient
10+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
911
= note: `#[warn(nontrivial_structural_match)]` on by default
1012

1113
warning: 1 warning emitted

tests/ui/consts/const_in_pattern/incomplete-slice.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | E_SL => {}
66
|
77
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
88
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
9+
= note: the traits must be derived, manual `impl`s are not sufficient
10+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
911
= note: `#[warn(indirect_structural_match)]` on by default
1012

1113
error[E0004]: non-exhaustive patterns: `&_` not covered

tests/ui/consts/const_in_pattern/issue-78057.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: to use a constant of type `Opaque` in a pattern, `Opaque` must be annotat
33
|
44
LL | FOO => {},
55
| ^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: unreachable pattern
811
--> $DIR/issue-78057.rs:14:9

tests/ui/consts/const_in_pattern/no-eq-branch-fail.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated wit
33
|
44
LL | BAR_BAZ => panic!(),
55
| ^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: aborting due to previous error
811

tests/ui/consts/const_in_pattern/reject_non_partial_eq.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: to use a constant of type `NoPartialEq` in a pattern, `NoPartialEq` must
33
|
44
LL | NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
55
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: aborting due to previous error
811

tests/ui/consts/const_in_pattern/reject_non_structural.rs

+22
Original file line numberDiff line numberDiff line change
@@ -39,45 +39,67 @@ fn main() {
3939
const ENUM: Derive<NoDerive> = Derive::Some(NoDerive);
4040
match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"), };
4141
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
42+
//~| NOTE the traits must be derived
43+
//~| NOTE StructuralEq.html for details
4244

4345
const FIELD: OND = TrivialEq(Some(NoDerive)).0;
4446
match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
4547
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
48+
//~| NOTE the traits must be derived
49+
//~| NOTE StructuralEq.html for details
4650

4751
const NO_DERIVE_SOME: OND = Some(NoDerive);
4852
const INDIRECT: OND = NO_DERIVE_SOME;
4953
match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
5054
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
55+
//~| NOTE the traits must be derived
56+
//~| NOTE StructuralEq.html for details
5157

5258
const TUPLE: (OND, OND) = (None, Some(NoDerive));
5359
match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
5460
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
61+
//~| NOTE the traits must be derived
62+
//~| NOTE StructuralEq.html for details
5563

5664
const TYPE_ASCRIPTION: OND = type_ascribe!(Some(NoDerive), OND);
5765
match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
5866
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
67+
//~| NOTE the traits must be derived
68+
//~| NOTE StructuralEq.html for details
5969

6070
const ARRAY: [OND; 2] = [None, Some(NoDerive)];
6171
match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
6272
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
73+
//~| NOTE the traits must be derived
74+
//~| NOTE StructuralEq.html for details
6375

6476
const REPEAT: [OND; 2] = [Some(NoDerive); 2];
6577
match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
6678
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
79+
//~| NOTE the traits must be derived
80+
//~| NOTE StructuralEq.html for details
6781
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
82+
//~| NOTE the traits must be derived
83+
//~| NOTE StructuralEq.html for details
6884

6985
trait Trait: Sized { const ASSOC: Option<Self>; }
7086
impl Trait for NoDerive { const ASSOC: Option<NoDerive> = Some(NoDerive); }
7187
match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
7288
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
89+
//~| NOTE the traits must be derived
90+
//~| NOTE StructuralEq.html for details
7391

7492
const BLOCK: OND = { NoDerive; Some(NoDerive) };
7593
match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
7694
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
95+
//~| NOTE the traits must be derived
96+
//~| NOTE StructuralEq.html for details
7797

7898
const ADDR_OF: &OND = &Some(NoDerive);
7999
match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
80100
//~^ WARN must be annotated with `#[derive(PartialEq, Eq)]`
101+
//~| NOTE the traits must be derived
102+
//~| NOTE StructuralEq.html for details
81103
//~| WARN previously accepted by the compiler but is being phased out
82104
//~| NOTE for more information, see issue #62411
83105
}

tests/ui/consts/const_in_pattern/reject_non_structural.stderr

+42-10
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,101 @@ error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be ann
33
|
44
LL | match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"), };
55
| ^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
8-
--> $DIR/reject_non_structural.rs:44:28
11+
--> $DIR/reject_non_structural.rs:46:28
912
|
1013
LL | match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
1114
| ^^^^^
15+
|
16+
= note: the traits must be derived, manual `impl`s are not sufficient
17+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
1218

1319
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
14-
--> $DIR/reject_non_structural.rs:49:27
20+
--> $DIR/reject_non_structural.rs:53:27
1521
|
1622
LL | match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
1723
| ^^^^^^^^
24+
|
25+
= note: the traits must be derived, manual `impl`s are not sufficient
26+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
1827

1928
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
20-
--> $DIR/reject_non_structural.rs:53:36
29+
--> $DIR/reject_non_structural.rs:59:36
2130
|
2231
LL | match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
2332
| ^^^^^
33+
|
34+
= note: the traits must be derived, manual `impl`s are not sufficient
35+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
2436

2537
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
26-
--> $DIR/reject_non_structural.rs:57:28
38+
--> $DIR/reject_non_structural.rs:65:28
2739
|
2840
LL | match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
2941
| ^^^^^^^^^^^^^^^
42+
|
43+
= note: the traits must be derived, manual `impl`s are not sufficient
44+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
3045

3146
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
32-
--> $DIR/reject_non_structural.rs:61:36
47+
--> $DIR/reject_non_structural.rs:71:36
3348
|
3449
LL | match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
3550
| ^^^^^
51+
|
52+
= note: the traits must be derived, manual `impl`s are not sufficient
53+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
3654

3755
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
38-
--> $DIR/reject_non_structural.rs:65:33
56+
--> $DIR/reject_non_structural.rs:77:33
3957
|
4058
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
4159
| ^^^^^^
60+
|
61+
= note: the traits must be derived, manual `impl`s are not sufficient
62+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
4263

4364
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
44-
--> $DIR/reject_non_structural.rs:65:33
65+
--> $DIR/reject_non_structural.rs:77:33
4566
|
4667
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
4768
| ^^^^^^
69+
|
70+
= note: the traits must be derived, manual `impl`s are not sufficient
71+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
4872

4973
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
50-
--> $DIR/reject_non_structural.rs:71:28
74+
--> $DIR/reject_non_structural.rs:87:28
5175
|
5276
LL | match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
5377
| ^^^^^^^^^^^^^^^
78+
|
79+
= note: the traits must be derived, manual `impl`s are not sufficient
80+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
5481

5582
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
56-
--> $DIR/reject_non_structural.rs:75:28
83+
--> $DIR/reject_non_structural.rs:93:28
5784
|
5885
LL | match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
5986
| ^^^^^
87+
|
88+
= note: the traits must be derived, manual `impl`s are not sufficient
89+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
6090

6191
warning: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
62-
--> $DIR/reject_non_structural.rs:79:29
92+
--> $DIR/reject_non_structural.rs:99:29
6393
|
6494
LL | match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
6595
| ^^^^^^^
6696
|
6797
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
6898
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
99+
= note: the traits must be derived, manual `impl`s are not sufficient
100+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69101
note: the lint level is defined here
70102
--> $DIR/reject_non_structural.rs:12:9
71103
|

tests/ui/consts/const_in_pattern/warn_corner_cases.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
66
|
77
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
88
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
9+
= note: the traits must be derived, manual `impl`s are not sufficient
10+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
911
= note: `#[warn(nontrivial_structural_match)]` on by default
1012

1113
warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
@@ -16,6 +18,8 @@ LL | match None { Some(_) => panic!("whoops"), CALL => dbg!(CALL), };
1618
|
1719
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1820
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
21+
= note: the traits must be derived, manual `impl`s are not sufficient
22+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
1923

2024
warning: to use a constant of type `NoDerive` in a pattern, the constant's initializer must be trivial or `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
2125
--> $DIR/warn_corner_cases.rs:38:47
@@ -25,6 +29,8 @@ LL | match None { Some(_) => panic!("whoops"), METHOD_CALL => dbg!(METHOD_CA
2529
|
2630
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
2731
= note: for more information, see issue #73448 <https://github.com/rust-lang/rust/issues/73448>
32+
= note: the traits must be derived, manual `impl`s are not sufficient
33+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
2834

2935
warning: 3 warnings emitted
3036

tests/ui/consts/match_ice.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: to use a constant of type `S` in a pattern, `S` must be annotated with `#
33
|
44
LL | C => {}
55
| ^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: aborting due to previous error
811

tests/ui/pattern/usefulness/const-partial_eq-fallback-ice.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ error: to use a constant of type `MyType` in a pattern, `MyType` must be annotat
33
|
44
LL | if let CONSTANT = &&MyType {
55
| ^^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)