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(Limit::new(0), false);
18 let mut cx = InterpCx::new(tcx, rustc_span::DUMMY_SP, ParamEnv::reveal_all(), machine);
21 .allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
22 .expect("OOM: failed to allocate for uninit check");
24 if kind == InitKind::Zero {
27 std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
29 .expect("failed to write bytes for zero valid check");
32 let ot: OpTy<'_, _> = allocated.into();
34 // Assume that if it failed, it's a validation failure.
35 cx.validate_operand(&ot).is_ok()
37 let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
38 ty.might_permit_raw_init(&layout_cx, kind)