Skip to content

Commit 351b7d0

Browse files
committed
also use mir_assign_valid_types in from_known_layout check
1 parent 100c809 commit 351b7d0

File tree

4 files changed

+58
-63
lines changed

4 files changed

+58
-63
lines changed

src/librustc_mir/interpret/eval_context.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'mir, 'tcx, M> {
210210
}
211211
}
212212

213+
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
214+
/// This test should be symmetric, as it is primarily about layout compatibility.
215+
pub(super) fn mir_assign_valid_types<'tcx>(src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
216+
src == dest
217+
|| match (&src.kind, &dest.kind) {
218+
(ty::Ref(_, src_pointee, _), ty::Ref(_, dest_pointee, _)) => {
219+
// After optimizations, there can be assignments that change reference mutability.
220+
// This does not affect reference layout, so that is fine.
221+
src_pointee == dest_pointee
222+
}
223+
(ty::FnPtr(_), ty::FnPtr(_)) => {
224+
// All function pointers have equal layout, and thus can be assigned.
225+
true
226+
}
227+
_ => false,
228+
}
229+
}
230+
231+
/// Use the already known layout if given (but sanity check in debug mode),
232+
/// or compute the layout.
233+
#[cfg_attr(not(debug_assertions), inline(always))]
234+
pub(super) fn from_known_layout<'tcx>(
235+
known_layout: Option<TyAndLayout<'tcx>>,
236+
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
237+
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
238+
match known_layout {
239+
None => compute(),
240+
Some(known_layout) => {
241+
if cfg!(debug_assertions) {
242+
let check_layout = compute()?;
243+
assert!(
244+
mir_assign_valid_types(check_layout.ty, known_layout.ty),
245+
"expected type differs from actual type.\nexpected: {:?}\nactual: {:?}",
246+
known_layout.ty,
247+
check_layout.ty,
248+
);
249+
}
250+
Ok(known_layout)
251+
}
252+
}
253+
}
254+
213255
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
214256
pub fn new(
215257
tcx: TyCtxtAt<'tcx>,
@@ -377,7 +419,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
377419
// have to support that case (mostly by skipping all caching).
378420
match frame.locals.get(local).and_then(|state| state.layout.get()) {
379421
None => {
380-
let layout = crate::interpret::operand::from_known_layout(layout, || {
422+
let layout = from_known_layout(layout, || {
381423
let local_ty = frame.body.local_decls[local].ty;
382424
let local_ty =
383425
self.subst_from_frame_and_normalize_erasing_regions(frame, local_ty);

src/librustc_mir/interpret/mod.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ mod visitor;
1818
pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in one place: here
1919

2020
pub use self::eval_context::{Frame, InterpCx, LocalState, LocalValue, StackPopCleanup};
21-
22-
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
23-
24-
pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind};
25-
21+
pub use self::intern::{intern_const_alloc_recursive, InternKind};
2622
pub use self::machine::{AllocMap, Machine, MayLeak, StackPopJump};
27-
28-
pub use self::operand::{ImmTy, Immediate, OpTy, Operand, ScalarMaybeUndef};
29-
30-
pub use self::visitor::{MutValueVisitor, ValueVisitor};
31-
23+
pub use self::memory::{AllocCheck, FnVal, Memory, MemoryKind};
24+
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
25+
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
3226
pub use self::validity::RefTracking;
33-
34-
pub use self::intern::{intern_const_alloc_recursive, InternKind};
27+
pub use self::visitor::{MutValueVisitor, ValueVisitor};
3528

3629
crate use self::intrinsics::eval_nullary_intrinsic;
30+
use eval_context::{from_known_layout, mir_assign_valid_types};

src/librustc_mir/interpret/operand.rs

+6-29
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
//! All high-level functions to read from memory work on operands as sources.
33
44
use std::convert::TryFrom;
5+
use std::fmt::Write;
56

6-
use super::{InterpCx, MPlaceTy, Machine, MemPlace, Place, PlaceTy};
77
use rustc_hir::def::Namespace;
88
use rustc_macros::HashStable;
9-
pub use rustc_middle::mir::interpret::ScalarMaybeUndef;
10-
use rustc_middle::mir::interpret::{
11-
sign_extend, truncate, AllocId, ConstValue, GlobalId, InterpResult, Pointer, Scalar,
12-
};
139
use rustc_middle::ty::layout::{IntegerExt, PrimitiveExt, TyAndLayout};
1410
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
1511
use rustc_middle::ty::Ty;
1612
use rustc_middle::{mir, ty};
1713
use rustc_target::abi::{Abi, DiscriminantKind, HasDataLayout, Integer, LayoutOf, Size};
1814
use rustc_target::abi::{VariantIdx, Variants};
19-
use std::fmt::Write;
15+
16+
use super::{
17+
from_known_layout, sign_extend, truncate, AllocId, ConstValue, GlobalId, InterpCx,
18+
InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Scalar, ScalarMaybeUndef,
19+
};
2020

2121
/// An `Immediate` represents a single immediate self-contained Rust value.
2222
///
@@ -203,29 +203,6 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
203203
}
204204
}
205205

206-
// Use the existing layout if given (but sanity check in debug mode),
207-
// or compute the layout.
208-
#[inline(always)]
209-
pub(super) fn from_known_layout<'tcx>(
210-
layout: Option<TyAndLayout<'tcx>>,
211-
compute: impl FnOnce() -> InterpResult<'tcx, TyAndLayout<'tcx>>,
212-
) -> InterpResult<'tcx, TyAndLayout<'tcx>> {
213-
match layout {
214-
None => compute(),
215-
Some(layout) => {
216-
if cfg!(debug_assertions) {
217-
let layout2 = compute()?;
218-
assert_eq!(
219-
layout.layout, layout2.layout,
220-
"mismatch in layout of supposedly equal-layout types {:?} and {:?}",
221-
layout.ty, layout2.ty
222-
);
223-
}
224-
Ok(layout)
225-
}
226-
}
227-
}
228-
229206
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
230207
/// Normalice `place.ptr` to a `Pointer` if this is a place and not a ZST.
231208
/// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.

src/librustc_mir/interpret/place.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ use std::hash::Hash;
77

88
use rustc_macros::HashStable;
99
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::truncate;
1110
use rustc_middle::ty::layout::{PrimitiveExt, TyAndLayout};
1211
use rustc_middle::ty::{self, Ty};
1312
use rustc_target::abi::{Abi, Align, DiscriminantKind, FieldsShape};
1413
use rustc_target::abi::{HasDataLayout, LayoutOf, Size, VariantIdx, Variants};
1514

1615
use super::{
17-
AllocId, AllocMap, Allocation, AllocationExtra, ImmTy, Immediate, InterpCx, InterpResult,
18-
LocalValue, Machine, MemoryKind, OpTy, Operand, Pointer, PointerArithmetic, RawConst, Scalar,
19-
ScalarMaybeUndef,
16+
mir_assign_valid_types, truncate, AllocId, AllocMap, Allocation, AllocationExtra, ImmTy,
17+
Immediate, InterpCx, InterpResult, LocalValue, Machine, MemoryKind, OpTy, Operand, Pointer,
18+
PointerArithmetic, RawConst, Scalar, ScalarMaybeUndef,
2019
};
2120

2221
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, HashStable)]
@@ -283,23 +282,6 @@ impl<'tcx, Tag: ::std::fmt::Debug> PlaceTy<'tcx, Tag> {
283282
}
284283
}
285284

286-
/// Test if it is valid for a MIR assignment to assign `src`-typed place to `dest`-typed value.
287-
fn mir_assign_valid_types<'tcx>(src: Ty<'tcx>, dest: Ty<'tcx>) -> bool {
288-
src == dest
289-
|| match (&src.kind, &dest.kind) {
290-
(ty::Ref(_, src_pointee, _), ty::Ref(_, dest_pointee, _)) => {
291-
// After optimizations, there can be assignments that change reference mutability.
292-
// This does not affect reference layout, so that is fine.
293-
src_pointee == dest_pointee
294-
}
295-
(ty::FnPtr(_), ty::FnPtr(_)) => {
296-
// All function pointers have equal layout, and thus can be assigned.
297-
true
298-
}
299-
_ => false,
300-
}
301-
}
302-
303285
// separating the pointer tag for `impl Trait`, see https://github.com/rust-lang/rust/issues/54385
304286
impl<'mir, 'tcx, Tag, M> InterpCx<'mir, 'tcx, M>
305287
where

0 commit comments

Comments
 (0)