Skip to content

Commit bfa7d44

Browse files
committed
fix box icing when it has aggregate abi
1 parent 6cbc6c3 commit bfa7d44

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
453453
};
454454
for elem in place_ref.projection[base..].iter() {
455455
cg_base = match elem.clone() {
456-
mir::ProjectionElem::Deref => bx.load_operand(cg_base).deref(bx.cx()),
456+
mir::ProjectionElem::Deref => {
457+
// custom allocators can change box's abi, making it unable to be derefed directly
458+
if cg_base.layout.ty.is_box()
459+
&& matches!(cg_base.layout.abi, Abi::Aggregate { .. })
460+
{
461+
let ptr = cg_base.project_field(bx, 0).project_field(bx, 0);
462+
463+
bx.load_operand(ptr).deref(bx.cx())
464+
} else {
465+
bx.load_operand(cg_base).deref(bx.cx())
466+
}
467+
}
457468
mir::ProjectionElem::Field(ref field, _) => {
458469
cg_base.project_field(bx, field.index())
459470
}

src/test/ui/box/issue-81270-ice.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// check-pass
2+
#![feature(allocator_api)]
3+
4+
use std::alloc::Allocator;
5+
6+
struct BigAllocator([usize; 2]);
7+
8+
unsafe impl Allocator for BigAllocator {
9+
fn allocate(
10+
&self,
11+
_: std::alloc::Layout,
12+
) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
13+
todo!()
14+
}
15+
unsafe fn deallocate(&self, _: std::ptr::NonNull<u8>, _: std::alloc::Layout) {
16+
todo!()
17+
}
18+
}
19+
20+
fn main() {
21+
Box::new_in((), BigAllocator([0; 2]));
22+
}

0 commit comments

Comments
 (0)