@@ -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) ) ]
@@ -722,35 +723,25 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
722723 OperandRef { val : OperandValue :: Immediate ( static_) , layout }
723724 }
724725 mir:: Rvalue :: Use ( ref operand) => self . codegen_operand ( bx, operand) ,
725- mir:: Rvalue :: Aggregate ( box mir:: AggregateKind :: RawPtr ( ..) , ref fields) => {
726- let ty = rvalue. ty ( self . mir , self . cx . tcx ( ) ) ;
727- let layout = self . cx . layout_of ( self . monomorphize ( ty) ) ;
728- let [ data, meta] = & * fields. raw else {
729- bug ! ( "RawPtr fields: {fields:?}" ) ;
730- } ;
731- let data = self . codegen_operand ( bx, data) ;
732- let meta = self . codegen_operand ( bx, meta) ;
733- match ( data. val , meta. val ) {
734- ( p @ OperandValue :: Immediate ( _) , OperandValue :: ZeroSized ) => {
735- OperandRef { val : p, layout }
736- }
737- ( OperandValue :: Immediate ( p) , OperandValue :: Immediate ( m) ) => {
738- OperandRef { val : OperandValue :: Pair ( p, m) , layout }
739- }
740- _ => bug ! ( "RawPtr operands {data:?} {meta:?}" ) ,
741- }
742- }
743726 mir:: Rvalue :: Repeat ( ..) => bug ! ( "{rvalue:?} in codegen_rvalue_operand" ) ,
744- mir:: Rvalue :: Aggregate ( _ , ref fields) => {
727+ mir:: Rvalue :: Aggregate ( ref kind , ref fields) => {
745728 let ty = rvalue. ty ( self . mir , self . cx . tcx ( ) ) ;
746729 let ty = self . monomorphize ( ty) ;
747730 let layout = self . cx . layout_of ( ty) ;
748731
732+ let field_indices = if let mir:: AggregateKind :: RawPtr ( ..) = * * kind {
733+ // `index_by_increasing_offset` gives an empty iterator for primitives
734+ Either :: Left ( [ 0_usize , 1_usize ] . iter ( ) . copied ( ) )
735+ } else {
736+ Either :: Right ( layout. fields . index_by_increasing_offset ( ) )
737+ } ;
738+ debug_assert_eq ! ( field_indices. len( ) , fields. len( ) ) ;
739+
749740 // `rvalue_creates_operand` has arranged that we only get here if
750741 // we can build the aggregate immediate from the field immediates.
751742 let mut inputs = ArrayVec :: < Bx :: Value , 2 > :: new ( ) ;
752743 let mut input_scalars = ArrayVec :: < abi:: Scalar , 2 > :: new ( ) ;
753- for field_idx in layout . fields . index_by_increasing_offset ( ) {
744+ for field_idx in field_indices {
754745 let field_idx = FieldIdx :: from_usize ( field_idx) ;
755746 let op = self . codegen_operand ( bx, & fields[ field_idx] ) ;
756747 let values = op. val . immediates_or_place ( ) . left_or_else ( |p| {
@@ -774,6 +765,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
774765 ) ;
775766
776767 let val = OperandValue :: from_immediates ( inputs) ;
768+ debug_assert ! (
769+ val. is_expected_variant_for_type( self . cx, layout) ,
770+ "Made wrong variant {val:?} for type {layout:?}" ,
771+ ) ;
777772 OperandRef { val, layout }
778773 }
779774 mir:: Rvalue :: ShallowInitBox ( ref operand, content_ty) => {
0 commit comments