Skip to content

Commit df3597b

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
Expose enum discriminant signedness
1 parent af41097 commit df3597b

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
@@ -795,43 +795,40 @@ pub fn trans_switch<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
795795
}
796796
}
797797

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

800808
/// Obtain the actual discriminant of a value.
801809
pub fn trans_get_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
802810
scrutinee: ValueRef, cast_to: Option<Type>)
803811
-> ValueRef {
804-
let signed;
805-
let val;
806812
debug!("trans_get_discr r: {:?}", r);
807-
match *r {
808-
CEnum(ity, min, max) => {
809-
val = load_discr(bcx, ity, scrutinee, min, max);
810-
signed = ity.is_signed();
811-
}
813+
let val = match *r {
814+
CEnum(ity, min, max) => load_discr(bcx, ity, scrutinee, min, max),
812815
General(ity, ref cases, _) => {
813816
let ptr = GEPi(bcx, scrutinee, &[0, 0]);
814-
val = load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr);
815-
signed = ity.is_signed();
816-
}
817-
Univariant(..) => {
818-
val = C_u8(bcx.ccx(), 0);
819-
signed = false;
817+
load_discr(bcx, ity, ptr, 0, (cases.len() - 1) as Disr)
820818
}
819+
Univariant(..) => C_u8(bcx.ccx(), 0),
821820
RawNullablePointer { nndiscr, nnty, .. } => {
822821
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
823822
let llptrty = type_of::sizing_type_of(bcx.ccx(), nnty);
824-
val = ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty), DebugLoc::None);
825-
signed = false;
823+
ICmp(bcx, cmp, Load(bcx, scrutinee), C_null(llptrty), DebugLoc::None)
826824
}
827825
StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
828-
val = struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee);
829-
signed = false;
826+
struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee)
830827
}
831-
}
828+
};
832829
match cast_to {
833830
None => val,
834-
Some(llty) => if signed { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
831+
Some(llty) => if is_discr_signed(r) { SExt(bcx, val, llty) } else { ZExt(bcx, val, llty) }
835832
}
836833
}
837834

0 commit comments

Comments
 (0)