From 7a28ffce90271b82f89fd6a3fa110b5c25795ce2 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 31 Mar 2018 12:47:49 +0200 Subject: [PATCH] Fix miri Discriminant() for non-ADT --- src/librustc_mir/interpret/eval_context.rs | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index b8bfcd756cd23..cf3241fe9be66 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -743,20 +743,29 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M Discriminant(ref place) => { let ty = self.place_ty(place); + let layout = self.layout_of(ty)?; let place = self.eval_place(place)?; let discr_val = self.read_discriminant_value(place, ty)?; - if let ty::TyAdt(adt_def, _) = ty.sty { - trace!("Read discriminant {}, valid discriminants {:?}", discr_val, adt_def.discriminants(*self.tcx).collect::>()); - if adt_def.discriminants(*self.tcx).all(|v| { - discr_val != v.val - }) - { - return err!(InvalidDiscriminant); + match layout.variants { + layout::Variants::Single { index } => { + assert_eq!(discr_val, index as u128); + } + layout::Variants::Tagged { .. } | + layout::Variants::NicheFilling { .. } => { + if let ty::TyAdt(adt_def, _) = ty.sty { + trace!("Read discriminant {}, valid discriminants {:?}", discr_val, adt_def.discriminants(*self.tcx).collect::>()); + if adt_def.discriminants(*self.tcx).all(|v| { + discr_val != v.val + }) + { + return err!(InvalidDiscriminant); + } + } else { + bug!("rustc only generates Rvalue::Discriminant for enums"); + } } - self.write_primval(dest, PrimVal::Bytes(discr_val), dest_ty)?; - } else { - bug!("rustc only generates Rvalue::Discriminant for enums"); } + self.write_primval(dest, PrimVal::Bytes(discr_val), dest_ty)?; } }