@@ -14,7 +14,7 @@ use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
14
14
use rustc_hir:: lang_items:: LangItem ;
15
15
use rustc_index:: vec:: Idx ;
16
16
use rustc_middle:: mir:: { self , AssertKind , SwitchTargets } ;
17
- use rustc_middle:: ty:: layout:: { HasTyCtxt , InitKind , LayoutOf } ;
17
+ use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , ValidityRequirement } ;
18
18
use rustc_middle:: ty:: print:: { with_no_trimmed_paths, with_no_visible_paths} ;
19
19
use rustc_middle:: ty:: { self , Instance , Ty , TypeVisitableExt } ;
20
20
use rustc_session:: config:: OptLevel ;
@@ -655,44 +655,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
655
655
// Emit a panic or a no-op for `assert_*` intrinsics.
656
656
// These are intrinsics that compile to panics so that we can get a message
657
657
// 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 {
673
660
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
+
674
667
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
+
689
669
Some ( if do_panic {
690
670
let msg_str = with_no_visible_paths ! ( {
691
671
with_no_trimmed_paths!( {
692
672
if layout. abi. is_uninhabited( ) {
693
673
// Use this error even for the other intrinsics as it is more precise.
694
674
format!( "attempted to instantiate uninhabited type `{}`" , ty)
695
- } else if intrinsic == ZeroValid {
675
+ } else if requirement == ValidityRequirement :: Zero {
696
676
format!( "attempted to zero-initialize type `{}`, which is invalid" , ty)
697
677
} else {
698
678
format!(
0 commit comments