mirror of
https://github.com/rust-lang/rust
synced 2024-10-14 12:33:57 +00:00
Rollup merge of #72419 - RalfJung:read-discriminant, r=oli-obk,eddyb
Miri read_discriminant: return a scalar instead of raw underlying bytes r? @oli-obk @eddyb
This commit is contained in:
commit
a5fb7fcab3
|
@ -1,4 +1,4 @@
|
|||
use super::{AllocId, Pointer, RawConst, ScalarMaybeUninit};
|
||||
use super::{AllocId, Pointer, RawConst, Scalar};
|
||||
|
||||
use crate::mir::interpret::ConstValue;
|
||||
use crate::ty::layout::LayoutError;
|
||||
|
@ -391,7 +391,7 @@ pub enum UndefinedBehaviorInfo<'tcx> {
|
|||
/// Using a non-character `u32` as character.
|
||||
InvalidChar(u32),
|
||||
/// An enum discriminant was set to a value which was outside the range of valid values.
|
||||
InvalidDiscriminant(ScalarMaybeUninit),
|
||||
InvalidDiscriminant(Scalar),
|
||||
/// Using a pointer-not-to-a-function as function pointer.
|
||||
InvalidFunctionPointer(Pointer),
|
||||
/// Using a string that is not valid UTF-8,
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
use crate::mir::*;
|
||||
use crate::ty::subst::Subst;
|
||||
use crate::ty::util::IntTypeExt;
|
||||
use crate::ty::{self, Ty, TyCtxt};
|
||||
use rustc_hir as hir;
|
||||
use rustc_target::abi::VariantIdx;
|
||||
|
@ -174,17 +173,7 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
|
|||
tcx.intern_tup(&[ty, tcx.types.bool])
|
||||
}
|
||||
Rvalue::UnaryOp(UnOp::Not | UnOp::Neg, ref operand) => operand.ty(local_decls, tcx),
|
||||
Rvalue::Discriminant(ref place) => {
|
||||
let ty = place.ty(local_decls, tcx).ty;
|
||||
match ty.kind {
|
||||
ty::Adt(adt_def, _) => adt_def.repr.discr_type().to_ty(tcx),
|
||||
ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx),
|
||||
_ => {
|
||||
// This can only be `0`, for now, so `u8` will suffice.
|
||||
tcx.types.u8
|
||||
}
|
||||
}
|
||||
}
|
||||
Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
|
||||
Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
|
||||
Rvalue::NullaryOp(NullOp::SizeOf, _) => tcx.types.usize,
|
||||
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
|
||||
|
|
|
@ -2037,6 +2037,8 @@ pub fn hide_niche(&self) -> bool {
|
|||
self.flags.contains(ReprFlags::HIDE_NICHE)
|
||||
}
|
||||
|
||||
/// Returns the discriminant type, given these `repr` options.
|
||||
/// This must only be called on enums!
|
||||
pub fn discr_type(&self) -> attr::IntType {
|
||||
self.int.unwrap_or(attr::SignedInt(ast::IntTy::Isize))
|
||||
}
|
||||
|
@ -2269,6 +2271,7 @@ pub fn variant_of_res(&self, res: Res) -> &VariantDef {
|
|||
|
||||
#[inline]
|
||||
pub fn eval_explicit_discr(&self, tcx: TyCtxt<'tcx>, expr_did: DefId) -> Option<Discr<'tcx>> {
|
||||
assert!(self.is_enum());
|
||||
let param_env = tcx.param_env(expr_did);
|
||||
let repr_type = self.repr.discr_type();
|
||||
match tcx.const_eval_poly(expr_did) {
|
||||
|
@ -2305,6 +2308,7 @@ pub fn discriminants(
|
|||
&'tcx self,
|
||||
tcx: TyCtxt<'tcx>,
|
||||
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + Captures<'tcx> {
|
||||
assert!(self.is_enum());
|
||||
let repr_type = self.repr.discr_type();
|
||||
let initial = repr_type.initial_discriminant(tcx);
|
||||
let mut prev_discr = None::<Discr<'tcx>>;
|
||||
|
@ -2337,6 +2341,7 @@ pub fn discriminant_for_variant(
|
|||
tcx: TyCtxt<'tcx>,
|
||||
variant_index: VariantIdx,
|
||||
) -> Discr<'tcx> {
|
||||
assert!(self.is_enum());
|
||||
let (val, offset) = self.discriminant_def_for_variant(variant_index);
|
||||
let explicit_value = val
|
||||
.and_then(|expr_did| self.eval_explicit_discr(tcx, expr_did))
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
use std::cmp::Ordering;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Range;
|
||||
use ty::util::IntTypeExt;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
|
||||
#[derive(HashStable, TypeFoldable, Lift)]
|
||||
|
@ -2096,7 +2097,9 @@ pub fn discriminant_for_variant(
|
|||
variant_index: VariantIdx,
|
||||
) -> Option<Discr<'tcx>> {
|
||||
match self.kind {
|
||||
TyKind::Adt(adt, _) => Some(adt.discriminant_for_variant(tcx, variant_index)),
|
||||
TyKind::Adt(adt, _) if adt.is_enum() => {
|
||||
Some(adt.discriminant_for_variant(tcx, variant_index))
|
||||
}
|
||||
TyKind::Generator(def_id, substs, _) => {
|
||||
Some(substs.as_generator().discriminant_for_variant(def_id, tcx, variant_index))
|
||||
}
|
||||
|
@ -2104,6 +2107,18 @@ pub fn discriminant_for_variant(
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns the type of the discriminant of this type.
|
||||
pub fn discriminant_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
||||
match self.kind {
|
||||
ty::Adt(adt, _) if adt.is_enum() => adt.repr.discr_type().to_ty(tcx),
|
||||
ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx),
|
||||
_ => {
|
||||
// This can only be `0`, for now, so `u8` will suffice.
|
||||
tcx.types.u8
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// When we create a closure, we record its kind (i.e., what trait
|
||||
/// it implements) into its `ClosureSubsts` using a type
|
||||
/// parameter. This is kind of a phantom type, except that the
|
||||
|
|
|
@ -220,15 +220,7 @@ pub fn emulate_intrinsic(
|
|||
sym::discriminant_value => {
|
||||
let place = self.deref_operand(args[0])?;
|
||||
let discr_val = self.read_discriminant(place.into())?.0;
|
||||
let scalar = match dest.layout.ty.kind {
|
||||
ty::Int(_) => Scalar::from_int(
|
||||
self.sign_extend(discr_val, dest.layout) as i128,
|
||||
dest.layout.size,
|
||||
),
|
||||
ty::Uint(_) => Scalar::from_uint(discr_val, dest.layout.size),
|
||||
_ => bug!("invalid `discriminant_value` return layout: {:?}", dest.layout),
|
||||
};
|
||||
self.write_scalar(scalar, dest)?;
|
||||
self.write_scalar(discr_val, dest)?;
|
||||
}
|
||||
sym::unchecked_shl
|
||||
| sym::unchecked_shr
|
||||
|
|
|
@ -7,15 +7,15 @@
|
|||
use rustc_errors::ErrorReported;
|
||||
use rustc_hir::def::Namespace;
|
||||
use rustc_macros::HashStable;
|
||||
use rustc_middle::ty::layout::{IntegerExt, PrimitiveExt, TyAndLayout};
|
||||
use rustc_middle::ty::layout::{PrimitiveExt, TyAndLayout};
|
||||
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
|
||||
use rustc_middle::ty::Ty;
|
||||
use rustc_middle::{mir, ty};
|
||||
use rustc_target::abi::{Abi, DiscriminantKind, HasDataLayout, Integer, LayoutOf, Size};
|
||||
use rustc_target::abi::{Abi, DiscriminantKind, HasDataLayout, LayoutOf, Size};
|
||||
use rustc_target::abi::{VariantIdx, Variants};
|
||||
|
||||
use super::{
|
||||
from_known_layout, sign_extend, truncate, ConstValue, GlobalId, InterpCx, InterpResult,
|
||||
from_known_layout, mir_assign_valid_types, ConstValue, GlobalId, InterpCx, InterpResult,
|
||||
MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Scalar, ScalarMaybeUninit,
|
||||
};
|
||||
|
||||
|
@ -469,6 +469,14 @@ pub fn eval_place_to_op(
|
|||
.try_fold(base_op, |op, elem| self.operand_projection(op, elem))?;
|
||||
|
||||
trace!("eval_place_to_op: got {:?}", *op);
|
||||
// Sanity-check the type we ended up with.
|
||||
debug_assert!(mir_assign_valid_types(
|
||||
*self.tcx,
|
||||
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
|
||||
place.ty(&self.frame().body.local_decls, *self.tcx).ty
|
||||
))?,
|
||||
op.layout,
|
||||
));
|
||||
Ok(op)
|
||||
}
|
||||
|
||||
|
@ -576,98 +584,113 @@ pub(super) fn eval_operands(
|
|||
/// Read discriminant, return the runtime value as well as the variant index.
|
||||
pub fn read_discriminant(
|
||||
&self,
|
||||
rval: OpTy<'tcx, M::PointerTag>,
|
||||
) -> InterpResult<'tcx, (u128, VariantIdx)> {
|
||||
trace!("read_discriminant_value {:#?}", rval.layout);
|
||||
op: OpTy<'tcx, M::PointerTag>,
|
||||
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, VariantIdx)> {
|
||||
trace!("read_discriminant_value {:#?}", op.layout);
|
||||
|
||||
let (discr_layout, discr_kind, discr_index) = match rval.layout.variants {
|
||||
// Get type and layout of the discriminant.
|
||||
let discr_layout = self.layout_of(op.layout.ty.discriminant_ty(*self.tcx))?;
|
||||
trace!("discriminant type: {:?}", discr_layout.ty);
|
||||
|
||||
// We use "discriminant" to refer to the value associated with a particular enum variant.
|
||||
// This is not to be confused with its "variant index", which is just determining its position in the
|
||||
// declared list of variants -- they can differ with explicitly assigned discriminants.
|
||||
// We use "tag" to refer to how the discriminant is encoded in memory, which can be either
|
||||
// straight-forward (`DiscriminantKind::Tag`) or with a niche (`DiscriminantKind::Niche`).
|
||||
// Unfortunately, the rest of the compiler calls the latter "discriminant", too, which makes things
|
||||
// rather confusing.
|
||||
let (tag_scalar_layout, tag_kind, tag_index) = match op.layout.variants {
|
||||
Variants::Single { index } => {
|
||||
let discr_val = rval
|
||||
.layout
|
||||
.ty
|
||||
.discriminant_for_variant(*self.tcx, index)
|
||||
.map_or(u128::from(index.as_u32()), |discr| discr.val);
|
||||
return Ok((discr_val, index));
|
||||
let discr = match op.layout.ty.discriminant_for_variant(*self.tcx, index) {
|
||||
Some(discr) => {
|
||||
// This type actually has discriminants.
|
||||
assert_eq!(discr.ty, discr_layout.ty);
|
||||
Scalar::from_uint(discr.val, discr_layout.size)
|
||||
}
|
||||
None => {
|
||||
// On a type without actual discriminants, variant is 0.
|
||||
assert_eq!(index.as_u32(), 0);
|
||||
Scalar::from_uint(index.as_u32(), discr_layout.size)
|
||||
}
|
||||
};
|
||||
return Ok((discr, index));
|
||||
}
|
||||
Variants::Multiple { discr: ref discr_layout, ref discr_kind, discr_index, .. } => {
|
||||
(discr_layout, discr_kind, discr_index)
|
||||
Variants::Multiple { ref discr, ref discr_kind, discr_index, .. } => {
|
||||
(discr, discr_kind, discr_index)
|
||||
}
|
||||
};
|
||||
|
||||
// read raw discriminant value
|
||||
let discr_op = self.operand_field(rval, discr_index)?;
|
||||
let discr_val = self.read_immediate(discr_op)?;
|
||||
let raw_discr = discr_val.to_scalar_or_undef();
|
||||
trace!("discr value: {:?}", raw_discr);
|
||||
// post-process
|
||||
Ok(match *discr_kind {
|
||||
// There are *three* layouts that come into play here:
|
||||
// - The discriminant has a type for typechecking. This is `discr_layout`, and is used for
|
||||
// the `Scalar` we return.
|
||||
// - The tag (encoded discriminant) has layout `tag_layout`. This is always an integer type,
|
||||
// and used to interpret the value we read from the tag field.
|
||||
// For the return value, a cast to `discr_layout` is performed.
|
||||
// - The field storing the tag has a layout, which is very similar to `tag_layout` but
|
||||
// may be a pointer. This is `tag_val.layout`; we just use it for sanity checks.
|
||||
|
||||
// Get layout for tag.
|
||||
let tag_layout = self.layout_of(tag_scalar_layout.value.to_int_ty(*self.tcx))?;
|
||||
|
||||
// Read tag and sanity-check `tag_layout`.
|
||||
let tag_val = self.read_immediate(self.operand_field(op, tag_index)?)?;
|
||||
assert_eq!(tag_layout.size, tag_val.layout.size);
|
||||
assert_eq!(tag_layout.abi.is_signed(), tag_val.layout.abi.is_signed());
|
||||
let tag_val = tag_val.to_scalar()?;
|
||||
trace!("tag value: {:?}", tag_val);
|
||||
|
||||
// Figure out which discriminant and variant this corresponds to.
|
||||
Ok(match *tag_kind {
|
||||
DiscriminantKind::Tag => {
|
||||
let bits_discr = raw_discr
|
||||
.not_undef()
|
||||
.and_then(|raw_discr| self.force_bits(raw_discr, discr_val.layout.size))
|
||||
.map_err(|_| err_ub!(InvalidDiscriminant(raw_discr.erase_tag())))?;
|
||||
let real_discr = if discr_val.layout.abi.is_signed() {
|
||||
// going from layout tag type to typeck discriminant type
|
||||
// requires first sign extending with the discriminant layout
|
||||
let sexted = sign_extend(bits_discr, discr_val.layout.size);
|
||||
// and then zeroing with the typeck discriminant type
|
||||
let discr_ty = rval
|
||||
.layout
|
||||
.ty
|
||||
.ty_adt_def()
|
||||
.expect("tagged layout corresponds to adt")
|
||||
.repr
|
||||
.discr_type();
|
||||
let size = Integer::from_attr(self, discr_ty).size();
|
||||
truncate(sexted, size)
|
||||
} else {
|
||||
bits_discr
|
||||
};
|
||||
// Make sure we catch invalid discriminants
|
||||
let index = match rval.layout.ty.kind {
|
||||
let tag_bits = self
|
||||
.force_bits(tag_val, tag_layout.size)
|
||||
.map_err(|_| err_ub!(InvalidDiscriminant(tag_val.erase_tag())))?;
|
||||
// Cast bits from tag layout to discriminant layout.
|
||||
let discr_val_cast = self.cast_from_scalar(tag_bits, tag_layout, discr_layout.ty);
|
||||
let discr_bits = discr_val_cast.assert_bits(discr_layout.size);
|
||||
// Convert discriminant to variant index, and catch invalid discriminants.
|
||||
let index = match op.layout.ty.kind {
|
||||
ty::Adt(adt, _) => {
|
||||
adt.discriminants(self.tcx.tcx).find(|(_, var)| var.val == real_discr)
|
||||
adt.discriminants(self.tcx.tcx).find(|(_, var)| var.val == discr_bits)
|
||||
}
|
||||
ty::Generator(def_id, substs, _) => {
|
||||
let substs = substs.as_generator();
|
||||
substs
|
||||
.discriminants(def_id, self.tcx.tcx)
|
||||
.find(|(_, var)| var.val == real_discr)
|
||||
.find(|(_, var)| var.val == discr_bits)
|
||||
}
|
||||
_ => bug!("tagged layout for non-adt non-generator"),
|
||||
}
|
||||
.ok_or_else(|| err_ub!(InvalidDiscriminant(raw_discr.erase_tag())))?;
|
||||
(real_discr, index.0)
|
||||
.ok_or_else(|| err_ub!(InvalidDiscriminant(tag_val.erase_tag())))?;
|
||||
// Return the cast value, and the index.
|
||||
(discr_val_cast, index.0)
|
||||
}
|
||||
DiscriminantKind::Niche { dataful_variant, ref niche_variants, niche_start } => {
|
||||
// Compute the variant this niche value/"tag" corresponds to. With niche layout,
|
||||
// discriminant (encoded in niche/tag) and variant index are the same.
|
||||
let variants_start = niche_variants.start().as_u32();
|
||||
let variants_end = niche_variants.end().as_u32();
|
||||
let raw_discr = raw_discr
|
||||
.not_undef()
|
||||
.map_err(|_| err_ub!(InvalidDiscriminant(ScalarMaybeUninit::Uninit)))?;
|
||||
match raw_discr.to_bits_or_ptr(discr_val.layout.size, self) {
|
||||
let variant = match tag_val.to_bits_or_ptr(tag_layout.size, self) {
|
||||
Err(ptr) => {
|
||||
// The niche must be just 0 (which an inbounds pointer value never is)
|
||||
let ptr_valid = niche_start == 0
|
||||
&& variants_start == variants_end
|
||||
&& !self.memory.ptr_may_be_null(ptr);
|
||||
if !ptr_valid {
|
||||
throw_ub!(InvalidDiscriminant(raw_discr.erase_tag().into()))
|
||||
throw_ub!(InvalidDiscriminant(tag_val.erase_tag()))
|
||||
}
|
||||
(u128::from(dataful_variant.as_u32()), dataful_variant)
|
||||
dataful_variant
|
||||
}
|
||||
Ok(raw_discr) => {
|
||||
Ok(tag_bits) => {
|
||||
// We need to use machine arithmetic to get the relative variant idx:
|
||||
// variant_index_relative = discr_val - niche_start_val
|
||||
let discr_layout =
|
||||
self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
|
||||
let discr_val = ImmTy::from_uint(raw_discr, discr_layout);
|
||||
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
|
||||
// variant_index_relative = tag_val - niche_start_val
|
||||
let tag_val = ImmTy::from_uint(tag_bits, tag_layout);
|
||||
let niche_start_val = ImmTy::from_uint(niche_start, tag_layout);
|
||||
let variant_index_relative_val =
|
||||
self.binary_op(mir::BinOp::Sub, discr_val, niche_start_val)?;
|
||||
self.binary_op(mir::BinOp::Sub, tag_val, niche_start_val)?;
|
||||
let variant_index_relative = variant_index_relative_val
|
||||
.to_scalar()?
|
||||
.assert_bits(discr_val.layout.size);
|
||||
.assert_bits(tag_val.layout.size);
|
||||
// Check if this is in the range that indicates an actual discriminant.
|
||||
if variant_index_relative <= u128::from(variants_end - variants_start) {
|
||||
let variant_index_relative = u32::try_from(variant_index_relative)
|
||||
|
@ -676,7 +699,7 @@ pub fn read_discriminant(
|
|||
let variant_index = variants_start
|
||||
.checked_add(variant_index_relative)
|
||||
.expect("overflow computing absolute variant idx");
|
||||
let variants_len = rval
|
||||
let variants_len = op
|
||||
.layout
|
||||
.ty
|
||||
.ty_adt_def()
|
||||
|
@ -684,12 +707,16 @@ pub fn read_discriminant(
|
|||
.variants
|
||||
.len();
|
||||
assert!(usize::try_from(variant_index).unwrap() < variants_len);
|
||||
(u128::from(variant_index), VariantIdx::from_u32(variant_index))
|
||||
VariantIdx::from_u32(variant_index)
|
||||
} else {
|
||||
(u128::from(dataful_variant.as_u32()), dataful_variant)
|
||||
dataful_variant
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
// Compute the size of the scalar we need to return.
|
||||
// No need to cast, because the variant index directly serves as discriminant and is
|
||||
// encoded in the tag.
|
||||
(Scalar::from_uint(variant.as_u32(), discr_layout.size), variant)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -638,6 +638,14 @@ pub fn eval_place(
|
|||
}
|
||||
|
||||
self.dump_place(place_ty.place);
|
||||
// Sanity-check the type we ended up with.
|
||||
debug_assert!(mir_assign_valid_types(
|
||||
*self.tcx,
|
||||
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
|
||||
place.ty(&self.frame().body.local_decls, *self.tcx).ty
|
||||
))?,
|
||||
place_ty.layout,
|
||||
));
|
||||
Ok(place_ty)
|
||||
}
|
||||
|
||||
|
|
|
@ -262,8 +262,7 @@ pub fn eval_rvalue_into_place(
|
|||
Discriminant(place) => {
|
||||
let op = self.eval_place_to_op(place, None)?;
|
||||
let discr_val = self.read_discriminant(op)?.0;
|
||||
let size = dest.layout.size;
|
||||
self.write_scalar(Scalar::from_uint(discr_val, size), dest)?;
|
||||
self.write_scalar(discr_val, dest)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue