提交 343b3f01 编写于 作者: R Ralf Jung

switch assignment check back to testing layout equality

上级 351b7d09
......@@ -18,7 +18,7 @@
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc_span::source_map::DUMMY_SP;
use rustc_target::abi::{Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
use rustc_target::abi::{Abi, Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
use super::{
Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy,
......@@ -212,20 +212,25 @@ fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyAndLayout {
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
/// This test should be symmetric, as it is primarily about layout compatibility.
pub(super) fn mir_assign_valid_types<'tcx>(src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
src == dest
|| match (&src.kind, &dest.kind) {
(ty::Ref(_, src_pointee, _), ty::Ref(_, dest_pointee, _)) => {
// After optimizations, there can be assignments that change reference mutability.
// This does not affect reference layout, so that is fine.
src_pointee == dest_pointee
}
(ty::FnPtr(_), ty::FnPtr(_)) => {
// All function pointers have equal layout, and thus can be assigned.
true
}
_ => false,
}
pub(super) fn mir_assign_valid_types<'tcx>(
src: TyAndLayout<'tcx>,
dest: TyAndLayout<'tcx>,
) -> bool {
if src.ty == dest.ty {
// Equal types, all is good.
return true;
}
// Type-changing assignments can happen for (at least) two reasons:
// - `&mut T` -> `&T` gets optimized from a reborrow to a mere assignment.
// - Subtyping is used. While all normal lifetimes are erased, higher-ranked lifetime
// bounds are still around and can lead to type differences.
// There is no good way to check the latter, so we compare layouts instead -- but only
// for values with `Scalar`/`ScalarPair` abi.
// FIXME: Do something more accurate, type-based.
match &src.abi {
Abi::Scalar(..) | Abi::ScalarPair(..) => src.layout == dest.layout,
_ => false,
}
}
/// Use the already known layout if given (but sanity check in debug mode),
......@@ -241,7 +246,7 @@ pub(super) fn from_known_layout<'tcx>(
if cfg!(debug_assertions) {
let check_layout = compute()?;
assert!(
mir_assign_valid_types(check_layout.ty, known_layout.ty),
mir_assign_valid_types(check_layout, known_layout),
"expected type differs from actual type.\nexpected: {:?}\nactual: {:?}",
known_layout.ty,
check_layout.ty,
......
......@@ -868,7 +868,7 @@ fn copy_op_no_validate(
// We do NOT compare the types for equality, because well-typed code can
// actually "transmute" `&mut T` to `&T` in an assignment without a cast.
assert!(
mir_assign_valid_types(src.layout.ty, dest.layout.ty),
mir_assign_valid_types(src.layout, dest.layout),
"type mismatch when copying!\nsrc: {:?},\ndest: {:?}",
src.layout.ty,
dest.layout.ty,
......@@ -922,7 +922,7 @@ pub fn copy_op_transmute(
src: OpTy<'tcx, M::PointerTag>,
dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> {
if mir_assign_valid_types(src.layout.ty, dest.layout.ty) {
if mir_assign_valid_types(src.layout, dest.layout) {
// Fast path: Just use normal `copy_op`
return self.copy_op(src, dest);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册