1- use rustc_middle:: ty:: layout:: { InitKind , LayoutCx , LayoutError , LayoutOf , TyAndLayout } ;
1+ use rustc_middle:: ty:: layout:: { LayoutCx , LayoutError , LayoutOf , TyAndLayout , ValidityRequirement } ;
22use rustc_middle:: ty:: { ParamEnv , ParamEnvAnd , Ty , TyCtxt } ;
33use rustc_session:: Limit ;
44use rustc_target:: abi:: { Abi , FieldsShape , Scalar , Variants } ;
@@ -18,16 +18,23 @@ use crate::interpret::{InterpCx, MemoryKind, OpTy};
1818/// Rust UB as long as there is no risk of miscompilations. The `strict_init_checks` can be set to
1919/// do a full check against Rust UB instead (in which case we will also ignore the 0x01-filling and
2020/// to the full uninit check).
21- pub fn might_permit_raw_init < ' tcx > (
21+ pub fn check_validity_requirement < ' tcx > (
2222 tcx : TyCtxt < ' tcx > ,
23- kind : InitKind ,
23+ kind : ValidityRequirement ,
2424 param_env_and_ty : ParamEnvAnd < ' tcx , Ty < ' tcx > > ,
2525) -> Result < bool , LayoutError < ' tcx > > {
26+ let layout = tcx. layout_of ( param_env_and_ty) ?;
27+
28+ // There is nothing strict or lax about inhabitedness.
29+ if kind == ValidityRequirement :: Inhabited {
30+ return Ok ( !layout. abi . is_uninhabited ( ) ) ;
31+ }
32+
2633 if tcx. sess . opts . unstable_opts . strict_init_checks {
27- might_permit_raw_init_strict ( tcx . layout_of ( param_env_and_ty ) ? , tcx, kind)
34+ might_permit_raw_init_strict ( layout , tcx, kind)
2835 } else {
2936 let layout_cx = LayoutCx { tcx, param_env : param_env_and_ty. param_env } ;
30- might_permit_raw_init_lax ( tcx . layout_of ( param_env_and_ty ) ? , & layout_cx, kind)
37+ might_permit_raw_init_lax ( layout , & layout_cx, kind)
3138 }
3239}
3340
@@ -36,7 +43,7 @@ pub fn might_permit_raw_init<'tcx>(
3643fn might_permit_raw_init_strict < ' tcx > (
3744 ty : TyAndLayout < ' tcx > ,
3845 tcx : TyCtxt < ' tcx > ,
39- kind : InitKind ,
46+ kind : ValidityRequirement ,
4047) -> Result < bool , LayoutError < ' tcx > > {
4148 let machine = CompileTimeInterpreter :: new (
4249 Limit :: new ( 0 ) ,
@@ -50,7 +57,7 @@ fn might_permit_raw_init_strict<'tcx>(
5057 . allocate ( ty, MemoryKind :: Machine ( crate :: const_eval:: MemoryKind :: Heap ) )
5158 . expect ( "OOM: failed to allocate for uninit check" ) ;
5259
53- if kind == InitKind :: Zero {
60+ if kind == ValidityRequirement :: Zero {
5461 cx. write_bytes_ptr (
5562 allocated. ptr ,
5663 std:: iter:: repeat ( 0_u8 ) . take ( ty. layout . size ( ) . bytes_usize ( ) ) ,
@@ -72,15 +79,18 @@ fn might_permit_raw_init_strict<'tcx>(
7279fn might_permit_raw_init_lax < ' tcx > (
7380 this : TyAndLayout < ' tcx > ,
7481 cx : & LayoutCx < ' tcx , TyCtxt < ' tcx > > ,
75- init_kind : InitKind ,
82+ init_kind : ValidityRequirement ,
7683) -> Result < bool , LayoutError < ' tcx > > {
7784 let scalar_allows_raw_init = move |s : Scalar | -> bool {
7885 match init_kind {
79- InitKind :: Zero => {
86+ ValidityRequirement :: Inhabited => {
87+ bug ! ( "ValidityRequirement::Inhabited should have been handled above" )
88+ }
89+ ValidityRequirement :: Zero => {
8090 // The range must contain 0.
8191 s. valid_range ( cx) . contains ( 0 )
8292 }
83- InitKind :: UninitMitigated0x01Fill => {
93+ ValidityRequirement :: UninitMitigated0x01Fill => {
8494 // The range must include an 0x01-filled buffer.
8595 let mut val: u128 = 0x01 ;
8696 for _ in 1 ..s. size ( cx) . bytes ( ) {
0 commit comments