Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/analyze/basic_block/visitor/reborrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl<'a, 'tcx, 'ctx> mir::visit::MutVisitor<'tcx> for ReborrowVisitor<'a, 'tcx,
return;
}

let inner_place = if place.projection.last() == Some(&mir::PlaceElem::Deref) {
let assigns_through_deref = place.projection.last() == Some(&mir::PlaceElem::Deref);
let inner_place = if assigns_through_deref {
// *m = *m + 1 => m1 = &mut m; *m1 = *m + 1
let mut projection = place.projection.as_ref().to_vec();
projection.pop();
Expand All @@ -74,11 +75,11 @@ impl<'a, 'tcx, 'ctx> mir::visit::MutVisitor<'tcx> for ReborrowVisitor<'a, 'tcx,

let ty = inner_place.ty(&self.analyzer.local_decls, self.tcx).ty;
let (new_local, new_place) = match ty.kind() {
mir_ty::TyKind::Ref(_, inner_ty, m) if m.is_mut() => {
mir_ty::TyKind::Ref(_, inner_ty, m) if assigns_through_deref && m.is_mut() => {
let new_local = self.insert_reborrow(*place, *inner_ty);
(new_local, new_local.into())
}
mir_ty::TyKind::Adt(adt, args) if adt.is_box() => {
mir_ty::TyKind::Adt(adt, args) if assigns_through_deref && adt.is_box() => {
let inner_ty = args.type_at(0);
let new_local = self.insert_borrow(*place, inner_ty);
(new_local, new_local.into())
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/fail/assign_box_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@error-in-other-file: Unsat
//@compile-flags: -C debug-assertions=off

struct Cache {
page: Box<i64>,
}

impl thrust_models::Model for Cache {
type Ty = Self;
}

fn main() {
let mut c = Cache { page: Box::new(1) };
c.page = Box::new(5);
assert!(*c.page == 1);
}
16 changes: 16 additions & 0 deletions tests/ui/pass/assign_box_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@check-pass
//@compile-flags: -C debug-assertions=off

struct Cache {
page: Box<i64>,
}

impl thrust_models::Model for Cache {
type Ty = Self;
}

fn main() {
let mut c = Cache { page: Box::new(1) };
c.page = Box::new(5);
assert!(*c.page == 5);
}
Loading