Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 19485cc

Browse files
committedApr 16, 2019
Miri: refactor new allocation tagging
1 parent ee621f4 commit 19485cc

File tree

9 files changed

+99
-164
lines changed

9 files changed

+99
-164
lines changed
 

‎src/librustc/mir/interpret/allocation.rs

Lines changed: 20 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ pub struct Allocation<Tag=(),Extra=()> {
4545
}
4646

4747

48-
pub trait AllocationExtra<Tag, MemoryExtra>: ::std::fmt::Debug + Clone {
49-
/// Hook to initialize the extra data when an allocation gets created.
50-
fn memory_allocated(
51-
_size: Size,
52-
_memory_extra: &MemoryExtra
53-
) -> Self;
48+
pub trait AllocationExtra<Tag>: ::std::fmt::Debug + Clone {
49+
// There is no constructor in here because the constructor's type depends
50+
// on `MemoryKind`, and making things sufficiently generic leads to painful
51+
// inference failure.
5452

5553
/// Hook for performing extra checks on a memory read access.
5654
///
@@ -88,15 +86,8 @@ pub trait AllocationExtra<Tag, MemoryExtra>: ::std::fmt::Debug + Clone {
8886
}
8987
}
9088

91-
impl AllocationExtra<(), ()> for () {
92-
#[inline(always)]
93-
fn memory_allocated(
94-
_size: Size,
95-
_memory_extra: &()
96-
) -> Self {
97-
()
98-
}
99-
}
89+
// For Tag=() and no extra state, we have is a trivial implementation.
90+
impl AllocationExtra<()> for () { }
10091

10192
impl<Tag, Extra> Allocation<Tag, Extra> {
10293
/// Creates a read-only allocation initialized by the given bytes
@@ -159,23 +150,21 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
159150
}
160151

161152
/// Byte accessors
162-
impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
153+
impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
163154
/// The last argument controls whether we error out when there are undefined
164155
/// or pointer bytes. You should never call this, call `get_bytes` or
165156
/// `get_bytes_with_undef_and_ptr` instead,
166157
///
167158
/// This function also guarantees that the resulting pointer will remain stable
168159
/// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
169160
/// on that.
170-
fn get_bytes_internal<MemoryExtra>(
161+
fn get_bytes_internal(
171162
&self,
172163
cx: &impl HasDataLayout,
173164
ptr: Pointer<Tag>,
174165
size: Size,
175166
check_defined_and_ptr: bool,
176167
) -> EvalResult<'tcx, &[u8]>
177-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
178-
where Extra: AllocationExtra<Tag, MemoryExtra>
179168
{
180169
self.check_bounds(cx, ptr, size)?;
181170

@@ -196,43 +185,37 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
196185
}
197186

198187
#[inline]
199-
pub fn get_bytes<MemoryExtra>(
188+
pub fn get_bytes(
200189
&self,
201190
cx: &impl HasDataLayout,
202191
ptr: Pointer<Tag>,
203192
size: Size,
204193
) -> EvalResult<'tcx, &[u8]>
205-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
206-
where Extra: AllocationExtra<Tag, MemoryExtra>
207194
{
208195
self.get_bytes_internal(cx, ptr, size, true)
209196
}
210197

211198
/// It is the caller's responsibility to handle undefined and pointer bytes.
212199
/// However, this still checks that there are no relocations on the *edges*.
213200
#[inline]
214-
pub fn get_bytes_with_undef_and_ptr<MemoryExtra>(
201+
pub fn get_bytes_with_undef_and_ptr(
215202
&self,
216203
cx: &impl HasDataLayout,
217204
ptr: Pointer<Tag>,
218205
size: Size,
219206
) -> EvalResult<'tcx, &[u8]>
220-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
221-
where Extra: AllocationExtra<Tag, MemoryExtra>
222207
{
223208
self.get_bytes_internal(cx, ptr, size, false)
224209
}
225210

226211
/// Just calling this already marks everything as defined and removes relocations,
227212
/// so be sure to actually put data there!
228-
pub fn get_bytes_mut<MemoryExtra>(
213+
pub fn get_bytes_mut(
229214
&mut self,
230215
cx: &impl HasDataLayout,
231216
ptr: Pointer<Tag>,
232217
size: Size,
233218
) -> EvalResult<'tcx, &mut [u8]>
234-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
235-
where Extra: AllocationExtra<Tag, MemoryExtra>
236219
{
237220
assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
238221
self.check_bounds(cx, ptr, size)?;
@@ -250,16 +233,14 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
250233
}
251234

252235
/// Reading and writing
253-
impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
236+
impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
254237
/// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
255238
/// before a `0` is found.
256-
pub fn read_c_str<MemoryExtra>(
239+
pub fn read_c_str(
257240
&self,
258241
cx: &impl HasDataLayout,
259242
ptr: Pointer<Tag>,
260243
) -> EvalResult<'tcx, &[u8]>
261-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
262-
where Extra: AllocationExtra<Tag, MemoryExtra>
263244
{
264245
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
265246
let offset = ptr.offset.bytes() as usize;
@@ -278,15 +259,13 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
278259
/// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
279260
/// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
280261
/// given range contains neither relocations nor undef bytes.
281-
pub fn check_bytes<MemoryExtra>(
262+
pub fn check_bytes(
282263
&self,
283264
cx: &impl HasDataLayout,
284265
ptr: Pointer<Tag>,
285266
size: Size,
286267
allow_ptr_and_undef: bool,
287268
) -> EvalResult<'tcx>
288-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
289-
where Extra: AllocationExtra<Tag, MemoryExtra>
290269
{
291270
// Check bounds and relocations on the edges
292271
self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
@@ -301,30 +280,26 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
301280
/// Writes `src` to the memory starting at `ptr.offset`.
302281
///
303282
/// Will do bounds checks on the allocation.
304-
pub fn write_bytes<MemoryExtra>(
283+
pub fn write_bytes(
305284
&mut self,
306285
cx: &impl HasDataLayout,
307286
ptr: Pointer<Tag>,
308287
src: &[u8],
309288
) -> EvalResult<'tcx>
310-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
311-
where Extra: AllocationExtra<Tag, MemoryExtra>
312289
{
313290
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
314291
bytes.clone_from_slice(src);
315292
Ok(())
316293
}
317294

318295
/// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
319-
pub fn write_repeat<MemoryExtra>(
296+
pub fn write_repeat(
320297
&mut self,
321298
cx: &impl HasDataLayout,
322299
ptr: Pointer<Tag>,
323300
val: u8,
324301
count: Size
325302
) -> EvalResult<'tcx>
326-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
327-
where Extra: AllocationExtra<Tag, MemoryExtra>
328303
{
329304
let bytes = self.get_bytes_mut(cx, ptr, count)?;
330305
for b in bytes {
@@ -341,14 +316,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
341316
/// being valid for ZSTs
342317
///
343318
/// Note: This function does not do *any* alignment checks, you need to do these before calling
344-
pub fn read_scalar<MemoryExtra>(
319+
pub fn read_scalar(
345320
&self,
346321
cx: &impl HasDataLayout,
347322
ptr: Pointer<Tag>,
348323
size: Size
349324
) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
350-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
351-
where Extra: AllocationExtra<Tag, MemoryExtra>
352325
{
353326
// get_bytes_unchecked tests relocation edges
354327
let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
@@ -379,13 +352,11 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
379352
}
380353

381354
/// Note: This function does not do *any* alignment checks, you need to do these before calling
382-
pub fn read_ptr_sized<MemoryExtra>(
355+
pub fn read_ptr_sized(
383356
&self,
384357
cx: &impl HasDataLayout,
385358
ptr: Pointer<Tag>,
386359
) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
387-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
388-
where Extra: AllocationExtra<Tag, MemoryExtra>
389360
{
390361
self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
391362
}
@@ -398,15 +369,13 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
398369
/// being valid for ZSTs
399370
///
400371
/// Note: This function does not do *any* alignment checks, you need to do these before calling
401-
pub fn write_scalar<MemoryExtra>(
372+
pub fn write_scalar(
402373
&mut self,
403374
cx: &impl HasDataLayout,
404375
ptr: Pointer<Tag>,
405376
val: ScalarMaybeUndef<Tag>,
406377
type_size: Size,
407378
) -> EvalResult<'tcx>
408-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
409-
where Extra: AllocationExtra<Tag, MemoryExtra>
410379
{
411380
let val = match val {
412381
ScalarMaybeUndef::Scalar(scalar) => scalar,
@@ -446,14 +415,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
446415
}
447416

448417
/// Note: This function does not do *any* alignment checks, you need to do these before calling
449-
pub fn write_ptr_sized<MemoryExtra>(
418+
pub fn write_ptr_sized(
450419
&mut self,
451420
cx: &impl HasDataLayout,
452421
ptr: Pointer<Tag>,
453422
val: ScalarMaybeUndef<Tag>
454423
) -> EvalResult<'tcx>
455-
// FIXME: Working around https://github.com/rust-lang/rust/issues/56209
456-
where Extra: AllocationExtra<Tag, MemoryExtra>
457424
{
458425
let ptr_size = cx.data_layout().pointer_size;
459426
self.write_scalar(cx, ptr.into(), val, ptr_size)

‎src/librustc/mir/interpret/pointer.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ impl<'tcx> Pointer<()> {
9494
Pointer { alloc_id, offset, tag: () }
9595
}
9696

97+
#[inline(always)]
98+
pub fn with_tag<Tag>(self, tag: Tag) -> Pointer<Tag>
99+
{
100+
Pointer::new_with_tag(self.alloc_id, self.offset, tag)
101+
}
102+
97103
#[inline(always)]
98104
pub fn with_default_tag<Tag>(self) -> Pointer<Tag>
99105
where Tag: Default
100106
{
101-
Pointer::new_with_tag(self.alloc_id, self.offset, Default::default())
107+
self.with_tag(Tag::default())
102108
}
103109
}
104110

‎src/librustc/mir/interpret/value.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,19 @@ impl<Tag> fmt::Display for Scalar<Tag> {
119119

120120
impl<'tcx> Scalar<()> {
121121
#[inline]
122-
pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
123-
where Tag: Default
124-
{
122+
pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
125123
match self {
126-
Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_default_tag()),
124+
Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
127125
Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
128126
}
129127
}
128+
129+
#[inline(always)]
130+
pub fn with_default_tag<Tag>(self) -> Scalar<Tag>
131+
where Tag: Default
132+
{
133+
self.with_tag(Tag::default())
134+
}
130135
}
131136

132137
impl<'tcx, Tag> Scalar<Tag> {
@@ -138,14 +143,6 @@ impl<'tcx, Tag> Scalar<Tag> {
138143
}
139144
}
140145

141-
#[inline]
142-
pub fn with_tag(self, new_tag: Tag) -> Self {
143-
match self {
144-
Scalar::Ptr(ptr) => Scalar::Ptr(Pointer { tag: new_tag, ..ptr }),
145-
Scalar::Bits { bits, size } => Scalar::Bits { bits, size },
146-
}
147-
}
148-
149146
#[inline]
150147
pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
151148
Scalar::Bits {
@@ -434,14 +431,19 @@ impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
434431

435432
impl<'tcx> ScalarMaybeUndef<()> {
436433
#[inline]
437-
pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
438-
where Tag: Default
439-
{
434+
pub fn with_tag<Tag>(self, new_tag: Tag) -> ScalarMaybeUndef<Tag> {
440435
match self {
441-
ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_default_tag()),
436+
ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.with_tag(new_tag)),
442437
ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
443438
}
444439
}
440+
441+
#[inline(always)]
442+
pub fn with_default_tag<Tag>(self) -> ScalarMaybeUndef<Tag>
443+
where Tag: Default
444+
{
445+
self.with_tag(Tag::default())
446+
}
445447
}
446448

447449
impl<'tcx, Tag> ScalarMaybeUndef<Tag> {

‎src/librustc_mir/const_eval.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc::hir::def::Def;
1111
use rustc::mir::interpret::{ConstEvalErr, ErrorHandled};
1212
use rustc::mir;
1313
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
14-
use rustc::ty::layout::{self, LayoutOf, VariantIdx};
14+
use rustc::ty::layout::{self, LayoutOf, VariantIdx, Size};
1515
use rustc::ty::subst::Subst;
1616
use rustc::traits::Reveal;
1717
use rustc::util::common::ErrorReported;
@@ -21,7 +21,7 @@ use syntax::ast::Mutability;
2121
use syntax::source_map::{Span, DUMMY_SP};
2222

2323
use crate::interpret::{self,
24-
PlaceTy, MPlaceTy, MemPlace, OpTy, ImmTy, Immediate, Scalar, Pointer,
24+
PlaceTy, MPlaceTy, MemPlace, OpTy, ImmTy, Immediate, Scalar,
2525
RawConst, ConstValue,
2626
EvalResult, EvalError, InterpError, GlobalId, InterpretCx, StackPopCleanup,
2727
Allocation, AllocId, MemoryKind,
@@ -406,6 +406,15 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
406406
Cow::Borrowed(alloc)
407407
}
408408

409+
#[inline(always)]
410+
fn new_allocation(
411+
_size: Size,
412+
_extra: &Self::MemoryExtra,
413+
_kind: MemoryKind<!>,
414+
) -> (Self::AllocExtra, Self::PointerTag) {
415+
((), ())
416+
}
417+
409418
fn box_alloc(
410419
_ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
411420
_dest: PlaceTy<'tcx>,
@@ -439,15 +448,6 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
439448
)
440449
}
441450

442-
#[inline(always)]
443-
fn tag_new_allocation(
444-
_ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
445-
ptr: Pointer,
446-
_kind: MemoryKind<Self::MemoryKinds>,
447-
) -> Pointer {
448-
ptr
449-
}
450-
451451
#[inline(always)]
452452
fn stack_push(
453453
_ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,

‎src/librustc_mir/interpret/machine.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use std::hash::Hash;
77

88
use rustc::hir::{self, def_id::DefId};
99
use rustc::mir;
10-
use rustc::ty::{self, query::TyCtxtAt};
10+
use rustc::ty::{self, query::TyCtxtAt, layout::Size};
1111

1212
use super::{
1313
Allocation, AllocId, EvalResult, Scalar, AllocationExtra,
14-
InterpretCx, PlaceTy, MPlaceTy, OpTy, ImmTy, Pointer, MemoryKind,
14+
InterpretCx, PlaceTy, MPlaceTy, OpTy, ImmTy, MemoryKind,
1515
};
1616

1717
/// Whether this kind of memory is allowed to leak
@@ -76,7 +76,7 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
7676
type MemoryExtra: Default;
7777

7878
/// Extra data stored in every allocation.
79-
type AllocExtra: AllocationExtra<Self::PointerTag, Self::MemoryExtra> + 'static;
79+
type AllocExtra: AllocationExtra<Self::PointerTag> + 'static;
8080

8181
/// Memory's allocation map
8282
type MemoryMap:
@@ -139,18 +139,6 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
139139
memory_extra: &Self::MemoryExtra,
140140
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Self::PointerTag, Self::AllocExtra>>>;
141141

142-
/// Called to turn an allocation obtained from the `tcx` into one that has
143-
/// the right type for this machine.
144-
///
145-
/// This should avoid copying if no work has to be done! If this returns an owned
146-
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
147-
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
148-
/// owned allocation to the map even when the map is shared.)
149-
fn adjust_static_allocation<'b>(
150-
alloc: &'b Allocation,
151-
memory_extra: &Self::MemoryExtra,
152-
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
153-
154142
/// Called for all binary operations on integer(-like) types when one operand is a pointer
155143
/// value, and for the `Offset` operation that is inherently about pointers.
156144
///
@@ -168,12 +156,24 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
168156
dest: PlaceTy<'tcx, Self::PointerTag>,
169157
) -> EvalResult<'tcx>;
170158

171-
/// Adds the tag for a newly allocated pointer.
172-
fn tag_new_allocation(
173-
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
174-
ptr: Pointer,
159+
/// Called to turn an allocation obtained from the `tcx` into one that has
160+
/// the right type for this machine.
161+
///
162+
/// This should avoid copying if no work has to be done! If this returns an owned
163+
/// allocation (because a copy had to be done to add tags or metadata), machine memory will
164+
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
165+
/// owned allocation to the map even when the map is shared.)
166+
fn adjust_static_allocation<'b>(
167+
alloc: &'b Allocation,
168+
memory_extra: &Self::MemoryExtra,
169+
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
170+
171+
/// Computes the extra state and the tag for a new allocation.
172+
fn new_allocation(
173+
size: Size,
174+
extra: &Self::MemoryExtra,
175175
kind: MemoryKind<Self::MemoryKinds>,
176-
) -> Pointer<Self::PointerTag>;
176+
) -> (Self::AllocExtra, Self::PointerTag);
177177

178178
/// Executed when evaluating the `*` operator: Following a reference.
179179
/// This has the chance to adjust the tag. It should not change anything else!

‎src/librustc_mir/interpret/memory.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
132132
size: Size,
133133
align: Align,
134134
kind: MemoryKind<M::MemoryKinds>,
135-
) -> Pointer {
136-
let extra = AllocationExtra::memory_allocated(size, &self.extra);
137-
Pointer::from(self.allocate_with(Allocation::undef(size, align, extra), kind))
135+
) -> Pointer<M::PointerTag> {
136+
let (extra, tag) = M::new_allocation(size, &self.extra, kind);
137+
Pointer::from(self.allocate_with(Allocation::undef(size, align, extra), kind)).with_tag(tag)
138138
}
139139

140140
pub fn reallocate(
@@ -145,7 +145,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
145145
new_size: Size,
146146
new_align: Align,
147147
kind: MemoryKind<M::MemoryKinds>,
148-
) -> EvalResult<'tcx, Pointer> {
148+
) -> EvalResult<'tcx, Pointer<M::PointerTag>> {
149149
if ptr.offset.bytes() != 0 {
150150
return err!(ReallocateNonBasePtr);
151151
}
@@ -156,7 +156,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
156156
self.copy(
157157
ptr.into(),
158158
old_align,
159-
new_ptr.with_default_tag().into(),
159+
new_ptr.into(),
160160
new_align,
161161
old_size.min(new_size),
162162
/*nonoverlapping*/ true,

‎src/librustc_mir/interpret/operand.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,6 @@ pub enum Immediate<Tag=(), Id=AllocId> {
3131
ScalarPair(ScalarMaybeUndef<Tag, Id>, ScalarMaybeUndef<Tag, Id>),
3232
}
3333

34-
impl Immediate {
35-
#[inline]
36-
pub fn with_default_tag<Tag>(self) -> Immediate<Tag>
37-
where Tag: Default
38-
{
39-
match self {
40-
Immediate::Scalar(x) => Immediate::Scalar(x.with_default_tag()),
41-
Immediate::ScalarPair(x, y) =>
42-
Immediate::ScalarPair(x.with_default_tag(), y.with_default_tag()),
43-
}
44-
}
45-
}
46-
4734
impl<'tcx, Tag> Immediate<Tag> {
4835
#[inline]
4936
pub fn from_scalar(val: Scalar<Tag>) -> Self {
@@ -142,18 +129,6 @@ pub enum Operand<Tag=(), Id=AllocId> {
142129
Indirect(MemPlace<Tag, Id>),
143130
}
144131

145-
impl Operand {
146-
#[inline]
147-
pub fn with_default_tag<Tag>(self) -> Operand<Tag>
148-
where Tag: Default
149-
{
150-
match self {
151-
Operand::Immediate(x) => Operand::Immediate(x.with_default_tag()),
152-
Operand::Indirect(x) => Operand::Indirect(x.with_default_tag()),
153-
}
154-
}
155-
}
156-
157132
impl<Tag> Operand<Tag> {
158133
#[inline]
159134
pub fn erase_tag(self) -> Operand
@@ -554,16 +529,17 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
554529
// We rely on mutability being set correctly in that allocation to prevent writes
555530
// where none should happen -- and for `static mut`, we copy on demand anyway.
556531
Operand::Indirect(
557-
MemPlace::from_ptr(ptr, alloc.align)
558-
).with_default_tag()
532+
MemPlace::from_ptr(ptr.with_default_tag(), alloc.align)
533+
)
559534
},
560535
ConstValue::Slice(a, b) =>
561536
Operand::Immediate(Immediate::ScalarPair(
562-
a.into(),
563-
Scalar::from_uint(b, self.tcx.data_layout.pointer_size).into(),
564-
)).with_default_tag(),
537+
a.with_default_tag().into(),
538+
Scalar::from_uint(b, self.tcx.data_layout.pointer_size)
539+
.with_default_tag().into(),
540+
)),
565541
ConstValue::Scalar(x) =>
566-
Operand::Immediate(Immediate::Scalar(x.into())).with_default_tag(),
542+
Operand::Immediate(Immediate::Scalar(x.with_default_tag().into())),
567543
ConstValue::Unevaluated(def_id, substs) => {
568544
let instance = self.resolve(def_id, substs)?;
569545
return Ok(OpTy::from(self.const_eval_raw(GlobalId {

‎src/librustc_mir/interpret/place.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,40 +83,26 @@ impl<'tcx, Tag> From<MPlaceTy<'tcx, Tag>> for PlaceTy<'tcx, Tag> {
8383
}
8484
}
8585

86-
impl MemPlace {
86+
impl<Tag> MemPlace<Tag> {
87+
/// Replace ptr tag, maintain vtable tag (if any)
8788
#[inline]
88-
pub fn with_default_tag<Tag>(self) -> MemPlace<Tag>
89-
where Tag: Default
90-
{
89+
pub fn replace_tag(self, new_tag: Tag) -> Self {
9190
MemPlace {
92-
ptr: self.ptr.with_default_tag(),
91+
ptr: self.ptr.erase_tag().with_tag(new_tag),
9392
align: self.align,
94-
meta: self.meta.map(Scalar::with_default_tag),
93+
meta: self.meta,
9594
}
9695
}
97-
}
9896

99-
impl<Tag> MemPlace<Tag> {
10097
#[inline]
101-
pub fn erase_tag(self) -> MemPlace
102-
{
98+
pub fn erase_tag(self) -> MemPlace {
10399
MemPlace {
104100
ptr: self.ptr.erase_tag(),
105101
align: self.align,
106102
meta: self.meta.map(Scalar::erase_tag),
107103
}
108104
}
109105

110-
#[inline]
111-
pub fn with_tag(self, new_tag: Tag) -> Self
112-
{
113-
MemPlace {
114-
ptr: self.ptr.with_tag(new_tag),
115-
align: self.align,
116-
meta: self.meta,
117-
}
118-
}
119-
120106
#[inline(always)]
121107
pub fn from_scalar_ptr(ptr: Scalar<Tag>, align: Align) -> Self {
122108
MemPlace {
@@ -189,11 +175,11 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
189175
}
190176
}
191177

178+
/// Replace ptr tag, maintain vtable tag (if any)
192179
#[inline]
193-
pub fn with_tag(self, new_tag: Tag) -> Self
194-
{
180+
pub fn replace_tag(self, new_tag: Tag) -> Self {
195181
MPlaceTy {
196-
mplace: self.mplace.with_tag(new_tag),
182+
mplace: self.mplace.replace_tag(new_tag),
197183
layout: self.layout,
198184
}
199185
}
@@ -312,7 +298,7 @@ where
312298
M: Machine<'a, 'mir, 'tcx, PointerTag=Tag>,
313299
// FIXME: Working around https://github.com/rust-lang/rust/issues/24159
314300
M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation<Tag, M::AllocExtra>)>,
315-
M::AllocExtra: AllocationExtra<Tag, M::MemoryExtra>,
301+
M::AllocExtra: AllocationExtra<Tag>,
316302
{
317303
/// Take a value, which represents a (thin or fat) reference, and make it a place.
318304
/// Alignment is just based on the type. This is the inverse of `MemPlace::to_ref()`.
@@ -943,7 +929,6 @@ where
943929
let (size, align) = self.size_and_align_of(meta, local_layout)?
944930
.expect("Cannot allocate for non-dyn-sized type");
945931
let ptr = self.memory.allocate(size, align, MemoryKind::Stack);
946-
let ptr = M::tag_new_allocation(self, ptr, MemoryKind::Stack);
947932
let mplace = MemPlace { ptr: ptr.into(), align, meta };
948933
if let Some(value) = old_val {
949934
// Preserve old value.
@@ -981,7 +966,6 @@ where
981966
kind: MemoryKind<M::MemoryKinds>,
982967
) -> MPlaceTy<'tcx, M::PointerTag> {
983968
let ptr = self.memory.allocate(layout.size, layout.align.abi, kind);
984-
let ptr = M::tag_new_allocation(self, ptr, kind);
985969
MPlaceTy::from_aligned_ptr(ptr, layout)
986970
}
987971

‎src/librustc_mir/interpret/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
5252
ptr_size * (3 + methods.len() as u64),
5353
ptr_align,
5454
MemoryKind::Vtable,
55-
).with_default_tag();
55+
);
5656
let tcx = &*self.tcx;
5757

5858
let drop = crate::monomorphize::resolve_drop_in_place(*tcx, ty);

0 commit comments

Comments
 (0)
Please sign in to comment.