Rollup merge of #70806 - RalfJung:miri-assignment-check, r=eddyb
fix Miri assignment sanity check Thanks @eddyb for pointing me to the right APIs! r? @eddyb Fixes https://github.com/rust-lang/rust/issues/70804
This commit is contained in:
commit
31b8d65803
4 changed files with 49 additions and 19 deletions
|
@ -14,11 +14,11 @@ use rustc_middle::mir::interpret::{
|
||||||
sign_extend, truncate, AllocId, FrameInfo, GlobalId, InterpResult, Pointer, Scalar,
|
sign_extend, truncate, AllocId, FrameInfo, GlobalId, InterpResult, Pointer, Scalar,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::layout::{self, TyAndLayout};
|
use rustc_middle::ty::layout::{self, TyAndLayout};
|
||||||
use rustc_middle::ty::query::TyCtxtAt;
|
use rustc_middle::ty::{
|
||||||
use rustc_middle::ty::subst::SubstsRef;
|
self, fold::BottomUpFolder, query::TyCtxtAt, subst::SubstsRef, 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::{Abi, Align, HasDataLayout, LayoutOf, Size, TargetDataLayout};
|
use rustc_target::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,
|
||||||
|
@ -213,6 +213,7 @@ 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>(
|
pub(super) fn mir_assign_valid_types<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
src: TyAndLayout<'tcx>,
|
src: TyAndLayout<'tcx>,
|
||||||
dest: TyAndLayout<'tcx>,
|
dest: TyAndLayout<'tcx>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
@ -220,23 +221,42 @@ pub(super) fn mir_assign_valid_types<'tcx>(
|
||||||
// Equal types, all is good.
|
// Equal types, all is good.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// Type-changing assignments can happen for (at least) two reasons:
|
if src.layout != dest.layout {
|
||||||
// - `&mut T` -> `&T` gets optimized from a reborrow to a mere assignment.
|
// Layout differs, definitely not equal.
|
||||||
// - Subtyping is used. While all normal lifetimes are erased, higher-ranked lifetime
|
// We do this here because Miri would *do the wrong thing* if we allowed layout-changing
|
||||||
// bounds are still around and can lead to type differences.
|
// assignments.
|
||||||
// There is no good way to check the latter, so we compare layouts instead -- but only
|
return false;
|
||||||
// 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,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Type-changing assignments can happen for (at least) two reasons:
|
||||||
|
// 1. `&mut T` -> `&T` gets optimized from a reborrow to a mere assignment.
|
||||||
|
// 2. Subtyping is used. While all normal lifetimes are erased, higher-ranked types
|
||||||
|
// with their late-bound lifetimes are still around and can lead to type differences.
|
||||||
|
// Normalize both of them away.
|
||||||
|
let normalize = |ty: Ty<'tcx>| {
|
||||||
|
ty.fold_with(&mut BottomUpFolder {
|
||||||
|
tcx,
|
||||||
|
// Normalize all references to immutable.
|
||||||
|
ty_op: |ty| match ty.kind {
|
||||||
|
ty::Ref(_, pointee, _) => tcx.mk_imm_ref(tcx.lifetimes.re_erased, pointee),
|
||||||
|
_ => ty,
|
||||||
|
},
|
||||||
|
// We just erase all late-bound lifetimes, but this is not fully correct (FIXME):
|
||||||
|
// lifetimes in invariant positions could matter (e.g. through associated types).
|
||||||
|
// We rely on the fact that layout was confirmed to be equal above.
|
||||||
|
lt_op: |_| tcx.lifetimes.re_erased,
|
||||||
|
// Leave consts unchanged.
|
||||||
|
ct_op: |ct| ct,
|
||||||
|
})
|
||||||
|
};
|
||||||
|
normalize(src.ty) == normalize(dest.ty)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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),
|
||||||
/// or compute the layout.
|
/// or compute the layout.
|
||||||
#[cfg_attr(not(debug_assertions), inline(always))]
|
#[cfg_attr(not(debug_assertions), inline(always))]
|
||||||
pub(super) fn from_known_layout<'tcx>(
|
pub(super) fn from_known_layout<'tcx>(
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
known_layout: Option<TyAndLayout<'tcx>>,
|
known_layout: Option<TyAndLayout<'tcx>>,
|
||||||
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
|
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
|
||||||
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
|
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
|
||||||
|
@ -246,7 +266,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, known_layout),
|
mir_assign_valid_types(tcx, 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,
|
||||||
|
@ -424,7 +444,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
// have to support that case (mostly by skipping all caching).
|
// have to support that case (mostly by skipping all caching).
|
||||||
match frame.locals.get(local).and_then(|state| state.layout.get()) {
|
match frame.locals.get(local).and_then(|state| state.layout.get()) {
|
||||||
None => {
|
None => {
|
||||||
let layout = from_known_layout(layout, || {
|
let layout = from_known_layout(self.tcx.tcx, layout, || {
|
||||||
let local_ty = frame.body.local_decls[local].ty;
|
let local_ty = frame.body.local_decls[local].ty;
|
||||||
let local_ty =
|
let local_ty =
|
||||||
self.subst_from_frame_and_normalize_erasing_regions(frame, local_ty);
|
self.subst_from_frame_and_normalize_erasing_regions(frame, local_ty);
|
||||||
|
|
|
@ -529,7 +529,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
ty::ConstKind::Value(val_val) => val_val,
|
ty::ConstKind::Value(val_val) => val_val,
|
||||||
};
|
};
|
||||||
// Other cases need layout.
|
// Other cases need layout.
|
||||||
let layout = from_known_layout(layout, || self.layout_of(val.ty))?;
|
let layout = from_known_layout(self.tcx.tcx, layout, || self.layout_of(val.ty))?;
|
||||||
let op = match val_val {
|
let op = match val_val {
|
||||||
ConstValue::ByRef { alloc, offset } => {
|
ConstValue::ByRef { alloc, offset } => {
|
||||||
let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc);
|
let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc);
|
||||||
|
|
|
@ -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, dest.layout),
|
mir_assign_valid_types(self.tcx.tcx, 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, dest.layout) {
|
if mir_assign_valid_types(self.tcx.tcx, 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);
|
||||||
}
|
}
|
||||||
|
|
10
src/test/ui/consts/const-eval/issue-70804-fn-subtyping.rs
Normal file
10
src/test/ui/consts/const-eval/issue-70804-fn-subtyping.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// check-pass
|
||||||
|
#![feature(const_fn)]
|
||||||
|
|
||||||
|
const fn nested(x: (for<'a> fn(&'a ()), String)) -> (fn(&'static ()), String) {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TEST: (fn(&'static ()), String) = nested((|_x| (), String::new()));
|
||||||
|
|
||||||
|
fn main() {}
|
Loading…
Add table
Add a link
Reference in a new issue