Skip to content

Commit b398fa5

Browse files
committed
Cleanup, move might_permit_raw_init to own file
1 parent ab4a80e commit b398fa5

File tree

5 files changed

+56
-52
lines changed

5 files changed

+56
-52
lines changed

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
673673
}
674674

675675
if intrinsic == sym::assert_zero_valid
676-
&& !rustc_const_eval::might_permit_raw_init(
676+
&& !rustc_const_eval::might_permit_raw_init::might_permit_raw_init(
677677
fx.tcx,
678678
source_info.span,
679679
layout,
@@ -691,7 +691,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
691691
}
692692

693693
if intrinsic == sym::assert_uninit_valid
694-
&& !rustc_const_eval::might_permit_raw_init(
694+
&& !rustc_const_eval::might_permit_raw_init::might_permit_raw_init(
695695
fx.tcx,
696696
source_info.span,
697697
layout,

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,21 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
546546
_ => None,
547547
});
548548
if let Some(intrinsic) = panic_intrinsic {
549+
use rustc_const_eval::might_permit_raw_init::might_permit_raw_init;
549550
use AssertIntrinsic::*;
551+
550552
let ty = instance.unwrap().substs.type_at(0);
551553
let layout = bx.layout_of(ty);
552554
let do_panic = match intrinsic {
553555
Inhabited => layout.abi.is_uninhabited(),
554-
ZeroValid => !rustc_const_eval::might_permit_raw_init(
556+
ZeroValid => !might_permit_raw_init(
555557
bx.tcx(),
556558
source_info.span,
557559
layout,
558560
strict_validity,
559561
InitKind::Zero,
560562
),
561-
UninitValid => !rustc_const_eval::might_permit_raw_init(
563+
UninitValid => !might_permit_raw_init(
562564
bx.tcx(),
563565
source_info.span,
564566
layout,

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use super::{
2222
Pointer,
2323
};
2424

25+
use crate::might_permit_raw_init::might_permit_raw_init;
26+
2527
mod caller_location;
2628
mod type_name;
2729

@@ -415,7 +417,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
415417
}
416418

417419
if intrinsic_name == sym::assert_zero_valid {
418-
let should_panic = !crate::might_permit_raw_init(
420+
let should_panic = !might_permit_raw_init(
419421
*self.tcx,
420422
self.cur_span(),
421423
layout,
@@ -435,7 +437,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
435437
}
436438

437439
if intrinsic_name == sym::assert_uninit_valid {
438-
let should_panic = !crate::might_permit_raw_init(
440+
let should_panic = !might_permit_raw_init(
439441
*self.tcx,
440442
self.cur_span(),
441443
layout,

compiler/rustc_const_eval/src/lib.rs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extern crate rustc_middle;
3333
pub mod const_eval;
3434
mod errors;
3535
pub mod interpret;
36+
pub mod might_permit_raw_init;
3637
pub mod transform;
3738
pub mod util;
3839

@@ -60,49 +61,3 @@ pub fn provide(providers: &mut Providers) {
6061
const_eval::deref_mir_constant(tcx, param_env, value)
6162
};
6263
}
63-
64-
use crate::const_eval::CompileTimeInterpreter;
65-
use crate::interpret::{InterpCx, MemoryKind, OpTy};
66-
use rustc_middle::ty::layout::LayoutCx;
67-
use rustc_middle::ty::{layout::TyAndLayout, ParamEnv, TyCtxt};
68-
use rustc_session::Limit;
69-
use rustc_span::Span;
70-
use rustc_target::abi::InitKind;
71-
72-
pub fn might_permit_raw_init<'tcx>(
73-
tcx: TyCtxt<'tcx>,
74-
root_span: Span,
75-
ty: TyAndLayout<'tcx>,
76-
strict: bool,
77-
kind: InitKind,
78-
) -> bool {
79-
if strict {
80-
let machine = CompileTimeInterpreter::new(Limit::new(0), false);
81-
82-
let mut cx = InterpCx::new(tcx, root_span, ParamEnv::reveal_all(), machine);
83-
84-
// We could panic here... Or we could just return "yeah it's valid whatever". Or let
85-
// codegen_panic_intrinsic return an error that halts compilation.
86-
// I'm not exactly sure *when* this can fail. OOM?
87-
let allocated = cx
88-
.allocate(ty, MemoryKind::Machine(const_eval::MemoryKind::Heap))
89-
.expect("failed to allocate for uninit check");
90-
91-
if kind == InitKind::Zero {
92-
// Again, unclear what to do here if it fails.
93-
cx.write_bytes_ptr(
94-
allocated.ptr,
95-
std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
96-
)
97-
.expect("failed to write bytes for zero valid check");
98-
}
99-
100-
let ot: OpTy<'_, _> = allocated.into();
101-
102-
// Assume that if it failed, it's a validation failure.
103-
cx.validate_operand(&ot).is_ok()
104-
} else {
105-
let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
106-
!ty.might_permit_raw_init(&layout_cx, kind)
107-
}
108-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_span::Span;
7+
use rustc_target::abi::InitKind;
8+
9+
pub fn might_permit_raw_init<'tcx>(
10+
tcx: TyCtxt<'tcx>,
11+
root_span: Span,
12+
ty: TyAndLayout<'tcx>,
13+
strict: bool,
14+
kind: InitKind,
15+
) -> bool {
16+
if strict {
17+
let machine = CompileTimeInterpreter::new(Limit::new(0), false);
18+
19+
let mut cx = InterpCx::new(tcx, root_span, ParamEnv::reveal_all(), machine);
20+
21+
// We could panic here... Or we could just return "yeah it's valid whatever". Or let
22+
// codegen_panic_intrinsic return an error that halts compilation.
23+
// I'm not exactly sure *when* this can fail. OOM?
24+
let allocated = cx
25+
.allocate(ty, MemoryKind::Machine(crate::const_eval::MemoryKind::Heap))
26+
.expect("failed to allocate for uninit check");
27+
28+
if kind == InitKind::Zero {
29+
// Again, unclear what to do here if it fails.
30+
cx.write_bytes_ptr(
31+
allocated.ptr,
32+
std::iter::repeat(0_u8).take(ty.layout.size().bytes_usize()),
33+
)
34+
.expect("failed to write bytes for zero valid check");
35+
}
36+
37+
let ot: OpTy<'_, _> = allocated.into();
38+
39+
// Assume that if it failed, it's a validation failure.
40+
cx.validate_operand(&ot).is_ok()
41+
} else {
42+
let layout_cx = LayoutCx { tcx, param_env: ParamEnv::reveal_all() };
43+
ty.might_permit_raw_init(&layout_cx, kind)
44+
}
45+
}

0 commit comments

Comments
 (0)