Skip to content

Commit a8cff6d

Browse files
authored
Rollup merge of rust-lang#37708 - oli-obk:box_free, r=eddyb
change the `box_free` lang item to accept pointers to unsized types in miri we use the `box_free` lang item as the destructor for `Box` objects, since the function's api matches that of an `fn drop(&mut self)` in a hypothetical `impl<T: ?Sized> Drop for Box<T>` exactly. This works fine except if we insert a check in the `size_of` intrinsic to ensure that it is only called with sized types, since the `box_free` lang item calls that intrinsic. cc @eddyb no clue who to r? here, probably lang team?
2 parents 03a6d8e + 323c20c commit a8cff6d

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/doc/book/lang-items.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ unsafe fn deallocate(ptr: *mut u8, _size: usize, _align: usize) {
4646
}
4747
4848
#[lang = "box_free"]
49-
unsafe fn box_free<T>(ptr: *mut T) {
50-
deallocate(ptr as *mut u8, ::core::mem::size_of::<T>(), ::core::mem::align_of::<T>());
49+
unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
50+
deallocate(ptr as *mut u8, ::core::mem::size_of_val(&*ptr), ::core::mem::align_of_val(&*ptr));
5151
}
5252
5353
#[start]

src/liballoc/heap.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use core::{isize, usize};
1919
#[cfg(not(test))]
20-
use core::intrinsics::{min_align_of, size_of};
20+
use core::intrinsics::{min_align_of_val, size_of_val};
2121

2222
#[allow(improper_ctypes)]
2323
extern "C" {
@@ -152,11 +152,12 @@ unsafe fn exchange_free(ptr: *mut u8, old_size: usize, align: usize) {
152152
#[cfg(not(test))]
153153
#[lang = "box_free"]
154154
#[inline]
155-
unsafe fn box_free<T>(ptr: *mut T) {
156-
let size = size_of::<T>();
155+
unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
156+
let size = size_of_val(&*ptr);
157+
let align = min_align_of_val(&*ptr);
157158
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
158159
if size != 0 {
159-
deallocate(ptr as *mut u8, size, min_align_of::<T>());
160+
deallocate(ptr as *mut u8, size, align);
160161
}
161162
}
162163

0 commit comments

Comments
 (0)