diff --git a/compiler/rustc_codegen_ssa/src/mir/place.rs b/compiler/rustc_codegen_ssa/src/mir/place.rs index 2b8fa3be56d..12cdc2b2c7d 100644 --- a/compiler/rustc_codegen_ssa/src/mir/place.rs +++ b/compiler/rustc_codegen_ssa/src/mir/place.rs @@ -454,10 +454,8 @@ 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 => { - // 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) - { + // a box with a non-zst allocator should not be directly dereferenced + if cg_base.layout.ty.is_box() && !cg_base.layout.field(cx, 0).is_zst() { let ptr = cg_base.project_field(bx, 0).project_field(bx, 0); bx.load_operand(ptr).deref(bx.cx()) diff --git a/src/test/ui/box/issue-95036.rs b/src/test/ui/box/issue-95036.rs new file mode 100644 index 00000000000..e062599d74c --- /dev/null +++ b/src/test/ui/box/issue-95036.rs @@ -0,0 +1,10 @@ +// compile-flags: -O +// compile-pass + +#![feature(allocator_api, bench_black_box)] + +pub fn main() { + let mut node = Box::new_in([5u8], &std::alloc::Global); + node[0] = 7u8; + std::hint::black_box(node); +}