@@ -620,23 +620,41 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
620
620
}
621
621
}
622
622
CastKind :: Transmute => {
623
- let src_layout = self . tcx . layout_of ( self . param_env . and ( op_ty) ) ;
624
- let dst_layout = self . tcx . layout_of ( self . param_env . and ( * target_type) ) ;
625
- if let Err ( e) = src_layout {
623
+ if let MirPhase :: Runtime ( ..) = self . mir_phase {
624
+ // `mem::transmute` currently requires types concrete enough
625
+ // to *know* that their sizes are the same, and we repeat
626
+ // that check here. This restriction may be lifted in future,
627
+ // should some optimizations need to it be more general.
628
+ // (It might just end up being UB if they don't match, say.)
629
+
630
+ let src_layout = self . tcx . layout_of ( self . param_env . and ( op_ty) ) ;
631
+ let dst_layout = self . tcx . layout_of ( self . param_env . and ( * target_type) ) ;
632
+ if let Err ( e) = src_layout {
633
+ self . fail (
634
+ location,
635
+ format ! (
636
+ "Unable to compute layout for source type {op_ty:?}: {e}"
637
+ ) ,
638
+ ) ;
639
+ }
640
+ if let Err ( e) = dst_layout {
641
+ self . fail ( location, format ! ( "Unable to compute layout for destination type {target_type:?}: {e}" ) ) ;
642
+ }
643
+
644
+ if let ( Ok ( src_layout) , Ok ( dst_layout) ) = ( src_layout, dst_layout) {
645
+ if src_layout. layout . size ( ) != dst_layout. layout . size ( ) {
646
+ self . fail ( location, format ! ( "Source and destination layouts have different sizes: {src_layout:?} vs {dst_layout:?}" ) ) ;
647
+ }
648
+ }
649
+ } else {
626
650
self . fail (
627
651
location,
628
- format ! ( "Unable to compute layout for source type {op_ty:?}: {e}" ) ,
652
+ format ! (
653
+ "Transmute is not supported in non-runtime phase {:?}." ,
654
+ self . mir_phase
655
+ ) ,
629
656
) ;
630
657
}
631
- if let Err ( e) = dst_layout {
632
- self . fail ( location, format ! ( "Unable to compute layout for destination type {target_type:?}: {e}" ) ) ;
633
- }
634
-
635
- if let ( Ok ( src_layout) , Ok ( dst_layout) ) = ( src_layout, dst_layout) {
636
- if src_layout. layout . size ( ) != dst_layout. layout . size ( ) {
637
- self . fail ( location, format ! ( "Source and destination layouts have different sizes: {src_layout:?} vs {dst_layout:?}" ) ) ;
638
- }
639
- }
640
658
}
641
659
}
642
660
}
0 commit comments