Skip to content

Commit

Permalink
Rollup merge of rust-lang#94414 - DrMeepster:box_alloc_ice2, r=tmiasko
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
matthiaskrgr committed Feb 28, 2022
2 parents 9340791 + d316aba commit 975a0e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
13 changes: 12 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
};
for elem in place_ref.projection[base..].iter() {
cg_base = match elem.clone() {
mir::ProjectionElem::Deref => bx.load_operand(cg_base).deref(bx.cx()),
mir::ProjectionElem::Deref => {
// custom allocators can change box's abi, making it unable to be derefed directly
if cg_base.layout.ty.is_box()
&& matches!(cg_base.layout.abi, Abi::Aggregate { .. } | Abi::Uninhabited)
{
let ptr = cg_base.project_field(bx, 0).project_field(bx, 0);

bx.load_operand(ptr).deref(bx.cx())
} else {
bx.load_operand(cg_base).deref(bx.cx())
}
}
mir::ProjectionElem::Field(ref field, _) => {
cg_base.project_field(bx, field.index())
}
Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/box/issue-78459-ice.rs

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/ui/box/large-allocator-ice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// build-pass
#![feature(allocator_api)]

use std::alloc::Allocator;

struct BigAllocator([usize; 2]);

unsafe impl Allocator for BigAllocator {
fn allocate(
&self,
_: std::alloc::Layout,
) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
todo!()
}
unsafe fn deallocate(&self, _: std::ptr::NonNull<u8>, _: std::alloc::Layout) {
todo!()
}
}

fn main() {
Box::new_in((), &std::alloc::Global);
Box::new_in((), BigAllocator([0; 2]));
}

0 comments on commit 975a0e0

Please sign in to comment.