Skip to content

Commit

Permalink
Implement RFC #43
Browse files Browse the repository at this point in the history
Remove the ability of the borrow checker to determine that repeated
dereferences of a Box<T> refer to the same memory object. This will
usually require one of two workarounds:

1) The interior of a Box<T> will sometimes need to be moved / borrowed
into a temporary before moving / borrowing individual derived paths.

2) A `ref x` pattern will have to be replaced with a `box ref x`
pattern.

Fixes #16094.

[breaking-change]
  • Loading branch information
Cameron Zwarich committed Jul 30, 2014
1 parent 8c4dbf3 commit 3607c7a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/librustc/middle/borrowck/check_loans.rs
Expand Up @@ -261,6 +261,7 @@ impl<'a> CheckLoanCtxt<'a> {
// let x = &mut a.b.c; // Restricts a, a.b, and a.b.c
// let y = a; // Conflicts with restriction

let loan_path = owned_ptr_base_path(loan_path);
let cont = self.each_in_scope_loan(scope_id, |loan| {
let mut ret = true;
for restr_path in loan.restricted_paths.iter() {
Expand Down Expand Up @@ -395,8 +396,9 @@ impl<'a> CheckLoanCtxt<'a> {
return true;
}

let loan2_base_path = owned_ptr_base_path_rc(&loan2.loan_path);
for restr_path in loan1.restricted_paths.iter() {
if *restr_path != loan2.loan_path { continue; }
if *restr_path != loan2_base_path { continue; }

let old_pronoun = if new_loan.loan_path == old_loan.loan_path {
"it".to_string()
Expand Down Expand Up @@ -648,7 +650,8 @@ impl<'a> CheckLoanCtxt<'a> {

debug!("check_if_path_is_moved(id={:?}, use_kind={:?}, lp={})",
id, use_kind, lp.repr(self.bccx.tcx));
self.move_data.each_move_of(id, lp, |move, moved_lp| {
let base_lp = owned_ptr_base_path_rc(lp);
self.move_data.each_move_of(id, &base_lp, |move, moved_lp| {
self.bccx.report_use_of_moved_value(
span,
use_kind,
Expand Down
9 changes: 5 additions & 4 deletions src/test/compile-fail/borrowck-borrow-from-owned-ptr.rs
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -52,14 +52,15 @@ fn borrow_same_field_twice_imm_imm() {
fn borrow_both_fields_mut() {
let mut foo = make_foo();
let bar1 = &mut foo.bar1;
let _bar2 = &mut foo.bar2;
let _bar2 = &mut foo.bar2; //~ ERROR cannot borrow
*bar1;
}

fn borrow_both_mut_pattern() {
let mut foo = make_foo();
match *foo {
Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
//~^ ERROR cannot borrow
}
}

Expand Down Expand Up @@ -120,7 +121,7 @@ fn borrow_imm_and_base_imm() {
fn borrow_mut_and_imm() {
let mut foo = make_foo();
let bar1 = &mut foo.bar1;
let _foo1 = &foo.bar2;
let _foo1 = &foo.bar2; //~ ERROR cannot borrow
*bar1;
}

Expand All @@ -133,7 +134,7 @@ fn borrow_mut_from_imm() {
fn borrow_long_path_both_mut() {
let mut foo = make_foo();
let bar1 = &mut foo.bar1.int1;
let foo1 = &mut foo.bar2.int2;
let foo1 = &mut foo.bar2.int2; //~ ERROR cannot borrow
*bar1;
*foo1;
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/run-pass/match-implicit-copy-unique.rs
Expand Up @@ -13,8 +13,9 @@ struct Pair { a: Box<int>, b: Box<int> }

pub fn main() {
let mut x = box Pair {a: box 10, b: box 20};
match x {
box Pair {a: ref mut a, b: ref mut _b} => {
let x_internal = &mut *x;
match *x_internal {
Pair {a: ref mut a, b: ref mut _b} => {
assert!(**a == 10); *a = box 30; assert!(**a == 30);
}
}
Expand Down

0 comments on commit 3607c7a

Please sign in to comment.