Skip to content

Commit 75fe84f

Browse files
committed
factor getting the discriminant layout to a new method
1 parent b73f1a5 commit 75fe84f

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/librustc/ty/layout.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl IntegerExt for Integer {
127127

128128
pub trait PrimitiveExt {
129129
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
130+
fn to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
130131
}
131132

132133
impl PrimitiveExt for Primitive {
@@ -138,6 +139,16 @@ impl PrimitiveExt for Primitive {
138139
Pointer => tcx.mk_mut_ptr(tcx.mk_unit()),
139140
}
140141
}
142+
143+
/// Return an *integer* type matching this primitive.
144+
/// Useful in particular when dealing with enum discriminants.
145+
fn to_int_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
146+
match *self {
147+
Int(i, signed) => i.to_ty(tcx, signed),
148+
Pointer => tcx.types.usize,
149+
Float(..) => bug!("floats do not have an int type"),
150+
}
151+
}
141152
}
142153

143154
/// The first half of a fat pointer.

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::convert::TryInto;
55

66
use rustc::{mir, ty};
77
use rustc::ty::layout::{
8-
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx,
8+
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, PrimitiveExt, VariantIdx,
99
};
1010

1111
use rustc::mir::interpret::{
@@ -687,13 +687,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
687687
(dataful_variant.as_u32() as u128, dataful_variant)
688688
},
689689
Ok(raw_discr) => {
690-
// FIXME: WTF, some discriminants don't have integer type.
691-
use layout::Primitive;
692-
let discr_layout = self.layout_of(match discr_layout.value {
693-
Primitive::Int(int, signed) => int.to_ty(*self.tcx, signed),
694-
Primitive::Pointer => self.tcx.types.usize,
695-
Primitive::Float(..) => bug!("there are no float discriminants"),
696-
})?;
690+
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
697691
let discr_val = ImmTy::from_uint(raw_discr, discr_layout);
698692
// We need to use machine arithmetic.
699693
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);

src/librustc_mir/interpret/place.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::mir;
99
use rustc::mir::interpret::truncate;
1010
use rustc::ty::{self, Ty};
1111
use rustc::ty::layout::{
12-
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, IntegerExt
12+
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
1313
};
1414
use rustc::ty::TypeFoldable;
1515

@@ -1060,14 +1060,7 @@ where
10601060
variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len(),
10611061
);
10621062
if variant_index != dataful_variant {
1063-
// FIXME: WTF, some discriminants don't have integer type.
1064-
use layout::Primitive;
1065-
let discr_layout = self.layout_of(match discr_layout.value {
1066-
Primitive::Int(int, signed) => int.to_ty(*self.tcx, signed),
1067-
Primitive::Pointer => self.tcx.types.usize,
1068-
Primitive::Float(..) => bug!("there are no float discriminants"),
1069-
})?;
1070-
1063+
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
10711064
// We need to use machine arithmetic.
10721065
let variants_start = niche_variants.start().as_u32();
10731066
let variants_start_val = ImmTy::from_uint(variants_start, discr_layout);

0 commit comments

Comments
 (0)