@@ -45,12 +45,10 @@ pub struct Allocation<Tag=(),Extra=()> {
45
45
}
46
46
47
47
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.
54
52
55
53
/// Hook for performing extra checks on a memory read access.
56
54
///
@@ -88,15 +86,8 @@ pub trait AllocationExtra<Tag, MemoryExtra>: ::std::fmt::Debug + Clone {
88
86
}
89
87
}
90
88
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 ( ) { }
100
91
101
92
impl < Tag , Extra > Allocation < Tag , Extra > {
102
93
/// Creates a read-only allocation initialized by the given bytes
@@ -159,23 +150,21 @@ impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
159
150
}
160
151
161
152
/// Byte accessors
162
- impl < ' tcx , Tag : Copy , Extra > Allocation < Tag , Extra > {
153
+ impl < ' tcx , Tag : Copy , Extra : AllocationExtra < Tag > > Allocation < Tag , Extra > {
163
154
/// The last argument controls whether we error out when there are undefined
164
155
/// or pointer bytes. You should never call this, call `get_bytes` or
165
156
/// `get_bytes_with_undef_and_ptr` instead,
166
157
///
167
158
/// This function also guarantees that the resulting pointer will remain stable
168
159
/// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
169
160
/// on that.
170
- fn get_bytes_internal < MemoryExtra > (
161
+ fn get_bytes_internal (
171
162
& self ,
172
163
cx : & impl HasDataLayout ,
173
164
ptr : Pointer < Tag > ,
174
165
size : Size ,
175
166
check_defined_and_ptr : bool ,
176
167
) -> EvalResult < ' tcx , & [ u8 ] >
177
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
178
- where Extra : AllocationExtra < Tag , MemoryExtra >
179
168
{
180
169
self . check_bounds ( cx, ptr, size) ?;
181
170
@@ -196,43 +185,37 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
196
185
}
197
186
198
187
#[ inline]
199
- pub fn get_bytes < MemoryExtra > (
188
+ pub fn get_bytes (
200
189
& self ,
201
190
cx : & impl HasDataLayout ,
202
191
ptr : Pointer < Tag > ,
203
192
size : Size ,
204
193
) -> EvalResult < ' tcx , & [ u8 ] >
205
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
206
- where Extra : AllocationExtra < Tag , MemoryExtra >
207
194
{
208
195
self . get_bytes_internal ( cx, ptr, size, true )
209
196
}
210
197
211
198
/// It is the caller's responsibility to handle undefined and pointer bytes.
212
199
/// However, this still checks that there are no relocations on the *edges*.
213
200
#[ inline]
214
- pub fn get_bytes_with_undef_and_ptr < MemoryExtra > (
201
+ pub fn get_bytes_with_undef_and_ptr (
215
202
& self ,
216
203
cx : & impl HasDataLayout ,
217
204
ptr : Pointer < Tag > ,
218
205
size : Size ,
219
206
) -> EvalResult < ' tcx , & [ u8 ] >
220
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
221
- where Extra : AllocationExtra < Tag , MemoryExtra >
222
207
{
223
208
self . get_bytes_internal ( cx, ptr, size, false )
224
209
}
225
210
226
211
/// Just calling this already marks everything as defined and removes relocations,
227
212
/// so be sure to actually put data there!
228
- pub fn get_bytes_mut < MemoryExtra > (
213
+ pub fn get_bytes_mut (
229
214
& mut self ,
230
215
cx : & impl HasDataLayout ,
231
216
ptr : Pointer < Tag > ,
232
217
size : Size ,
233
218
) -> EvalResult < ' tcx , & mut [ u8 ] >
234
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
235
- where Extra : AllocationExtra < Tag , MemoryExtra >
236
219
{
237
220
assert_ne ! ( size. bytes( ) , 0 , "0-sized accesses should never even get a `Pointer`" ) ;
238
221
self . check_bounds ( cx, ptr, size) ?;
@@ -250,16 +233,14 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
250
233
}
251
234
252
235
/// Reading and writing
253
- impl < ' tcx , Tag : Copy , Extra > Allocation < Tag , Extra > {
236
+ impl < ' tcx , Tag : Copy , Extra : AllocationExtra < Tag > > Allocation < Tag , Extra > {
254
237
/// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
255
238
/// before a `0` is found.
256
- pub fn read_c_str < MemoryExtra > (
239
+ pub fn read_c_str (
257
240
& self ,
258
241
cx : & impl HasDataLayout ,
259
242
ptr : Pointer < Tag > ,
260
243
) -> EvalResult < ' tcx , & [ u8 ] >
261
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
262
- where Extra : AllocationExtra < Tag , MemoryExtra >
263
244
{
264
245
assert_eq ! ( ptr. offset. bytes( ) as usize as u64 , ptr. offset. bytes( ) ) ;
265
246
let offset = ptr. offset . bytes ( ) as usize ;
@@ -278,15 +259,13 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
278
259
/// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
279
260
/// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
280
261
/// given range contains neither relocations nor undef bytes.
281
- pub fn check_bytes < MemoryExtra > (
262
+ pub fn check_bytes (
282
263
& self ,
283
264
cx : & impl HasDataLayout ,
284
265
ptr : Pointer < Tag > ,
285
266
size : Size ,
286
267
allow_ptr_and_undef : bool ,
287
268
) -> EvalResult < ' tcx >
288
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
289
- where Extra : AllocationExtra < Tag , MemoryExtra >
290
269
{
291
270
// Check bounds and relocations on the edges
292
271
self . get_bytes_with_undef_and_ptr ( cx, ptr, size) ?;
@@ -301,30 +280,26 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
301
280
/// Writes `src` to the memory starting at `ptr.offset`.
302
281
///
303
282
/// Will do bounds checks on the allocation.
304
- pub fn write_bytes < MemoryExtra > (
283
+ pub fn write_bytes (
305
284
& mut self ,
306
285
cx : & impl HasDataLayout ,
307
286
ptr : Pointer < Tag > ,
308
287
src : & [ u8 ] ,
309
288
) -> EvalResult < ' tcx >
310
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
311
- where Extra : AllocationExtra < Tag , MemoryExtra >
312
289
{
313
290
let bytes = self . get_bytes_mut ( cx, ptr, Size :: from_bytes ( src. len ( ) as u64 ) ) ?;
314
291
bytes. clone_from_slice ( src) ;
315
292
Ok ( ( ) )
316
293
}
317
294
318
295
/// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
319
- pub fn write_repeat < MemoryExtra > (
296
+ pub fn write_repeat (
320
297
& mut self ,
321
298
cx : & impl HasDataLayout ,
322
299
ptr : Pointer < Tag > ,
323
300
val : u8 ,
324
301
count : Size
325
302
) -> EvalResult < ' tcx >
326
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
327
- where Extra : AllocationExtra < Tag , MemoryExtra >
328
303
{
329
304
let bytes = self . get_bytes_mut ( cx, ptr, count) ?;
330
305
for b in bytes {
@@ -341,14 +316,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
341
316
/// being valid for ZSTs
342
317
///
343
318
/// 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 (
345
320
& self ,
346
321
cx : & impl HasDataLayout ,
347
322
ptr : Pointer < Tag > ,
348
323
size : Size
349
324
) -> EvalResult < ' tcx , ScalarMaybeUndef < Tag > >
350
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
351
- where Extra : AllocationExtra < Tag , MemoryExtra >
352
325
{
353
326
// get_bytes_unchecked tests relocation edges
354
327
let bytes = self . get_bytes_with_undef_and_ptr ( cx, ptr, size) ?;
@@ -379,13 +352,11 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
379
352
}
380
353
381
354
/// 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 (
383
356
& self ,
384
357
cx : & impl HasDataLayout ,
385
358
ptr : Pointer < Tag > ,
386
359
) -> EvalResult < ' tcx , ScalarMaybeUndef < Tag > >
387
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
388
- where Extra : AllocationExtra < Tag , MemoryExtra >
389
360
{
390
361
self . read_scalar ( cx, ptr, cx. data_layout ( ) . pointer_size )
391
362
}
@@ -398,15 +369,13 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
398
369
/// being valid for ZSTs
399
370
///
400
371
/// 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 (
402
373
& mut self ,
403
374
cx : & impl HasDataLayout ,
404
375
ptr : Pointer < Tag > ,
405
376
val : ScalarMaybeUndef < Tag > ,
406
377
type_size : Size ,
407
378
) -> EvalResult < ' tcx >
408
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
409
- where Extra : AllocationExtra < Tag , MemoryExtra >
410
379
{
411
380
let val = match val {
412
381
ScalarMaybeUndef :: Scalar ( scalar) => scalar,
@@ -446,14 +415,12 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
446
415
}
447
416
448
417
/// 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 (
450
419
& mut self ,
451
420
cx : & impl HasDataLayout ,
452
421
ptr : Pointer < Tag > ,
453
422
val : ScalarMaybeUndef < Tag >
454
423
) -> EvalResult < ' tcx >
455
- // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
456
- where Extra : AllocationExtra < Tag , MemoryExtra >
457
424
{
458
425
let ptr_size = cx. data_layout ( ) . pointer_size ;
459
426
self . write_scalar ( cx, ptr. into ( ) , val, ptr_size)
0 commit comments