Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 177d0ce

Browse files
committed
Deduplicate errors in const to pat conversion
1 parent d486486 commit 177d0ce

29 files changed

+55
-200
lines changed

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
252252
ty::Adt(adt_def, _) if adt_def.is_union() => {
253253
// Matching on union fields is unsafe, we can't hide it in constants
254254
self.saw_const_match_error.set(true);
255-
tcx.sess.span_err(span, "cannot use unions in constant patterns");
255+
let msg = "cannot use unions in constant patterns";
256+
if self.include_lint_checks {
257+
tcx.sess.span_err(span, msg);
258+
} else {
259+
tcx.sess.delay_span_bug(span, msg)
260+
}
256261
PatKind::Wild
257262
}
258263
ty::Adt(..)
@@ -267,7 +272,11 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
267272
cv.ty, cv.ty,
268273
);
269274
self.saw_const_match_error.set(true);
270-
self.tcx().sess.span_err(self.span, &msg);
275+
if self.include_lint_checks {
276+
tcx.sess.span_err(self.span, &msg);
277+
} else {
278+
tcx.sess.delay_span_bug(self.span, &msg)
279+
}
271280
PatKind::Wild
272281
}
273282
// If the type is not structurally comparable, just emit the constant directly,
@@ -308,7 +317,11 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
308317
path, path,
309318
);
310319
self.saw_const_match_error.set(true);
311-
tcx.sess.span_err(span, &msg);
320+
if self.include_lint_checks {
321+
tcx.sess.span_err(span, &msg);
322+
} else {
323+
tcx.sess.delay_span_bug(span, &msg)
324+
}
312325
PatKind::Wild
313326
}
314327
ty::Adt(adt_def, substs) if adt_def.is_enum() => {
@@ -340,7 +353,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
340353
// These are not allowed and will error elsewhere anyway.
341354
ty::Dynamic(..) => {
342355
self.saw_const_match_error.set(true);
343-
tcx.sess.span_err(span, &format!("`{}` cannot be used in patterns", cv.ty));
356+
let msg = format!("`{}` cannot be used in patterns", cv.ty);
357+
if self.include_lint_checks {
358+
tcx.sess.span_err(span, &msg);
359+
} else {
360+
tcx.sess.delay_span_bug(span, &msg)
361+
}
344362
PatKind::Wild
345363
}
346364
// `&str` and `&[u8]` are represented as `ConstValue::Slice`, let's keep using this
@@ -427,7 +445,12 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
427445
}
428446
_ => {
429447
self.saw_const_match_error.set(true);
430-
tcx.sess.span_err(span, &format!("`{}` cannot be used in patterns", cv.ty));
448+
let msg = format!("`{}` cannot be used in patterns", cv.ty);
449+
if self.include_lint_checks {
450+
tcx.sess.span_err(span, &msg);
451+
} else {
452+
tcx.sess.delay_span_bug(span, &msg)
453+
}
431454
PatKind::Wild
432455
}
433456
};

src/test/ui/consts/const_in_pattern/cross-crate-fail.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ fn main() {
1212
match None {
1313
consts::SOME => panic!(),
1414
//~^ must be annotated with `#[derive(PartialEq, Eq)]`
15-
//~| must be annotated with `#[derive(PartialEq, Eq)]`
1615

1716
_ => {}
1817
}
1918

2019
match None {
2120
<Defaulted as consts::AssocConst>::SOME => panic!(),
2221
//~^ must be annotated with `#[derive(PartialEq, Eq)]`
23-
//~| must be annotated with `#[derive(PartialEq, Eq)]`
2422

2523
_ => {}
2624
}

src/test/ui/consts/const_in_pattern/cross-crate-fail.stderr

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,10 @@ LL | consts::SOME => panic!(),
55
| ^^^^^^^^^^^^
66

77
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
8-
--> $DIR/cross-crate-fail.rs:21:9
8+
--> $DIR/cross-crate-fail.rs:20:9
99
|
1010
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
14-
--> $DIR/cross-crate-fail.rs:13:9
15-
|
16-
LL | consts::SOME => panic!(),
17-
| ^^^^^^^^^^^^
18-
19-
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq, Eq)]`
20-
--> $DIR/cross-crate-fail.rs:21:9
21-
|
22-
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24-
25-
error: aborting due to 4 previous errors
13+
error: aborting due to 2 previous errors
2614

src/test/ui/consts/const_in_pattern/no-eq-branch-fail.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn main() {
2020
match Foo::Qux(NoEq) {
2121
BAR_BAZ => panic!(),
2222
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
23-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
2423
_ => {}
2524
}
2625
}

src/test/ui/consts/const_in_pattern/no-eq-branch-fail.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated wit
44
LL | BAR_BAZ => panic!(),
55
| ^^^^^^^
66

7-
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq, Eq)]`
8-
--> $DIR/no-eq-branch-fail.rs:21:9
9-
|
10-
LL | BAR_BAZ => panic!(),
11-
| ^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

