Skip to content

Commit ad7179d

Browse files
committed
fix discriminant_ty for non-enums
1 parent 64fbe2f commit ad7179d

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

src/librustc_middle/mir/tcx.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ impl<'tcx> Rvalue<'tcx> {
173173
tcx.intern_tup(&[ty, tcx.types.bool])
174174
}
175175
Rvalue::UnaryOp(UnOp::Not | UnOp::Neg, ref operand) => operand.ty(local_decls, tcx),
176-
Rvalue::Discriminant(ref place) => {
177-
place.ty(local_decls, tcx).ty.discriminant_type(tcx)
178-
}
176+
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
179177
Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
180178
Rvalue::NullaryOp(NullOp::SizeOf, _) => tcx.types.usize,
181179
Rvalue::Aggregate(ref ak, ref ops) => match **ak {

src/librustc_middle/ty/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,8 @@ impl ReprOptions {
20402040
self.flags.contains(ReprFlags::HIDE_NICHE)
20412041
}
20422042

2043+
/// Returns the discriminant type, given these `repr` options.
2044+
/// This must only be called on enums!
20432045
pub fn discr_type(&self) -> attr::IntType {
20442046
self.int.unwrap_or(attr::SignedInt(ast::IntTy::Isize))
20452047
}
@@ -2272,6 +2274,7 @@ impl<'tcx> AdtDef {
22722274

22732275
#[inline]
22742276
pub fn eval_explicit_discr(&self, tcx: TyCtxt<'tcx>, expr_did: DefId) -> Option<Discr<'tcx>> {
2277+
assert!(self.is_enum());
22752278
let param_env = tcx.param_env(expr_did);
22762279
let repr_type = self.repr.discr_type();
22772280
match tcx.const_eval_poly(expr_did) {
@@ -2308,6 +2311,7 @@ impl<'tcx> AdtDef {
23082311
&'tcx self,
23092312
tcx: TyCtxt<'tcx>,
23102313
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + Captures<'tcx> {
2314+
assert!(self.is_enum());
23112315
let repr_type = self.repr.discr_type();
23122316
let initial = repr_type.initial_discriminant(tcx);
23132317
let mut prev_discr = None::<Discr<'tcx>>;
@@ -2340,6 +2344,7 @@ impl<'tcx> AdtDef {
23402344
tcx: TyCtxt<'tcx>,
23412345
variant_index: VariantIdx,
23422346
) -> Discr<'tcx> {
2347+
assert!(self.is_enum());
23432348
let (val, offset) = self.discriminant_def_for_variant(variant_index);
23442349
let explicit_value = val
23452350
.and_then(|expr_did| self.eval_explicit_discr(tcx, expr_did))

src/librustc_middle/ty/sty.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -2097,7 +2097,9 @@ impl<'tcx> TyS<'tcx> {
20972097
variant_index: VariantIdx,
20982098
) -> Option<Discr<'tcx>> {
20992099
match self.kind {
2100-
TyKind::Adt(adt, _) => Some(adt.discriminant_for_variant(tcx, variant_index)),
2100+
TyKind::Adt(adt, _) if adt.is_enum() => {
2101+
Some(adt.discriminant_for_variant(tcx, variant_index))
2102+
}
21012103
TyKind::Generator(def_id, substs, _) => {
21022104
Some(substs.as_generator().discriminant_for_variant(def_id, tcx, variant_index))
21032105
}
@@ -2106,11 +2108,14 @@ impl<'tcx> TyS<'tcx> {
21062108
}
21072109

21082110
/// Returns the type of the discriminant of this type.
2109-
pub fn discriminant_type(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
2111+
pub fn discriminant_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
21102112
match self.kind {
2111-
ty::Adt(adt_def, _) => adt_def.repr.discr_type().to_ty(tcx),
2113+
ty::Adt(adt, _) if adt.is_enum() => adt.repr.discr_type().to_ty(tcx),
21122114
ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx),
2113-
_ => bug!("{:?} does not have a discriminant", self),
2115+
_ => {
2116+
// This can only be `0`, for now, so `u8` will suffice.
2117+
tcx.types.u8
2118+
}
21142119
}
21152120
}
21162121

src/librustc_mir/interpret/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
581581
trace!("read_discriminant_value {:#?}", op.layout);
582582

583583
// Get type and layout of the discriminant.
584-
let discr_layout = self.layout_of(op.layout.ty.discriminant_type(*self.tcx))?;
584+
let discr_layout = self.layout_of(op.layout.ty.discriminant_ty(*self.tcx))?;
585585
trace!("discriminant type: {:?}", discr_layout.ty);
586586

587-
// We use "discriminant" to refer to the value associated with a particualr enum variant.
587+
// We use "discriminant" to refer to the value associated with a particular enum variant.
588588
// This is not to be confused with its "variant index", which is just determining its position in the
589589
// declared list of variants -- they can differ with explicitly assigned discriminants.
590590
// We use "tag" to refer to how the discriminant is encoded in memory, which can be either

0 commit comments

Comments
 (0)