librustc: Allow &mut to be loaned; allow self to be loaned; make &mut loanable to &. r=nmatsakis
This commit is contained in:
parent
bbbb80559c
commit
ad25e208ee
29 changed files with 338 additions and 166 deletions
|
|
@ -20,7 +20,7 @@ fn main() {
|
|||
do (&mut x).with |opt| { //~ ERROR illegal borrow
|
||||
match opt {
|
||||
&Right(ref f) => {
|
||||
x = X(Left((0,0)));
|
||||
x = X(Left((0,0))); //~ ERROR assigning to captured outer mutable variable
|
||||
(*f)()
|
||||
},
|
||||
_ => fail
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ impl Foo {
|
|||
}
|
||||
|
||||
fn a(x: &mut Foo) {
|
||||
x.f(); //~ ERROR illegal borrow unless pure
|
||||
x.f();
|
||||
x.g();
|
||||
x.h();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,8 +22,9 @@ fn main() {
|
|||
let q = &mut b.foo; //~ ERROR loan of mutable field as mutable conflicts with prior loan
|
||||
//~^ ERROR loan of mutable local variable as mutable conflicts with prior loan
|
||||
let r = &mut b; //~ ERROR loan of mutable local variable as mutable conflicts with prior loan
|
||||
//~^ ERROR loan of mutable local variable as mutable conflicts with prior loan
|
||||
io::println(fmt!("*p = %u", *p));
|
||||
q.x += 1;
|
||||
r.foo.x += 1;
|
||||
io::println(fmt!("*p = %u", *p));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,9 +10,9 @@
|
|||
|
||||
fn main() {
|
||||
let mut _a = 3;
|
||||
let _b = &mut _a;
|
||||
let _b = &mut _a; //~ NOTE loan of mutable local variable granted here
|
||||
{
|
||||
let _c = &*_b; //~ ERROR illegal borrow unless pure
|
||||
_a = 4; //~ NOTE impure due to assigning to mutable local variable
|
||||
let _c = &*_b;
|
||||
_a = 4; //~ ERROR assigning to mutable local variable prohibited
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
fn borrow(_v: &int) {}
|
||||
|
||||
fn box_mut(v: &mut ~int) {
|
||||
borrow(*v); //~ ERROR illegal borrow unless pure
|
||||
borrow(*v); // OK: &mut -> &imm
|
||||
}
|
||||
|
||||
fn box_rec_mut(v: &{mut f: ~int}) {
|
||||
|
|
@ -19,11 +19,11 @@ fn box_rec_mut(v: &{mut f: ~int}) {
|
|||
}
|
||||
|
||||
fn box_mut_rec(v: &mut {f: ~int}) {
|
||||
borrow(v.f); //~ ERROR illegal borrow unless pure
|
||||
borrow(v.f); // OK: &mut -> &imm
|
||||
}
|
||||
|
||||
fn box_mut_recs(v: &mut {f: {g: {h: ~int}}}) {
|
||||
borrow(v.f.g.h); //~ ERROR illegal borrow unless pure
|
||||
borrow(v.f.g.h); // OK: &mut -> &imm
|
||||
}
|
||||
|
||||
fn box_imm(v: &~int) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
fn main() {
|
||||
let mut b = ~3;
|
||||
let _x = &mut *b; //~ NOTE prior loan as mutable granted here
|
||||
let _y = &mut *b; //~ ERROR loan of dereference of mutable ~ pointer as mutable conflicts with prior loan
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fn main() {
|
||||
let mut a = ~3;
|
||||
let mut b = &mut a; //~ NOTE loan of mutable local variable granted here
|
||||
let _c = &mut *b;
|
||||
let mut d = /*move*/ a; //~ ERROR moving out of mutable local variable prohibited due to outstanding loan
|
||||
*d += 1;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fn main() {
|
||||
let mut b = ~3;
|
||||
let _x = &mut *b; //~ NOTE loan of mutable local variable granted here
|
||||
let mut y = /*move*/ b; //~ ERROR moving out of mutable local variable prohibited
|
||||
*y += 1;
|
||||
}
|
||||
|
||||
11
src/test/compile-fail/borrowck-wg-move-base-2.rs
Normal file
11
src/test/compile-fail/borrowck-wg-move-base-2.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fn foo(x: &mut int) {
|
||||
let mut a = 3;
|
||||
let mut _y = &mut *x;
|
||||
let _z = &mut *_y;
|
||||
_y = &mut a; //~ ERROR assigning to mutable local variable prohibited
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -15,7 +15,7 @@ fn broken() {
|
|||
while x < 10 {
|
||||
let mut z = x;
|
||||
_y.push(&mut z); //~ ERROR illegal borrow
|
||||
x += 1;
|
||||
x += 1; //~ ERROR assigning to mutable local variable prohibited due to outstanding loan
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,8 +16,7 @@ fn main() {
|
|||
|
||||
match x {
|
||||
{f: ref mut v} => {
|
||||
impure(*v); //~ ERROR illegal borrow unless pure
|
||||
//~^ NOTE impure due to access to impure function
|
||||
impure(*v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,8 +15,7 @@ fn borrow_from_arg_imm_ref(&&v: ~int) {
|
|||
}
|
||||
|
||||
fn borrow_from_arg_mut_ref(v: &mut ~int) {
|
||||
borrow(*v); //~ ERROR illegal borrow unless pure
|
||||
//~^ NOTE impure due to access to impure function
|
||||
borrow(*v);
|
||||
}
|
||||
|
||||
fn borrow_from_arg_move(-v: ~int) {
|
||||
13
src/test/run-pass/borrowck-wg-borrow-mut-to-imm-2.rs
Normal file
13
src/test/run-pass/borrowck-wg-borrow-mut-to-imm-2.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
struct Cat;
|
||||
|
||||
fn bar(_: &Cat) {
|
||||
}
|
||||
|
||||
fn foo(cat: &mut Cat) {
|
||||
bar(&*cat);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut mimi = ~Cat;
|
||||
foo(mimi);
|
||||
}
|
||||
18
src/test/run-pass/borrowck-wg-borrow-mut-to-imm-3.rs
Normal file
18
src/test/run-pass/borrowck-wg-borrow-mut-to-imm-3.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
struct Wizard {
|
||||
spells: ~[&static/str]
|
||||
}
|
||||
|
||||
impl Wizard {
|
||||
fn cast(&mut self) {
|
||||
for self.spells.each |&spell| {
|
||||
io::println(spell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut harry = Wizard {
|
||||
spells: ~[ "expelliarmus", "expecto patronum", "incendio" ]
|
||||
};
|
||||
harry.cast();
|
||||
}
|
||||
12
src/test/run-pass/borrowck-wg-borrow-mut-to-imm.rs
Normal file
12
src/test/run-pass/borrowck-wg-borrow-mut-to-imm.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fn g(x: &Option<int>) {
|
||||
io::println(x.get().to_str());
|
||||
}
|
||||
|
||||
fn f(x: &mut Option<int>) {
|
||||
g(&*x);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut x = ~Some(3);
|
||||
f(x);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue