Skip to content

Commit ecba16f

Browse files
committed
Various enum to int cast fixes
1 parent 7fc3483 commit ecba16f

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

src/base.rs

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -627,50 +627,22 @@ fn codegen_stmt<'tcx>(
627627
ty::Uint(_) | ty::Int(_) => {}
628628
_ => unreachable!("cast adt {} -> {}", from_ty, to_ty),
629629
}
630+
let to_clif_ty = fx.clif_type(to_ty).unwrap();
630631

631-
use rustc_target::abi::{Int, TagEncoding, Variants};
632-
633-
match operand.layout().variants {
634-
Variants::Single { index } => {
635-
let discr = operand
636-
.layout()
637-
.ty
638-
.discriminant_for_variant(fx.tcx, index)
639-
.unwrap();
640-
let discr = if discr.ty.is_signed() {
641-
fx.layout_of(discr.ty).size.sign_extend(discr.val)
642-
} else {
643-
discr.val
644-
};
645-
let discr = discr.into();
646-
647-
let discr = CValue::const_val(fx, fx.layout_of(to_ty), discr);
648-
lval.write_cvalue(fx, discr);
649-
}
650-
Variants::Multiple {
651-
ref tag,
652-
tag_field,
653-
tag_encoding: TagEncoding::Direct,
654-
variants: _,
655-
} => {
656-
let cast_to = fx.clif_type(dest_layout.ty).unwrap();
657-
658-
// Read the tag/niche-encoded discriminant from memory.
659-
let encoded_discr =
660-
operand.value_field(fx, mir::Field::new(tag_field));
661-
let encoded_discr = encoded_discr.load_scalar(fx);
662-
663-
// Decode the discriminant (specifically if it's niche-encoded).
664-
let signed = match tag.value {
665-
Int(_, signed) => signed,
666-
_ => false,
667-
};
668-
let val = clif_intcast(fx, encoded_discr, cast_to, signed);
669-
let val = CValue::by_val(val, dest_layout);
670-
lval.write_cvalue(fx, val);
671-
}
672-
Variants::Multiple { .. } => unreachable!(),
673-
}
632+
let discriminant = crate::discriminant::codegen_get_discriminant(
633+
fx,
634+
operand,
635+
fx.layout_of(operand.layout().ty.discriminant_ty(fx.tcx)),
636+
)
637+
.load_scalar(fx);
638+
639+
let res = crate::cast::clif_intcast(
640+
fx,
641+
discriminant,
642+
to_clif_ty,
643+
to_ty.is_signed(),
644+
);
645+
lval.write_cvalue(fx, CValue::by_val(res, dest_layout));
674646
} else {
675647
let to_clif_ty = fx.clif_type(to_ty).unwrap();
676648
let from = operand.load_scalar(fx);

src/value_and_place.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ impl<'tcx> CValue<'tcx> {
233233
layout: TyAndLayout<'tcx>,
234234
const_val: ty::ScalarInt,
235235
) -> CValue<'tcx> {
236-
assert_eq!(const_val.size(), layout.size);
236+
assert_eq!(
237+
const_val.size(),
238+
layout.size,
239+
"{:#?}: {:?}",
240+
const_val,
241+
layout
242+
);
237243
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
238244

239245
let clif_ty = fx.clif_type(layout.ty).unwrap();

0 commit comments

Comments
 (0)