2020-03-21 13:49:02 +01:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_apfloat::ieee::{Double, Single};
|
|
|
|
use rustc_apfloat::{Float, FloatConvert};
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::FloatTy;
|
2020-05-24 19:17:30 +02:00
|
|
|
use rustc_attr as attr;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
|
|
|
|
use rustc_middle::mir::CastKind;
|
|
|
|
use rustc_middle::ty::adjustment::PointerCast;
|
2020-05-24 19:17:30 +02:00
|
|
|
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
|
2020-07-24 13:16:54 +01:00
|
|
|
use rustc_middle::ty::{self, Ty, TypeAndMut};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2020-05-24 19:17:30 +02:00
|
|
|
use rustc_target::abi::{Integer, LayoutOf, Variants};
|
2016-09-07 18:34:59 +02:00
|
|
|
|
2020-07-24 13:16:54 +01:00
|
|
|
use super::{
|
|
|
|
truncate, util::ensure_monomorphic_enough, FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy,
|
|
|
|
PlaceTy,
|
|
|
|
};
|
2020-05-24 09:38:29 +02:00
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2018-08-24 16:39:25 +02:00
|
|
|
pub fn cast(
|
2018-07-18 11:39:17 +02:00
|
|
|
&mut self,
|
2018-09-21 23:32:59 +02:00
|
|
|
src: OpTy<'tcx, M::PointerTag>,
|
2020-05-24 09:38:29 +02:00
|
|
|
cast_kind: CastKind,
|
|
|
|
cast_ty: Ty<'tcx>,
|
2018-09-21 23:32:59 +02:00
|
|
|
dest: PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::CastKind::*;
|
2020-05-24 09:38:29 +02:00
|
|
|
// FIXME: In which cases should we trigger UB when the source is uninit?
|
|
|
|
match cast_kind {
|
2019-04-15 19:20:44 +05:30
|
|
|
Pointer(PointerCast::Unsize) => {
|
2020-05-24 15:10:15 +02:00
|
|
|
let cast_ty = self.layout_of(cast_ty)?;
|
|
|
|
self.unsize_into(src, cast_ty, dest)?;
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
Misc => {
|
2018-10-26 12:33:26 +02:00
|
|
|
let src = self.read_immediate(src)?;
|
2020-05-24 09:38:29 +02:00
|
|
|
let res = self.misc_cast(src, cast_ty)?;
|
2019-07-24 19:01:12 +02:00
|
|
|
self.write_immediate(res, dest)?;
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
Pointer(PointerCast::MutToConstPointer | PointerCast::ArrayToPointer) => {
|
|
|
|
// These are NOPs, but can be wide pointers.
|
|
|
|
let v = self.read_immediate(src)?;
|
|
|
|
self.write_immediate(*v, dest)?;
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:20:44 +05:30
|
|
|
Pointer(PointerCast::ReifyFnPointer) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
// The src operand does not matter, just its type
|
2020-08-03 00:49:11 +02:00
|
|
|
match *src.layout.ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnDef(def_id, substs) => {
|
2019-08-12 20:23:57 +03:00
|
|
|
// All reifications must be monomorphic, bail out otherwise.
|
2020-07-24 13:16:54 +01:00
|
|
|
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
|
2019-08-12 20:23:57 +03:00
|
|
|
|
2019-05-08 13:21:18 +10:00
|
|
|
if self.tcx.has_attr(def_id, sym::rustc_args_required_const) {
|
2020-06-21 16:13:31 +02:00
|
|
|
span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"reifying a fn ptr that requires const arguments"
|
|
|
|
);
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
2019-10-09 21:13:08 -07:00
|
|
|
|
|
|
|
let instance = ty::Instance::resolve_for_fn_ptr(
|
2020-06-14 15:02:51 +02:00
|
|
|
*self.tcx,
|
2019-10-09 21:13:08 -07:00
|
|
|
self.param_env,
|
|
|
|
def_id,
|
|
|
|
substs,
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
.ok_or_else(|| err_inval!(TooGeneric))?;
|
2019-10-09 21:13:08 -07:00
|
|
|
|
2019-07-31 16:38:39 +05:30
|
|
|
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
|
2019-11-29 11:09:26 +01:00
|
|
|
self.write_scalar(fn_ptr, dest)?;
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:20:44 +05:30
|
|
|
Pointer(PointerCast::UnsafeFnPointer) => {
|
2018-10-26 12:33:26 +02:00
|
|
|
let src = self.read_immediate(src)?;
|
2020-08-03 00:49:11 +02:00
|
|
|
match cast_ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::FnPtr(_) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
// No change to value
|
2018-10-26 12:33:26 +02:00
|
|
|
self.write_immediate(*src, dest)?;
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "fn to unsafe fn cast on {:?}", cast_ty),
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:20:44 +05:30
|
|
|
Pointer(PointerCast::ClosureFnPointer(_)) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
// The src operand does not matter, just its type
|
2020-08-03 00:49:11 +02:00
|
|
|
match *src.layout.ty.kind() {
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Closure(def_id, substs) => {
|
2019-08-12 20:23:57 +03:00
|
|
|
// All reifications must be monomorphic, bail out otherwise.
|
2020-07-24 13:16:54 +01:00
|
|
|
ensure_monomorphic_enough(*self.tcx, src.layout.ty)?;
|
2019-08-12 20:23:57 +03:00
|
|
|
|
2018-07-18 11:39:17 +02:00
|
|
|
let instance = ty::Instance::resolve_closure(
|
2020-06-14 15:02:51 +02:00
|
|
|
*self.tcx,
|
2018-07-18 11:39:17 +02:00
|
|
|
def_id,
|
|
|
|
substs,
|
|
|
|
ty::ClosureKind::FnOnce,
|
|
|
|
);
|
2019-06-30 13:51:18 +02:00
|
|
|
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
|
2019-11-29 11:09:26 +01:00
|
|
|
self.write_scalar(fn_ptr, dest)?;
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),
|
2018-07-18 11:39:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
fn misc_cast(
|
2019-07-24 19:01:12 +02:00
|
|
|
&self,
|
|
|
|
src: ImmTy<'tcx, M::PointerTag>,
|
2020-05-24 09:38:29 +02:00
|
|
|
cast_ty: Ty<'tcx>,
|
2019-07-24 19:01:12 +02:00
|
|
|
) -> InterpResult<'tcx, Immediate<M::PointerTag>> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::TyKind::*;
|
2020-05-24 09:38:29 +02:00
|
|
|
trace!("Casting {:?}: {:?} to {:?}", *src, src.layout.ty, cast_ty);
|
2016-11-26 22:58:01 -08:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match src.layout.ty.kind() {
|
2019-06-09 12:31:19 +02:00
|
|
|
// Floating point
|
2019-12-22 17:42:04 -05:00
|
|
|
Float(FloatTy::F32) => {
|
2020-05-24 09:38:29 +02:00
|
|
|
return Ok(self.cast_from_float(src.to_scalar()?.to_f32()?, cast_ty).into());
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
Float(FloatTy::F64) => {
|
2020-05-24 09:38:29 +02:00
|
|
|
return Ok(self.cast_from_float(src.to_scalar()?.to_f64()?, cast_ty).into());
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-07-24 20:05:53 +02:00
|
|
|
// The rest is integer/pointer-"like", including fn ptr casts and casts from enums that
|
|
|
|
// are represented as integers.
|
2019-12-22 17:42:04 -05:00
|
|
|
_ => assert!(
|
|
|
|
src.layout.ty.is_bool()
|
|
|
|
|| src.layout.ty.is_char()
|
|
|
|
|| src.layout.ty.is_enum()
|
|
|
|
|| src.layout.ty.is_integral()
|
|
|
|
|| src.layout.ty.is_any_ptr(),
|
|
|
|
"Unexpected cast from type {:?}",
|
|
|
|
src.layout.ty
|
|
|
|
),
|
2019-07-24 20:05:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
// # First handle non-scalar source values.
|
|
|
|
|
2020-09-01 10:55:49 -04:00
|
|
|
// Handle cast from a ZST enum (0 or 1 variants).
|
2019-07-25 00:22:05 +02:00
|
|
|
match src.layout.variants {
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Single { index } => {
|
2020-09-01 10:55:49 -04:00
|
|
|
if src.layout.abi.is_uninhabited() {
|
|
|
|
// This is dead code, because an uninhabited enum is UB to
|
|
|
|
// instantiate.
|
|
|
|
throw_ub!(Unreachable);
|
|
|
|
}
|
2020-06-14 15:02:51 +02:00
|
|
|
if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) {
|
2019-07-25 00:22:05 +02:00
|
|
|
assert!(src.layout.is_zst());
|
2020-03-18 22:15:23 -04:00
|
|
|
let discr_layout = self.layout_of(discr.ty)?;
|
2020-05-24 09:38:29 +02:00
|
|
|
return Ok(self.cast_from_scalar(discr.val, discr_layout, cast_ty).into());
|
2019-07-25 00:22:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Multiple { .. } => {}
|
2019-07-25 00:22:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle casting any ptr to raw ptr (might be a fat ptr).
|
2020-05-24 09:38:29 +02:00
|
|
|
if src.layout.ty.is_any_ptr() && cast_ty.is_unsafe_ptr() {
|
|
|
|
let dest_layout = self.layout_of(cast_ty)?;
|
|
|
|
if dest_layout.size == src.layout.size {
|
|
|
|
// Thin or fat pointer that just hast the ptr kind of target type changed.
|
|
|
|
return Ok(*src);
|
|
|
|
} else {
|
|
|
|
// Casting the metadata away from a fat ptr.
|
|
|
|
assert_eq!(src.layout.size, 2 * self.memory.pointer_size());
|
|
|
|
assert_eq!(dest_layout.size, self.memory.pointer_size());
|
|
|
|
assert!(src.layout.ty.is_unsafe_ptr());
|
|
|
|
return match *src {
|
|
|
|
Immediate::ScalarPair(data, _) => Ok(data.into()),
|
2020-06-21 16:13:31 +02:00
|
|
|
Immediate::Scalar(..) => span_bug!(
|
|
|
|
self.cur_span(),
|
2020-05-24 09:38:29 +02:00
|
|
|
"{:?} input to a fat-to-thin cast ({:?} -> {:?})",
|
|
|
|
*src,
|
|
|
|
src.layout.ty,
|
|
|
|
cast_ty
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
2019-07-24 20:09:18 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
// # The remaining source values are scalar.
|
|
|
|
|
2019-07-25 00:22:05 +02:00
|
|
|
// For all remaining casts, we either
|
|
|
|
// (a) cast a raw ptr to usize, or
|
|
|
|
// (b) cast from an integer-like (including bool, char, enums).
|
|
|
|
// In both cases we want the bits.
|
|
|
|
let bits = self.force_bits(src.to_scalar()?, src.layout.size)?;
|
2020-05-24 09:38:29 +02:00
|
|
|
Ok(self.cast_from_scalar(bits, src.layout, cast_ty).into())
|
2016-09-07 18:34:59 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 09:38:29 +02:00
|
|
|
pub(super) fn cast_from_scalar(
|
2017-08-10 08:48:38 -07:00
|
|
|
&self,
|
2020-05-24 09:38:29 +02:00
|
|
|
v: u128, // raw bits (there is no ScalarTy so we separate data+layout)
|
2020-03-04 14:50:21 +00:00
|
|
|
src_layout: TyAndLayout<'tcx>,
|
2020-05-24 09:38:29 +02:00
|
|
|
cast_ty: Ty<'tcx>,
|
2020-04-14 10:30:33 +02:00
|
|
|
) -> Scalar<M::PointerTag> {
|
2019-06-09 12:31:19 +02:00
|
|
|
// Let's make sure v is sign-extended *if* it has a signed type.
|
2020-05-24 19:28:44 +02:00
|
|
|
let signed = src_layout.abi.is_signed(); // Also asserts that abi is `Scalar`.
|
2019-12-22 17:42:04 -05:00
|
|
|
let v = if signed { self.sign_extend(v, src_layout) } else { v };
|
2020-05-24 09:38:29 +02:00
|
|
|
trace!("cast_from_scalar: {}, {} -> {}", v, src_layout.ty, cast_ty);
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::TyKind::*;
|
2020-08-03 00:49:11 +02:00
|
|
|
match *cast_ty.kind() {
|
2019-05-26 14:13:12 +02:00
|
|
|
Int(_) | Uint(_) | RawPtr(_) => {
|
2020-08-03 00:49:11 +02:00
|
|
|
let size = match *cast_ty.kind() {
|
2020-05-24 19:17:30 +02:00
|
|
|
Int(t) => Integer::from_attr(self, attr::IntType::SignedInt(t)).size(),
|
|
|
|
Uint(t) => Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size(),
|
2020-05-24 09:38:29 +02:00
|
|
|
RawPtr(_) => self.pointer_size(),
|
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
let v = truncate(v, size);
|
|
|
|
Scalar::from_uint(v, size)
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2020-04-14 10:30:33 +02:00
|
|
|
Float(FloatTy::F32) if signed => Scalar::from_f32(Single::from_i128(v as i128).value),
|
|
|
|
Float(FloatTy::F64) if signed => Scalar::from_f64(Double::from_i128(v as i128).value),
|
|
|
|
Float(FloatTy::F32) => Scalar::from_f32(Single::from_u128(v).value),
|
|
|
|
Float(FloatTy::F64) => Scalar::from_f64(Double::from_u128(v).value),
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2018-08-22 01:35:55 +01:00
|
|
|
Char => {
|
2018-08-26 20:42:52 +02:00
|
|
|
// `u8` to `char` cast
|
2020-04-14 10:30:33 +02:00
|
|
|
Scalar::from_u32(u8::try_from(v).unwrap().into())
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2018-02-21 22:02:52 +01:00
|
|
|
// Casts to bool are not permitted by rustc, no need to handle them here.
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "invalid int to {:?} cast", cast_ty),
|
2016-09-07 18:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 10:30:33 +02:00
|
|
|
fn cast_from_float<F>(&self, f: F, dest_ty: Ty<'tcx>) -> Scalar<M::PointerTag>
|
2019-12-22 17:42:04 -05:00
|
|
|
where
|
|
|
|
F: Float + Into<Scalar<M::PointerTag>> + FloatConvert<Single> + FloatConvert<Double>,
|
2019-06-09 12:31:19 +02:00
|
|
|
{
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::TyKind::*;
|
2020-08-03 00:49:11 +02:00
|
|
|
match *dest_ty.kind() {
|
2018-02-21 22:02:52 +01:00
|
|
|
// float -> uint
|
2018-08-22 01:35:55 +01:00
|
|
|
Uint(t) => {
|
2020-05-24 19:17:30 +02:00
|
|
|
let size = Integer::from_attr(self, attr::IntType::UnsignedInt(t)).size();
|
2020-03-26 19:09:31 +01:00
|
|
|
// `to_u128` is a saturating cast, which is what we need
|
|
|
|
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
|
2020-05-24 19:17:30 +02:00
|
|
|
let v = f.to_u128(size.bits_usize()).value;
|
2018-08-16 11:38:16 +02:00
|
|
|
// This should already fit the bit width
|
2020-05-24 19:17:30 +02:00
|
|
|
Scalar::from_uint(v, size)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-02-21 22:02:52 +01:00
|
|
|
// float -> int
|
2018-08-22 01:35:55 +01:00
|
|
|
Int(t) => {
|
2020-05-24 19:17:30 +02:00
|
|
|
let size = Integer::from_attr(self, attr::IntType::SignedInt(t)).size();
|
2020-03-26 19:09:31 +01:00
|
|
|
// `to_i128` is a saturating cast, which is what we need
|
|
|
|
// (https://doc.rust-lang.org/nightly/nightly-rustc/rustc_apfloat/trait.Float.html#method.to_i128_r).
|
2020-05-24 19:17:30 +02:00
|
|
|
let v = f.to_i128(size.bits_usize()).value;
|
|
|
|
Scalar::from_int(v, size)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-06-09 12:31:19 +02:00
|
|
|
// float -> f32
|
2020-04-14 10:30:33 +02:00
|
|
|
Float(FloatTy::F32) => Scalar::from_f32(f.convert(&mut false).value),
|
2019-06-09 12:31:19 +02:00
|
|
|
// float -> f64
|
2020-04-14 10:30:33 +02:00
|
|
|
Float(FloatTy::F64) => Scalar::from_f64(f.convert(&mut false).value),
|
2019-06-09 12:31:19 +02:00
|
|
|
// That's it.
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "invalid float to {:?} cast", dest_ty),
|
2016-09-07 18:34:59 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-20 04:42:19 -06:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
fn unsize_into_ptr(
|
|
|
|
&mut self,
|
2018-09-21 23:32:59 +02:00
|
|
|
src: OpTy<'tcx, M::PointerTag>,
|
|
|
|
dest: PlaceTy<'tcx, M::PointerTag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
// The pointee types
|
2019-09-16 18:59:31 +01:00
|
|
|
source_ty: Ty<'tcx>,
|
2020-05-24 15:10:15 +02:00
|
|
|
cast_ty: Ty<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-08-13 16:14:22 +02:00
|
|
|
// A<Struct> -> A<Trait> conversion
|
2019-07-11 13:27:41 +02:00
|
|
|
let (src_pointee_ty, dest_pointee_ty) =
|
2020-05-24 15:10:15 +02:00
|
|
|
self.tcx.struct_lockstep_tails_erasing_lifetimes(source_ty, cast_ty, self.param_env);
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
match (&src_pointee_ty.kind(), &dest_pointee_ty.kind()) {
|
2018-08-22 11:54:46 +01:00
|
|
|
(&ty::Array(_, length), &ty::Slice(_)) => {
|
2019-11-05 23:04:17 +01:00
|
|
|
let ptr = self.read_immediate(src)?.to_scalar()?;
|
2018-08-13 16:14:22 +02:00
|
|
|
// u64 cast is from usize to u64, which is always good
|
2020-06-01 11:17:38 +02:00
|
|
|
let val =
|
2020-06-14 15:02:51 +02:00
|
|
|
Immediate::new_slice(ptr, length.eval_usize(*self.tcx, self.param_env), self);
|
2018-10-26 12:33:26 +02:00
|
|
|
self.write_immediate(val, dest)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2018-08-22 11:54:46 +01:00
|
|
|
(&ty::Dynamic(..), &ty::Dynamic(..)) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
// For now, upcasts are limited to changes in marker
|
|
|
|
// traits, and hence never actually require an actual
|
|
|
|
// change to the vtable.
|
2018-10-26 12:33:26 +02:00
|
|
|
let val = self.read_immediate(src)?;
|
|
|
|
self.write_immediate(*val, dest)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2018-08-22 11:54:46 +01:00
|
|
|
(_, &ty::Dynamic(ref data, _)) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
// Initial cast from sized to dyn trait
|
2018-10-12 16:45:17 +02:00
|
|
|
let vtable = self.get_vtable(src_pointee_ty, data.principal())?;
|
2019-11-05 23:04:17 +01:00
|
|
|
let ptr = self.read_immediate(src)?.to_scalar()?;
|
2018-10-26 12:33:26 +02:00
|
|
|
let val = Immediate::new_dyn_trait(ptr, vtable);
|
|
|
|
self.write_immediate(val, dest)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => {
|
|
|
|
span_bug!(self.cur_span(), "invalid unsizing {:?} -> {:?}", src.layout.ty, cast_ty)
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unsize_into(
|
|
|
|
&mut self,
|
2018-09-21 23:32:59 +02:00
|
|
|
src: OpTy<'tcx, M::PointerTag>,
|
2020-05-24 15:10:15 +02:00
|
|
|
cast_ty: TyAndLayout<'tcx>,
|
2018-09-21 23:32:59 +02:00
|
|
|
dest: PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-05-24 15:10:15 +02:00
|
|
|
trace!("Unsizing {:?} of type {} into {:?}", *src, src.layout.ty, cast_ty.ty);
|
2020-08-03 00:49:11 +02:00
|
|
|
match (&src.layout.ty.kind(), &cast_ty.ty.kind()) {
|
2020-05-24 15:10:15 +02:00
|
|
|
(&ty::Ref(_, s, _), &ty::Ref(_, c, _) | &ty::RawPtr(TypeAndMut { ty: c, .. }))
|
|
|
|
| (&ty::RawPtr(TypeAndMut { ty: s, .. }), &ty::RawPtr(TypeAndMut { ty: c, .. })) => {
|
|
|
|
self.unsize_into_ptr(src, dest, s, c)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2018-08-22 11:54:46 +01:00
|
|
|
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
assert_eq!(def_a, def_b);
|
|
|
|
if def_a.is_box() || def_b.is_box() {
|
|
|
|
if !def_a.is_box() || !def_b.is_box() {
|
2020-06-21 16:13:31 +02:00
|
|
|
span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"invalid unsizing between {:?} -> {:?}",
|
|
|
|
src.layout.ty,
|
|
|
|
cast_ty.ty
|
|
|
|
);
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
return self.unsize_into_ptr(
|
|
|
|
src,
|
|
|
|
dest,
|
|
|
|
src.layout.ty.boxed_ty(),
|
2020-05-24 15:10:15 +02:00
|
|
|
cast_ty.ty.boxed_ty(),
|
2018-08-13 16:14:22 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsizing of generic struct with pointer fields
|
|
|
|
// Example: `Arc<T>` -> `Arc<Trait>`
|
|
|
|
// here we need to increase the size of every &T thin ptr field to a fat ptr
|
|
|
|
for i in 0..src.layout.fields.count() {
|
2020-05-24 15:10:15 +02:00
|
|
|
let cast_ty_field = cast_ty.field(self, i)?;
|
|
|
|
if cast_ty_field.is_zst() {
|
2018-08-13 16:14:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-03-21 17:17:01 +01:00
|
|
|
let src_field = self.operand_field(src, i)?;
|
2020-05-24 15:10:15 +02:00
|
|
|
let dst_field = self.place_field(dest, i)?;
|
|
|
|
if src_field.layout.ty == cast_ty_field.ty {
|
2018-08-13 16:14:22 +02:00
|
|
|
self.copy_op(src_field, dst_field)?;
|
|
|
|
} else {
|
2020-05-24 15:10:15 +02:00
|
|
|
self.unsize_into(src_field, cast_ty_field, dst_field)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"unsize_into: invalid conversion: {:?} -> {:?}",
|
|
|
|
src.layout,
|
|
|
|
dest.layout
|
|
|
|
),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 18:34:59 +02:00
|
|
|
}
|