1
Fork 0

Tweak unevaluated constant in pattern error

Silence errors that are implied by the errors in the `const` item definition.

Add a primary span label.
This commit is contained in:
Esteban Küber 2024-11-20 02:37:56 +00:00
parent c6205055e0
commit cc492edc9d
22 changed files with 37 additions and 165 deletions

View file

@ -92,6 +92,7 @@ mir_build_const_pattern_depends_on_generic_parameter =
constant pattern depends on a generic parameter constant pattern depends on a generic parameter
mir_build_could_not_eval_const_pattern = could not evaluate constant pattern mir_build_could_not_eval_const_pattern = could not evaluate constant pattern
.label = could not evaluate constant
mir_build_deref_raw_pointer_requires_unsafe = mir_build_deref_raw_pointer_requires_unsafe =
dereference of raw pointer is unsafe and requires unsafe block dereference of raw pointer is unsafe and requires unsafe block

View file

@ -702,6 +702,7 @@ pub(crate) struct ConstPatternDependsOnGenericParameter {
#[diag(mir_build_could_not_eval_const_pattern)] #[diag(mir_build_could_not_eval_const_pattern)]
pub(crate) struct CouldNotEvalConstPattern { pub(crate) struct CouldNotEvalConstPattern {
#[primary_span] #[primary_span]
#[label]
pub(crate) span: Span, pub(crate) span: Span,
} }

View file

@ -122,7 +122,16 @@ impl<'tcx> ConstToPat<'tcx> {
Ok(Ok(c)) => c, Ok(Ok(c)) => c,
Err(ErrorHandled::Reported(_, _)) => { Err(ErrorHandled::Reported(_, _)) => {
// Let's tell the use where this failing const occurs. // Let's tell the use where this failing const occurs.
let err = self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span }); let mut err =
self.tcx.dcx().create_err(CouldNotEvalConstPattern { span: self.span });
// We've emitted an error on the original const, it would be redundant to complain
// on its use as well.
if let ty::ConstKind::Unevaluated(uv) = self.c.kind()
&& let hir::def::DefKind::Const | hir::def::DefKind::AssocConst =
self.tcx.def_kind(uv.def)
{
err.downgrade_to_delayed_bug();
}
return self.mk_err(err, ty); return self.mk_err(err, ty);
} }
Err(ErrorHandled::TooGeneric(_)) => { Err(ErrorHandled::TooGeneric(_)) => {

View file

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

View file

@ -17,18 +17,7 @@ help: you might be missing a type parameter
LL | pub struct Opcode2<S>(&'a S); LL | pub struct Opcode2<S>(&'a S);
| +++ | +++
error: could not evaluate constant pattern error: aborting due to 2 previous errors
--> $DIR/ice-type-mismatch-when-copying-112824.rs:15:9
|
LL | impl Opcode2 {
| ------------
LL | pub const OP2: Opcode2 = Opcode2(Opcode(0x1));
| ---------------------- constant defined here
...
LL | Opcode2::OP2 => unimplemented!(),
| ^^^^^^^^^^^^
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0261, E0412. Some errors have detailed explanations: E0261, E0412.
For more information about an error, try `rustc --explain E0261`. For more information about an error, try `rustc --explain E0261`.

View file

@ -12,8 +12,7 @@ const NEG_NEG_128: i8 = -NEG_128; //~ ERROR constant
fn main() { fn main() {
match -128i8 { match -128i8 {
NEG_NEG_128 => println!("A"), NEG_NEG_128 => println!("A"), // ok, `const` error already emitted
//~^ ERROR could not evaluate constant pattern
_ => println!("B"), _ => println!("B"),
} }
} }

View file

@ -4,15 +4,6 @@ error[E0080]: evaluation of constant value failed
LL | const NEG_NEG_128: i8 = -NEG_128; LL | const NEG_NEG_128: i8 = -NEG_128;
| ^^^^^^^^ attempt to negate `i8::MIN`, which would overflow | ^^^^^^^^ attempt to negate `i8::MIN`, which would overflow
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/const-eval-overflow-2.rs:15:9
|
LL | const NEG_NEG_128: i8 = -NEG_128;
| --------------------- constant defined here
...
LL | NEG_NEG_128 => println!("A"),
| ^^^^^^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View file

@ -7,15 +7,6 @@ LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
= help: this code performed an operation that depends on the underlying bytes representing a pointer = help: this code performed an operation that depends on the underlying bytes representing a pointer
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/ref_to_int_match.rs:7:14
|
LL | 10..=BAR => {},
| ^^^
...
LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
| -------------- constant defined here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View file

@ -4,7 +4,7 @@ fn main() {
let n: Int = 40; let n: Int = 40;
match n { match n {
0..=10 => {}, 0..=10 => {},
10..=BAR => {}, //~ ERROR could not evaluate constant pattern 10..=BAR => {}, // ok, `const` error already emitted
_ => {}, _ => {},
} }
} }

View file

@ -11,7 +11,7 @@ fn invalid() {
// This must be rejected here (or earlier), since it's not a valid `&bool`. // This must be rejected here (or earlier), since it's not a valid `&bool`.
match &true { match &true {
C => {} //~ERROR: could not evaluate constant pattern C => {} // ok, `const` already emitted an error
_ => {} _ => {}
} }
} }
@ -27,7 +27,7 @@ fn extern_() {
// This must be rejected here (or earlier), since the pattern cannot be read. // This must be rejected here (or earlier), since the pattern cannot be read.
match &0 { match &0 {
C => {} //~ERROR: could not evaluate constant pattern C => {} // ok, `const` already emitted an error
_ => {} _ => {}
} }
} }
@ -42,7 +42,7 @@ fn mutable() {
// This *must not build*, the constant we are matching against // This *must not build*, the constant we are matching against
// could change its value! // could change its value!
match &42 { match &42 {
C => {} //~ERROR: could not evaluate constant pattern C => {} // ok, `const` already emitted an error
_ => {} _ => {}
} }
} }

