Change:
<!-- [31mICE[0m: Rustc '' 'error: internal compiler error: interpret const eval failure of Unevaluated(UnevaluatedConst { def: DefId(0:11 ~ ice[ec8e]::EEKZERO), args: [], promoted: Some(promoted[0]) }, &'{erased} [Eek; 0usize]) which is not in requiredconsts', 'error: internal compiler error: interpret const eval failure of Unevaluated(UnevaluatedConst { def: DefId(0:11 ~ ice[ec8e]::EEKZERO), args: [], promoted: Some(promoted[0]) }, &'{erased} [Eek; 0usize]) which is not in requiredconsts' File: --auto-reduced (treereduce-rust): &lt;details&lt;summary&lt;strongoriginal code&lt;/strong&lt;/summary&lt;poriginal: </p</detailsVersion information ` Command: <!-- Include a backtrace in the code block by setting in your environment. E.g. . --<details<summary<strongProgram output</strong</summary<p</p</details<!-- query stack: 6 | const EEKZERO: &[Eek] = &[]; [evaltoallocationraw] const-evaluating + checking [evaltoconstvalueraw] simplifying constant for the type system [analysis] running analysis passes on this crate --
cc
Caused by running the interpreter on code that doesn't even pass name resolution. Not sure where to fix this, but probably not in the interpreter.^^ Cc
this is a promoted failing to compile because it gets the layout of this broken type, which gives us back an . I'm certain we can prevent const eval from running by checking such layouts earlier in the MIR pipeline, but the fix isn't clear to me yet.
The issue is that in the sanity check we only know it's an , we don't know whether it was a typeck error on a const-eval error. So we can't really distinguish these cases. If this is a , we could probably fix it by tweaking the error handling . But there probably are similar cases where a promoted fails to evaluate due to a SizeOverflow and then we have to report the error somewhere. The only option I see right now is to add another variant to (or a flag in ) to track whether this was a failure inside the constant itself or a failure in something the constant referenced. But this distinction isn't always very clear anyway... it would basically have to be "is this an error that can occur even for supposedly infallible operations like ", i.e., match exactly the usecase of the const-eval / promoted sanity check. Which is not great.
We already got the flag that we can reuse for that

Discussion:
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg}; use rustc_hir::CRATE_HIR_ID; <del> use rustc_middle::mir::interpret::Provenance; </del> <ins> use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo}; </ins> use rustc_middle::mir::AssertKind; use rustc_middle::query::TyCtxtAt; use rustc_middle::ty::TyCtxt;

ErrorHandled::TooGeneric(span) } err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar, span), <del> err_inval!(Layout(LayoutError::ReferencesError(guar))) => { ErrorHandled::Reported(guar.into(), span) } </del> <ins> err_inval!(Layout(LayoutError::ReferencesError(guar))) => ErrorHandled::Reported( ReportedErrorInfo::tainted_by_errors(guar), span, ), </ins> // Report remaining errors. _ => { let (our_span, frames) = get_span_and_frames();

) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> { M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| { let const_val = val.eval(*ecx.tcx, ecx.param_env, span).map_err(|err| { <del> if M::ALL_CONSTS_ARE_PRECHECKED && !matches!(err, ErrorHandled::TooGeneric(..)) { // Looks like the const is not captued by `required_consts`, that's bad. bug!("interpret const eval failure of {val:?} which is not in required_consts"); </del> <ins> if M::ALL_CONSTS_ARE_PRECHECKED { match err { ErrorHandled::TooGeneric(..) => {}, ErrorHandled::Reported(reported, span) => { if reported.is_tainted_by_errors() { // const-eval will return "tainted" errors if e.g. the layout cannot // be computed as the type references non-existing names. // See <https://github.com/rust-lang/rust/issues/124348>. } else { // Looks like the const is not captued by `required_consts`, that's bad. span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts"); } } } </ins> } err.emit_note(*ecx.tcx); err

pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo { ReportedErrorInfo { is_tainted_by_errors: true, error } } <ins> pub fn is_tainted_by_errors(&self) -> bool { self.is_tainted_by_errors } </ins> } impl From<ErrorGuaranteed> for ReportedErrorInfo {

<del> //@ known-bug: #124348 enum Eek { TheConst, UnusedByTheConst(Sum), } const EEK_ZERO: &[Eek] = &[]; </del>

<ins> //! ICE test #124348 //! We should not be running const eval if the layout has errors. enum Eek { TheConst, UnusedByTheConst(Sum), //~^ ERROR cannot find type `Sum` in this scope } const EEK_ZERO: &[Eek] = &[]; fn main() {} </ins>

<ins> error[E0412]: cannot find type `Sum` in this scope --> $DIR/erroneous_type_in_const_return_value.rs:6:22 | LL |     UnusedByTheConst(Sum), |                      ^^^ not found in this scope | help: consider importing this trait | LL + use std::iter::Sum; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0412`. </ins>

<ins> //! ICE test #124348 //! We should not be running const eval if the layout has errors. enum Eek { TheConst, UnusedByTheConst(Sum), //~^ ERROR cannot find type `Sum` in this scope } const fn foo() { let x: &'static [Eek] = &[]; } fn main() {} </ins>

<ins> error[E0412]: cannot find type `Sum` in this scope --> $DIR/erroneous_type_in_promoted.rs:6:22 | LL |     UnusedByTheConst(Sum), |                      ^^^ not found in this scope | help: consider importing this trait | LL + use std::iter::Sum; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0412`. </ins>