Skip to content

Commit b072cc3

Browse files
committed
Allow const eval failures if the cause is a type layout issue
1 parent c65b2dc commit b072cc3

File tree

6 files changed

+44
-14
lines changed

6 files changed

+44
-14
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::mem;
22

33
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
44
use rustc_hir::CRATE_HIR_ID;
5-
use rustc_middle::mir::interpret::Provenance;
5+
use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo};
66
use rustc_middle::mir::AssertKind;
77
use rustc_middle::query::TyCtxtAt;
88
use rustc_middle::ty::TyCtxt;
@@ -138,9 +138,10 @@ where
138138
ErrorHandled::TooGeneric(span.unwrap_or(DUMMY_SP))
139139
}
140140
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar, span.unwrap_or(DUMMY_SP)),
141-
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {
142-
ErrorHandled::Reported(guar.into(), span.unwrap_or(DUMMY_SP))
143-
}
141+
err_inval!(Layout(LayoutError::ReferencesError(guar))) => ErrorHandled::Reported(
142+
ReportedErrorInfo::tainted_by_errors(guar),
143+
span.unwrap_or(DUMMY_SP),
144+
),
144145
// Report remaining errors.
145146
_ => {
146147
let (our_span, frames) = get_span_and_frames();

compiler/rustc_const_eval/src/interpret/eval_context.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1179,9 +1179,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11791179
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
11801180
M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
11811181
let const_val = val.eval(*ecx.tcx, ecx.param_env, span).map_err(|err| {
1182-
if M::ALL_CONSTS_ARE_PRECHECKED && !matches!(err, ErrorHandled::TooGeneric(..)) {
1183-
// Looks like the const is not captued by `required_consts`, that's bad.
1184-
bug!("interpret const eval failure of {val:?} which is not in required_consts");
1182+
if M::ALL_CONSTS_ARE_PRECHECKED {
1183+
match err {
1184+
ErrorHandled::TooGeneric(..) => {},
1185+
ErrorHandled::Reported(reported, span) => {
1186+
if !reported.is_tainted_by_errors() {
1187+
// Looks like the const is not captued by `required_consts`, that's bad.
1188+
span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
1189+
}
1190+
}
1191+
}
11851192
}
11861193
err.emit_note(*ecx.tcx);
11871194
err

compiler/rustc_middle/src/mir/interpret/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl ReportedErrorInfo {
6363
pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo {
6464
ReportedErrorInfo { is_tainted_by_errors: true, error }
6565
}
66+
pub fn is_tainted_by_errors(&self) -> bool {
67+
self.is_tainted_by_errors
68+
}
6669
}
6770

6871
impl From<ErrorGuaranteed> for ReportedErrorInfo {

tests/crashes/124348.rs

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! ICE test #124348
2+
//! We should not be running const eval if the layout has errors.
3+
4+
enum Eek {
5+
TheConst,
6+
UnusedByTheConst(Sum),
7+
//~^ ERROR cannot find type `Sum` in this scope
8+
}
9+
10+
const EEK_ZERO: &[Eek] = &[];
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0412]: cannot find type `Sum` in this scope
2+
--> $DIR/erroneous_type_in_const_return_value.rs:6:22
3+
|
4+
LL | UnusedByTheConst(Sum),
5+
| ^^^ not found in this scope
6+
|
7+
help: consider importing this trait
8+
|
9+
LL + use std::iter::Sum;
10+
|
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)