Skip to content

Commit 906db02

Browse files
committed
Avoid CValue::const_val for discriminants
1 parent 8cf40c4 commit 906db02

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/discriminant.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,20 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
2828
} => {
2929
let ptr = place.place_field(fx, FieldIdx::new(tag_field));
3030
let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val;
31-
let to = if ptr.layout().abi.is_signed() {
32-
ty::ScalarInt::try_from_int(
33-
ptr.layout().size.sign_extend(to) as i128,
34-
ptr.layout().size,
35-
)
36-
.unwrap()
37-
} else {
38-
ty::ScalarInt::try_from_uint(to, ptr.layout().size).unwrap()
31+
let to = match ptr.layout().ty.kind() {
32+
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
33+
let lsb = fx.bcx.ins().iconst(types::I64, to as u64 as i64);
34+
let msb = fx.bcx.ins().iconst(types::I64, (to >> 64) as u64 as i64);
35+
fx.bcx.ins().iconcat(lsb, msb)
36+
}
37+
ty::Uint(_) | ty::Int(_) => {
38+
let clif_ty = fx.clif_type(ptr.layout().ty).unwrap();
39+
let raw_val = ptr.layout().size.truncate(to);
40+
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
41+
}
42+
_ => unreachable!(),
3943
};
40-
let discr = CValue::const_val(fx, ptr.layout(), to);
44+
let discr = CValue::by_val(to, ptr.layout());
4145
ptr.write_cvalue(fx, discr);
4246
}
4347
Variants::Multiple {
@@ -85,16 +89,21 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
8589
.ty
8690
.discriminant_for_variant(fx.tcx, *index)
8791
.map_or(u128::from(index.as_u32()), |discr| discr.val);
88-
let discr_val = if dest_layout.abi.is_signed() {
89-
ty::ScalarInt::try_from_int(
90-
dest_layout.size.sign_extend(discr_val) as i128,
91-
dest_layout.size,
92-
)
93-
.unwrap()
94-
} else {
95-
ty::ScalarInt::try_from_uint(discr_val, dest_layout.size).unwrap()
92+
93+
let val = match dest_layout.ty.kind() {
94+
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
95+
let lsb = fx.bcx.ins().iconst(types::I64, discr_val as u64 as i64);
96+
let msb = fx.bcx.ins().iconst(types::I64, (discr_val >> 64) as u64 as i64);
97+
fx.bcx.ins().iconcat(lsb, msb)
98+
}
99+
ty::Uint(_) | ty::Int(_) => {
100+
let clif_ty = fx.clif_type(dest_layout.ty).unwrap();
101+
let raw_val = dest_layout.size.truncate(discr_val);
102+
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
103+
}
104+
_ => unreachable!(),
96105
};
97-
let res = CValue::const_val(fx, dest_layout, discr_val);
106+
let res = CValue::by_val(val, dest_layout);
98107
dest.write_cvalue(fx, res);
99108
return;
100109
}

0 commit comments

Comments
 (0)