1 use crate::const_eval::CompileTimeInterpreter;
2 use crate::interpret::{InterpCx, MemoryKind, OpTy};
3 use rustc_middle::ty::layout::LayoutCx;
4 use rustc_middle::ty::{layout::TyAndLayout, ParamEnv, TyCtxt};
5 use rustc_session::Limit;
6 use rustc_target::abi::InitKind;
8 pub fn might_permit_raw_init<'tcx>(
10 ty: TyAndLayout<'tcx>,
13 let strict = tcx.sess.opts.unstable_opts.strict_init_checks;
16 let machine = CompileTimeInterpreter::new(
18 /*can_access_statics:*/ false,
19 /*check_alignment:*/ true,
22 let mut cx = InterpCx::new(tcx, rustc_span::DUMMY_SP, ParamEnv::reveal_all(), machine);
25 .allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
26 .expect("OOM: failed to allocate for uninit check");
28 if kind == InitKind::Zero {
31 std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
33 .expect("failed to write bytes for zero valid check");
36 let ot: OpTy<'_, _> = allocated.into();
38 // Assume that if it failed, it's a validation failure.
39 cx.validate_operand(&ot).is_ok()
41 let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
42 ty.might_permit_raw_init(&layout_cx, kind)