@@ -14,7 +14,7 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1414use rustc_hir:: lang_items:: LangItem ;
1515use rustc_index:: vec:: Idx ;
1616use rustc_middle:: mir:: { self , AssertKind , SwitchTargets } ;
17- use rustc_middle:: ty:: layout:: { HasTyCtxt , InitKind , LayoutOf } ;
17+ use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , ValidityRequirement } ;
1818use rustc_middle:: ty:: print:: { with_no_trimmed_paths, with_no_visible_paths} ;
1919use rustc_middle:: ty:: { self , Instance , Ty , TypeVisitableExt } ;
2020use rustc_session:: config:: OptLevel ;
@@ -655,44 +655,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
655655 // Emit a panic or a no-op for `assert_*` intrinsics.
656656 // These are intrinsics that compile to panics so that we can get a message
657657 // which mentions the offending type, even from a const context.
658- #[ derive( Debug , PartialEq ) ]
659- enum AssertIntrinsic {
660- Inhabited ,
661- ZeroValid ,
662- MemUninitializedValid ,
663- }
664- let panic_intrinsic = intrinsic. and_then ( |i| match i {
665- sym:: assert_inhabited => Some ( AssertIntrinsic :: Inhabited ) ,
666- sym:: assert_zero_valid => Some ( AssertIntrinsic :: ZeroValid ) ,
667- sym:: assert_mem_uninitialized_valid => Some ( AssertIntrinsic :: MemUninitializedValid ) ,
668- _ => None ,
669- } ) ;
670- if let Some ( intrinsic) = panic_intrinsic {
671- use AssertIntrinsic :: * ;
672-
658+ let panic_intrinsic = intrinsic. and_then ( |s| ValidityRequirement :: from_intrinsic ( s) ) ;
659+ if let Some ( requirement) = panic_intrinsic {
673660 let ty = instance. unwrap ( ) . substs . type_at ( 0 ) ;
661+
662+ let do_panic = !bx
663+ . tcx ( )
664+ . check_validity_requirement ( ( requirement, bx. param_env ( ) . and ( ty) ) )
665+ . expect ( "expect to have layout during codegen" ) ;
666+
674667 let layout = bx. layout_of ( ty) ;
675- let do_panic = match intrinsic {
676- Inhabited => layout. abi . is_uninhabited ( ) ,
677- ZeroValid => !bx
678- . tcx ( )
679- . check_validity_of_init ( ( InitKind :: Zero , bx. param_env ( ) . and ( ty) ) )
680- . expect ( "expected to have layout during codegen" ) ,
681- MemUninitializedValid => !bx
682- . tcx ( )
683- . check_validity_of_init ( (
684- InitKind :: UninitMitigated0x01Fill ,
685- bx. param_env ( ) . and ( ty) ,
686- ) )
687- . expect ( "expected to have layout during codegen" ) ,
688- } ;
668+
689669 Some ( if do_panic {
690670 let msg_str = with_no_visible_paths ! ( {
691671 with_no_trimmed_paths!( {
692672 if layout. abi. is_uninhabited( ) {
693673 // Use this error even for the other intrinsics as it is more precise.
694674 format!( "attempted to instantiate uninhabited type `{}`" , ty)
695- } else if intrinsic == ZeroValid {
675+ } else if requirement == ValidityRequirement :: Zero {
696676 format!( "attempted to zero-initialize type `{}`, which is invalid" , ty)
697677 } else {
698678 format!(
0 commit comments