From cc492edc9d65125a25a42446bbffefb8087dedf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 20 Nov 2024 02:37:56 +0000 Subject: [PATCH] Tweak unevaluated constant in pattern error Silence errors that are implied by the errors in the `const` item definition. Add a primary span label. --- compiler/rustc_mir_build/messages.ftl | 1 + compiler/rustc_mir_build/src/errors.rs | 1 + .../src/thir/pattern/const_to_pat.rs | 11 +++++- .../ice-type-mismatch-when-copying-112824.rs | 3 +- ...e-type-mismatch-when-copying-112824.stderr | 13 +------ .../const-eval/const-eval-overflow-2.rs | 3 +- .../const-eval/const-eval-overflow-2.stderr | 11 +----- .../const-eval/ref_to_int_match.64bit.stderr | 11 +----- .../ui/consts/const-eval/ref_to_int_match.rs | 2 +- .../const_refs_to_static_fail_invalid.rs | 6 +-- .../const_refs_to_static_fail_invalid.stderr | 29 +------------- .../invalid-inline-const-in-match-arm.stderr | 2 +- tests/ui/consts/issue-43105.rs | 3 +- tests/ui/consts/issue-43105.stderr | 11 +----- tests/ui/consts/issue-78655.rs | 3 +- tests/ui/consts/issue-78655.stderr | 11 +----- .../const_refers_to_static_cross_crate.rs | 12 ++---- .../const_refers_to_static_cross_crate.stderr | 38 +------------------ tests/ui/consts/missing_assoc_const_type.rs | 5 +-- .../ui/consts/missing_assoc_const_type.stderr | 13 +------ .../transmute-size-mismatch-before-typeck.rs | 2 +- ...ansmute-size-mismatch-before-typeck.stderr | 11 +----- 22 files changed, 37 insertions(+), 165 deletions(-) diff --git a/compiler/rustc_mir_build/messages.ftl b/compiler/rustc_mir_build/messages.ftl index fdefc0bfd19..ad2f13f6d21 100644 --- a/compiler/rustc_mir_build/messages.ftl +++ b/compiler/rustc_mir_build/messages.ftl @@ -92,6 +92,7 @@ mir_build_const_pattern_depends_on_generic_parameter = constant pattern depends on a generic parameter mir_build_could_not_eval_const_pattern = could not evaluate constant pattern + .label = could not evaluate constant mir_build_deref_raw_pointer_requires_unsafe = dereference of raw pointer is unsafe and requires unsafe block diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 58487a48184..226c469b7d7 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -702,6 +702,7 @@ pub(crate) struct ConstPatternDependsOnGenericParameter { #[diag(mir_build_could_not_eval_const_pattern)] pub(crate) struct CouldNotEvalConstPattern { #[primary_span] + #[label] pub(crate) span: Span, } diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index aa98874654e..3690459bf51 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -122,7 +122,16 @@ impl<'tcx> ConstToPat<'tcx> { Ok(Ok(c)) => c, Err(ErrorHandled::Reported(_, _)) => { // 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); } Err(ErrorHandled::TooGeneric(_)) => { diff --git a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs index 09f7e2ba5b1..270496d45a6 100644 --- a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs +++ b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs @@ -12,8 +12,7 @@ impl Opcode2 { pub fn example2(msg_type: Opcode2) -> impl FnMut(&[u8]) { move |i| match msg_type { - Opcode2::OP2 => unimplemented!(), - //~^ ERROR: could not evaluate constant pattern + Opcode2::OP2 => unimplemented!(), // ok, `const` already emitted an error } } diff --git a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr index 9cce6fee55a..d95a8861230 100644 --- a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr +++ b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr @@ -17,18 +17,7 @@ help: you might be missing a type parameter LL | pub struct Opcode2(&'a S); | +++ -error: could not evaluate constant pattern - --> $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 +error: aborting due to 2 previous errors Some errors have detailed explanations: E0261, E0412. For more information about an error, try `rustc --explain E0261`. diff --git a/tests/ui/consts/const-eval/const-eval-overflow-2.rs b/tests/ui/consts/const-eval/const-eval-overflow-2.rs index c19a0c443ec..bae8a7ce243 100644 --- a/tests/ui/consts/const-eval/const-eval-overflow-2.rs +++ b/tests/ui/consts/const-eval/const-eval-overflow-2.rs @@ -12,8 +12,7 @@ const NEG_NEG_128: i8 = -NEG_128; //~ ERROR constant fn main() { match -128i8 { - NEG_NEG_128 => println!("A"), - //~^ ERROR could not evaluate constant pattern + NEG_NEG_128 => println!("A"), // ok, `const` error already emitted _ => println!("B"), } } diff --git a/tests/ui/consts/const-eval/const-eval-overflow-2.stderr b/tests/ui/consts/const-eval/const-eval-overflow-2.stderr index 66c54af4f83..5599ff931e8 100644 --- a/tests/ui/consts/const-eval/const-eval-overflow-2.stderr +++ b/tests/ui/consts/const-eval/const-eval-overflow-2.stderr @@ -4,15 +4,6 @@ error[E0080]: evaluation of constant value failed LL | const NEG_NEG_128: i8 = -NEG_128; | ^^^^^^^^ attempt to negate `i8::MIN`, which would overflow -error: could not evaluate constant pattern - --> $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 +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/const-eval/ref_to_int_match.64bit.stderr b/tests/ui/consts/const-eval/ref_to_int_match.64bit.stderr index b1c6cfd3246..18935626af1 100644 --- a/tests/ui/consts/const-eval/ref_to_int_match.64bit.stderr +++ b/tests/ui/consts/const-eval/ref_to_int_match.64bit.stderr @@ -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: the absolute address of a pointer is not known at compile-time, so such operations are not supported -error: could not evaluate constant pattern - --> $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 +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/const-eval/ref_to_int_match.rs b/tests/ui/consts/const-eval/ref_to_int_match.rs index c627ad97bb0..be9420e0215 100644 --- a/tests/ui/consts/const-eval/ref_to_int_match.rs +++ b/tests/ui/consts/const-eval/ref_to_int_match.rs @@ -4,7 +4,7 @@ fn main() { let n: Int = 40; match n { 0..=10 => {}, - 10..=BAR => {}, //~ ERROR could not evaluate constant pattern + 10..=BAR => {}, // ok, `const` error already emitted _ => {}, } } diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.rs b/tests/ui/consts/const_refs_to_static_fail_invalid.rs index a160862a0fa..aa101cf9d8a 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.rs +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.rs @@ -11,7 +11,7 @@ fn invalid() { // This must be rejected here (or earlier), since it's not a valid `&bool`. 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. 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 // could change its value! match &42 { - C => {} //~ERROR: could not evaluate constant pattern + C => {} // ok, `const` already emitted an error _ => {} } } diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr index 9f2633a64d5..c9d5cb60bf7 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr @@ -31,33 +31,6 @@ LL | const C: &i32 = unsafe { &S_MUT }; HEX_DUMP } -error: could not evaluate constant pattern - --> $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 +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/invalid-inline-const-in-match-arm.stderr b/tests/ui/consts/invalid-inline-const-in-match-arm.stderr index 2e48837bdcd..b22f99f40d3 100644 --- a/tests/ui/consts/invalid-inline-const-in-match-arm.stderr +++ b/tests/ui/consts/invalid-inline-const-in-match-arm.stderr @@ -11,7 +11,7 @@ error: could not evaluate constant pattern --> $DIR/invalid-inline-const-in-match-arm.rs:5:9 | LL | const { (|| {})() } => {} - | ^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^ could not evaluate constant error: aborting due to 2 previous errors diff --git a/tests/ui/consts/issue-43105.rs b/tests/ui/consts/issue-43105.rs index 20b78d64209..a4ee34c0532 100644 --- a/tests/ui/consts/issue-43105.rs +++ b/tests/ui/consts/issue-43105.rs @@ -5,8 +5,7 @@ const NUM: u8 = xyz(); fn main() { match 1 { - NUM => unimplemented!(), - //~^ ERROR could not evaluate constant pattern + NUM => unimplemented!(), // ok, the `const` already emitted an error _ => unimplemented!(), } } diff --git a/tests/ui/consts/issue-43105.stderr b/tests/ui/consts/issue-43105.stderr index 4c59bdd4520..0e08feb58de 100644 --- a/tests/ui/consts/issue-43105.stderr +++ b/tests/ui/consts/issue-43105.stderr @@ -6,15 +6,6 @@ LL | const NUM: u8 = xyz(); | = note: calls in constants are limited to constant functions, tuple structs and tuple variants -error: could not evaluate constant pattern - --> $DIR/issue-43105.rs:8:9 - | -LL | const NUM: u8 = xyz(); - | ------------- constant defined here -... -LL | NUM => unimplemented!(), - | ^^^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0015`. diff --git a/tests/ui/consts/issue-78655.rs b/tests/ui/consts/issue-78655.rs index cd95ee32c60..b0f862e8418 100644 --- a/tests/ui/consts/issue-78655.rs +++ b/tests/ui/consts/issue-78655.rs @@ -4,6 +4,5 @@ const FOO: *const u32 = { }; fn main() { - let FOO = FOO; - //~^ ERROR could not evaluate constant pattern + let FOO = FOO; // ok, the `const` already emitted an error } diff --git a/tests/ui/consts/issue-78655.stderr b/tests/ui/consts/issue-78655.stderr index 5d684227be8..6a93c1a8cec 100644 --- a/tests/ui/consts/issue-78655.stderr +++ b/tests/ui/consts/issue-78655.stderr @@ -11,15 +11,6 @@ help: consider assigning a value LL | let x = 42; | ++++ -error: could not evaluate constant pattern - --> $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 +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0381`. diff --git a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs index a6d75658c75..facb21a04ef 100644 --- a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs +++ b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs @@ -37,16 +37,14 @@ const U8_MUT3: &u8 = { pub fn test(x: &[u8; 1]) -> bool { match x { - SLICE_MUT => true, - //~^ ERROR could not evaluate constant pattern + SLICE_MUT => true, // ok, `const` error already emitted &[1..] => false, } } pub fn test2(x: &u8) -> bool { match x { - U8_MUT => true, - //~^ ERROR could not evaluate constant pattern + U8_MUT => true, // ok, `const` error already emitted &(1..) => false, } } @@ -55,15 +53,13 @@ pub fn test2(x: &u8) -> bool { // the errors above otherwise stop compilation too early? pub fn test3(x: &u8) -> bool { match x { - U8_MUT2 => true, - //~^ ERROR could not evaluate constant pattern + U8_MUT2 => true, // ok, `const` error already emitted &(1..) => false, } } pub fn test4(x: &u8) -> bool { match x { - U8_MUT3 => true, - //~^ ERROR could not evaluate constant pattern + U8_MUT3 => true, // ok, `const` error already emitted &(1..) => false, } } diff --git a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr index 513f456bdf4..8f8271cce38 100644 --- a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr +++ b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.stderr @@ -37,42 +37,6 @@ error[E0080]: evaluation of constant value failed LL | match static_cross_crate::OPT_ZERO { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constant accesses mutable global memory -error: could not evaluate constant pattern - --> $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 +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/missing_assoc_const_type.rs b/tests/ui/consts/missing_assoc_const_type.rs index 633998e9bc1..61042bfa96d 100644 --- a/tests/ui/consts/missing_assoc_const_type.rs +++ b/tests/ui/consts/missing_assoc_const_type.rs @@ -9,14 +9,13 @@ trait Range { struct TwoDigits; impl Range for TwoDigits { - const FIRST: = 10; - //~^ ERROR: missing type for `const` item + const FIRST: = 10; //~ ERROR missing type for `const` item const LAST: u8 = 99; } const fn digits(x: u8) -> usize { 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!(), } } diff --git a/tests/ui/consts/missing_assoc_const_type.stderr b/tests/ui/consts/missing_assoc_const_type.stderr index 8123a43ef74..28af1f0f321 100644 --- a/tests/ui/consts/missing_assoc_const_type.stderr +++ b/tests/ui/consts/missing_assoc_const_type.stderr @@ -4,16 +4,5 @@ error: missing type for `const` item LL | const FIRST: = 10; | ^ help: provide a type for the associated constant: `u8` -error: could not evaluate constant pattern - --> $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 +error: aborting due to 1 previous error diff --git a/tests/ui/consts/transmute-size-mismatch-before-typeck.rs b/tests/ui/consts/transmute-size-mismatch-before-typeck.rs index 44eac5b16cc..ffb143da2d4 100644 --- a/tests/ui/consts/transmute-size-mismatch-before-typeck.rs +++ b/tests/ui/consts/transmute-size-mismatch-before-typeck.rs @@ -5,7 +5,7 @@ fn main() { match &b""[..] { - ZST => {} //~ ERROR: could not evaluate constant pattern + ZST => {} // ok, `const` error already emitted } } diff --git a/tests/ui/consts/transmute-size-mismatch-before-typeck.stderr b/tests/ui/consts/transmute-size-mismatch-before-typeck.stderr index 79f59ba865d..6bc7e7203aa 100644 --- a/tests/ui/consts/transmute-size-mismatch-before-typeck.stderr +++ b/tests/ui/consts/transmute-size-mismatch-before-typeck.stderr @@ -7,15 +7,6 @@ LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; = note: source type: `usize` (word size) = note: target type: `&[u8]` (2 * word size) -error: could not evaluate constant pattern - --> $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 +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0512`.