Skip to content

Commit

Permalink
Cleanup, move might_permit_raw_init to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Jul 9, 2022
1 parent ab4a80e commit b398fa5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 52 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
}

if intrinsic == sym::assert_zero_valid
&& !rustc_const_eval::might_permit_raw_init(
&& !rustc_const_eval::might_permit_raw_init::might_permit_raw_init(
fx.tcx,
source_info.span,
layout,
Expand All @@ -691,7 +691,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
}

if intrinsic == sym::assert_uninit_valid
&& !rustc_const_eval::might_permit_raw_init(
&& !rustc_const_eval::might_permit_raw_init::might_permit_raw_init(
fx.tcx,
source_info.span,
layout,
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,19 +546,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
_ => None,
});
if let Some(intrinsic) = panic_intrinsic {
use rustc_const_eval::might_permit_raw_init::might_permit_raw_init;
use AssertIntrinsic::*;

let ty = instance.unwrap().substs.type_at(0);
let layout = bx.layout_of(ty);
let do_panic = match intrinsic {
Inhabited => layout.abi.is_uninhabited(),
ZeroValid => !rustc_const_eval::might_permit_raw_init(
ZeroValid => !might_permit_raw_init(
bx.tcx(),
source_info.span,
layout,
strict_validity,
InitKind::Zero,
),
UninitValid => !rustc_const_eval::might_permit_raw_init(
UninitValid => !might_permit_raw_init(
bx.tcx(),
source_info.span,
layout,
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_const_eval/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use super::{
Pointer,
};

use crate::might_permit_raw_init::might_permit_raw_init;

mod caller_location;
mod type_name;

Expand Down Expand Up @@ -415,7 +417,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}

if intrinsic_name == sym::assert_zero_valid {
let should_panic = !crate::might_permit_raw_init(
let should_panic = !might_permit_raw_init(
*self.tcx,
self.cur_span(),
layout,
Expand All @@ -435,7 +437,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
}

if intrinsic_name == sym::assert_uninit_valid {
let should_panic = !crate::might_permit_raw_init(
let should_panic = !might_permit_raw_init(
*self.tcx,
self.cur_span(),
layout,
Expand Down
47 changes: 1 addition & 46 deletions compiler/rustc_const_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extern crate rustc_middle;
pub mod const_eval;
mod errors;
pub mod interpret;
pub mod might_permit_raw_init;
pub mod transform;
pub mod util;

Expand Down Expand Up @@ -60,49 +61,3 @@ pub fn provide(providers: &mut Providers) {
const_eval::deref_mir_constant(tcx, param_env, value)
};
}

use crate::const_eval::CompileTimeInterpreter;
use crate::interpret::{InterpCx, MemoryKind, OpTy};
use rustc_middle::ty::layout::LayoutCx;
use rustc_middle::ty::{layout::TyAndLayout, ParamEnv, TyCtxt};
use rustc_session::Limit;
use rustc_span::Span;
use rustc_target::abi::InitKind;

pub fn might_permit_raw_init<'tcx>(
tcx: TyCtxt<'tcx>,
root_span: Span,
ty: TyAndLayout<'tcx>,
strict: bool,
kind: InitKind,
) -> bool {
if strict {
let machine = CompileTimeInterpreter::new(Limit::new(0), false);

let mut cx = InterpCx::new(tcx, root_span, ParamEnv::reveal_all(), machine);

// We could panic here... Or we could just return "yeah it's valid whatever". Or let
// codegen_panic_intrinsic return an error that halts compilation.
// I'm not exactly sure *when* this can fail. OOM?
let allocated = cx
.allocate(ty, MemoryKind::Machine(const_eval::MemoryKind::Heap))
.expect("failed to allocate for uninit check");

if kind == InitKind::Zero {
// Again, unclear what to do here if it fails.
cx.write_bytes_ptr(
allocated.ptr,
std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
)
.expect("failed to write bytes for zero valid check");
}

let ot: OpTy<'_, _> = allocated.into();

// Assume that if it failed, it's a validation failure.
cx.validate_operand(&ot).is_ok()
} else {
let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
!ty.might_permit_raw_init(&layout_cx, kind)
}
}
45 changes: 45 additions & 0 deletions compiler/rustc_const_eval/src/might_permit_raw_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::const_eval::CompileTimeInterpreter;
use crate::interpret::{InterpCx, MemoryKind, OpTy};
use rustc_middle::ty::layout::LayoutCx;
use rustc_middle::ty::{layout::TyAndLayout, ParamEnv, TyCtxt};
use rustc_session::Limit;
use rustc_span::Span;
use rustc_target::abi::InitKind;

pub fn might_permit_raw_init<'tcx>(
tcx: TyCtxt<'tcx>,
root_span: Span,
ty: TyAndLayout<'tcx>,
strict: bool,
kind: InitKind,
) -> bool {
if strict {
let machine = CompileTimeInterpreter::new(Limit::new(0), false);

let mut cx = InterpCx::new(tcx, root_span, ParamEnv::reveal_all(), machine);

// We could panic here... Or we could just return "yeah it's valid whatever". Or let
// codegen_panic_intrinsic return an error that halts compilation.
// I'm not exactly sure *when* this can fail. OOM?
let allocated = cx
.allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
.expect("failed to allocate for uninit check");

if kind == InitKind::Zero {
// Again, unclear what to do here if it fails.
cx.write_bytes_ptr(
allocated.ptr,
std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
)
.expect("failed to write bytes for zero valid check");
}

let ot: OpTy<'_, _> = allocated.into();

// Assume that if it failed, it's a validation failure.
cx.validate_operand(&ot).is_ok()
} else {
let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
ty.might_permit_raw_init(&layout_cx, kind)
}
}

0 comments on commit b398fa5

Please sign in to comment.