View file

@ -31,33 +31,6 @@ LL | const C: &i32 = unsafe { &S_MUT };
HEX_DUMP HEX_DUMP
} }
error: could not evaluate constant pattern error: aborting due to 3 previous errors
--> $DIR/const_refs_to_static_fail_invalid.rs:14:9
|
LL | const C: &bool = unsafe { std::mem::transmute(&S) };
| -------------- constant defined here
...
LL | C => {}
| ^
error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:30:9
|
LL | const C: &i8 = unsafe { &S };
| ------------ constant defined here
...
LL | C => {}
| ^
error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:45:9
|
LL | const C: &i32 = unsafe { &S_MUT };
| ------------- constant defined here
...
LL | C => {}
| ^
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View file

@ -11,7 +11,7 @@ error: could not evaluate constant pattern
--> $DIR/invalid-inline-const-in-match-arm.rs:5:9 --> $DIR/invalid-inline-const-in-match-arm.rs:5:9
| |
LL | const { (|| {})() } => {} LL | const { (|| {})() } => {}
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^ could not evaluate constant
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -5,8 +5,7 @@ const NUM: u8 = xyz();
fn main() { fn main() {
match 1 { match 1 {
NUM => unimplemented!(), NUM => unimplemented!(), // ok, the `const` already emitted an error
//~^ ERROR could not evaluate constant pattern
_ => unimplemented!(), _ => unimplemented!(),
} }
} }

View file

@ -6,15 +6,6 @@ LL | const NUM: u8 = xyz();
| |
= note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: calls in constants are limited to constant functions, tuple structs and tuple variants
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/issue-43105.rs:8:9
|
LL | const NUM: u8 = xyz();
| ------------- constant defined here
...
LL | NUM => unimplemented!(),
| ^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0015`. For more information about this error, try `rustc --explain E0015`.

View file

@ -4,6 +4,5 @@ const FOO: *const u32 = {
}; };
fn main() { fn main() {
let FOO = FOO; let FOO = FOO; // ok, the `const` already emitted an error
//~^ ERROR could not evaluate constant pattern
} }

View file

@ -11,15 +11,6 @@ help: consider assigning a value
LL | let x = 42; LL | let x = 42;
| ++++ | ++++
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/issue-78655.rs:7:9
|
LL | const FOO: *const u32 = {
| --------------------- constant defined here
...
LL | let FOO = FOO;
| ^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0381`. For more information about this error, try `rustc --explain E0381`.

View file

