Skip to content

Commit 58e49fa

Browse files
committed
Prevent stack overflow for deeply nested consts.
1 parent 1293d25 commit 58e49fa

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

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

33
use either::{Left, Right};
44

5+
use rustc_data_structures::stack::ensure_sufficient_stack;
56
use rustc_hir::def::DefKind;
67
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo};
78
use rustc_middle::mir::pretty::write_allocation_bytes;
@@ -305,10 +306,13 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
305306
// they do not have to behave "as if" they were evaluated at runtime.
306307
CompileTimeInterpreter::new(CanAccessStatics::from(is_static), CheckAlignment::Error),
307308
);
308-
eval_in_interpreter(ecx, cid, is_static)
309+
// Evaluating this MIR in the interpreter may in turn require evaluating constants to
310+
// allocations. As the recursion depends on what the user wrote, we need to protect ourselves
311+
// from stack overflow.
312+
ensure_sufficient_stack(|| eval_in_interpreter(ecx, cid, is_static))
309313
}
310314

311-
pub fn eval_in_interpreter<'mir, 'tcx>(
315+
fn eval_in_interpreter<'mir, 'tcx>(
312316
mut ecx: InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
313317
cid: GlobalId<'tcx>,
314318
is_static: bool,

0 commit comments

Comments
 (0)