Skip to content

Commit 25930e4

Browse files
committed
update codegen of discriminant_value
1 parent b6975bf commit 25930e4

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
188188
}
189189
"size_of" | "pref_align_of" | "min_align_of" | "needs_drop" | "type_id"
190190
| "type_name" => {
191-
let ty_name = self
191+
let value = self
192192
.tcx
193193
.const_eval_instance(ty::ParamEnv::reveal_all(), instance, None)
194194
.unwrap();
195-
OperandRef::from_const(self, ty_name, ret_ty).immediate_or_packed_pair(self)
195+
OperandRef::from_const(self, value, ret_ty).immediate_or_packed_pair(self)
196196
}
197197
// Effectively no-op
198198
"forget" => {
@@ -549,7 +549,13 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
549549
}
550550
}
551551

552-
"discriminant_value" => args[0].deref(self.cx()).codegen_get_discr(self, ret_ty),
552+
"discriminant_value" => {
553+
if ret_ty.is_integral() {
554+
args[0].deref(self.cx()).codegen_get_discr(self, ret_ty)
555+
} else {
556+
span_bug!(span, "Invalid discriminant type for `{:?}`", arg_tys[0])
557+
}
558+
}
553559

554560
name if name.starts_with("simd_") => {
555561
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
33
//! and miri.
44
5-
use std::convert::TryFrom;
6-
75
use rustc_hir::def_id::DefId;
86
use rustc_middle::mir::{
97
self,
@@ -220,7 +218,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
220218
sym::discriminant_value => {
221219
let place = self.deref_operand(args[0])?;
222220
let discr_val = self.read_discriminant(place.into())?.0;
223-
self.write_scalar(Scalar::from_u64(u64::try_from(discr_val).unwrap()), dest)?;
221+
let scalar = match dest.layout.ty.kind {
222+
ty::Int(_) => Scalar::from_int(discr_val as i128, dest.layout.size),
223+
ty::Uint(_) => Scalar::from_uint(discr_val, dest.layout.size),
224+
_ => bug!("invalid `discriminant_value` return layout: {:?}", dest.layout),
225+
};
226+
self.write_scalar(scalar, dest)?;
224227
}
225228
sym::unchecked_shl
226229
| sym::unchecked_shr

src/librustc_typeck/check/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir as hir;
88
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
99
use rustc_middle::ty::subst::Subst;
1010
use rustc_middle::ty::{self, Ty, TyCtxt};
11-
use rustc_span::symbol::{Ident, Symbol};
11+
use rustc_span::symbol::Symbol;
1212
use rustc_target::spec::abi::Abi;
1313

1414
use std::iter;

0 commit comments

Comments
 (0)