Skip to content

Commit

Permalink
Rollup merge of rust-lang#49540 - bjorn3:fix_miri_discriminant, r=oli…
Browse files Browse the repository at this point in the history
…-obk

Fix miri Discriminant() for non-ADT

Fixes rust-lang#49327
  • Loading branch information
kennytm authored Apr 4, 2018
2 parents 6c8b809 + 7a28ffc commit b5ceda8
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/librustc_mir/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>());
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::<Vec<_>>());
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)?;
}
}

Expand Down

0 comments on commit b5ceda8

Please sign in to comment.