Skip to content

Commit

Permalink
auto merge of #15171 : pcwalton/rust/remove-cross-borrowing, r=brson
Browse files Browse the repository at this point in the history
This will break code like:

    fn f(x: &mut int) {}

    let mut a = box 1i;
    f(a);

Change it to:

    fn f(x: &mut int) {}

    let mut a = box 1i;
    f(&mut *a);

RFC 33; issue #10504.

[breaking-change]

r? @brson
  • Loading branch information
bors committed Jun 25, 2014
2 parents 7da94c1 + f6bfd2c commit 9f8149e
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ impl<T> DList<T> {
fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
match self.list_head {
None => {
self.list_tail = Rawlink::some(new_head);
self.list_tail = Rawlink::some(&mut *new_head);
self.list_head = link_with_prev(new_head, Rawlink::none());
}
Some(ref mut head) => {
new_head.prev = Rawlink::none();
head.prev = Rawlink::some(new_head);
head.prev = Rawlink::some(&mut *new_head);
mem::swap(head, &mut new_head);
head.next = Some(new_head);
}
Expand All @@ -188,7 +188,7 @@ impl<T> DList<T> {
match self.list_tail.resolve() {
None => return self.push_front_node(new_tail),
Some(tail) => {
self.list_tail = Rawlink::some(new_tail);
self.list_tail = Rawlink::some(&mut *new_tail);
tail.next = link_with_prev(new_tail, Rawlink::some(tail));
}
}
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<T> DList<T> {
#[inline]
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
let head_raw = match self.list_head {
Some(ref mut h) => Rawlink::some(*h),
Some(ref mut h) => Rawlink::some(&mut **h),
None => Rawlink::none(),
};
MutItems{
Expand Down Expand Up @@ -530,7 +530,7 @@ impl<'a, A> MutItems<'a, A> {
Some(prev) => prev,
};
let node_own = prev_node.next.take_unwrap();
ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node));
ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
self.list.length += 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn mut_deref<K, V>(x: &mut Option<Box<TreeNode<K, V>>>)
-> *mut TreeNode<K, V> {
match *x {
Some(ref mut n) => {
let n: &mut TreeNode<K, V> = *n;
let n: &mut TreeNode<K, V> = &mut **n;
n as *mut TreeNode<K, V>
}
None => ptr::mut_null()
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ mod tests {
let mut b = box 7u;

let a_r = &mut a as &mut Any;
let tmp: &mut uint = b;
let tmp: &mut uint = &mut *b;
let b_r = tmp as &mut Any;

match a_r.as_mut::<uint>() {
Expand Down
2 changes: 1 addition & 1 deletion src/libgreen/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl Scheduler {
};

let (current_task_context, next_task_context) =
Scheduler::get_contexts(current_task, next_task);
Scheduler::get_contexts(current_task, &mut *next_task);

// Done with everything - put the next task in TLS. This
// works because due to transmute the borrow checker
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/middle/typeck/infer/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ impl<'f> Coerce<'f> {
let r_borrow = self.get_ref().infcx.next_region_var(coercion);

let inner_ty = match *sty_a {
ty::ty_box(typ) | ty::ty_uniq(typ) => typ,
ty::ty_box(typ) | ty::ty_uniq(typ) => {
if mt_b.mutbl == ast::MutMutable {
return Err(ty::terr_mutability)
}
typ
}
ty::ty_rptr(_, mt_a) => mt_a.ty,
_ => {
return self.subtype(a, b);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/util/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ mod tests {

let mut sh = box Sha256::new();

test_hash(sh, tests.as_slice());
test_hash(&mut *sh, tests.as_slice());
}

/// Feed 1,000,000 'a's into the digest with varying input sizes and check that the result is
Expand Down
2 changes: 1 addition & 1 deletion src/libsync/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ mod tests {
});
}
{
access_shared(sharedstate, &x, mode2, 10);
access_shared(&mut *sharedstate, &x, mode2, 10);
let _ = rx.recv();

assert_eq!(*sharedstate, 20);
Expand Down
12 changes: 6 additions & 6 deletions src/test/bench/shootout-k-nucleotide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ impl Table {
count: 0,
next: None,
};
c.f(entry);
c.f(&mut *entry);
item.next = Some(entry);
}
Some(ref mut entry) => {
if entry.code == key {
c.f(*entry);
c.f(&mut **entry);
return;
}

Table::search_remainder(*entry, key, c)
Table::search_remainder(&mut **entry, key, c)
}
}
}
Expand All @@ -139,7 +139,7 @@ impl Table {
count: 0,
next: None,
};
c.f(entry);
c.f(&mut *entry);
*self.items.get_mut(index as uint) = Some(entry);
return;
}
Expand All @@ -148,11 +148,11 @@ impl Table {
{
let entry = &mut *self.items.get_mut(index as uint).get_mut_ref();
if entry.code == key {
c.f(*entry);
c.f(&mut **entry);
return;
}

Table::search_remainder(*entry, key, c)
Table::search_remainder(&mut **entry, key, c)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/bench/sudoku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Sudoku {
let mut avail = box Colors::new(start_color);

// drop colors already in use in neighbourhood
self.drop_colors(avail, row, col);
self.drop_colors(&mut *avail, row, col);

// find first remaining color that is available
let next = avail.next();
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/borrowck-lend-flow-if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn pre_freeze_cond() {
if cond() {
_w = &v;
}
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
}

fn pre_freeze_else() {
Expand All @@ -46,7 +46,7 @@ fn pre_freeze_else() {
if cond() {
_w = &v;
} else {
borrow_mut(v);
borrow_mut(&mut *v);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/test/compile-fail/borrowck-lend-flow-loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn loop_aliased_mut() {
let mut w = box 4;
let mut _x = &w;
loop {
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
_x = &v;
}
}
Expand All @@ -65,7 +65,7 @@ fn while_aliased_mut() {
let mut w = box 4;
let mut _x = &w;
while cond() {
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
_x = &v;
}
}
Expand All @@ -78,11 +78,11 @@ fn loop_aliased_mut_break() {
let mut w = box 4;
let mut _x = &w;
loop {
borrow_mut(v);
borrow_mut(&mut *v);
_x = &v;
break;
}
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
}

fn while_aliased_mut_break() {
Expand All @@ -92,11 +92,11 @@ fn while_aliased_mut_break() {
let mut w = box 4;
let mut _x = &w;
while cond() {
borrow_mut(v);
borrow_mut(&mut *v);
_x = &v;
break;
}
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
}

fn while_aliased_mut_cond(cond: bool, cond2: bool) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/borrowck-lend-flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ fn pre_freeze() {

let mut v = box 3;
let _w = &v;
borrow_mut(v); //~ ERROR cannot borrow
borrow_mut(&mut *v); //~ ERROR cannot borrow
}

fn post_freeze() {
// In this instance, the const alias starts after the borrow.

let mut v = box 3;
borrow_mut(v);
borrow_mut(&mut *v);
let _w = &v;
}

Expand Down
2 changes: 0 additions & 2 deletions src/test/compile-fail/lint-allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#![deny(unnecessary_allocation)]

fn f(_: &int) {}
fn g(_: &mut int) {}

fn main() {
f(box 1); //~ ERROR unnecessary allocation, use & instead
g(box 1); //~ ERROR unnecessary allocation, use &mut instead
}
17 changes: 17 additions & 0 deletions src/test/compile-fail/mut-cross-borrowing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn f(_: &mut int) {}

fn main() {
let mut x = box 3i;
f(x) //~ ERROR mismatched types
}

4 changes: 2 additions & 2 deletions src/test/run-pass/borrowck-mut-uniq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fn iter_ints(x: &Ints, f: |x: &int| -> bool) -> bool {

pub fn main() {
let mut ints = box Ints {sum: box 0, values: Vec::new()};
add_int(ints, 22);
add_int(ints, 44);
add_int(&mut *ints, 22);
add_int(&mut *ints, 44);

iter_ints(ints, |i| {
println!("int = {}", *i);
Expand Down

0 comments on commit 9f8149e

Please sign in to comment.