@@ -18,6 +18,7 @@ use rustc_span::{Span, DUMMY_SP};
1818use rustc_target:: abi:: { self , FieldIdx , FIRST_VARIANT } ;
1919
2020use arrayvec:: ArrayVec ;
21+ use either:: Either ;
2122
2223impl < ' a , ' tcx , Bx : BuilderMethods < ' a , ' tcx > > FunctionCx < ' a , ' tcx , Bx > {
2324 #[ instrument( level = "trace" , skip( self , bx) ) ]
@@ -696,35 +697,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
696697 OperandRef { val : OperandValue :: Immediate ( static_) , layout }
697698 }
698699 mir:: Rvalue :: Use ( ref operand) => self . codegen_operand ( bx, operand) ,
699- mir:: Rvalue :: Aggregate ( box mir:: AggregateKind :: RawPtr ( ..) , ref fields) => {
700- let ty = rvalue. ty ( self . mir , self . cx . tcx ( ) ) ;
701- let layout = self . cx . layout_of ( self . monomorphize ( ty) ) ;
702- let [ data, meta] = & * fields. raw else {
703- bug ! ( "RawPtr fields: {fields:?}" ) ;
704- } ;
705- let data = self . codegen_operand ( bx, data) ;
706- let meta = self . codegen_operand ( bx, meta) ;
707- match ( data. val , meta. val ) {
708- ( p @ OperandValue :: Immediate ( _) , OperandValue :: ZeroSized ) => {
709- OperandRef { val : p, layout }
710- }
711- ( OperandValue :: Immediate ( p) , OperandValue :: Immediate ( m) ) => {
712- OperandRef { val : OperandValue :: Pair ( p, m) , layout }
713- }
714- _ => bug ! ( "RawPtr operands {data:?} {meta:?}" ) ,
715- }
716- }
717700 mir:: Rvalue :: Repeat ( ..) => bug ! ( "{rvalue:?} in codegen_rvalue_operand" ) ,
718- mir:: Rvalue :: Aggregate ( _ , ref fields) => {
701+ mir:: Rvalue :: Aggregate ( ref kind , ref fields) => {
719702 let ty = rvalue. ty ( self . mir , self . cx . tcx ( ) ) ;
720703 let ty = self . monomorphize ( ty) ;
721704 let layout = self . cx . layout_of ( ty) ;
722705
706+ let field_indices = if let mir:: AggregateKind :: RawPtr ( ..) = * * kind {
707+ // `index_by_increasing_offset` gives an empty iterator for primitives
708+ Either :: Left ( [ 0_usize , 1_usize ] . iter ( ) . copied ( ) )
709+ } else {
710+ Either :: Right ( layout. fields . index_by_increasing_offset ( ) )
711+ } ;
712+ debug_assert_eq ! ( field_indices. len( ) , fields. len( ) ) ;
713+
723714 // `rvalue_creates_operand` has arranged that we only get here if
724715 // we can build the aggregate immediate from the field immediates.
725716 let mut inputs = ArrayVec :: < Bx :: Value , 2 > :: new ( ) ;
726717 let mut input_scalars = ArrayVec :: < abi:: Scalar , 2 > :: new ( ) ;
727- for field_idx in layout . fields . index_by_increasing_offset ( ) {
718+ for field_idx in field_indices {
728719 let field_idx = FieldIdx :: from_usize ( field_idx) ;
729720 let op = self . codegen_operand ( bx, & fields[ field_idx] ) ;
730721 let values = op. val . immediates_or_place ( ) . left_or_else ( |p| {
@@ -748,6 +739,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
748739 ) ;
749740
750741 let val = OperandValue :: from_immediates ( inputs) ;
742+ debug_assert ! (
743+ val. is_expected_variant_for_type( self . cx, layout) ,
744+ "Made wrong variant {val:?} for type {layout:?}" ,
745+ ) ;
751746 OperandRef { val, layout }
752747 }
753748 mir:: Rvalue :: ShallowInitBox ( ref operand, content_ty) => {
@@ -792,7 +787,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
792787 debug_assert ! (
793788 if bx. cx( ) . type_has_metadata( ty) {
794789 matches!( val, OperandValue :: Pair ( ..) )
795- } else {
790+ } else {
796791 matches!( val, OperandValue :: Immediate ( ..) )
797792 } ,
798793 "Address of place was unexpectedly {val:?} for pointee type {ty:?}" ,
0 commit comments