src/test/ui/consts/const_in_pattern/reject_non_partial_eq.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ fn main() {
2727
match None {
2828
NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
2929
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
30-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
3130
_ => panic!("whoops"),
3231
}
3332
}

src/test/ui/consts/const_in_pattern/reject_non_partial_eq.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: to use a constant of type `Option<NoPartialEq>` in a pattern, `Option<NoP
44
LL | NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
55
| ^^^^^^^^^^^^^^^^^^
66

7-
error: to use a constant of type `Option<NoPartialEq>` in a pattern, `Option<NoPartialEq>` must be annotated with `#[derive(PartialEq, Eq)]`
8-
--> $DIR/reject_non_partial_eq.rs:28:9
9-
|
10-
LL | NO_PARTIAL_EQ_NONE => println!("NO_PARTIAL_EQ_NONE"),
11-
| ^^^^^^^^^^^^^^^^^^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

src/test/ui/consts/const_in_pattern/reject_non_structural.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,51 +39,41 @@ 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-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
4342

4443
const FIELD: OND = TrivialEq(Some(NoDerive)).0;
4544
match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
4645
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
47-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
4846

4947
const NO_DERIVE_SOME: OND = Some(NoDerive);
5048
const INDIRECT: OND = NO_DERIVE_SOME;
5149
match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
5250
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
53-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
5451

5552
const TUPLE: (OND, OND) = (None, Some(NoDerive));
5653
match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
5754
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
58-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
5955

6056
const TYPE_ASCRIPTION: OND = Some(NoDerive): OND;
6157
match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
6258
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
63-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
6459

6560
const ARRAY: [OND; 2] = [None, Some(NoDerive)];
6661
match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
6762
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
68-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
6963

7064
const REPEAT: [OND; 2] = [Some(NoDerive); 2];
7165
match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
7266
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
7367
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
74-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
75-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
7668

7769
trait Trait: Sized { const ASSOC: Option<Self>; }
7870
impl Trait for NoDerive { const ASSOC: Option<NoDerive> = Some(NoDerive); }
7971
match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
8072
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
81-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
8273

8374
const BLOCK: OND = { NoDerive; Some(NoDerive) };
8475
match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
8576
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
86-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
8777

8878
const ADDR_OF: &OND = &Some(NoDerive);
8979
match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };

src/test/ui/consts/const_in_pattern/reject_non_structural.stderr

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,61 +5,61 @@ LL | match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"
55
| ^^^^
66

77
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:45:28
8+
--> $DIR/reject_non_structural.rs:44:28
99
|
1010
LL | match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
1111
| ^^^^^
1212

1313
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:51:27
14+
--> $DIR/reject_non_structural.rs:49:27
1515
|
1616
LL | match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
1717
| ^^^^^^^^
1818

1919
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:56:36
20+
--> $DIR/reject_non_structural.rs:53:36
2121
|
2222
LL | match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
2323
| ^^^^^
2424

2525
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:61:28
26+
--> $DIR/reject_non_structural.rs:57:28
2727
|
2828
LL | match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
2929
| ^^^^^^^^^^^^^^^
3030

3131
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:66:36
32+
--> $DIR/reject_non_structural.rs:61:36
3333
|
3434
LL | match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
3535
| ^^^^^
3636

3737
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:71:33
38+
--> $DIR/reject_non_structural.rs:65:33
3939
|
4040
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
4141
| ^^^^^^
4242

4343
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:71:33
44+
--> $DIR/reject_non_structural.rs:65:33
4545
|
4646
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
4747
| ^^^^^^
4848

4949
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:79:28
50+
--> $DIR/reject_non_structural.rs:71:28
5151
|
5252
LL | match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
5353
| ^^^^^^^^^^^^^^^
5454

5555
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:84:28
56+
--> $DIR/reject_non_structural.rs:75:28
5757
|
5858
LL | match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
5959
| ^^^^^
6060

6161
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:89:29
62+
--> $DIR/reject_non_structural.rs:79:29
6363
|
6464
LL | match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
6565
| ^^^^^^^
@@ -72,65 +72,5 @@ LL | #![warn(indirect_structural_match)]
7272
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
7373
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411>
7474

