2018-08-13 16:14:22 +02:00
|
|
|
//! Functions concerning immediate values and operands, and reading from operands.
|
|
|
|
//! All high-level functions to read from memory work on operands as sources.
|
|
|
|
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
2018-12-11 19:56:59 +01:00
|
|
|
use rustc::{mir, ty};
|
2018-11-01 19:03:38 +01:00
|
|
|
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx};
|
2018-07-25 16:14:04 +03:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::mir::interpret::{
|
2018-12-19 16:26:46 +01:00
|
|
|
GlobalId, AllocId, InboundsCheck,
|
2018-09-30 13:09:26 +02:00
|
|
|
ConstValue, Pointer, Scalar,
|
2018-11-20 11:04:07 +01:00
|
|
|
EvalResult, EvalErrorKind,
|
2018-08-13 16:14:22 +02:00
|
|
|
};
|
2018-08-25 14:36:24 +02:00
|
|
|
use super::{EvalContext, Machine, MemPlace, MPlaceTy, MemoryKind};
|
2018-10-23 18:19:34 +02:00
|
|
|
pub use rustc::mir::interpret::ScalarMaybeUndef;
|
2018-09-30 13:09:26 +02:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
/// A `Value` represents a single immediate self-contained Rust value.
|
|
|
|
///
|
|
|
|
/// For optimization of a few very common cases, there is also a representation for a pair of
|
|
|
|
/// primitive values (`ScalarPair`). It allows Miri to avoid making allocations for checked binary
|
|
|
|
/// operations and fat pointers. This idea was taken from rustc's codegen.
|
|
|
|
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
|
2018-11-02 12:51:26 +01:00
|
|
|
/// defined on `Immediate`, and do not have to work with a `Place`.
|
2018-08-13 16:14:22 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub enum Immediate<Tag=(), Id=AllocId> {
|
2018-10-05 10:28:33 +02:00
|
|
|
Scalar(ScalarMaybeUndef<Tag, Id>),
|
|
|
|
ScalarPair(ScalarMaybeUndef<Tag, Id>, ScalarMaybeUndef<Tag, Id>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
impl Immediate {
|
2018-09-21 23:32:59 +02:00
|
|
|
#[inline]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub fn with_default_tag<Tag>(self) -> Immediate<Tag>
|
2018-09-21 23:32:59 +02:00
|
|
|
where Tag: Default
|
|
|
|
{
|
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(x) => Immediate::Scalar(x.with_default_tag()),
|
|
|
|
Immediate::ScalarPair(x, y) =>
|
|
|
|
Immediate::ScalarPair(x.with_default_tag(), y.with_default_tag()),
|
2018-09-21 23:32:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
impl<'tcx, Tag> Immediate<Tag> {
|
2018-09-21 23:32:59 +02:00
|
|
|
#[inline]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub fn erase_tag(self) -> Immediate
|
2018-09-21 23:32:59 +02:00
|
|
|
{
|
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(x) => Immediate::Scalar(x.erase_tag()),
|
|
|
|
Immediate::ScalarPair(x, y) =>
|
|
|
|
Immediate::ScalarPair(x.erase_tag(), y.erase_tag()),
|
2018-09-21 23:32:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn new_slice(
|
2018-09-21 23:32:59 +02:00
|
|
|
val: Scalar<Tag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
len: u64,
|
2018-11-03 22:57:53 +02:00
|
|
|
cx: &impl HasDataLayout
|
2018-08-13 16:14:22 +02:00
|
|
|
) -> Self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::ScalarPair(
|
|
|
|
val.into(),
|
|
|
|
Scalar::from_uint(len, cx.data_layout().pointer_size).into(),
|
|
|
|
)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::ScalarPair(val.into(), Scalar::Ptr(vtable).into())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn to_scalar_or_undef(self) -> ScalarMaybeUndef<Tag> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(val) => val,
|
|
|
|
Immediate::ScalarPair(..) => bug!("Got a fat pointer where a scalar was expected"),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn to_scalar(self) -> EvalResult<'tcx, Scalar<Tag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
self.to_scalar_or_undef().not_undef()
|
|
|
|
}
|
|
|
|
|
2018-08-25 14:36:24 +02:00
|
|
|
#[inline]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn to_scalar_pair(self) -> EvalResult<'tcx, (Scalar<Tag>, Scalar<Tag>)> {
|
2018-08-25 14:36:24 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(..) => bug!("Got a thin pointer where a scalar pair was expected"),
|
|
|
|
Immediate::ScalarPair(a, b) => Ok((a.not_undef()?, b.not_undef()?))
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Convert the immediate into a pointer (or a pointer-sized integer).
|
2018-08-17 17:47:37 +02:00
|
|
|
/// Throws away the second half of a ScalarPair!
|
|
|
|
#[inline]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn to_scalar_ptr(self) -> EvalResult<'tcx, Scalar<Tag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(ptr) |
|
|
|
|
Immediate::ScalarPair(ptr, _) => ptr.not_undef(),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 18:01:42 +02:00
|
|
|
|
|
|
|
/// Convert the value into its metadata.
|
|
|
|
/// Throws away the first half of a ScalarPair!
|
|
|
|
#[inline]
|
|
|
|
pub fn to_meta(self) -> EvalResult<'tcx, Option<Scalar<Tag>>> {
|
|
|
|
Ok(match self {
|
2018-11-02 13:06:45 +01:00
|
|
|
Immediate::Scalar(_) => None,
|
|
|
|
Immediate::ScalarPair(_, meta) => Some(meta.not_undef()?),
|
2018-10-18 18:01:42 +02:00
|
|
|
})
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
// ScalarPair needs a type to interpret, so we often have an immediate and a type together
|
2018-08-13 16:14:22 +02:00
|
|
|
// as input for binary and cast operations.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub struct ImmTy<'tcx, Tag=()> {
|
|
|
|
immediate: Immediate<Tag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
pub layout: TyLayout<'tcx>,
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
|
|
|
|
type Target = Immediate<Tag>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
fn deref(&self) -> &Immediate<Tag> {
|
|
|
|
&self.immediate
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
|
|
|
|
/// or still in memory. The latter is an optimization, to delay reading that chunk of
|
|
|
|
/// memory and to avoid having to store arbitrary-sized data here.
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub enum Operand<Tag=(), Id=AllocId> {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate(Immediate<Tag, Id>),
|
2018-09-21 23:32:59 +02:00
|
|
|
Indirect(MemPlace<Tag, Id>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Operand {
|
|
|
|
#[inline]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn with_default_tag<Tag>(self) -> Operand<Tag>
|
|
|
|
where Tag: Default
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
Operand::Immediate(x) => Operand::Immediate(x.with_default_tag()),
|
|
|
|
Operand::Indirect(x) => Operand::Indirect(x.with_default_tag()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Tag> Operand<Tag> {
|
|
|
|
#[inline]
|
|
|
|
pub fn erase_tag(self) -> Operand
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
Operand::Immediate(x) => Operand::Immediate(x.erase_tag()),
|
|
|
|
Operand::Indirect(x) => Operand::Indirect(x.erase_tag()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn to_mem_place(self) -> MemPlace<Tag>
|
|
|
|
where Tag: ::std::fmt::Debug
|
|
|
|
{
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
|
|
|
Operand::Indirect(mplace) => mplace,
|
|
|
|
_ => bug!("to_mem_place: expected Operand::Indirect, got {:?}", self),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub fn to_immediate(self) -> Immediate<Tag>
|
2018-09-21 23:32:59 +02:00
|
|
|
where Tag: ::std::fmt::Debug
|
|
|
|
{
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Operand::Immediate(imm) => imm,
|
2018-08-13 16:14:22 +02:00
|
|
|
_ => bug!("to_immediate: expected Operand::Immediate, got {:?}", self),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:48:44 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub struct OpTy<'tcx, Tag=()> {
|
2018-10-19 14:12:42 +02:00
|
|
|
crate op: Operand<Tag>, // ideally we'd make this private, but const_prop needs this
|
2018-08-13 16:14:22 +02:00
|
|
|
pub layout: TyLayout<'tcx>,
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
impl<'tcx, Tag> ::std::ops::Deref for OpTy<'tcx, Tag> {
|
|
|
|
type Target = Operand<Tag>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-09-21 23:32:59 +02:00
|
|
|
fn deref(&self) -> &Operand<Tag> {
|
2018-08-13 16:14:22 +02:00
|
|
|
&self.op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
impl<'tcx, Tag: Copy> From<MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-09-21 23:32:59 +02:00
|
|
|
fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
|
2018-08-13 16:14:22 +02:00
|
|
|
OpTy {
|
|
|
|
op: Operand::Indirect(*mplace),
|
|
|
|
layout: mplace.layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
fn from(val: ImmTy<'tcx, Tag>) -> Self {
|
2018-08-13 16:14:22 +02:00
|
|
|
OpTy {
|
2018-10-26 12:33:26 +02:00
|
|
|
op: Operand::Immediate(val.immediate),
|
2018-08-13 16:14:22 +02:00
|
|
|
layout: val.layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
impl<'tcx, Tag> OpTy<'tcx, Tag>
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
pub fn erase_tag(self) -> OpTy<'tcx>
|
|
|
|
{
|
|
|
|
OpTy {
|
|
|
|
op: self.op.erase_tag(),
|
|
|
|
layout: self.layout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 15:27:05 +02:00
|
|
|
|
2018-08-20 15:21:04 +02:00
|
|
|
// Use the existing layout if given (but sanity check in debug mode),
|
|
|
|
// or compute the layout.
|
|
|
|
#[inline(always)]
|
|
|
|
fn from_known_layout<'tcx>(
|
|
|
|
layout: Option<TyLayout<'tcx>>,
|
|
|
|
compute: impl FnOnce() -> EvalResult<'tcx, TyLayout<'tcx>>
|
|
|
|
) -> EvalResult<'tcx, TyLayout<'tcx>> {
|
|
|
|
match layout {
|
|
|
|
None => compute(),
|
|
|
|
Some(layout) => {
|
2018-08-20 20:08:37 +02:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
let layout2 = compute()?;
|
|
|
|
assert_eq!(layout.details, layout2.details,
|
|
|
|
"Mismatch in layout of supposedly equal-layout types {:?} and {:?}",
|
|
|
|
layout.ty, layout2.ty);
|
|
|
|
}
|
2018-08-20 15:21:04 +02:00
|
|
|
Ok(layout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Try reading an immediate in memory; this is interesting particularly for ScalarPair.
|
2018-08-13 16:14:22 +02:00
|
|
|
/// Return None if the layout does not permit loading this as a value.
|
2018-10-26 12:33:26 +02:00
|
|
|
pub(super) fn try_read_immediate_from_mplace(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
mplace: MPlaceTy<'tcx, M::PointerTag>,
|
2018-10-26 12:33:26 +02:00
|
|
|
) -> EvalResult<'tcx, Option<Immediate<M::PointerTag>>> {
|
2018-08-26 14:35:15 +02:00
|
|
|
if mplace.layout.is_unsized() {
|
2018-10-22 18:21:55 +02:00
|
|
|
// Don't touch unsized
|
2018-08-16 09:36:25 +02:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let (ptr, ptr_align) = mplace.to_scalar_ptr_align();
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2018-09-15 16:34:30 +02:00
|
|
|
if mplace.layout.is_zst() {
|
2018-08-17 17:47:37 +02:00
|
|
|
// Not all ZSTs have a layout we would handle below, so just short-circuit them
|
|
|
|
// all here.
|
2018-11-16 11:46:58 +01:00
|
|
|
self.memory.check_align(ptr, ptr_align)?;
|
2018-10-26 12:33:26 +02:00
|
|
|
return Ok(Some(Immediate::Scalar(Scalar::zst().into())));
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 14:43:58 +01:00
|
|
|
// check for integer pointers before alignment to report better errors
|
2018-08-13 16:14:22 +02:00
|
|
|
let ptr = ptr.to_ptr()?;
|
2018-11-16 11:46:58 +01:00
|
|
|
self.memory.check_align(ptr.into(), ptr_align)?;
|
2018-08-16 09:36:25 +02:00
|
|
|
match mplace.layout.abi {
|
2018-08-13 16:14:22 +02:00
|
|
|
layout::Abi::Scalar(..) => {
|
2018-11-12 13:26:53 +01:00
|
|
|
let scalar = self.memory
|
|
|
|
.get(ptr.alloc_id)?
|
2018-11-15 14:43:58 +01:00
|
|
|
.read_scalar(self, ptr, mplace.layout.size)?;
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(Some(Immediate::Scalar(scalar)))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
layout::Abi::ScalarPair(ref a, ref b) => {
|
|
|
|
let (a, b) = (&a.value, &b.value);
|
|
|
|
let (a_size, b_size) = (a.size(self), b.size(self));
|
|
|
|
let a_ptr = ptr;
|
2018-09-09 01:16:45 +03:00
|
|
|
let b_offset = a_size.align_to(b.align(self).abi);
|
2018-08-13 16:14:22 +02:00
|
|
|
assert!(b_offset.bytes() > 0); // we later use the offset to test which field to use
|
2018-11-15 14:43:58 +01:00
|
|
|
let b_ptr = ptr.offset(b_offset, self)?;
|
2018-11-12 13:26:53 +01:00
|
|
|
let a_val = self.memory
|
|
|
|
.get(ptr.alloc_id)?
|
2018-11-15 14:43:58 +01:00
|
|
|
.read_scalar(self, a_ptr, a_size)?;
|
2018-11-16 11:46:58 +01:00
|
|
|
let b_align = ptr_align.restrict_for_offset(b_offset);
|
|
|
|
self.memory.check_align(b_ptr.into(), b_align)?;
|
2018-11-12 13:26:53 +01:00
|
|
|
let b_val = self.memory
|
|
|
|
.get(ptr.alloc_id)?
|
2018-11-15 14:43:58 +01:00
|
|
|
.read_scalar(self, b_ptr, b_size)?;
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(Some(Immediate::ScalarPair(a_val, b_val)))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
_ => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Try returning an immediate for the operand.
|
|
|
|
/// If the layout does not permit loading this as an immediate, return where in memory
|
2018-08-13 16:14:22 +02:00
|
|
|
/// we can find the data.
|
|
|
|
/// Note that for a given layout, this operation will either always fail or always
|
|
|
|
/// succeed! Whether it succeeds depends on whether the layout can be represented
|
2018-10-26 12:33:26 +02:00
|
|
|
/// in a `Immediate`, not on which data is stored there currently.
|
|
|
|
pub(crate) fn try_read_immediate(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
src: OpTy<'tcx, M::PointerTag>,
|
2018-10-26 12:33:26 +02:00
|
|
|
) -> EvalResult<'tcx, Result<Immediate<M::PointerTag>, MemPlace<M::PointerTag>>> {
|
2018-08-16 09:36:25 +02:00
|
|
|
Ok(match src.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
2018-10-26 12:33:26 +02:00
|
|
|
if let Some(val) = self.try_read_immediate_from_mplace(mplace)? {
|
2018-08-16 09:36:25 +02:00
|
|
|
Ok(val)
|
|
|
|
} else {
|
|
|
|
Err(*mplace)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
},
|
2018-08-16 09:36:25 +02:00
|
|
|
Err(val) => Ok(val),
|
|
|
|
})
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Read an immediate from a place, asserting that that is possible with the given layout.
|
2018-08-13 16:14:22 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub fn read_immediate(
|
2018-09-21 23:32:59 +02:00
|
|
|
&self,
|
|
|
|
op: OpTy<'tcx, M::PointerTag>
|
2018-10-26 12:33:26 +02:00
|
|
|
) -> EvalResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
|
|
|
|
if let Ok(immediate) = self.try_read_immediate(op)? {
|
|
|
|
Ok(ImmTy { immediate, layout: op.layout })
|
2018-08-13 16:14:22 +02:00
|
|
|
} else {
|
|
|
|
bug!("primitive read failed for type: {:?}", op.layout.ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a scalar from a place
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn read_scalar(
|
|
|
|
&self,
|
|
|
|
op: OpTy<'tcx, M::PointerTag>
|
|
|
|
) -> EvalResult<'tcx, ScalarMaybeUndef<M::PointerTag>> {
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(self.read_immediate(op)?.to_scalar_or_undef())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 14:36:24 +02:00
|
|
|
// Turn the MPlace into a string (must already be dereferenced!)
|
2018-08-23 19:04:33 +02:00
|
|
|
pub fn read_str(
|
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
mplace: MPlaceTy<'tcx, M::PointerTag>,
|
2018-08-23 19:04:33 +02:00
|
|
|
) -> EvalResult<'tcx, &str> {
|
2018-08-25 14:36:24 +02:00
|
|
|
let len = mplace.len(self)?;
|
2018-11-13 16:13:51 +01:00
|
|
|
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
|
2018-08-25 14:36:24 +02:00
|
|
|
let str = ::std::str::from_utf8(bytes)
|
|
|
|
.map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
|
|
|
|
Ok(str)
|
2018-08-23 19:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn uninit_operand(
|
|
|
|
&mut self,
|
|
|
|
layout: TyLayout<'tcx>
|
|
|
|
) -> EvalResult<'tcx, Operand<M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
// This decides which types we will use the Immediate optimization for, and hence should
|
2018-10-26 12:33:26 +02:00
|
|
|
// match what `try_read_immediate` and `eval_place_to_op` support.
|
2018-08-16 09:36:53 +02:00
|
|
|
if layout.is_zst() {
|
2018-10-26 12:33:26 +02:00
|
|
|
return Ok(Operand::Immediate(Immediate::Scalar(Scalar::zst().into())));
|
2018-08-16 09:36:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(match layout.abi {
|
|
|
|
layout::Abi::Scalar(..) =>
|
2018-10-26 12:33:26 +02:00
|
|
|
Operand::Immediate(Immediate::Scalar(ScalarMaybeUndef::Undef)),
|
2018-08-13 16:14:22 +02:00
|
|
|
layout::Abi::ScalarPair(..) =>
|
2018-10-26 12:33:26 +02:00
|
|
|
Operand::Immediate(Immediate::ScalarPair(
|
2018-08-13 16:14:22 +02:00
|
|
|
ScalarMaybeUndef::Undef,
|
|
|
|
ScalarMaybeUndef::Undef,
|
|
|
|
)),
|
|
|
|
_ => {
|
|
|
|
trace!("Forcing allocation for local of type {:?}", layout.ty);
|
|
|
|
Operand::Indirect(
|
2018-12-19 14:11:01 +01:00
|
|
|
*self.allocate(layout, MemoryKind::Stack)
|
2018-08-13 16:14:22 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Projection functions
|
|
|
|
pub fn operand_field(
|
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
op: OpTy<'tcx, M::PointerTag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
field: u64,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
let base = match op.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
|
|
|
// The easy case
|
|
|
|
let field = self.mplace_field(mplace, field)?;
|
|
|
|
return Ok(field.into());
|
|
|
|
},
|
|
|
|
Err(value) => value
|
|
|
|
};
|
|
|
|
|
|
|
|
let field = field.try_into().unwrap();
|
|
|
|
let field_layout = op.layout.field(self, field)?;
|
2018-09-10 17:46:30 +02:00
|
|
|
if field_layout.is_zst() {
|
2018-10-26 12:33:26 +02:00
|
|
|
let immediate = Immediate::Scalar(Scalar::zst().into());
|
|
|
|
return Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout });
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
let offset = op.layout.fields.offset(field);
|
2018-10-26 12:33:26 +02:00
|
|
|
let immediate = match base {
|
2018-08-13 16:14:22 +02:00
|
|
|
// the field covers the entire type
|
|
|
|
_ if offset.bytes() == 0 && field_layout.size == op.layout.size => base,
|
|
|
|
// extract fields from types with `ScalarPair` ABI
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::ScalarPair(a, b) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let val = if offset.bytes() == 0 { a } else { b };
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(val)
|
2018-08-13 16:14:22 +02:00
|
|
|
},
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(val) =>
|
2018-08-13 16:14:22 +02:00
|
|
|
bug!("field access on non aggregate {:#?}, {:#?}", val, op.layout),
|
|
|
|
};
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout })
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-24 16:39:25 +02:00
|
|
|
pub fn operand_downcast(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
op: OpTy<'tcx, M::PointerTag>,
|
2018-11-01 19:03:38 +01:00
|
|
|
variant: VariantIdx,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
// Downcasts only change the layout
|
|
|
|
Ok(match op.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
|
|
|
self.mplace_downcast(mplace, variant)?.into()
|
|
|
|
},
|
|
|
|
Err(..) => {
|
|
|
|
let layout = op.layout.for_variant(self, variant);
|
|
|
|
OpTy { layout, ..op }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operand_projection(
|
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
base: OpTy<'tcx, M::PointerTag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
proj_elem: &mir::PlaceElem<'tcx>,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::mir::ProjectionElem::*;
|
|
|
|
Ok(match *proj_elem {
|
|
|
|
Field(field, _) => self.operand_field(base, field.index() as u64)?,
|
|
|
|
Downcast(_, variant) => self.operand_downcast(base, variant)?,
|
|
|
|
Deref => self.deref_operand(base)?.into(),
|
2018-09-10 17:46:30 +02:00
|
|
|
Subslice { .. } | ConstantIndex { .. } | Index(_) => if base.layout.is_zst() {
|
|
|
|
OpTy {
|
2018-10-26 12:33:26 +02:00
|
|
|
op: Operand::Immediate(Immediate::Scalar(Scalar::zst().into())),
|
2018-09-10 17:46:30 +02:00
|
|
|
// the actual index doesn't matter, so we just pick a convenient one like 0
|
|
|
|
layout: base.layout.field(self, 0)?,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The rest should only occur as mplace, we do not use Immediates for types
|
|
|
|
// allowing such operations. This matches place_projection forcing an allocation.
|
2018-08-13 16:14:22 +02:00
|
|
|
let mplace = base.to_mem_place();
|
|
|
|
self.mplace_projection(mplace, proj_elem)?.into()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:02:39 +02:00
|
|
|
/// This is used by [priroda](https://github.com/oli-obk/priroda) to get an OpTy from a local
|
2019-01-16 20:45:53 +01:00
|
|
|
fn access_local(
|
2018-10-20 11:02:39 +02:00
|
|
|
&self,
|
2018-11-15 17:14:53 +01:00
|
|
|
frame: &super::Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>,
|
2018-10-20 11:02:39 +02:00
|
|
|
local: mir::Local,
|
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-10-21 16:16:23 +02:00
|
|
|
assert_ne!(local, mir::RETURN_PLACE);
|
2018-10-20 11:02:39 +02:00
|
|
|
let op = *frame.locals[local].access()?;
|
2019-01-16 20:45:53 +01:00
|
|
|
let layout = self.layout_of_local(frame, local)?;
|
2018-10-20 11:02:39 +02:00
|
|
|
Ok(OpTy { op, layout })
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
// Evaluate a place with the goal of reading from it. This lets us sometimes
|
2019-01-16 20:45:53 +01:00
|
|
|
// avoid allocations.
|
2018-08-13 16:14:22 +02:00
|
|
|
fn eval_place_to_op(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
mir_place: &mir::Place<'tcx>,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::mir::Place::*;
|
2018-08-23 19:27:14 +02:00
|
|
|
let op = match *mir_place {
|
2018-08-13 16:14:22 +02:00
|
|
|
Local(mir::RETURN_PLACE) => return err!(ReadFromReturnPointer),
|
2019-01-16 20:45:53 +01:00
|
|
|
Local(local) => self.access_local(self.frame(), local)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
|
|
|
|
Projection(ref proj) => {
|
2019-01-16 20:45:53 +01:00
|
|
|
let op = self.eval_place_to_op(&proj.base)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.operand_projection(op, &proj.elem)?
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:27:14 +02:00
|
|
|
_ => self.eval_place_to_mplace(mir_place)?.into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("eval_place_to_op: got {:?}", *op);
|
|
|
|
Ok(op)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the operand, returning a place where you can then find the data.
|
2018-08-20 15:21:04 +02:00
|
|
|
/// if you already know the layout, you can save two some table lookups
|
|
|
|
/// by passing it in here.
|
|
|
|
pub fn eval_operand(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-20 15:21:04 +02:00
|
|
|
mir_op: &mir::Operand<'tcx>,
|
|
|
|
layout: Option<TyLayout<'tcx>>,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::mir::Operand::*;
|
2018-08-15 20:18:40 +02:00
|
|
|
let op = match *mir_op {
|
2018-08-13 16:14:22 +02:00
|
|
|
// FIXME: do some more logic on `move` to invalidate the old location
|
|
|
|
Copy(ref place) |
|
|
|
|
Move(ref place) =>
|
2019-01-16 20:45:53 +01:00
|
|
|
self.eval_place_to_op(place)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
|
|
|
|
Constant(ref constant) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
let layout = from_known_layout(layout, || {
|
2019-01-23 11:34:02 +01:00
|
|
|
let ty = self.monomorphize(mir_op.ty(self.mir(), *self.tcx))?;
|
2018-08-20 15:21:04 +02:00
|
|
|
self.layout_of(ty)
|
|
|
|
})?;
|
2018-12-11 19:56:59 +01:00
|
|
|
let op = self.const_value_to_op(*constant.literal)?;
|
2018-08-15 20:18:40 +02:00
|
|
|
OpTy { op, layout }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2018-08-15 20:18:40 +02:00
|
|
|
};
|
|
|
|
trace!("{:?}: {:?}", mir_op, *op);
|
|
|
|
Ok(op)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a bunch of operands at once
|
2018-08-24 16:39:25 +02:00
|
|
|
pub(super) fn eval_operands(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
ops: &[mir::Operand<'tcx>],
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, Vec<OpTy<'tcx, M::PointerTag>>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
ops.into_iter()
|
2018-08-20 15:21:04 +02:00
|
|
|
.map(|op| self.eval_operand(op, None))
|
2018-08-13 16:14:22 +02:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2018-11-06 16:16:27 +01:00
|
|
|
// Used when miri runs into a constant, and by CTFE.
|
|
|
|
// FIXME: CTFE should use allocations, then we can make this private (embed it into
|
|
|
|
// `eval_operand`, ideally).
|
|
|
|
pub(crate) fn const_value_to_op(
|
2018-08-23 19:04:33 +02:00
|
|
|
&self,
|
2018-12-11 19:56:59 +01:00
|
|
|
val: ty::LazyConst<'tcx>,
|
2018-09-21 23:32:59 +02:00
|
|
|
) -> EvalResult<'tcx, Operand<M::PointerTag>> {
|
2018-08-23 19:04:33 +02:00
|
|
|
trace!("const_value_to_op: {:?}", val);
|
2018-12-11 19:56:59 +01:00
|
|
|
let val = match val {
|
|
|
|
ty::LazyConst::Unevaluated(def_id, substs) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let instance = self.resolve(def_id, substs)?;
|
2018-12-11 19:56:59 +01:00
|
|
|
return Ok(*OpTy::from(self.const_eval_raw(GlobalId {
|
2018-08-13 16:14:22 +02:00
|
|
|
instance,
|
|
|
|
promoted: None,
|
2018-12-11 19:56:59 +01:00
|
|
|
})?));
|
|
|
|
},
|
|
|
|
ty::LazyConst::Evaluated(c) => c,
|
|
|
|
};
|
|
|
|
match val.val {
|
2018-08-23 19:04:33 +02:00
|
|
|
ConstValue::ByRef(id, alloc, offset) => {
|
|
|
|
// We rely on mutability being set correctly in that allocation to prevent writes
|
|
|
|
// where none should happen -- and for `static mut`, we copy on demand anyway.
|
2018-09-21 23:32:59 +02:00
|
|
|
Ok(Operand::Indirect(
|
|
|
|
MemPlace::from_ptr(Pointer::new(id, offset), alloc.align)
|
|
|
|
).with_default_tag())
|
2018-08-13 16:14:22 +02:00
|
|
|
},
|
2019-01-08 13:49:37 +01:00
|
|
|
ConstValue::Slice(a, b) =>
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(Operand::Immediate(Immediate::ScalarPair(
|
|
|
|
a.into(),
|
2019-01-08 13:49:37 +01:00
|
|
|
Scalar::from_uint(b, self.tcx.data_layout.pointer_size).into(),
|
2018-10-26 12:33:26 +02:00
|
|
|
)).with_default_tag()),
|
2018-08-13 16:14:22 +02:00
|
|
|
ConstValue::Scalar(x) =>
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(Operand::Immediate(Immediate::Scalar(x.into())).with_default_tag()),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 11:07:03 +02:00
|
|
|
/// Read discriminant, return the runtime value as well as the variant index.
|
|
|
|
pub fn read_discriminant(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
rval: OpTy<'tcx, M::PointerTag>,
|
2018-11-01 19:03:38 +01:00
|
|
|
) -> EvalResult<'tcx, (u128, VariantIdx)> {
|
2018-08-13 16:14:22 +02:00
|
|
|
trace!("read_discriminant_value {:#?}", rval.layout);
|
|
|
|
|
|
|
|
match rval.layout.variants {
|
|
|
|
layout::Variants::Single { index } => {
|
|
|
|
let discr_val = rval.layout.ty.ty_adt_def().map_or(
|
2018-11-01 19:03:38 +01:00
|
|
|
index.as_u32() as u128,
|
2018-08-13 16:14:22 +02:00
|
|
|
|def| def.discriminant_for_variant(*self.tcx, index).val);
|
2018-08-25 11:07:03 +02:00
|
|
|
return Ok((discr_val, index));
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
layout::Variants::Tagged { .. } |
|
|
|
|
layout::Variants::NicheFilling { .. } => {},
|
|
|
|
}
|
2018-08-25 11:07:03 +02:00
|
|
|
// read raw discriminant value
|
2018-08-13 16:14:22 +02:00
|
|
|
let discr_op = self.operand_field(rval, 0)?;
|
2018-10-26 12:33:26 +02:00
|
|
|
let discr_val = self.read_immediate(discr_op)?;
|
2018-11-12 10:32:23 +01:00
|
|
|
let raw_discr = discr_val.to_scalar_or_undef();
|
2018-08-25 11:07:03 +02:00
|
|
|
trace!("discr value: {:?}", raw_discr);
|
|
|
|
// post-process
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(match rval.layout.variants {
|
|
|
|
layout::Variants::Single { .. } => bug!(),
|
|
|
|
layout::Variants::Tagged { .. } => {
|
2018-11-01 14:14:51 +01:00
|
|
|
let bits_discr = match raw_discr.to_bits(discr_val.layout.size) {
|
|
|
|
Ok(raw_discr) => raw_discr,
|
|
|
|
Err(_) => return err!(InvalidDiscriminant(raw_discr.erase_tag())),
|
|
|
|
};
|
2018-08-25 11:07:03 +02:00
|
|
|
let real_discr = if discr_val.layout.ty.is_signed() {
|
2018-11-01 14:14:51 +01:00
|
|
|
let i = bits_discr as i128;
|
2018-08-13 16:14:22 +02:00
|
|
|
// going from layout tag type to typeck discriminant type
|
|
|
|
// requires first sign extending with the layout discriminant
|
|
|
|
let shift = 128 - discr_val.layout.size.bits();
|
|
|
|
let sexted = (i << shift) >> shift;
|
|
|
|
// 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();
|
2018-11-03 22:57:53 +02:00
|
|
|
let discr_ty = layout::Integer::from_attr(self, discr_ty);
|
2018-08-13 16:14:22 +02:00
|
|
|
let shift = 128 - discr_ty.size().bits();
|
|
|
|
let truncatee = sexted as u128;
|
|
|
|
(truncatee << shift) >> shift
|
|
|
|
} else {
|
2018-11-01 14:14:51 +01:00
|
|
|
bits_discr
|
2018-08-25 11:07:03 +02:00
|
|
|
};
|
|
|
|
// Make sure we catch invalid discriminants
|
|
|
|
let index = rval.layout.ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.expect("tagged layout for non adt")
|
|
|
|
.discriminants(self.tcx.tcx)
|
2018-11-01 19:03:38 +01:00
|
|
|
.find(|(_, var)| var.val == real_discr)
|
2018-11-01 14:14:51 +01:00
|
|
|
.ok_or_else(|| EvalErrorKind::InvalidDiscriminant(raw_discr.erase_tag()))?;
|
2018-11-01 19:03:38 +01:00
|
|
|
(real_discr, index.0)
|
2018-08-13 16:14:22 +02:00
|
|
|
},
|
|
|
|
layout::Variants::NicheFilling {
|
|
|
|
dataful_variant,
|
|
|
|
ref niche_variants,
|
|
|
|
niche_start,
|
|
|
|
..
|
|
|
|
} => {
|
2018-11-01 19:03:38 +01:00
|
|
|
let variants_start = niche_variants.start().as_u32() as u128;
|
|
|
|
let variants_end = niche_variants.end().as_u32() as u128;
|
|
|
|
match raw_discr {
|
2018-11-12 11:22:18 +01:00
|
|
|
ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) => {
|
|
|
|
// The niche must be just 0 (which an inbounds pointer value never is)
|
|
|
|
let ptr_valid = niche_start == 0 && variants_start == variants_end &&
|
2018-12-19 16:26:46 +01:00
|
|
|
self.memory.check_bounds_ptr(ptr, InboundsCheck::MaybeDead).is_ok();
|
2018-11-12 11:22:18 +01:00
|
|
|
if !ptr_valid {
|
|
|
|
return err!(InvalidDiscriminant(raw_discr.erase_tag()));
|
|
|
|
}
|
2018-11-01 19:03:38 +01:00
|
|
|
(dataful_variant.as_u32() as u128, dataful_variant)
|
2018-08-13 16:14:22 +02:00
|
|
|
},
|
2018-11-12 10:32:23 +01:00
|
|
|
ScalarMaybeUndef::Scalar(Scalar::Bits { bits: raw_discr, size }) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
assert_eq!(size as u64, discr_val.layout.size.bytes());
|
2018-11-12 11:22:18 +01:00
|
|
|
let adjusted_discr = raw_discr.wrapping_sub(niche_start)
|
2018-08-13 16:14:22 +02:00
|
|
|
.wrapping_add(variants_start);
|
2018-11-12 11:22:18 +01:00
|
|
|
if variants_start <= adjusted_discr && adjusted_discr <= variants_end {
|
|
|
|
let index = adjusted_discr as usize;
|
|
|
|
assert_eq!(index as u128, adjusted_discr);
|
2018-11-01 19:03:38 +01:00
|
|
|
assert!(index < rval.layout.ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.expect("tagged layout for non adt")
|
|
|
|
.variants.len());
|
2018-11-12 11:22:18 +01:00
|
|
|
(adjusted_discr, VariantIdx::from_usize(index))
|
2018-08-13 16:14:22 +02:00
|
|
|
} else {
|
2018-11-01 19:03:38 +01:00
|
|
|
(dataful_variant.as_u32() as u128, dataful_variant)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
},
|
2018-11-12 10:32:23 +01:00
|
|
|
ScalarMaybeUndef::Undef =>
|
|
|
|
return err!(InvalidDiscriminant(ScalarMaybeUndef::Undef)),
|
2018-11-01 19:03:38 +01:00
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|