Skip to content

Commit 0841a67

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
Expose enum discriminant signedness
1 parent 3ca008d commit 0841a67

File tree

1 file changed

+17
-20
lines changed
  • src/librustc_trans/trans

1 file changed

+17
-20
lines changed

src/librustc_trans/trans/adt.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -793,43 +793,40 @@ pub fn trans_switch<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
793793
}
794794
}
795795

796-
796+
pub fn is_discr_signed<'tcx>(r: &Repr<'tcx>) -> bool {
797+
match *r {
798+
CEnum(ity, _, _) => ity.is_signed(),
799+
General(ity, _, _) => ity.is_signed(),
800+
Univariant(..) => false,
801+
RawNullablePointer { .. } => false,
802+
StructWrappedNullablePointer { .. } => false,
803+
}
804+
}
797805

798806
/// Obtain the actual discriminant of a value.
799807
pub fn trans_get_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
800808
scrutinee: ValueRef, cast_to: Option<Type>)
801809
-> ValueRef {
802-
let signed;
803-
let val;
804810
debug!("trans_get_discr r: {:?}", r);
805-
match *r {
806-
CEnum(ity, min, max) => {
807-
val = load_discr(bcx, ity, scrutinee, min, max);
808-
signed = ity.is_signed();
809-
}
811+
let val = match *r {
812+
CEnum(ity, min, max) => load_discr(bcx, ity, scrutinee, min, max),
810813
General(ity, ref cases, _) => {
811814
let ptr = GEPi(bcx, scrutinee, &[0, 0]);
812-
val = load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr);
813-
signed = ity.is_signed();
814-
}
815-
Univariant(..) => {
816-
val = C_u8(bcx.ccx(), 0);
817-
signed = false;
815+
load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr)
818816
}
817+
Univariant(..) => C_u8(bcx.ccx(), 0),
819818
RawNullablePointer { nndiscr, nnty, .. } => {
820819
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
821820
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
822-
val = ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty), DebugLoc::None);
823-
signed = false;
821+
ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty), DebugLoc::None)
824822
}
825823
StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
826-
val = struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee);
827-
signed = false;
824+
struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee)
828825
}
829-
}
826+
};
830827
match cast_to {
831828
None => val,
832-
Some(llty) => if signed { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
829+
Some(llty) => if is_discr_signed(r) { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
833830
}
834831
}
835832

0 commit comments

Comments
 (0)