Skip to content

Commit 09ccc63

Browse files
committed
fix other source of box deref
1 parent ece64ed commit 09ccc63

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
441441
.find(|elem| matches!(elem.1, mir::ProjectionElem::Deref))
442442
{
443443
base = elem.0 + 1;
444-
self.codegen_consume(
444+
let cg_base = self.codegen_consume(
445445
bx,
446446
mir::PlaceRef { projection: &place_ref.projection[..elem.0], ..place_ref },
447-
)
448-
.deref(bx.cx())
447+
);
448+
449+
// a box with a non-zst allocator should not be directly dereferenced
450+
if cg_base.layout.ty.is_box() && !cg_base.layout.field(cx, 1).is_zst() {
451+
let ptr = cg_base.extract_field(bx, 0).extract_field(bx, 0);
452+
453+
ptr.deref(bx.cx())
454+
} else {
455+
cg_base.deref(bx.cx())
456+
}
449457
} else {
450458
bug!("using operand local {:?} as place", place_ref);
451459
}

src/test/ui/box/issue-95036.rs

+12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33

44
#![feature(allocator_api, bench_black_box)]
55

6+
#[inline(never)]
7+
pub fn by_ref(node: &mut Box<[u8; 1], &std::alloc::Global>) {
8+
node[0] = 9u8;
9+
}
10+
611
pub fn main() {
712
let mut node = Box::new_in([5u8], &std::alloc::Global);
813
node[0] = 7u8;
14+
15+
std::hint::black_box(node);
16+
17+
let mut node = Box::new_in([5u8], &std::alloc::Global);
18+
19+
by_ref(&mut node);
20+
921
std::hint::black_box(node);
1022
}

0 commit comments

Comments
 (0)