Skip to content

Commit cc492ed

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 c620505 commit cc492ed

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
@@ -702,6 +702,7 @@ pub(crate) struct ConstPatternDependsOnGenericParameter {
702702
#[diag(mir_build_could_not_eval_const_pattern)]
703703
pub(crate) struct CouldNotEvalConstPattern {
704704
#[primary_span]
705+
#[label]
705706
pub(crate) span: Span,
706707
}
707708

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
@@ -122,7 +122,16 @@ impl<'tcx> ConstToPat<'tcx> {
122122
Ok(Ok(c)) => c,
123123
Err(ErrorHandled::Reported(_, _)) => {
124124
// Let's tell the use where this failing const occurs.
125-
let err = self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
125+
let mut err =
126+
self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
127+
// We've emitted an error on the original const, it would be redundant to complain
128+
// on its use as well.
129+
if let ty::ConstKind::Unevaluated(uv) = self.c.kind()
130+
&& let hir::def::DefKind::Const | hir::def::DefKind::AssocConst =
131+
self.tcx.def_kind(uv.def)
132+
{
133+
err.downgrade_to_delayed_bug();
134+
}
126135
return self.mk_err(err, ty);
127136
}
128137
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
}

0 commit comments

Comments
 (0)