Skip to content

Commit

Permalink
Prevent stack overflow for deeply nested consts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Dec 23, 2023
1 parent 1293d25 commit 58e49fa
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem;

use either::{Left, Right};

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

pub fn eval_in_interpreter<'mir, 'tcx>(
fn eval_in_interpreter<'mir, 'tcx>(
mut ecx: InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>,
cid: GlobalId<'tcx>,
is_static: bool,
Expand Down

0 comments on commit 58e49fa

Please sign in to comment.