]>
Commit | Line | Data |
---|---|---|
064997fb FG |
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; | |
7 | ||
8 | pub fn might_permit_raw_init<'tcx>( | |
9 | tcx: TyCtxt<'tcx>, | |
10 | ty: TyAndLayout<'tcx>, | |
11 | kind: InitKind, | |
12 | ) -> bool { | |
13 | let strict = tcx.sess.opts.unstable_opts.strict_init_checks; | |
14 | ||
15 | if strict { | |
16 | let machine = CompileTimeInterpreter::new(Limit::new(0), false); | |
17 | ||
18 | let mut cx = InterpCx::new(tcx, rustc_span::DUMMY_SP, ParamEnv::reveal_all(), machine); | |
19 | ||
20 | let allocated = cx | |
21 | .allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap)) | |
22 | .expect("OOM: failed to allocate for uninit check"); | |
23 | ||
24 | if kind == InitKind::Zero { | |
25 | cx.write_bytes_ptr( | |
26 | allocated.ptr, | |
27 | std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()), | |
28 | ) | |
29 | .expect("failed to write bytes for zero valid check"); | |
30 | } | |
31 | ||
32 | let ot: OpTy<'_, _> = allocated.into(); | |
33 | ||
34 | // Assume that if it failed, it's a validation failure. | |
35 | cx.validate_operand(&ot).is_ok() | |
36 | } else { | |
37 | let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() }; | |
38 | ty.might_permit_raw_init(&layout_cx, kind) | |
39 | } | |
40 | } |