@@ -22,7 +22,7 @@ pub(crate) use cpuid::codegen_cpuid_call;
2222pub ( crate ) use llvm:: codegen_llvm_intrinsic_call;
2323
2424use rustc_middle:: ty;
25- use rustc_middle:: ty:: layout:: { HasParamEnv , InitKind } ;
25+ use rustc_middle:: ty:: layout:: { HasParamEnv , ValidityRequirement } ;
2626use rustc_middle:: ty:: print:: with_no_trimmed_paths;
2727use rustc_middle:: ty:: subst:: SubstsRef ;
2828use rustc_span:: symbol:: { kw, sym, Symbol } ;
@@ -628,57 +628,39 @@ fn codegen_regular_intrinsic_call<'tcx>(
628628 intrinsic_args ! ( fx, args => ( ) ; intrinsic) ;
629629
630630 let ty = substs. type_at ( 0 ) ;
631- let layout = fx. layout_of ( ty) ;
632- if layout. abi . is_uninhabited ( ) {
633- with_no_trimmed_paths ! ( {
634- crate :: base:: codegen_panic_nounwind(
635- fx,
636- & format!( "attempted to instantiate uninhabited type `{}`" , layout. ty) ,
637- source_info,
638- )
639- } ) ;
640- return ;
641- }
642631
643- if intrinsic == sym:: assert_zero_valid
644- && !fx
645- . tcx
646- . check_validity_of_init ( ( InitKind :: Zero , fx. param_env ( ) . and ( ty) ) )
647- . expect ( "expected to have layout during codegen" )
648- {
649- with_no_trimmed_paths ! ( {
650- crate :: base:: codegen_panic_nounwind(
651- fx,
652- & format!(
653- "attempted to zero-initialize type `{}`, which is invalid" ,
654- layout. ty
655- ) ,
656- source_info,
657- ) ;
658- } ) ;
659- return ;
660- }
632+ let requirement = ValidityRequirement :: from_intrinsic ( intrinsic) ;
661633
662- if intrinsic == sym :: assert_mem_uninitialized_valid
663- && !fx
634+ if let Some ( requirement ) = requirement {
635+ let do_panic = !fx
664636 . tcx
665- . check_validity_of_init ( (
666- InitKind :: UninitMitigated0x01Fill ,
667- fx. param_env ( ) . and ( ty) ,
668- ) )
669- . expect ( "expected to have layout during codegen" )
670- {
671- with_no_trimmed_paths ! ( {
672- crate :: base:: codegen_panic_nounwind(
673- fx,
674- & format!(
675- "attempted to leave type `{}` uninitialized, which is invalid" ,
676- layout. ty
677- ) ,
678- source_info,
679- )
680- } ) ;
681- return ;
637+ . check_validity_requirement ( ( requirement, fx. param_env ( ) . and ( ty) ) )
638+ . expect ( "expect to have layout during codegen" ) ;
639+
640+ if do_panic {
641+ let layout = fx. layout_of ( ty) ;
642+
643+ with_no_trimmed_paths ! ( {
644+ crate :: base:: codegen_panic_nounwind(
645+ fx,
646+ & if layout. abi. is_uninhabited( ) {
647+ format!( "attempted to instantiate uninhabited type `{}`" , layout. ty)
648+ } else if requirement == ValidityRequirement :: Zero {
649+ format!(
650+ "attempted to zero-initialize type `{}`, which is invalid" ,
651+ layout. ty
652+ )
653+ } else {
654+ format!(
655+ "attempted to leave type `{}` uninitialized, which is invalid" ,
656+ layout. ty
657+ )
658+ } ,
659+ source_info,
660+ )
661+ } ) ;
662+ return ;
663+ }
682664 }
683665 }
684666
0 commit comments