Skip to content

Commit d63ea3e

Browse files
authored
Merge pull request rust-lang#137 from rust-lang/fix/box-alloc-ice
Fix ice in box alloc
2 parents b3e50bd + be9789b commit d63ea3e

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

example/mini_core.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,22 @@ pub trait Deref {
443443
fn deref(&self) -> &Self::Target;
444444
}
445445

446+
pub trait Allocator {
447+
}
448+
449+
pub struct Global;
450+
451+
impl Allocator for Global {}
452+
446453
#[lang = "owned_box"]
447-
pub struct Box<T: ?Sized>(*mut T);
454+
pub struct Box<
455+
T: ?Sized,
456+
A: Allocator = Global,
457+
>(*mut T, A);
448458

449459
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
450460

451-
impl<T: ?Sized> Drop for Box<T> {
461+
impl<T: ?Sized, A: Allocator> Drop for Box<T, A> {
452462
fn drop(&mut self) {
453463
// drop is currently performed by compiler.
454464
}
@@ -468,7 +478,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
468478
}
469479

470480
#[lang = "box_free"]
471-
unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
481+
unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: *mut T, alloc: A) {
472482
libc::free(ptr as *mut u8);
473483
}
474484

src/type_of.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ impl<'tcx> LayoutGccExt<'tcx> for TyAndLayout<'tcx> {
251251
ty::Ref(..) | ty::RawPtr(_) => {
252252
return self.field(cx, index).gcc_type(cx, true);
253253
}
254-
ty::Adt(def, _) if def.is_box() => {
254+
// only wide pointer boxes are handled as pointers
255+
// thin pointer boxes with scalar allocators are handled by the general logic below
256+
ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
255257
let ptr_ty = cx.tcx.mk_mut_ptr(self.ty.boxed_ty());
256258
return cx.layout_of(ptr_ty).scalar_pair_element_gcc_type(cx, index, immediate);
257259
}

0 commit comments

Comments
 (0)