switch assignment check back to testing layout equality

This commit is contained in:
Ralf Jung 2020-04-02 22:49:38 +02:00
parent 351b7d099a
commit 343b3f010c
2 changed files with 23 additions and 18 deletions

View File

@ -18,7 +18,7 @@ use rustc_middle::ty::query::TyCtxtAt;
use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc_span::source_map::DUMMY_SP; 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::{ use super::{
Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy, Immediate, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Memory, OpTy, Operand, Place, PlaceTy,
@ -212,20 +212,25 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx, M> {
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value. /// 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. /// 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 { pub(super) fn mir_assign_valid_types<'tcx>(
src == dest src: TyAndLayout<'tcx>,
|| match (&src.kind, &dest.kind) { dest: TyAndLayout<'tcx>,
(ty::Ref(_, src_pointee, _), ty::Ref(_, dest_pointee, _)) => { ) -> bool {
// After optimizations, there can be assignments that change reference mutability. if src.ty == dest.ty {
// This does not affect reference layout, so that is fine. // Equal types, all is good.
src_pointee == dest_pointee return true;
} }
(ty::FnPtr(_), ty::FnPtr(_)) => { // Type-changing assignments can happen for (at least) two reasons:
// All function pointers have equal layout, and thus can be assigned. // - `&mut T` -> `&T` gets optimized from a reborrow to a mere assignment.
true // - Subtyping is used. While all normal lifetimes are erased, higher-ranked lifetime
} // bounds are still around and can lead to type differences.
_ => false, // 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), /// 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) { if cfg!(debug_assertions) {
let check_layout = compute()?; let check_layout = compute()?;
assert!( 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: {:?}", "expected type differs from actual type.\nexpected: {:?}\nactual: {:?}",
known_layout.ty, known_layout.ty,
check_layout.ty, check_layout.ty,

View File

@ -868,7 +868,7 @@ where
// We do NOT compare the types for equality, because well-typed code can // 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. // actually "transmute" `&mut T` to `&T` in an assignment without a cast.
assert!( 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: {:?}", "type mismatch when copying!\nsrc: {:?},\ndest: {:?}",
src.layout.ty, src.layout.ty,
dest.layout.ty, dest.layout.ty,
@ -922,7 +922,7 @@ where
src: OpTy<'tcx, M::PointerTag>, src: OpTy<'tcx, M::PointerTag>,
dest: PlaceTy<'tcx, M::PointerTag>, dest: PlaceTy<'tcx, M::PointerTag>,
) -> InterpResult<'tcx> { ) -> 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` // Fast path: Just use normal `copy_op`
return self.copy_op(src, dest); return self.copy_op(src, dest);
} }