Skip to content

Commit

Permalink
fix other source of box deref
Browse files Browse the repository at this point in the history
  • Loading branch information
beepster4096 committed Mar 27, 2022
1 parent ece64ed commit 09ccc63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,19 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
.find(|elem| matches!(elem.1, mir::ProjectionElem::Deref))
{
base = elem.0 + 1;
self.codegen_consume(
let cg_base = self.codegen_consume(
bx,
mir::PlaceRef { projection: &place_ref.projection[..elem.0], ..place_ref },
)
.deref(bx.cx())
);

// a box with a non-zst allocator should not be directly dereferenced
if cg_base.layout.ty.is_box() && !cg_base.layout.field(cx, 1).is_zst() {
let ptr = cg_base.extract_field(bx, 0).extract_field(bx, 0);

ptr.deref(bx.cx())
} else {
cg_base.deref(bx.cx())
}
} else {
bug!("using operand local {:?} as place", place_ref);
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/box/issue-95036.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@

#![feature(allocator_api, bench_black_box)]

#[inline(never)]
pub fn by_ref(node: &mut Box<[u8; 1], &std::alloc::Global>) {
node[0] = 9u8;
}

pub fn main() {
let mut node = Box::new_in([5u8], &std::alloc::Global);
node[0] = 7u8;

std::hint::black_box(node);

let mut node = Box::new_in([5u8], &std::alloc::Global);

by_ref(&mut node);

std::hint::black_box(node);
}

0 comments on commit 09ccc63

Please sign in to comment.