Skip to content

Commit 41486c8

Browse files
committed
Tweak unevaluated constant in pattern error
Silence errors that are implied by the errors in the `const` item definition. Add a primary span label.
1 parent 481d220 commit 41486c8

22 files changed

+37
-165
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ mir_build_const_pattern_depends_on_generic_parameter =
9292
constant pattern depends on a generic parameter
9393
9494
mir_build_could_not_eval_const_pattern = could not evaluate constant pattern
95+
.label = could not evaluate constant
9596
9697
mir_build_deref_raw_pointer_requires_unsafe =
9798
dereference of raw pointer is unsafe and requires unsafe block

compiler/rustc_mir_build/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ pub(crate) struct ConstPatternDependsOnGenericParameter {
613613
#[diag(mir_build_could_not_eval_const_pattern)]
614614
pub(crate) struct CouldNotEvalConstPattern {
615615
#[primary_span]
616+
#[label]
616617
pub(crate) span: Span,
617618
}
618619

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,16 @@ impl<'tcx> ConstToPat<'tcx> {
142142
Ok(Ok(c)) => c,
143143
Err(ErrorHandled::Reported(_, _)) => {
144144
// Let's tell the use where this failing const occurs.
145-
let err = self.tcx().dcx().create_err(CouldNotEvalConstPattern { span: self.span });
145+
let mut err =
146+
self.tcx().dcx().create_err(CouldNotEvalConstPattern { span: self.span });
147+
// We've emitted an error on the original const, it would be redundant to complain
148+
// on its use as well.
149+
if let ty::ConstKind::Unevaluated(uv) = self.c.kind()
150+
&& let hir::def::DefKind::Const | hir::def::DefKind::AssocConst =
151+
self.tcx().def_kind(uv.def)
152+
{
153+
err.downgrade_to_delayed_bug();
154+
}
146155
return self.mk_err(err, ty);
147156
}
148157
Err(ErrorHandled::TooGeneric(_)) => {

tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ impl Opcode2 {
1212

1313
pub fn example2(msg_type: Opcode2) -> impl FnMut(&[u8]) {
1414
move |i| match msg_type {
15-
Opcode2::OP2 => unimplemented!(),
16-
//~^ ERROR: could not evaluate constant pattern
15+
Opcode2::OP2 => unimplemented!(), // ok, `const` already emitted an error
1716
}
1817
}
1918

tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@ help: you might be missing a type parameter
1717
LL | pub struct Opcode2<S>(&'a S);
1818
| +++
1919

20-
error: could not evaluate constant pattern
21-
--> $DIR/ice-type-mismatch-when-copying-112824.rs:15:9
22-
|
23-
LL | impl Opcode2 {
24-
| ------------
25-
LL | pub const OP2: Opcode2 = Opcode2(Opcode(0x1));
26-
| ---------------------- constant defined here
27-
...
28-
LL | Opcode2::OP2 => unimplemented!(),
29-
| ^^^^^^^^^^^^
30-
31-
error: aborting due to 3 previous errors
20+
error: aborting due to 2 previous errors
3221

3322
Some errors have detailed explanations: E0261, E0412.
3423
For more information about an error, try `rustc --explain E0261`.

tests/ui/consts/const-eval/const-eval-overflow-2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const NEG_NEG_128: i8 = -NEG_128; //~ ERROR constant
1212

1313
fn main() {
1414
match -128i8 {
15-
NEG_NEG_128 => println!("A"),
16-
//~^ ERROR could not evaluate constant pattern
15+
NEG_NEG_128 => println!("A"), // ok, `const` error already emitted
1716
_ => println!("B"),
1817
}
1918
}

tests/ui/consts/const-eval/const-eval-overflow-2.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ error[E0080]: evaluation of constant value failed
44
LL | const NEG_NEG_128: i8 = -NEG_128;
55
| ^^^^^^^^ attempt to negate `i8::MIN`, which would overflow
66

7-
error: could not evaluate constant pattern
8-
--> $DIR/const-eval-overflow-2.rs:15:9
9-
|
10-
LL | const NEG_NEG_128: i8 = -NEG_128;
11-
| --------------------- constant defined here
12-
...
13-
LL | NEG_NEG_128 => println!("A"),
14-
| ^^^^^^^^^^^
15-
16-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
178

189
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/ref_to_int_match.64bit.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
77
= help: this code performed an operation that depends on the underlying bytes representing a pointer
88
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
99

10-
error: could not evaluate constant pattern
11-
--> $DIR/ref_to_int_match.rs:7:14
12-
|
13-
LL | 10..=BAR => {},
14-
| ^^^
15-
...
16-
LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
17-
| -------------- constant defined here
18-
19-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2011

2112
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/const-eval/ref_to_int_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() {
44
let n: Int = 40;
55
match n {
66
0..=10 => {},
7-
10..=BAR => {}, //~ ERROR could not evaluate constant pattern
7+
10..=BAR => {}, // ok, `const` error already emitted
88
_ => {},
99
}
1010
}

tests/ui/consts/const_refs_to_static_fail_invalid.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn invalid() {
1111

1212
// This must be rejected here (or earlier), since it's not a valid `&bool`.
1313
match &true {
14-
C => {} //~ERROR: could not evaluate constant pattern
14+
C => {} // ok, `const` already emitted an error
1515
_ => {}
1616
}
1717
}
@@ -27,7 +27,7 @@ fn extern_() {
2727

2828
// This must be rejected here (or earlier), since the pattern cannot be read.
2929
match &0 {
30-
C => {} //~ERROR: could not evaluate constant pattern
30+
C => {} // ok, `const` already emitted an error
3131
_ => {}
3232
}
3333
}
@@ -42,7 +42,7 @@ fn mutable() {
4242
// This *must not build*, the constant we are matching against
4343
// could change its value!
4444
match &42 {
45-
C => {} //~ERROR: could not evaluate constant pattern
45+
C => {} // ok, `const` already emitted an error
4646
_ => {}
4747
}
4848
}

tests/ui/consts/const_refs_to_static_fail_invalid.stderr

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,6 @@ LL | const C: &i32 = unsafe { &S_MUT };
3131
HEX_DUMP
3232
}
3333

34-
error: could not evaluate constant pattern
35-
--> $DIR/const_refs_to_static_fail_invalid.rs:14:9
36-
|
37-
LL | const C: &bool = unsafe { std::mem::transmute(&S) };
38-
| -------------- constant defined here
39-
...
40-
LL | C => {}
41-
| ^
42-
43-
error: could not evaluate constant pattern
44-
--> $DIR/const_refs_to_static_fail_invalid.rs:30:9
45-
|
46-
LL | const C: &i8 = unsafe { &S };
47-
| ------------ constant defined here
48-
...
49-
LL | C => {}
50-
| ^
51-
52-
error: could not evaluate constant pattern
53-
--> $DIR/const_refs_to_static_fail_invalid.rs:45:9
54-
|
55-
LL | const C: &i32 = unsafe { &S_MUT };
56-
| ------------- constant defined here
57-
...
58-
LL | C => {}
59-
| ^
60-
61-
error: aborting due to 6 previous errors
34+
error: aborting due to 3 previous errors
6235

6336
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/invalid-inline-const-in-match-arm.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ error: could not evaluate constant pattern
1111
--> $DIR/invalid-inline-const-in-match-arm.rs:5:9
1212
|
1313
LL | const { (|| {})() } => {}
14-
| ^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^ could not evaluate constant
1515

1616
error: aborting due to 2 previous errors
1717

tests/ui/consts/issue-43105.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const NUM: u8 = xyz();
55

66
fn main() {
77
match 1 {
8-
NUM => unimplemented!(),
9-
//~^ ERROR could not evaluate constant pattern
8+
NUM => unimplemented!(), // ok, the `const` already emitted an error
109
_ => unimplemented!(),
1110
}
1211
}

tests/ui/consts/issue-43105.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ LL | const NUM: u8 = xyz();
66
|
77
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
88

9-
error: could not evaluate constant pattern
10-
--> $DIR/issue-43105.rs:8:9
11-
|
12-
LL | const NUM: u8 = xyz();
13-
| ------------- constant defined here
14-
...
15-
LL | NUM => unimplemented!(),
16-
| ^^^
17-
18-
error: aborting due to 2 previous errors
9+
error: aborting due to 1 previous error
1910

2011
For more information about this error, try `rustc --explain E0015`.

tests/ui/consts/issue-78655.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ const FOO: *const u32 = {
44
};
55

66
fn main() {
7-
let FOO = FOO;
8-
//~^ ERROR could not evaluate constant pattern
7+
let FOO = FOO; // ok, the `const` already emitted an error
98
}

tests/ui/consts/issue-78655.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ help: consider assigning a value
1111
LL | let x = 42;
1212
| ++++
1313

14-
error: could not evaluate constant pattern
15-
--> $DIR/issue-78655.rs:7:9
16-
|
17-
LL | const FOO: *const u32 = {
18-
| --------------------- constant defined here
19-
...
20-
LL | let FOO = FOO;
21-
| ^^^
22-
23-
error: aborting due to 2 previous errors
14+
error: aborting due to 1 previous error
2415

2516
For more information about this error, try `rustc --explain E0381`.

tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,14 @@ const U8_MUT3: &u8 = {
3737

3838
pub fn test(x: &[u8; 1]) -> bool {
3939
match x {
40-
SLICE_MUT => true,
41-
//~^ ERROR could not evaluate constant pattern
40+
SLICE_MUT => true, // ok, `const` error already emitted
4241
&[1..] => false,
4342
}
4443
}
4544

4645
pub fn test2(x: &u8) -> bool {
4746
match x {
48-
U8_MUT => true,
49-
//~^ ERROR could not evaluate constant pattern
47+
U8_MUT => true, // ok, `const` error already emitted
5048
&(1..) => false,
5149
}
5250
}
@@ -55,15 +53,13 @@ pub fn test2(x: &u8) -> bool {
5553
// the errors above otherwise stop compilation too early?
5654
pub fn test3(x: &u8) -> bool {
5755
match x {
58-
U8_MUT2 => true,
59-
//~^ ERROR could not evaluate constant pattern
56+
U8_MUT2 => true, // ok, `const` error already emitted
6057
&(1..) => false,
6158
}
6259
}
6360
pub fn test4(x: &u8) -> bool {
6461
match x {
65-
U8_MUT3 => true,
66-
//~^ ERROR could not evaluate constant pattern
62+
U8_MUT3 => true, // ok, `const` error already emitted
6763
&(1..) => false,
6864
}
6965
}

tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,6 @@ error[E0080]: evaluation of constant value failed
3737
LL | match static_cross_crate::OPT_ZERO {
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses mutable global memory
3939

40-
error: could not evaluate constant pattern
41-
--> $DIR/const_refers_to_static_cross_crate.rs:40:9
42-
|
43-
LL | const SLICE_MUT: &[u8; 1] = {
44-
| ------------------------- constant defined here
45-
...
46-
LL | SLICE_MUT => true,
47-
| ^^^^^^^^^
48-
49-
error: could not evaluate constant pattern
50-
--> $DIR/const_refers_to_static_cross_crate.rs:48:9
51-
|
52-
LL | const U8_MUT: &u8 = {
53-
| ----------------- constant defined here
54-
...
55-
LL | U8_MUT => true,
56-
| ^^^^^^
57-
58-
error: could not evaluate constant pattern
59-
--> $DIR/const_refers_to_static_cross_crate.rs:58:9
60-
|
61-
LL | const U8_MUT2: &u8 = {
62-
| ------------------ constant defined here
63-
...
64-
LL | U8_MUT2 => true,
65-
| ^^^^^^^
66-
67-
error: could not evaluate constant pattern
68-
--> $DIR/const_refers_to_static_cross_crate.rs:65:9
69-
|
70-
LL | const U8_MUT3: &u8 = {
71-
| ------------------ constant defined here
72-
...
73-
LL | U8_MUT3 => true,
74-
| ^^^^^^^
75-
76-
error: aborting due to 8 previous errors
40+
error: aborting due to 4 previous errors
7741

7842
For more information about this error, try `rustc --explain E0080`.

tests/ui/consts/missing_assoc_const_type.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ trait Range {
99

1010
struct TwoDigits;
1111
impl Range for TwoDigits {
12-
const FIRST: = 10;
13-
//~^ ERROR: missing type for `const` item
12+
const FIRST: = 10; //~ ERROR missing type for `const` item
1413
const LAST: u8 = 99;
1514
}
1615

1716
const fn digits(x: u8) -> usize {
1817
match x {
19-
TwoDigits::FIRST..=TwoDigits::LAST => 0, //~ ERROR: could not evaluate constant pattern
18+
TwoDigits::FIRST..=TwoDigits::LAST => 0, // ok, `const` error already emitted
2019
0..=9 | 100..=255 => panic!(),
2120
}
2221
}

tests/ui/consts/missing_assoc_const_type.stderr

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,5 @@ error: missing type for `const` item
44
LL | const FIRST: = 10;
55
| ^ help: provide a type for the associated constant: `u8`
66

7-
error: could not evaluate constant pattern
8-
--> $DIR/missing_assoc_const_type.rs:19:9
9-
|
10-
LL | trait Range {
11-
| -----------
12-
LL | const FIRST: u8;
13-
| --------------- constant defined here
14-
...
15-
LL | TwoDigits::FIRST..=TwoDigits::LAST => 0,
16-
| ^^^^^^^^^^^^^^^^
17-
18-
error: aborting due to 2 previous errors
7+
error: aborting due to 1 previous error
198

tests/ui/consts/transmute-size-mismatch-before-typeck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
fn main() {
77
match &b""[..] {
8-
ZST => {} //~ ERROR: could not evaluate constant pattern
8+
ZST => {} // ok, `const` error already emitted
99
}
1010
}
1111

tests/ui/consts/transmute-size-mismatch-before-typeck.stderr

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
77
= note: source type: `usize` (word size)
88
= note: target type: `&[u8]` (2 * word size)
99

10-
error: could not evaluate constant pattern
11-
--> $DIR/transmute-size-mismatch-before-typeck.rs:8:9
12-
|
13-
LL | ZST => {}
14-
| ^^^
15-
...
16-
LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
17-
| ---------------- constant defined here
18-
19-
error: aborting due to 2 previous errors
10+
error: aborting due to 1 previous error
2011

2112
For more information about this error, try `rustc --explain E0512`.

0 commit comments

Comments
 (0)