Skip to content

Commit 89b6885

Browse files
committed
Auto merge of rust-lang#133164 - RalfJung:promoted-oom, r=jieyouxu
interpret: do not ICE when a promoted fails with OOM Fixes rust-lang#130687 try-job: aarch64-apple try-job: dist-x86_64-linux
2 parents 7d40450 + c697434 commit 89b6885

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,20 @@ where
152152
let span = span.substitute_dummy(our_span);
153153
let err = mk(span, frames);
154154
let mut err = tcx.dcx().create_err(err);
155+
let can_be_spurious = matches!(error, InterpErrorKind::ResourceExhaustion(_));
155156

156157
let msg = error.diagnostic_message();
157158
error.add_args(&mut err);
158159

159160
// Use *our* span to label the interp error
160161
err.span_label(our_span, msg);
161-
ErrorHandled::Reported(err.emit().into(), span)
162+
let g = err.emit();
163+
let reported = if can_be_spurious {
164+
ReportedErrorInfo::spurious(g)
165+
} else {
166+
ReportedErrorInfo::from(g)
167+
};
168+
ErrorHandled::Reported(reported, span)
162169
}
163170
}
164171
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
588588
// const-eval will return "tainted" errors if e.g. the layout cannot
589589
// be computed as the type references non-existing names.
590590
// See <https://github.com/rust-lang/rust/issues/124348>.
591+
} else if reported.can_be_spurious() {
592+
// These errors can just sometimes happen, even when the expression
593+
// is nominally "infallible", e.g. when running out of memory.
591594
} else {
592595
// Looks like the const is not captured by `required_consts`, that's bad.
593596
span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,33 @@ impl ErrorHandled {
5959
pub struct ReportedErrorInfo {
6060
error: ErrorGuaranteed,
6161
is_tainted_by_errors: bool,
62+
/// Whether this is the kind of error that can sometimes occur, and sometimes not.
63+
/// Used for resource exhaustion errors.
64+
can_be_spurious: bool,
6265
}
6366

6467
impl ReportedErrorInfo {
6568
#[inline]
6669
pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo {
67-
ReportedErrorInfo { is_tainted_by_errors: true, error }
70+
ReportedErrorInfo { is_tainted_by_errors: true, can_be_spurious: false, error }
6871
}
72+
#[inline]
73+
pub fn spurious(error: ErrorGuaranteed) -> ReportedErrorInfo {
74+
ReportedErrorInfo { can_be_spurious: true, is_tainted_by_errors: false, error }
75+
}
76+
6977
pub fn is_tainted_by_errors(&self) -> bool {
7078
self.is_tainted_by_errors
7179
}
80+
pub fn can_be_spurious(&self) -> bool {
81+
self.can_be_spurious
82+
}
7283
}
7384

7485
impl From<ErrorGuaranteed> for ReportedErrorInfo {
7586
#[inline]
7687
fn from(error: ErrorGuaranteed) -> ReportedErrorInfo {
77-
ReportedErrorInfo { is_tainted_by_errors: false, error }
88+
ReportedErrorInfo { is_tainted_by_errors: false, can_be_spurious: false, error }
7889
}
7990
}
8091

tests/crashes/130687.rs

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Ensure we do not ICE when a promoted fails to evaluate due to running out of memory.
2+
//! Also see <https://github.com/rust-lang/rust/issues/130687>.
3+
4+
// Needs the max type size to be much bigger than the RAM people typically have.
5+
//@ only-64bit
6+
7+
pub struct Data([u8; (1 << 47) - 1]);
8+
const _: &'static Data = &Data([0; (1 << 47) - 1]);
9+
//~^ERROR: evaluation of constant value failed
10+
//~| tried to allocate more memory than available to compiler
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/promoted_running_out_of_memory_issue-130687.rs:8:32
3+
|
4+
LL | const _: &'static Data = &Data([0; (1 << 47) - 1]);
5+
| ^^^^^^^^^^^^^^^^^^ tried to allocate more memory than available to compiler
6+
7+
note: erroneous constant encountered
8+
--> $DIR/promoted_running_out_of_memory_issue-130687.rs:8:26
9+
|
10+
LL | const _: &'static Data = &Data([0; (1 << 47) - 1]);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)