Skip to content

Commit 975a0e0

Browse files
authored
Rollup merge of rust-lang#94414 - DrMeepster:box_alloc_ice2, r=tmiasko
Fix ICE when using Box<T, A> with large A A sequel to rust-lang#94043 that fixes rust-lang#81270 and rust-lang#92054 (duplicate).
2 parents 9340791 + d316aba commit 975a0e0

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
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 { .. } | Abi::Uninhabited)
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-78459-ice.rs

-6
This file was deleted.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// build-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((), &std::alloc::Global);
22+
Box::new_in((), BigAllocator([0; 2]));
23+
}

0 commit comments

Comments
 (0)