75-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
76-
--> $DIR/reject_non_structural.rs:40:36
77-
|
78-
LL | match Derive::Some(NoDerive) { ENUM => dbg!(ENUM), _ => panic!("whoops"), };
79-
| ^^^^
80-
81-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
82-
--> $DIR/reject_non_structural.rs:45:28
83-
|
84-
LL | match Some(NoDerive) { FIELD => dbg!(FIELD), _ => panic!("whoops"), };
85-
| ^^^^^
86-
87-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
88-
--> $DIR/reject_non_structural.rs:51:27
89-
|
90-
LL | match Some(NoDerive) {INDIRECT => dbg!(INDIRECT), _ => panic!("whoops"), };
91-
| ^^^^^^^^
92-
93-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
94-
--> $DIR/reject_non_structural.rs:56:36
95-
|
96-
LL | match (None, Some(NoDerive)) { TUPLE => dbg!(TUPLE), _ => panic!("whoops"), };
97-
| ^^^^^
98-
99-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
100-
--> $DIR/reject_non_structural.rs:61:28
101-
|
102-
LL | match Some(NoDerive) { TYPE_ASCRIPTION => dbg!(TYPE_ASCRIPTION), _ => panic!("whoops"), };
103-
| ^^^^^^^^^^^^^^^
104-
105-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
106-
--> $DIR/reject_non_structural.rs:66:36
107-
|
108-
LL | match [None, Some(NoDerive)] { ARRAY => dbg!(ARRAY), _ => panic!("whoops"), };
109-
| ^^^^^
110-
111-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
112-
--> $DIR/reject_non_structural.rs:71:33
113-
|
114-
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
115-
| ^^^^^^
116-
117-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
118-
--> $DIR/reject_non_structural.rs:71:33
119-
|
120-
LL | match [Some(NoDerive); 2] { REPEAT => dbg!(REPEAT), _ => panic!("whoops"), };
121-
| ^^^^^^
122-
123-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
124-
--> $DIR/reject_non_structural.rs:79:28
125-
|
126-
LL | match Some(NoDerive) { NoDerive::ASSOC => dbg!(NoDerive::ASSOC), _ => panic!("whoops"), };
127-
| ^^^^^^^^^^^^^^^
128-
129-
error: to use a constant of type `NoDerive` in a pattern, `NoDerive` must be annotated with `#[derive(PartialEq, Eq)]`
130-
--> $DIR/reject_non_structural.rs:84:28
131-
|
132-
LL | match Some(NoDerive) { BLOCK => dbg!(BLOCK), _ => panic!("whoops"), };
133-
| ^^^^^
134-
135-
error: aborting due to 20 previous errors; 1 warning emitted
75+
error: aborting due to 10 previous errors; 1 warning emitted
13676

src/test/ui/match/issue-70972-dyn-trait.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
match a {
66
F => panic!(),
77
//~^ ERROR `&dyn Send` cannot be used in patterns
8-
//~| ERROR `&dyn Send` cannot be used in patterns
98
_ => {}
109
}
1110
}

src/test/ui/match/issue-70972-dyn-trait.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: `&dyn Send` cannot be used in patterns
44
LL | F => panic!(),
55
| ^
66

7-
error: `&dyn Send` cannot be used in patterns
8-
--> $DIR/issue-70972-dyn-trait.rs:6:9
9-
|
10-
LL | F => panic!(),
11-
| ^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

src/test/ui/pattern/issue-71042-opaquely-typed-constant-used-in-pattern.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn main() {
55
const C: impl Copy = 0;
66
match C {
77
C => {} //~ ERROR: `impl Copy` cannot be used in patterns
8-
//~^ ERROR: `impl Copy` cannot be used in patterns
98
_ => {}
109
}
1110
}

src/test/ui/pattern/issue-71042-opaquely-typed-constant-used-in-pattern.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error: `impl Copy` cannot be used in patterns
44
LL | C => {}
55
| ^
66

7-
error: `impl Copy` cannot be used in patterns
8-
--> $DIR/issue-71042-opaquely-typed-constant-used-in-pattern.rs:7:9
9-
|
10-
LL | C => {}
11-
| ^
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

src/test/ui/rfc1445/cant-hide-behind-direct-struct-embedded.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fn main() {
2121
match WRAP_DIRECT_INLINE {
2222
WRAP_DIRECT_INLINE => { panic!("WRAP_DIRECT_INLINE matched itself"); }
2323
//~^ ERROR must be annotated with `#[derive(PartialEq, Eq)]`
24-
//~| ERROR must be annotated with `#[derive(PartialEq, Eq)]`
2524
_ => { println!("WRAP_DIRECT_INLINE did not match itself"); }
2625
}
2726
}

0 commit comments

Comments
 (0)