@ -37,16 +37,14 @@ const U8_MUT3: &u8 = {
pub fn test(x: &[u8; 1]) -> bool { pub fn test(x: &[u8; 1]) -> bool {
match x { match x {
SLICE_MUT => true, SLICE_MUT => true, // ok, `const` error already emitted
//~^ ERROR could not evaluate constant pattern
&[1..] => false, &[1..] => false,
} }
} }
pub fn test2(x: &u8) -> bool { pub fn test2(x: &u8) -> bool {
match x { match x {
U8_MUT => true, U8_MUT => true, // ok, `const` error already emitted
//~^ ERROR could not evaluate constant pattern
&(1..) => false, &(1..) => false,
} }
} }
@ -55,15 +53,13 @@ pub fn test2(x: &u8) -> bool {
// the errors above otherwise stop compilation too early? // the errors above otherwise stop compilation too early?
pub fn test3(x: &u8) -> bool { pub fn test3(x: &u8) -> bool {
match x { match x {
U8_MUT2 => true, U8_MUT2 => true, // ok, `const` error already emitted
//~^ ERROR could not evaluate constant pattern
&(1..) => false, &(1..) => false,
} }
} }
pub fn test4(x: &u8) -> bool { pub fn test4(x: &u8) -> bool {
match x { match x {
U8_MUT3 => true, U8_MUT3 => true, // ok, `const` error already emitted
//~^ ERROR could not evaluate constant pattern
&(1..) => false, &(1..) => false,
} }
} }

View file

@ -37,42 +37,6 @@ error[E0080]: evaluation of constant value failed
LL | match static_cross_crate::OPT_ZERO { LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses mutable global memory | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses mutable global memory
error: could not evaluate constant pattern error: aborting due to 4 previous errors
--> $DIR/const_refers_to_static_cross_crate.rs:40:9
|
LL | const SLICE_MUT: &[u8; 1] = {
| ------------------------- constant defined here
...
LL | SLICE_MUT => true,
| ^^^^^^^^^
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:48:9
|
LL | const U8_MUT: &u8 = {
| ----------------- constant defined here
...
LL | U8_MUT => true,
| ^^^^^^
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:58:9
|
LL | const U8_MUT2: &u8 = {
| ------------------ constant defined here
...
LL | U8_MUT2 => true,
| ^^^^^^^
error: could not evaluate constant pattern
--> $DIR/const_refers_to_static_cross_crate.rs:65:9
|
LL | const U8_MUT3: &u8 = {
| ------------------ constant defined here
...
LL | U8_MUT3 => true,
| ^^^^^^^
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View file

@ -9,14 +9,13 @@ trait Range {
struct TwoDigits; struct TwoDigits;
impl Range for TwoDigits { impl Range for TwoDigits {
const FIRST: = 10; const FIRST: = 10; //~ ERROR missing type for `const` item
//~^ ERROR: missing type for `const` item
const LAST: u8 = 99; const LAST: u8 = 99;
} }
const fn digits(x: u8) -> usize { const fn digits(x: u8) -> usize {
match x { match x {
TwoDigits::FIRST..=TwoDigits::LAST => 0, //~ ERROR: could not evaluate constant pattern TwoDigits::FIRST..=TwoDigits::LAST => 0, // ok, `const` error already emitted
0..=9 | 100..=255 => panic!(), 0..=9 | 100..=255 => panic!(),
} }
} }

View file

@ -4,16 +4,5 @@ error: missing type for `const` item
LL | const FIRST: = 10; LL | const FIRST: = 10;
| ^ help: provide a type for the associated constant: `u8` | ^ help: provide a type for the associated constant: `u8`
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/missing_assoc_const_type.rs:19:9
|
LL | trait Range {
| -----------
LL | const FIRST: u8;
| --------------- constant defined here
...
LL | TwoDigits::FIRST..=TwoDigits::LAST => 0,
| ^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors

View file

@ -5,7 +5,7 @@
fn main() { fn main() {
match &b""[..] { match &b""[..] {
ZST => {} //~ ERROR: could not evaluate constant pattern ZST => {} // ok, `const` error already emitted
} }
} }

View file

@ -7,15 +7,6 @@ LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
= note: source type: `usize` (word size) = note: source type: `usize` (word size)
= note: target type: `&[u8]` (2 * word size) = note: target type: `&[u8]` (2 * word size)
error: could not evaluate constant pattern error: aborting due to 1 previous error
--> $DIR/transmute-size-mismatch-before-typeck.rs:8:9
|
LL | ZST => {}
| ^^^
...
LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
| ---------------- constant defined here
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0512`. For more information about this error, try `rustc --explain E0512`.