1
1
use std:: fmt;
2
2
3
+ use itertools:: Either ;
3
4
use rustc_abi as abi;
4
5
use rustc_abi:: {
5
6
Align , BackendRepr , FIRST_VARIANT , FieldIdx , Primitive , Size , TagEncoding , VariantIdx , Variants ,
@@ -567,12 +568,12 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
567
568
568
569
/// Creates an incomplete operand containing the [`abi::Scalar`]s expected based
569
570
/// on the `layout` passed. This is for use with [`OperandRef::insert_field`]
570
- /// later to set the necessary immediate(s).
571
+ /// later to set the necessary immediate(s), one-by-one converting all the `Right` to `Left` .
571
572
///
572
573
/// Returns `None` for `layout`s which cannot be built this way.
573
574
pub ( crate ) fn builder (
574
575
layout : TyAndLayout < ' tcx > ,
575
- ) -> Option < OperandRef < ' tcx , Result < V , abi:: Scalar > > > {
576
+ ) -> Option < OperandRef < ' tcx , Either < V , abi:: Scalar > > > {
576
577
// Uninhabited types are weird, because for example `Result<!, !>`
577
578
// shows up as `FieldsShape::Primitive` and we need to be able to write
578
579
// a field into `(u32, !)`. We'll do that in an `alloca` instead.
@@ -582,15 +583,15 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
582
583
583
584
let val = match layout. backend_repr {
584
585
BackendRepr :: Memory { .. } if layout. is_zst ( ) => OperandValue :: ZeroSized ,
585
- BackendRepr :: Scalar ( s) => OperandValue :: Immediate ( Err ( s) ) ,
586
- BackendRepr :: ScalarPair ( a, b) => OperandValue :: Pair ( Err ( a) , Err ( b) ) ,
586
+ BackendRepr :: Scalar ( s) => OperandValue :: Immediate ( Either :: Right ( s) ) ,
587
+ BackendRepr :: ScalarPair ( a, b) => OperandValue :: Pair ( Either :: Right ( a) , Either :: Right ( b) ) ,
587
588
BackendRepr :: Memory { .. } | BackendRepr :: SimdVector { .. } => return None ,
588
589
} ;
589
590
Some ( OperandRef { val, layout } )
590
591
}
591
592
}
592
593
593
- impl < ' a , ' tcx , V : CodegenObject > OperandRef < ' tcx , Result < V , abi:: Scalar > > {
594
+ impl < ' a , ' tcx , V : CodegenObject > OperandRef < ' tcx , Either < V , abi:: Scalar > > {
594
595
pub ( crate ) fn insert_field < Bx : BuilderMethods < ' a , ' tcx , Value = V > > (
595
596
& mut self ,
596
597
bx : & mut Bx ,
@@ -614,29 +615,29 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> {
614
615
( field_layout. is_zst ( ) , field_offset == Size :: ZERO )
615
616
} ;
616
617
617
- let mut update = |tgt : & mut Result < V , abi:: Scalar > , src, from_scalar| {
618
- let to_scalar = tgt. unwrap_err ( ) ;
618
+ let mut update = |tgt : & mut Either < V , abi:: Scalar > , src, from_scalar| {
619
+ let to_scalar = tgt. unwrap_right ( ) ;
619
620
let imm = transmute_scalar ( bx, src, from_scalar, to_scalar) ;
620
- * tgt = Ok ( imm) ;
621
+ * tgt = Either :: Left ( imm) ;
621
622
} ;
622
623
623
624
match ( operand. val , operand. layout . backend_repr ) {
624
625
( OperandValue :: ZeroSized , _) if expect_zst => { }
625
626
( OperandValue :: Immediate ( v) , BackendRepr :: Scalar ( from_scalar) ) => match & mut self . val {
626
- OperandValue :: Immediate ( val @ Err ( _) ) if is_zero_offset => {
627
+ OperandValue :: Immediate ( val @ Either :: Right ( _) ) if is_zero_offset => {
627
628
update ( val, v, from_scalar) ;
628
629
}
629
- OperandValue :: Pair ( fst @ Err ( _) , _) if is_zero_offset => {
630
+ OperandValue :: Pair ( fst @ Either :: Right ( _) , _) if is_zero_offset => {
630
631
update ( fst, v, from_scalar) ;
631
632
}
632
- OperandValue :: Pair ( _, snd @ Err ( _) ) if !is_zero_offset => {
633
+ OperandValue :: Pair ( _, snd @ Either :: Right ( _) ) if !is_zero_offset => {
633
634
update ( snd, v, from_scalar) ;
634
635
}
635
636
_ => bug ! ( "Tried to insert {operand:?} into {v:?}.{f:?} of {self:?}" ) ,
636
637
} ,
637
638
( OperandValue :: Pair ( a, b) , BackendRepr :: ScalarPair ( from_sa, from_sb) ) => {
638
639
match & mut self . val {
639
- OperandValue :: Pair ( fst @ Err ( _) , snd @ Err ( _) ) => {
640
+ OperandValue :: Pair ( fst @ Either :: Right ( _) , snd @ Either :: Right ( _) ) => {
640
641
update ( fst, a, from_sa) ;
641
642
update ( snd, b, from_sb) ;
642
643
}
@@ -656,21 +657,21 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> {
656
657
let field_offset = self . layout . fields . offset ( f. as_usize ( ) ) ;
657
658
let is_zero_offset = field_offset == Size :: ZERO ;
658
659
match & mut self . val {
659
- OperandValue :: Immediate ( val @ Err ( _) ) if is_zero_offset => {
660
- * val = Ok ( imm) ;
660
+ OperandValue :: Immediate ( val @ Either :: Right ( _) ) if is_zero_offset => {
661
+ * val = Either :: Left ( imm) ;
661
662
}
662
- OperandValue :: Pair ( fst @ Err ( _) , _) if is_zero_offset => {
663
- * fst = Ok ( imm) ;
663
+ OperandValue :: Pair ( fst @ Either :: Right ( _) , _) if is_zero_offset => {
664
+ * fst = Either :: Left ( imm) ;
664
665
}
665
- OperandValue :: Pair ( _, snd @ Err ( _) ) if !is_zero_offset => {
666
- * snd = Ok ( imm) ;
666
+ OperandValue :: Pair ( _, snd @ Either :: Right ( _) ) if !is_zero_offset => {
667
+ * snd = Either :: Left ( imm) ;
667
668
}
668
669
_ => bug ! ( "Tried to insert {imm:?} into field {f:?} of {self:?}" ) ,
669
670
}
670
671
}
671
672
672
673
/// After having set all necessary fields, this converts the
673
- /// `OperandValue<Result <V, _>>` (as obtained from [`OperandRef::builder`])
674
+ /// `OperandValue<Either <V, _>>` (as obtained from [`OperandRef::builder`])
674
675
/// to the normal `OperandValue<V>`.
675
676
///
676
677
/// ICEs if any required fields were not set.
@@ -681,13 +682,13 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, Result<V, abi::Scalar>> {
681
682
// payload scalar will not actually have been set, so this converts
682
683
// unset scalars to corresponding `undef` values so long as the scalar
683
684
// from the layout allows uninit.
684
- let unwrap = |r : Result < V , abi:: Scalar > | match r {
685
- Ok ( v) => v,
686
- Err ( s) if s. is_uninit_valid ( ) => {
685
+ let unwrap = |r : Either < V , abi:: Scalar > | match r {
686
+ Either :: Left ( v) => v,
687
+ Either :: Right ( s) if s. is_uninit_valid ( ) => {
687
688
let bty = cx. type_from_scalar ( s) ;
688
689
cx. const_undef ( bty)
689
690
}
690
- Err ( _) => bug ! ( "OperandRef::build called while fields are missing {self:?}" ) ,
691
+ Either :: Right ( _) => bug ! ( "OperandRef::build called while fields are missing {self:?}" ) ,
691
692
} ;
692
693
693
694
let val = match val {
0 commit comments