11use rustc_abi:: { HasDataLayout , Scalar , Size , TagEncoding , Variants , WrappingRange } ;
2+ use rustc_const_eval:: interpret:: CTFE_ALLOC_SALT ;
23use rustc_hir:: LangItem ;
34use rustc_index:: IndexVec ;
45use rustc_middle:: bug;
@@ -7,6 +8,7 @@ use rustc_middle::mir::*;
78use rustc_middle:: ty:: layout:: { IntegerExt , PrimitiveExt } ;
89use rustc_middle:: ty:: { self , AdtDef , Ty , TyCtxt , TypingEnv } ;
910use rustc_session:: Session ;
11+ use rustc_span:: DUMMY_SP ;
1012use tracing:: debug;
1113
1214/// This pass inserts checks for a valid enum discriminant where they are most
@@ -268,23 +270,24 @@ impl<'a, 'tcx> Visitor<'tcx> for EnumFinder<'a, 'tcx> {
268270
269271 let ( enum_ty, enum_adt_def) = if enum_adt_def. is_enum ( ) {
270272 ( pointee_ty, enum_adt_def)
271- } else if enum_adt_def. is_manually_drop ( ) {
272- // Find the type contained in the ManuallyDrop and check whether it is an enum.
273- let Some ( ( manual_drop_arg, adt_def) ) =
274- pointee_ty. walk ( ) . skip ( 1 ) . next ( ) . map_or ( None , |arg| {
275- if let Some ( ty) = arg. as_type ( )
276- && let ty:: Adt ( adt_def, _) = ty. kind ( )
277- {
278- Some ( ( ty, adt_def) )
279- } else {
280- None
281- }
282- } )
283- else {
284- return ;
285- } ;
286273
287- ( manual_drop_arg, adt_def)
274+ // } else if enum_adt_def.is_manually_drop() {
275+ // // Find the type contained in the ManuallyDrop and check whether it is an enum.
276+ // let Some((manual_drop_arg, adt_def)) =
277+ // pointee_ty.walk().skip(1).next().map_or(None, |arg| {
278+ // if let Some(ty) = arg.as_type()
279+ // && let ty::Adt(adt_def, _) = ty.kind()
280+ // {
281+ // Some((ty, adt_def))
282+ // } else {
283+ // None
284+ // }
285+ // })
286+ // else {
287+ // return;
288+ // };
289+
290+ // (manual_drop_arg, adt_def)
288291 } else {
289292 return ;
290293 } ;
@@ -504,7 +507,7 @@ fn insert_direct_enum_check<'tcx>(
504507 tcx,
505508 local_decls,
506509 block_data,
507- source_op,
510+ source_op. clone ( ) ,
508511 discr,
509512 op_size,
510513 None ,
@@ -544,6 +547,9 @@ fn insert_direct_enum_check<'tcx>(
544547 } ,
545548 } ) ;
546549
550+ let debug_str = format ! ( "{:#?}" , source_op. ty( local_decls, tcx) ) ;
551+ let allocation =
552+ tcx. allocate_bytes_dedup ( std:: borrow:: Cow :: Borrowed ( debug_str. as_bytes ( ) ) , CTFE_ALLOC_SALT ) ;
547553 // Abort in case of an invalid enum discriminant.
548554 basic_blocks[ invalid_discr_block] . terminator = Some ( Terminator {
549555 source_info,
@@ -555,7 +561,17 @@ fn insert_direct_enum_check<'tcx>(
555561 } ) ) ,
556562 expected : true ,
557563 target : new_block,
558- msg : Box :: new ( AssertKind :: InvalidEnumConstruction ( Operand :: Copy ( discr_masked) ) ) ,
564+ msg : Box :: new ( AssertKind :: InvalidEnumConstruction (
565+ Operand :: Constant ( Box :: new ( ConstOperand {
566+ span : DUMMY_SP ,
567+ user_ty : None ,
568+ const_ : Const :: Val (
569+ ConstValue :: Slice { alloc_id : allocation, meta : debug_str. len ( ) as u64 } ,
570+ Ty :: new_ref ( tcx, tcx. lifetimes . re_erased , tcx. types . str_ , Mutability :: Not ) ,
571+ ) ,
572+ } ) ) ,
573+ Operand :: Copy ( discr_masked) ,
574+ ) ) ,
559575 // This calls panic_invalid_enum_construction, which is #[rustc_nounwind].
560576 // We never want to insert an unwind into unsafe code, because unwinding could
561577 // make a failing UB check turn into much worse UB when we start unwinding.
@@ -585,19 +601,30 @@ fn insert_uninhabited_enum_check<'tcx>(
585601 ) ) ) ,
586602 ) ) ;
587603
604+ let debug_str = "None" . to_owned ( ) ;
605+ let allocation =
606+ tcx. allocate_bytes_dedup ( std:: borrow:: Cow :: Borrowed ( debug_str. as_bytes ( ) ) , CTFE_ALLOC_SALT ) ;
588607 block_data. terminator = Some ( Terminator {
589608 source_info,
590609 kind : TerminatorKind :: Assert {
591610 cond : Operand :: Copy ( is_ok) ,
592611 expected : true ,
593612 target : new_block,
594- msg : Box :: new ( AssertKind :: InvalidEnumConstruction ( Operand :: Constant ( Box :: new (
595- ConstOperand {
613+ msg : Box :: new ( AssertKind :: InvalidEnumConstruction (
614+ Operand :: Constant ( Box :: new ( ConstOperand {
615+ span : DUMMY_SP ,
616+ user_ty : None ,
617+ const_ : Const :: Val (
618+ ConstValue :: Slice { alloc_id : allocation, meta : debug_str. len ( ) as u64 } ,
619+ Ty :: new_ref ( tcx, tcx. lifetimes . re_erased , tcx. types . str_ , Mutability :: Not ) ,
620+ ) ,
621+ } ) ) ,
622+ Operand :: Constant ( Box :: new ( ConstOperand {
596623 span : source_info. span ,
597624 user_ty : None ,
598625 const_ : Const :: Val ( ConstValue :: from_u128 ( 0 ) , tcx. types . u128 ) ,
599- } ,
600- ) ) ) ) ,
626+ } ) ) ,
627+ ) ) ,
601628 // This calls panic_invalid_enum_construction, which is #[rustc_nounwind].
602629 // We never want to insert an unwind into unsafe code, because unwinding could
603630 // make a failing UB check turn into much worse UB when we start unwinding.
@@ -622,7 +649,7 @@ fn insert_niche_check<'tcx>(
622649 tcx,
623650 local_decls,
624651 block_data,
625- source_op,
652+ source_op. clone ( ) ,
626653 discr,
627654 op_size,
628655 Some ( offset) ,
@@ -668,13 +695,26 @@ fn insert_niche_check<'tcx>(
668695 ) ) ) ,
669696 ) ) ;
670697
698+ let debug_str = format ! ( "{:#?}" , source_op. ty( local_decls, tcx) ) ;
699+ let allocation =
700+ tcx. allocate_bytes_dedup ( std:: borrow:: Cow :: Borrowed ( debug_str. as_bytes ( ) ) , CTFE_ALLOC_SALT ) ;
671701 block_data. terminator = Some ( Terminator {
672702 source_info,
673703 kind : TerminatorKind :: Assert {
674704 cond : Operand :: Copy ( is_ok) ,
675705 expected : true ,
676706 target : new_block,
677- msg : Box :: new ( AssertKind :: InvalidEnumConstruction ( Operand :: Copy ( discr) ) ) ,
707+ msg : Box :: new ( AssertKind :: InvalidEnumConstruction (
708+ Operand :: Constant ( Box :: new ( ConstOperand {
709+ span : DUMMY_SP ,
710+ user_ty : None ,
711+ const_ : Const :: Val (
712+ ConstValue :: Slice { alloc_id : allocation, meta : debug_str. len ( ) as u64 } ,
713+ Ty :: new_ref ( tcx, tcx. lifetimes . re_erased , tcx. types . str_ , Mutability :: Not ) ,
714+ ) ,
715+ } ) ) ,
716+ Operand :: Copy ( discr) ,
717+ ) ) ,
678718 // This calls panic_invalid_enum_construction, which is #[rustc_nounwind].
679719 // We never want to insert an unwind into unsafe code, because unwinding could
680720 // make a failing UB check turn into much worse UB when we start unwinding.
0 commit comments