as a test.
+++ /dev/null
-// 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 main() {
- let i: isize;
-
- println!("{}", false && { i = 5; true });
- println!("{}", i); //~ ERROR use of possibly uninitialized variable: `i`
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that we are able to distinguish when loans borrow different
-// anonymous fields of a tuple vs the same anonymous field.
-
-struct Y(usize, usize);
-
-fn distinct_variant() {
- let mut y = Y(1, 2);
-
- let a = match y {
- Y(ref mut a, _) => a
- };
-
- let b = match y {
- Y(_, ref mut b) => b
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn same_variant() {
- let mut y = Y(1, 2);
-
- let a = match y {
- Y(ref mut a, _) => a
- };
-
- let b = match y {
- Y(ref mut b, _) => b //~ ERROR cannot borrow
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that we are able to distinguish when loans borrow different
-// anonymous fields of a tuple vs the same anonymous field.
-
-fn distinct_variant() {
- let mut y = (1, 2);
-
- let a = match y {
- (ref mut a, _) => a
- };
-
- let b = match y {
- (_, ref mut b) => b
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn same_variant() {
- let mut y = (1, 2);
-
- let a = match y {
- (ref mut a, _) => a
- };
-
- let b = match y {
- (ref mut b, _) => b //~ ERROR cannot borrow
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that we are able to distinguish when loans borrow different
-// anonymous fields of an enum variant vs the same anonymous field.
-
-enum Foo {
- X, Y(usize, usize)
-}
-
-fn distinct_variant() {
- let mut y = Foo::Y(1, 2);
-
- let a = match y {
- Foo::Y(ref mut a, _) => a,
- Foo::X => panic!()
- };
-
- let b = match y {
- Foo::Y(_, ref mut b) => b,
- Foo::X => panic!()
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn same_variant() {
- let mut y = Foo::Y(1, 2);
-
- let a = match y {
- Foo::Y(ref mut a, _) => a,
- Foo::X => panic!()
- };
-
- let b = match y {
- Foo::Y(ref mut b, _) => b, //~ ERROR cannot borrow
- Foo::X => panic!()
- };
-
- *a += 1;
- *b += 1;
-}
-
-fn main() {
-}
+++ /dev/null
-// Copyright 2015 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.
-
-#[derive(Copy, Clone)]
-struct S;
-
-impl S {
- fn mutate(&mut self) {
- }
-}
-
-fn func(arg: S) {
- arg.mutate(); //~ ERROR: cannot borrow immutable argument
-}
-
-impl S {
- fn method(&self, arg: S) {
- arg.mutate(); //~ ERROR: cannot borrow immutable argument
- }
-}
-
-trait T {
- fn default(&self, arg: S) {
- arg.mutate(); //~ ERROR: cannot borrow immutable argument
- }
-}
-
-impl T for S {}
-
-fn main() {
- let s = S;
- func(s);
- s.method(s);
- s.default(s);
- (|arg: S| { arg.mutate() })(s); //~ ERROR: cannot borrow immutable argument
-}
+++ /dev/null
-// 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.
-
-struct Point {
- x: isize,
- y: isize,
-}
-
-fn a() {
- let mut p = vec!(1);
-
- // Create an immutable pointer into p's contents:
- let q: &isize = &p[0];
-
- p[0] = 5; //~ ERROR cannot borrow
-
- println!("{}", *q);
-}
-
-fn borrow<F>(_x: &[isize], _f: F) where F: FnOnce() {}
-
-fn b() {
- // here we alias the mutable vector into an imm slice and try to
- // modify the original:
-
- let mut p = vec!(1);
-
- borrow(
- &p,
- || p[0] = 5); //~ ERROR cannot borrow `p` as mutable
-}
-
-fn c() {
- // Legal because the scope of the borrow does not include the
- // modification:
- let mut p = vec!(1);
- borrow(&p, ||{});
- p[0] = 5;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-struct point { x: isize, y: isize }
-
-fn a() {
- let mut p = point {x: 3, y: 4};
- let q = &p;
-
- // This assignment is illegal because the field x is not
- // inherently mutable; since `p` was made immutable, `p.x` is now
- // immutable. Otherwise the type of &_q.x (&isize) would be wrong.
- p.x = 5; //~ ERROR cannot assign to `p.x`
- q.x;
-}
-
-fn c() {
- // this is sort of the opposite. We take a loan to the interior of `p`
- // and then try to overwrite `p` as a whole.
-
- let mut p = point {x: 3, y: 4};
- let q = &p.y;
- p = point {x: 5, y: 7};//~ ERROR cannot assign to `p`
- p.x; // silence warning
- *q; // stretch loan
-}
-
-fn d() {
- // just for completeness's sake, the easy case, where we take the
- // address of a subcomponent and then modify that subcomponent:
-
- let mut p = point {x: 3, y: 4};
- let q = &p.y;
- p.y = 5; //~ ERROR cannot assign to `p.y`
- *q;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-// Test that assignments to an `&mut` pointer which is found in a
-// borrowed (but otherwise non-aliasable) location is illegal.
-
-struct S<'a> {
- pointer: &'a mut isize
-}
-
-fn a(s: &S) {
- *s.pointer += 1; //~ ERROR cannot assign
-}
-
-fn b(s: &mut S) {
- *s.pointer += 1;
-}
-
-fn c(s: & &mut S) {
- *s.pointer += 1; //~ ERROR cannot assign
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-// Test that assignments to an `&mut` pointer which is found in a
-// borrowed (but otherwise non-aliasable) location is illegal.
-
-struct S<'a> {
- pointer: &'a mut isize
-}
-
-fn copy_borrowed_ptr<'a>(p: &'a mut S<'a>) -> S<'a> {
- S { pointer: &mut *p.pointer }
-}
-
-fn main() {
- let mut x = 1;
-
- {
- let mut y = S { pointer: &mut x };
- let z = copy_borrowed_ptr(&mut y);
- *y.pointer += 1; //~ ERROR cannot assign
- *z.pointer += 1;
- }
-}
+++ /dev/null
-// 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.
-
-static foo: isize = 5;
-
-fn main() {
- // assigning to various global constants
- foo = 6; //~ ERROR cannot assign to immutable static item
-}
+++ /dev/null
-// 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.
-
-// Tests that auto-ref can't create mutable aliases to immutable memory.
-
-struct Foo {
- x: isize
-}
-
-impl Foo {
- pub fn printme(&mut self) {
- println!("{}", self.x);
- }
-}
-
-fn main() {
- let x = Foo { x: 3 };
- x.printme(); //~ ERROR cannot borrow
-}
+++ /dev/null
-// 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.
-
-enum Either<T, U> { Left(T), Right(U) }
-
-struct X(Either<(usize,usize), fn()>);
-
-impl X {
- pub fn with<F>(&self, blk: F) where F: FnOnce(&Either<(usize, usize), fn()>) {
- let X(ref e) = *self;
- blk(e)
- }
-}
-
-fn main() {
- let mut x = X(Either::Right(main as fn()));
- (&mut x).with(
- |opt| { //~ ERROR cannot borrow `x` as mutable more than once at a time
- match opt {
- &Either::Right(ref f) => {
- x = X(Either::Left((0, 0)));
- (*f)()
- },
- _ => panic!()
- }
- })
-}
+++ /dev/null
-// 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.
-
-// Test that we detect nested calls that could free pointers evaluated
-// for earlier arguments.
-
-#![feature(box_syntax)]
-
-fn rewrite(v: &mut Box<usize>) -> usize {
- *v = box 22;
- **v
-}
-
-fn add(v: &usize, w: usize) -> usize {
- *v + w
-}
-
-fn implicit() {
- let mut a: Box<_> = box 1;
-
- // Note the danger here:
- //
- // the pointer for the first argument has already been
- // evaluated, but it gets freed when evaluating the second
- // argument!
- add(
- &*a,
- rewrite(&mut a)); //~ ERROR cannot borrow
-}
-
-fn explicit() {
- let mut a: Box<_> = box 1;
- add(
- &*a,
- rewrite(&mut a)); //~ ERROR cannot borrow
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-// Test that we detect nested calls that could free pointers evaluated
-// for earlier arguments.
-
-#![feature(box_syntax)]
-
-fn rewrite(v: &mut Box<usize>) -> usize {
- *v = box 22;
- **v
-}
-
-fn add(v: &usize, w: Box<usize>) -> usize {
- *v + *w
-}
-
-fn implicit() {
- let mut a: Box<_> = box 1;
-
- // Note the danger here:
- //
- // the pointer for the first argument has already been
- // evaluated, but it gets moved when evaluating the second
- // argument!
- add(
- &*a,
- a); //~ ERROR cannot move
-}
-
-fn explicit() {
- let mut a: Box<_> = box 1;
- add(
- &*a,
- a); //~ ERROR cannot move
-}
-
-fn main() {}
+++ /dev/null
-// 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 force<F>(f: F) where F: FnOnce() { f(); }
-fn main() {
- let x: isize;
- force(|| { //~ ERROR capture of possibly uninitialized variable: `x`
- println!("{}", x);
- });
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-
-#[derive(Copy, Clone)]
-struct Foo {
- bar1: Bar,
- bar2: Bar
-}
-
-#[derive(Copy, Clone)]
-struct Bar {
- int1: isize,
- int2: isize,
-}
-
-fn make_foo() -> Box<Foo> { panic!() }
-
-fn borrow_same_field_twice_mut_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_mut_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _bar2 = &foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_imm_mut() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_imm_imm() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1;
- let _bar2 = &foo.bar1;
- *bar1;
-}
-
-fn borrow_both_fields_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- 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
- }
-}
-
-fn borrow_var_and_pattern() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- match *foo {
- Foo { bar1: ref mut _bar1, bar2: _ } => {}
- //~^ ERROR cannot borrow
- }
- *bar1;
-}
-
-fn borrow_mut_and_base_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo1 = &foo.bar1; //~ ERROR cannot borrow
- let _foo2 = &*foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_mut_and_base_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_mut_and_base_mut2() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo2 = &mut *foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_mut() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_mut2() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo2 = &mut *foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_imm() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo1 = &foo.bar1;
- let _foo2 = &*foo;
- *bar1;
-}
-
-fn borrow_mut_and_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _foo1 = &foo.bar2; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_mut_from_imm() {
- let foo = make_foo();
- let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_long_path_both_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let foo1 = &mut foo.bar2.int2; //~ ERROR cannot borrow
- *bar1;
- *foo1;
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-#[derive(Copy, Clone)]
-struct Foo {
- bar1: Bar,
- bar2: Bar
-}
-
-#[derive(Copy, Clone)]
-struct Bar {
- int1: isize,
- int2: isize,
-}
-
-fn make_foo() -> Foo { panic!() }
-
-fn borrow_same_field_twice_mut_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_mut_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _bar2 = &foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_imm_mut() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_same_field_twice_imm_imm() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1;
- let _bar2 = &foo.bar1;
- *bar1;
-}
-
-fn borrow_both_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _bar2 = &mut foo.bar2;
- *bar1;
-}
-
-fn borrow_both_mut_pattern() {
- let mut foo = make_foo();
- match foo {
- Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
- }
-}
-
-fn borrow_var_and_pattern() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- match foo {
- Foo { bar1: ref mut _bar1, bar2: _ } => {} //
- //~^ ERROR cannot borrow
- }
- *bar1;
-}
-
-fn borrow_mut_and_base_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo1 = &foo.bar1; //~ ERROR cannot borrow
- let _foo2 = &foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_mut_and_base_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_mut_and_base_mut2() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo2 = &mut foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_mut() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_mut2() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo2 = &mut foo; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_imm_and_base_imm() {
- let mut foo = make_foo();
- let bar1 = &foo.bar1.int1;
- let _foo1 = &foo.bar1;
- let _foo2 = &foo;
- *bar1;
-}
-
-fn borrow_mut_and_imm() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1;
- let _foo1 = &foo.bar2;
- *bar1;
-}
-
-fn borrow_mut_from_imm() {
- let foo = make_foo();
- let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
- *bar1;
-}
-
-fn borrow_long_path_both_mut() {
- let mut foo = make_foo();
- let bar1 = &mut foo.bar1.int1;
- let _foo1 = &mut foo.bar2.int2;
- *bar1;
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test lifetimes are linked properly when we take reference
-// to interior.
-
-struct Foo(isize);
-
-fn foo<'a>() -> &'a isize {
- let &Foo(ref x) = &Foo(3); //~ ERROR borrowed value does not live long enough
- x
-}
-
-pub fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-struct A;
-
-impl A {
- fn foo(&mut self) {
- }
-}
-
-pub fn main() {
- let a: Box<_> = box A;
- a.foo();
- //~^ ERROR cannot borrow immutable `Box` content `*a` as mutable
-}
+++ /dev/null
-// 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.
-
-// Test that attempt to reborrow an `&mut` pointer in an aliasable
-// location yields an error.
-//
-// Example from src/librustc_borrowck/borrowck/README.md
-
-fn foo(t0: & &mut isize) {
- let t1 = t0;
- let p: &isize = &**t0;
- **t1 = 22; //~ ERROR cannot assign
-}
-
-fn foo3(t0: &mut &mut isize) {
- let t1 = &mut *t0;
- let p: &isize = &**t0; //~ ERROR cannot borrow
- **t1 = 22;
-}
-
-fn foo4(t0: & &mut isize) {
- let x: &mut isize = &mut **t0; //~ ERROR cannot borrow
- *x += 1;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-// Check that `&mut` objects cannot be borrowed twice, just like
-// other `&mut` pointers.
-
-trait Foo {
- fn f1(&mut self) -> &();
- fn f2(&mut self);
-}
-
-fn test(x: &mut Foo) {
- let _y = x.f1();
- x.f2(); //~ ERROR cannot borrow `*x` as mutable
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test how overloaded deref interacts with borrows when DerefMut
-// is implemented.
-
-use std::ops::{Deref, DerefMut};
-
-struct Own<T> {
- value: *mut T
-}
-
-impl<T> Deref for Own<T> {
- type Target = T;
-
- fn deref(&self) -> &T {
- unsafe { &*self.value }
- }
-}
-
-impl<T> DerefMut for Own<T> {
- fn deref_mut(&mut self) -> &mut T {
- unsafe { &mut *self.value }
- }
-}
-
-struct Point {
- x: isize,
- y: isize
-}
-
-impl Point {
- fn get(&self) -> (isize, isize) {
- (self.x, self.y)
- }
-
- fn set(&mut self, x: isize, y: isize) {
- self.x = x;
- self.y = y;
- }
-
- fn x_ref(&self) -> &isize {
- &self.x
- }
-
- fn y_mut(&mut self) -> &mut isize {
- &mut self.y
- }
-}
-
-fn deref_imm_field(x: Own<Point>) {
- let __isize = &x.y;
-}
-
-fn deref_mut_field1(x: Own<Point>) {
- let __isize = &mut x.y; //~ ERROR cannot borrow
-}
-
-fn deref_mut_field2(mut x: Own<Point>) {
- let __isize = &mut x.y;
-}
-
-fn deref_extend_field(x: &Own<Point>) -> &isize {
- &x.y
-}
-
-fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
- &mut x.y //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
- &mut x.y
-}
-
-fn deref_extend_mut_field3(x: &mut Own<Point>) {
- // Hmm, this is unfortunate, because with box it would work,
- // but it's presently the expected outcome. See `deref_extend_mut_field4`
- // for the workaround.
-
- let _x = &mut x.x;
- let _y = &mut x.y; //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
- let p = &mut **x;
- let _x = &mut p.x;
- let _y = &mut p.y;
-}
-
-fn assign_field1<'a>(x: Own<Point>) {
- x.y = 3; //~ ERROR cannot borrow
-}
-
-fn assign_field2<'a>(x: &'a Own<Point>) {
- x.y = 3; //~ ERROR cannot assign
-}
-
-fn assign_field3<'a>(x: &'a mut Own<Point>) {
- x.y = 3;
-}
-
-fn assign_field4<'a>(x: &'a mut Own<Point>) {
- let _p: &mut Point = &mut **x;
- x.y = 3; //~ ERROR cannot borrow
-}
-
-fn deref_imm_method(x: Own<Point>) {
- let __isize = x.get();
-}
-
-fn deref_mut_method1(x: Own<Point>) {
- x.set(0, 0); //~ ERROR cannot borrow
-}
-
-fn deref_mut_method2(mut x: Own<Point>) {
- x.set(0, 0);
-}
-
-fn deref_extend_method(x: &Own<Point>) -> &isize {
- x.x_ref()
-}
-
-fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
- x.y_mut() //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
- x.y_mut()
-}
-
-fn assign_method1<'a>(x: Own<Point>) {
- *x.y_mut() = 3; //~ ERROR cannot borrow
-}
-
-fn assign_method2<'a>(x: &'a Own<Point>) {
- *x.y_mut() = 3; //~ ERROR cannot borrow
-}
-
-fn assign_method3<'a>(x: &'a mut Own<Point>) {
- *x.y_mut() = 3;
-}
-
-pub fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test how overloaded deref interacts with borrows when only
-// Deref and not DerefMut is implemented.
-
-use std::ops::Deref;
-
-struct Rc<T> {
- value: *const T
-}
-
-impl<T> Deref for Rc<T> {
- type Target = T;
-
- fn deref(&self) -> &T {
- unsafe { &*self.value }
- }
-}
-
-struct Point {
- x: isize,
- y: isize
-}
-
-impl Point {
- fn get(&self) -> (isize, isize) {
- (self.x, self.y)
- }
-
- fn set(&mut self, x: isize, y: isize) {
- self.x = x;
- self.y = y;
- }
-
- fn x_ref(&self) -> &isize {
- &self.x
- }
-
- fn y_mut(&mut self) -> &mut isize {
- &mut self.y
- }
-}
-
-fn deref_imm_field(x: Rc<Point>) {
- let __isize = &x.y;
-}
-
-fn deref_mut_field1(x: Rc<Point>) {
- let __isize = &mut x.y; //~ ERROR cannot borrow
-}
-
-fn deref_mut_field2(mut x: Rc<Point>) {
- let __isize = &mut x.y; //~ ERROR cannot borrow
-}
-
-fn deref_extend_field(x: &Rc<Point>) -> &isize {
- &x.y
-}
-
-fn deref_extend_mut_field1(x: &Rc<Point>) -> &mut isize {
- &mut x.y //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut_field2(x: &mut Rc<Point>) -> &mut isize {
- &mut x.y //~ ERROR cannot borrow
-}
-
-fn assign_field1<'a>(x: Rc<Point>) {
- x.y = 3; //~ ERROR cannot assign
-}
-
-fn assign_field2<'a>(x: &'a Rc<Point>) {
- x.y = 3; //~ ERROR cannot assign
-}
-
-fn assign_field3<'a>(x: &'a mut Rc<Point>) {
- x.y = 3; //~ ERROR cannot assign
-}
-
-fn deref_imm_method(x: Rc<Point>) {
- let __isize = x.get();
-}
-
-fn deref_mut_method1(x: Rc<Point>) {
- x.set(0, 0); //~ ERROR cannot borrow
-}
-
-fn deref_mut_method2(mut x: Rc<Point>) {
- x.set(0, 0); //~ ERROR cannot borrow
-}
-
-fn deref_extend_method(x: &Rc<Point>) -> &isize {
- x.x_ref()
-}
-
-fn deref_extend_mut_method1(x: &Rc<Point>) -> &mut isize {
- x.y_mut() //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut_method2(x: &mut Rc<Point>) -> &mut isize {
- x.y_mut() //~ ERROR cannot borrow
-}
-
-fn assign_method1<'a>(x: Rc<Point>) {
- *x.y_mut() = 3; //~ ERROR cannot borrow
-}
-
-fn assign_method2<'a>(x: &'a Rc<Point>) {
- *x.y_mut() = 3; //~ ERROR cannot borrow
-}
-
-fn assign_method3<'a>(x: &'a mut Rc<Point>) {
- *x.y_mut() = 3; //~ ERROR cannot borrow
-}
-
-pub fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test how overloaded deref interacts with borrows when DerefMut
-// is implemented.
-
-use std::ops::{Deref, DerefMut};
-
-struct Own<T> {
- value: *mut T
-}
-
-impl<T> Deref for Own<T> {
- type Target = T;
-
- fn deref<'a>(&'a self) -> &'a T {
- unsafe { &*self.value }
- }
-}
-
-impl<T> DerefMut for Own<T> {
- fn deref_mut<'a>(&'a mut self) -> &'a mut T {
- unsafe { &mut *self.value }
- }
-}
-
-fn deref_imm(x: Own<isize>) {
- let __isize = &*x;
-}
-
-fn deref_mut1(x: Own<isize>) {
- let __isize = &mut *x; //~ ERROR cannot borrow
-}
-
-fn deref_mut2(mut x: Own<isize>) {
- let __isize = &mut *x;
-}
-
-fn deref_extend<'a>(x: &'a Own<isize>) -> &'a isize {
- &**x
-}
-
-fn deref_extend_mut1<'a>(x: &'a Own<isize>) -> &'a mut isize {
- &mut **x //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut2<'a>(x: &'a mut Own<isize>) -> &'a mut isize {
- &mut **x
-}
-
-fn assign1<'a>(x: Own<isize>) {
- *x = 3; //~ ERROR cannot borrow
-}
-
-fn assign2<'a>(x: &'a Own<isize>) {
- **x = 3; //~ ERROR cannot borrow
-}
-
-fn assign3<'a>(x: &'a mut Own<isize>) {
- **x = 3;
-}
-
-pub fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test how overloaded deref interacts with borrows when only
-// Deref and not DerefMut is implemented.
-
-use std::ops::Deref;
-
-struct Rc<T> {
- value: *const T
-}
-
-impl<T> Deref for Rc<T> {
- type Target = T;
-
- fn deref<'a>(&'a self) -> &'a T {
- unsafe { &*self.value }
- }
-}
-
-fn deref_imm(x: Rc<isize>) {
- let __isize = &*x;
-}
-
-fn deref_mut1(x: Rc<isize>) {
- let __isize = &mut *x; //~ ERROR cannot borrow
-}
-
-fn deref_mut2(mut x: Rc<isize>) {
- let __isize = &mut *x; //~ ERROR cannot borrow
-}
-
-fn deref_extend<'a>(x: &'a Rc<isize>) -> &'a isize {
- &**x
-}
-
-fn deref_extend_mut1<'a>(x: &'a Rc<isize>) -> &'a mut isize {
- &mut **x //~ ERROR cannot borrow
-}
-
-fn deref_extend_mut2<'a>(x: &'a mut Rc<isize>) -> &'a mut isize {
- &mut **x //~ ERROR cannot borrow
-}
-
-fn assign1<'a>(x: Rc<isize>) {
- *x = 3; //~ ERROR cannot assign
-}
-
-fn assign2<'a>(x: &'a Rc<isize>) {
- **x = 3; //~ ERROR cannot assign
-}
-
-fn assign3<'a>(x: &'a mut Rc<isize>) {
- **x = 3; //~ ERROR cannot assign
-}
-
-pub fn main() {}
+++ /dev/null
-// 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.
-
-struct defer<'a> {
- x: &'a [&'a str],
-}
-
-impl<'a> Drop for defer<'a> {
- fn drop(&mut self) {
- unsafe {
- println!("{:?}", self.x);
- }
- }
-}
-
-fn defer<'r>(x: &'r [&'r str]) -> defer<'r> {
- defer {
- x: x
- }
-}
-
-fn main() {
- let x = defer(&vec!("Goodbye", "world!"));
- //~^ ERROR borrowed value does not live long enough
- x.x[0];
-}
+++ /dev/null
-// 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.
-
-//buggy.rs
-
-#![feature(box_syntax)]
-
-extern crate collections;
-use std::collections::HashMap;
-
-fn main() {
- let tmp: Box<_>;
- let mut buggy_map: HashMap<usize, &usize> = HashMap::new();
- // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
- buggy_map.insert(42, &*Box::new(1)); //~ ERROR borrowed value does not live long enough
-
- // but it is ok if we use a temporary
- tmp = box 2;
- buggy_map.insert(43, &*tmp);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-struct A {
- x: Box<isize>,
- y: isize,
-}
-
-struct B {
- x: Box<isize>,
- y: Box<isize>,
-}
-
-struct C {
- x: Box<A>,
- y: isize,
-}
-
-struct D {
- x: Box<A>,
- y: Box<isize>,
-}
-
-fn copy_after_move() {
- let a: Box<_> = box A { x: box 0, y: 1 };
- let _x = a.x;
- let _y = a.y; //~ ERROR use of moved
- //~^^ NOTE `a` moved here (through moving `a.x`)
-}
-
-fn move_after_move() {
- let a: Box<_> = box B { x: box 0, y: box 1 };
- let _x = a.x;
- let _y = a.y; //~ ERROR use of moved
- //~^^ NOTE `a` moved here (through moving `a.x`)
-}
-
-fn borrow_after_move() {
- let a: Box<_> = box A { x: box 0, y: 1 };
- let _x = a.x;
- let _y = &a.y; //~ ERROR use of moved
- //~^^ NOTE `a` moved here (through moving `a.x`)
-}
-
-fn move_after_borrow() {
- let a: Box<_> = box B { x: box 0, y: box 1 };
- let _x = &a.x;
- let _y = a.y; //~ ERROR cannot move
-}
-
-fn copy_after_mut_borrow() {
- let mut a: Box<_> = box A { x: box 0, y: 1 };
- let _x = &mut a.x;
- let _y = a.y; //~ ERROR cannot use
-}
-
-fn move_after_mut_borrow() {
- let mut a: Box<_> = box B { x: box 0, y: box 1 };
- let _x = &mut a.x;
- let _y = a.y; //~ ERROR cannot move
-}
-
-fn borrow_after_mut_borrow() {
- let mut a: Box<_> = box A { x: box 0, y: 1 };
- let _x = &mut a.x;
- let _y = &a.y; //~ ERROR cannot borrow
-}
-
-fn mut_borrow_after_borrow() {
- let mut a: Box<_> = box A { x: box 0, y: 1 };
- let _x = &a.x;
- let _y = &mut a.y; //~ ERROR cannot borrow
-}
-
-fn copy_after_move_nested() {
- let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
- let _x = a.x.x;
- let _y = a.y; //~ ERROR use of collaterally moved
-}
-
-fn move_after_move_nested() {
- let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
- let _x = a.x.x;
- let _y = a.y; //~ ERROR use of collaterally moved
-}
-
-fn borrow_after_move_nested() {
- let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
- let _x = a.x.x;
- let _y = &a.y; //~ ERROR use of collaterally moved
-}
-
-fn move_after_borrow_nested() {
- let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
- let _x = &a.x.x;
- let _y = a.y; //~ ERROR cannot move
-}
-
-fn copy_after_mut_borrow_nested() {
- let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
- let _x = &mut a.x.x;
- let _y = a.y; //~ ERROR cannot use
-}
-
-fn move_after_mut_borrow_nested() {
- let mut a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
- let _x = &mut a.x.x;
- let _y = a.y; //~ ERROR cannot move
-}
-
-fn borrow_after_mut_borrow_nested() {
- let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
- let _x = &mut a.x.x;
- let _y = &a.y; //~ ERROR cannot borrow
-}
-
-fn mut_borrow_after_borrow_nested() {
- let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
- let _x = &a.x.x;
- let _y = &mut a.y; //~ ERROR cannot borrow
-}
-
-fn main() {
- copy_after_move();
- move_after_move();
- borrow_after_move();
-
- move_after_borrow();
-
- copy_after_mut_borrow();
- move_after_mut_borrow();
- borrow_after_mut_borrow();
- mut_borrow_after_borrow();
-
- copy_after_move_nested();
- move_after_move_nested();
- borrow_after_move_nested();
-
- move_after_borrow_nested();
-
- copy_after_mut_borrow_nested();
- move_after_mut_borrow_nested();
- borrow_after_mut_borrow_nested();
- mut_borrow_after_borrow_nested();
-}
+++ /dev/null
-// 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 foo() -> isize {
- let x: isize;
-
- while 1 != 2 {
- break;
- x = 0;
- }
-
- println!("{}", x); //~ ERROR use of possibly uninitialized variable: `x`
-
- return 17;
-}
-
-fn main() { println!("{}", foo()); }
+++ /dev/null
-// 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 foo() -> isize {
- let x: isize;
-
- loop {
- break;
- x = 0;
- }
-
- println!("{}", x); //~ ERROR use of possibly uninitialized variable: `x`
-
- return 17;
-}
-
-fn main() { println!("{}", foo()); }
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Ensure that invoking a closure counts as a unique immutable borrow
-
-#![feature(unboxed_closures)]
-
-type Fn<'a> = Box<FnMut() + 'a>;
-
-struct Test<'a> {
- f: Box<FnMut() + 'a>
-}
-
-// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
-fn call<F>(mut f: F) where F: FnMut(Fn) {
- f(Box::new(|| {
- //~^ ERROR: cannot borrow `f` as mutable more than once
- f((Box::new(|| {})))
- }));
-}
-
-fn test1() {
- call(|mut a| {
- a.call_mut(());
- });
-}
-
-fn test2<F>(f: &F) where F: FnMut() {
- (*f)(); //~ ERROR: cannot borrow immutable borrowed content `*f` as mutable
-}
-
-fn test3<F>(f: &mut F) where F: FnMut() {
- (*f)();
-}
-
-fn test4(f: &Test) {
- f.f.call_mut(()) //~ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
-}
-
-fn test5(f: &mut Test) {
- f.f.call_mut(())
-}
-
-fn test6() {
- let mut f = || {};
- (|| {
- f();
- })();
-}
-
-fn test7() {
- fn foo<F>(_: F) where F: FnMut(Box<FnMut(isize)>, isize) {}
- let mut f = |g: Box<FnMut(isize)>, b: isize| {};
- // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
- f(Box::new(|a| {
- foo(f);
- //~^ ERROR cannot move `f` into closure because it is borrowed
- //~| ERROR cannot move out of captured outer variable in an `FnMut` closure
- }), 3);
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-struct Foo {
- x: isize,
-}
-
-impl Foo {
- pub fn f(&self) {}
- pub fn h(&mut self) {}
-}
-
-fn a(x: &mut Foo) {
- x.f();
- x.h();
-}
-
-fn b(x: &Foo) {
- x.f();
- x.h(); //~ ERROR cannot borrow
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that two closures cannot simultaneously have mutable
-// and immutable access to the variable. Issue #6801.
-
-#![feature(box_syntax)]
-
-fn get(x: &isize) -> isize {
- *x
-}
-
-fn set(x: &mut isize) {
- *x = 4;
-}
-
-fn a() {
- let mut x = 3;
- let c1 = || x = 4;
- let c2 = || x * 5; //~ ERROR cannot borrow `x`
-}
-
-fn b() {
- let mut x = 3;
- let c1 = || set(&mut x);
- let c2 = || get(&x); //~ ERROR cannot borrow `x`
-}
-
-fn c() {
- let mut x = 3;
- let c1 = || set(&mut x);
- let c2 = || x * 5; //~ ERROR cannot borrow `x`
-}
-
-fn d() {
- let mut x = 3;
- let c2 = || x * 5;
- x = 5; //~ ERROR cannot assign
-}
-
-fn e() {
- let mut x = 3;
- let c1 = || get(&x);
- x = 5; //~ ERROR cannot assign
-}
-
-fn f() {
- let mut x: Box<_> = box 3;
- let c1 = || get(&*x);
- *x = 5; //~ ERROR cannot assign
-}
-
-fn g() {
- struct Foo {
- f: Box<isize>
- }
-
- let mut x: Box<_> = box Foo { f: box 3 };
- let c1 = || get(&*x.f);
- *x.f = 5; //~ ERROR cannot assign to `*x.f`
-}
-
-fn h() {
- struct Foo {
- f: Box<isize>
- }
-
- let mut x: Box<_> = box Foo { f: box 3 };
- let c1 = || get(&*x.f);
- let c2 = || *x.f = 5; //~ ERROR cannot borrow `x` as mutable
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that two closures cannot simultaneously have mutable
-// and immutable access to the variable. Issue #6801.
-
-fn get(x: &isize) -> isize {
- *x
-}
-
-fn set(x: &mut isize) {
- *x = 4;
-}
-
-fn a(x: &isize) {
- let c1 = || set(&mut *x);
- //~^ ERROR cannot borrow
- let c2 = || set(&mut *x);
- //~^ ERROR cannot borrow
- //~| ERROR closure requires unique access
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that two closures cannot simultaneously have mutable
-// access to the variable, whether that mutable access be used
-// for direct assignment or for taking mutable ref. Issue #6801.
-
-#![feature(box_syntax)]
-
-fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
-
-fn a() {
- let mut x = 3;
- let c1 = to_fn_mut(|| x = 4);
- let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
-}
-
-fn set(x: &mut isize) {
- *x = 4;
-}
-
-fn b() {
- let mut x = 3;
- let c1 = to_fn_mut(|| set(&mut x));
- let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
-}
-
-fn c() {
- let mut x = 3;
- let c1 = to_fn_mut(|| x = 5);
- let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
-}
-
-fn d() {
- let mut x = 3;
- let c1 = to_fn_mut(|| x = 5);
- let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
- //~^ ERROR cannot borrow `x` as mutable more than once
-}
-
-fn g() {
- struct Foo {
- f: Box<isize>
- }
-
- let mut x: Box<_> = box Foo { f: box 3 };
- let c1 = to_fn_mut(|| set(&mut *x.f));
- let c2 = to_fn_mut(|| set(&mut *x.f));
- //~^ ERROR cannot borrow `x` as mutable more than once
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-struct Foo {
- x: isize,
-}
-
-pub fn main() {
- let mut this = &mut Foo {
- x: 1,
- };
- let mut r = || {
- let p = &this.x;
- &mut this.x; //~ ERROR cannot borrow
- };
- r()
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that a closure which requires mutable access to the referent
-// of an `&mut` requires a "unique" borrow -- that is, the variable to
-// be borrowed (here, `x`) will not be borrowed *mutably*, but
-// may be *immutable*, but we cannot allow
-// multiple borrows.
-
-fn get(x: &isize) -> isize {
- *x
-}
-
-fn set(x: &mut isize) -> isize {
- *x
-}
-
-fn a(x: &mut isize) {
- let c1 = || get(x);
- let c2 = || get(x);
-}
-
-fn b(x: &mut isize) {
- let c1 = || get(x);
- let c2 = || set(x); //~ ERROR closure requires unique access to `x`
-}
-
-fn c(x: &mut isize) {
- let c1 = || get(x);
- let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
-}
-
-fn d(x: &mut isize) {
- let c1 = || set(x);
- let c2 = || set(x); //~ ERROR closure requires unique access to `x`
-}
-
-fn e(x: &mut isize) {
- let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that a closure which mutates a local variable
-// cannot also be supplied a borrowed version of that
-// variable's contents. Issue #11192.
-
-#![feature(box_syntax)]
-
-struct Foo {
- x: isize
-}
-
-impl Drop for Foo {
- fn drop(&mut self) {
- println!("drop {}", self.x);
- }
-}
-
-fn main() {
- let mut ptr: Box<_> = box Foo { x: 0 };
- let mut test = |foo: &Foo| {
- ptr = box Foo { x: ptr.x + 1 };
- };
- test(&*ptr); //~ ERROR cannot borrow `*ptr`
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Check that we report an error if an upcast box is moved twice.
-
-fn consume(_: Box<[i32]>) {
-}
-
-fn foo(b: Box<[i32;5]>) {
- consume(b);
- consume(b); //~ ERROR use of moved value
-}
-
-fn main() {
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Check that we report an error if an upcast box is moved twice.
-
-trait Foo { fn dummy(&self); }
-
-fn consume(_: Box<Foo>) {
-}
-
-fn foo(b: Box<Foo+Send>) {
- consume(b);
- consume(b); //~ ERROR use of moved value
-}
-
-fn main() {
-}
+++ /dev/null
-// Copyright 2015 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.
-
-use std::thread::spawn;
-
-// Test that we give a custom error (E0373) for the case where a
-// closure is escaping current frame, and offer a suggested code edit.
-// I refrained from including the precise message here, but the
-// original text as of the time of this writing is:
-//
-// closure may outlive the current function, but it borrows `books`,
-// which is owned by the current function
-
-fn main() {
- let mut books = vec![1,2,3];
- spawn(|| books.push(4));
- //~^ ERROR E0373
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Test that we give a custom error (E0373) for the case where a
-// closure is escaping current frame, and offer a suggested code edit.
-// I refrained from including the precise message here, but the
-// original text as of the time of this writing is:
-//
-// closure may outlive the current function, but it borrows `books`,
-// which is owned by the current function
-
-fn foo<'a>(x: &'a i32) -> Box<FnMut()+'a> {
- let mut books = vec![1,2,3];
- Box::new(|| books.push(4))
- //~^ ERROR E0373
-}
-
-fn main() { }
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-struct A { a: isize, b: Box<isize> }
-
-fn deref_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- drop(*x.b); //~ ERROR use of moved value: `*x.b`
-}
-
-fn deref_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let y = A { a: 3, .. x };
- drop(*x.b); //~ ERROR use of moved value: `*x.b`
-}
-
-fn borrow_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- let p = &x.b; //~ ERROR use of moved value: `x.b`
- drop(**p);
-}
-
-fn borrow_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- let p = &x.b; //~ ERROR use of moved value: `x.b`
- drop(**p);
-}
-
-fn move_after_borrow() {
- let x = A { a: 1, b: box 2 };
- let p = &x.b;
- drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
- drop(**p);
-}
-
-fn fu_move_after_borrow() {
- let x = A { a: 1, b: box 2 };
- let p = &x.b;
- let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
- drop(**p);
-}
-
-fn mut_borrow_after_mut_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.a;
- let q = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
- drop(*p);
- drop(*q);
-}
-
-fn move_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- drop(x.b); //~ ERROR use of moved value: `x.b`
-}
-
-fn move_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- drop(x.b); //~ ERROR use of moved value: `x.b`
-}
-
-fn fu_move_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- let _z = A { a: 3, .. x }; //~ ERROR use of moved value: `x.b`
-}
-
-fn fu_move_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- let _z = A { a: 4, .. x }; //~ ERROR use of moved value: `x.b`
-}
-
-// The following functions aren't yet accepted, but they should be.
-
-fn copy_after_field_assign_after_uninit() {
- let mut x: A;
- x.a = 1;
- drop(x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
-}
-
-fn borrow_after_field_assign_after_uninit() {
- let mut x: A;
- x.a = 1;
- let p = &x.a; //~ ERROR use of possibly uninitialized variable: `x.a`
- drop(*p);
-}
-
-fn move_after_field_assign_after_uninit() {
- let mut x: A;
- x.b = box 1;
- drop(x.b); //~ ERROR use of possibly uninitialized variable: `x.b`
-}
-
-fn main() {
- deref_after_move();
- deref_after_fu_move();
-
- borrow_after_move();
- borrow_after_fu_move();
- move_after_borrow();
- fu_move_after_borrow();
- mut_borrow_after_mut_borrow();
-
- move_after_move();
- move_after_fu_move();
- fu_move_after_move();
- fu_move_after_fu_move();
-
- copy_after_field_assign_after_uninit();
- borrow_after_field_assign_after_uninit();
- move_after_field_assign_after_uninit();
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Check that we check fns appearing in constant declarations.
-// Issue #22382.
-
-const MOVE: fn(&String) -> String = {
- fn broken(x: &String) -> String {
- return *x //~ ERROR cannot move
- }
- broken
-};
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Check that we check fns appearing in constant declarations.
-// Issue #22382.
-
-// How about mutating an immutable vector?
-const MUTATE: fn(&Vec<String>) = {
- fn broken(x: &Vec<String>) {
- x.push(format!("this is broken"));
- //~^ ERROR cannot borrow
- }
- broken
-};
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Check that we check fns appearing in constant declarations.
-// Issue #22382.
-
-// Returning local references?
-struct DropString {
- inner: String
-}
-impl Drop for DropString {
- fn drop(&mut self) {
- self.inner.clear();
- self.inner.push_str("dropped");
- }
-}
-const LOCAL_REF: fn() -> &'static str = {
- fn broken() -> &'static str {
- let local = DropString { inner: format!("Some local string") };
- return &local.inner; //~ ERROR does not live long enough
- }
- broken
-};
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Issue #16205.
-
-#![feature(box_syntax)]
-
-struct Foo {
- a: [Box<isize>; 3],
-}
-
-fn main() {
- let mut y = 1;
- let x = Some(&mut y);
- for &a in x.iter() { //~ ERROR cannot move out
- }
-
- let f = Foo {
- a: [box 3, box 4, box 5],
- };
- for &a in &f.a { //~ ERROR cannot move out
- }
-
- let x: Option<Box<_>> = Some(box 1);
- for &a in x.iter() { //~ ERROR cannot move out
- }
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-use std::iter::repeat;
-
-fn main() {
- let mut vector = vec![1, 2];
- for &x in &vector {
- let cap = vector.capacity();
- vector.extend(repeat(0)); //~ ERROR cannot borrow
- vector[1] = 5; //~ ERROR cannot borrow
- }
-}
+++ /dev/null
-// 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 foo(x: isize) { println!("{}", x); }
-
-fn main() {
- let x: isize; if 1 > 2 { x = 10; }
- foo(x); //~ ERROR use of possibly uninitialized variable: `x`
-}
+++ /dev/null
-// 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 foo(x: isize) { println!("{}", x); }
-
-fn main() {
- let x: isize;
- if 1 > 2 {
- println!("whoops");
- } else {
- x = 10;
- }
- foo(x); //~ ERROR use of possibly uninitialized variable: `x`
-}
+++ /dev/null
-// 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 main() {
- let mut _a = 3;
- let _b = &mut _a;
- {
- let _c = &*_b;
- _a = 4; //~ ERROR cannot assign to `_a`
- }
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// check that borrowck looks inside consts/statics
-
-static FN : &'static (Fn() -> (Box<Fn()->Box<i32>>) + Sync) = &|| {
- let x = Box::new(0);
- Box::new(|| x) //~ ERROR cannot move out of captured outer variable
-};
-
-fn main() {
- let f = (FN)();
- f();
- f();
-}
+++ /dev/null
-// 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 main() {
- let j = || -> isize {
- let i: isize;
- i //~ ERROR use of possibly uninitialized variable: `i`
- };
- j();
-}
+++ /dev/null
-// 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 main() {
- let f = || -> isize {
- let i: isize;
- i //~ ERROR use of possibly uninitialized variable: `i`
- };
- println!("{}", f());
-}
+++ /dev/null
-// 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.
-
-#[derive(Clone)]
-struct point {
- x: isize,
- y: isize,
-}
-
-fn main() {
- let mut origin: point;
- origin = point {x: 10,.. origin}; //~ ERROR use of possibly uninitialized variable: `origin.y`
- origin.clone();
-}
+++ /dev/null
-// 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 test() {
- let v: isize;
- v += 1; //~ ERROR use of possibly uninitialized variable: `v`
- v.clone();
-}
-
-fn main() {
-}
+++ /dev/null
-// 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 test() {
- let mut v: isize;
- v = v + 1; //~ ERROR use of possibly uninitialized variable: `v`
- v.clone();
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-extern crate collections;
-use std::collections::HashSet;
-
-struct Foo {
- n: HashSet<isize>,
-}
-
-impl Foo {
- pub fn foo<F>(&mut self, mut fun: F) where F: FnMut(&isize) {
- for f in &self.n {
- fun(f);
- }
- }
-}
-
-fn bar(f: &mut Foo) {
- f.foo(
- |a| { //~ ERROR closure requires unique access to `f`
- f.n.insert(*a);
- })
-}
-
-fn main() {
- let mut f = Foo { n: HashSet::new() };
- bar(&mut f);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// This tests that we can't modify Box<&mut T> contents while they
-// are borrowed (#14498).
-//
-// Also includes tests of the errors reported when the Box in question
-// is immutable (#14270).
-
-#![feature(box_syntax)]
-
-struct A { a: isize }
-struct B<'a> { a: Box<&'a mut isize> }
-
-fn indirect_write_to_imm_box() {
- let mut x: isize = 1;
- let y: Box<_> = box &mut x;
- let p = &y;
- ***p = 2; //~ ERROR cannot assign to data in an immutable container
- drop(p);
-}
-
-fn borrow_in_var_from_var() {
- let mut x: isize = 1;
- let mut y: Box<_> = box &mut x;
- let p = &y;
- let q = &***p;
- **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
- drop(p);
- drop(q);
-}
-
-fn borrow_in_var_from_var_via_imm_box() {
- let mut x: isize = 1;
- let y: Box<_> = box &mut x;
- let p = &y;
- let q = &***p;
- **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
- //~^ ERROR cannot assign to data in an immutable container
- drop(p);
- drop(q);
-}
-
-fn borrow_in_var_from_field() {
- let mut x = A { a: 1 };
- let mut y: Box<_> = box &mut x.a;
- let p = &y;
- let q = &***p;
- **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
- drop(p);
- drop(q);
-}
-
-fn borrow_in_var_from_field_via_imm_box() {
- let mut x = A { a: 1 };
- let y: Box<_> = box &mut x.a;
- let p = &y;
- let q = &***p;
- **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
- //~^ ERROR cannot assign to data in an immutable container
- drop(p);
- drop(q);
-}
-
-fn borrow_in_field_from_var() {
- let mut x: isize = 1;
- let mut y = B { a: box &mut x };
- let p = &y.a;
- let q = &***p;
- **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
- drop(p);
- drop(q);
-}
-
-fn borrow_in_field_from_var_via_imm_box() {
- let mut x: isize = 1;
- let y = B { a: box &mut x };
- let p = &y.a;
- let q = &***p;
- **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
- //~^ ERROR cannot assign to data in an immutable container
- drop(p);
- drop(q);
-}
-
-fn borrow_in_field_from_field() {
- let mut x = A { a: 1 };
- let mut y = B { a: box &mut x.a };
- let p = &y.a;
- let q = &***p;
- **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
- drop(p);
- drop(q);
-}
-
-fn borrow_in_field_from_field_via_imm_box() {
- let mut x = A { a: 1 };
- let y = B { a: box &mut x.a };
- let p = &y.a;
- let q = &***p;
- **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
- //~^ ERROR cannot assign to data in an immutable container
- drop(p);
- drop(q);
-}
-
-fn main() {
- indirect_write_to_imm_box();
- borrow_in_var_from_var();
- borrow_in_var_from_var_via_imm_box();
- borrow_in_var_from_field();
- borrow_in_var_from_field_via_imm_box();
- borrow_in_field_from_var();
- borrow_in_field_from_var_via_imm_box();
- borrow_in_field_from_field();
- borrow_in_field_from_field_via_imm_box();
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-fn main() {
- let x: Option<Box<_>> = Some(box 1);
- match x {
- Some(ref _y) => {
- let _a = x; //~ ERROR cannot move
- }
- _ => {}
- }
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-fn main() {
- let x: Option<Box<_>> = Some(box 1);
- match x {
- Some(ref y) => {
- let _b = *y; //~ ERROR cannot move out
- }
- _ => {}
- }
-}
+++ /dev/null
-// 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.
-
-// Note: the borrowck analysis is currently flow-insensitive.
-// Therefore, some of these errors are marked as spurious and could be
-// corrected by a simple change to the analysis. The others are
-// either genuine or would require more advanced changes. The latter
-// cases are noted.
-
-#![feature(box_syntax)]
-
-fn borrow(_v: &isize) {}
-fn borrow_mut(_v: &mut isize) {}
-fn cond() -> bool { panic!() }
-fn for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
-fn produce<T>() -> T { panic!(); }
-
-fn inc(v: &mut Box<isize>) {
- *v = box (**v + 1);
-}
-
-fn pre_freeze_cond() {
- // In this instance, the freeze is conditional and starts before
- // the mut borrow.
-
- let mut v: Box<_> = box 3;
- let _w;
- if cond() {
- _w = &v;
- }
- borrow_mut(&mut *v); //~ ERROR cannot borrow
-}
-
-fn pre_freeze_else() {
- // In this instance, the freeze and mut borrow are on separate sides
- // of the if.
-
- let mut v: Box<_> = box 3;
- let _w;
- if cond() {
- _w = &v;
- } else {
- borrow_mut(&mut *v);
- }
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-// Note: the borrowck analysis is currently flow-insensitive.
-// Therefore, some of these errors are marked as spurious and could be
-// corrected by a simple change to the analysis. The others are
-// either genuine or would require more advanced changes. The latter
-// cases are noted.
-
-#![feature(box_syntax)]
-
-fn borrow(_v: &isize) {}
-fn borrow_mut(_v: &mut isize) {}
-fn cond() -> bool { panic!() }
-fn produce<T>() -> T { panic!(); }
-
-fn inc(v: &mut Box<isize>) {
- *v = box (**v + 1);
-}
-
-fn loop_overarching_alias_mut() {
- // In this instance, the borrow encompasses the entire loop.
-
- let mut v: Box<_> = box 3;
- let mut x = &mut v;
- **x += 1;
- loop {
- borrow(&*v); //~ ERROR cannot borrow
- }
-}
-
-fn block_overarching_alias_mut() {
- // In this instance, the borrow encompasses the entire closure call.
-
- let mut v: Box<_> = box 3;
- let mut x = &mut v;
- for _ in 0..3 {
- borrow(&*v); //~ ERROR cannot borrow
- }
- *x = box 5;
-}
-
-fn loop_aliased_mut() {
- // In this instance, the borrow is carried through the loop.
-
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut _x = &w;
- loop {
- borrow_mut(&mut *v); //~ ERROR cannot borrow
- _x = &v;
- }
-}
-
-fn while_aliased_mut() {
- // In this instance, the borrow is carried through the loop.
-
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut _x = &w;
- while cond() {
- borrow_mut(&mut *v); //~ ERROR cannot borrow
- _x = &v;
- }
-}
-
-
-fn loop_aliased_mut_break() {
- // In this instance, the borrow is carried through the loop.
-
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut _x = &w;
- loop {
- borrow_mut(&mut *v);
- _x = &v;
- break;
- }
- borrow_mut(&mut *v); //~ ERROR cannot borrow
-}
-
-fn while_aliased_mut_break() {
- // In this instance, the borrow is carried through the loop.
-
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut _x = &w;
- while cond() {
- borrow_mut(&mut *v);
- _x = &v;
- break;
- }
- borrow_mut(&mut *v); //~ ERROR cannot borrow
-}
-
-fn while_aliased_mut_cond(cond: bool, cond2: bool) {
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut x = &mut w;
- while cond {
- **x += 1;
- borrow(&*v); //~ ERROR cannot borrow
- if cond2 {
- x = &mut v; //~ ERROR cannot borrow
- }
- }
-}
-
-fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where
- F: FnMut(&'r mut usize) -> bool,
-{
- // Here we check that when you break out of an inner loop, the
- // borrows that go out of scope as you exit the inner loop are
- // removed from the bitset.
-
- while cond() {
- while cond() {
- // this borrow is limited to the scope of `r`...
- let r: &'r mut usize = produce();
- if !f(&mut *r) {
- break; // ...so it is not live as exit the `while` loop here
- }
- }
- }
-}
-
-fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F)
- where F: FnMut(&'r mut usize) -> bool
-{
- // Similar to `loop_break_pops_scopes` but for the `loop` keyword
-
- while cond() {
- while cond() {
- // this borrow is limited to the scope of `r`...
- let r: &'r mut usize = produce();
- if !f(&mut *r) {
- continue; // ...so it is not live as exit (and re-enter) the `while` loop here
- }
- }
- }
-}
-
-fn main() {}
+++ /dev/null
-// Copyright 2012-2014 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.
-
-#![allow(unused_variables)]
-#![allow(unused_assignments)]
-
-fn separate_arms() {
- // Here both arms perform assignments, but only is illegal.
-
- let mut x = None;
- match x {
- None => {
- // It is ok to reassign x here, because there is in
- // fact no outstanding loan of x!
- x = Some(0);
- }
- Some(ref __isize) => {
- x = Some(1); //~ ERROR cannot assign
- }
- }
- x.clone(); // just to prevent liveness warnings
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-// Note: the borrowck analysis is currently flow-insensitive.
-// Therefore, some of these errors are marked as spurious and could be
-// corrected by a simple change to the analysis. The others are
-// either genuine or would require more advanced changes. The latter
-// cases are noted.
-
-#![feature(box_syntax)]
-
-fn borrow(_v: &isize) {}
-fn borrow_mut(_v: &mut isize) {}
-fn cond() -> bool { panic!() }
-fn for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
-fn produce<T>() -> T { panic!(); }
-
-fn inc(v: &mut Box<isize>) {
- *v = box (**v + 1);
-}
-
-fn pre_freeze() {
- // In this instance, the freeze starts before the mut borrow.
-
- let mut v: Box<_> = box 3;
- let _w = &v;
- borrow_mut(&mut *v); //~ ERROR cannot borrow
-}
-
-fn post_freeze() {
- // In this instance, the const alias starts after the borrow.
-
- let mut v: Box<_> = box 3;
- borrow_mut(&mut *v);
- let _w = &v;
-}
-
-fn main() {}
+++ /dev/null
-// Copyright 2015 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() {
- let old = ['o']; // statement 0
- let mut v1 = Vec::new(); // statement 1
-
- let mut v2 = Vec::new(); // statement 2
- //~^ NOTE reference must be valid for the block suffix following statement 2
-
- let young = ['y']; // statement 3
- //~^ NOTE ...but borrowed value is only valid for the block suffix following statement 3
-
- v2.push(&young[0]); // statement 4
- //~^ ERROR `young[..]` does not live long enough
-
- let mut v3 = Vec::new(); // statement 5
- //~^ NOTE reference must be valid for the block suffix following statement 5
-
- v3.push(&'x'); // statement 6
- //~^ ERROR borrowed value does not live long enough
- //~| NOTE ...but borrowed value is only valid for the statement
- //~| HELP consider using a `let` binding to increase its lifetime
-
- {
-
- let mut v4 = Vec::new(); // (sub) statement 0
- //~^ NOTE reference must be valid for the block suffix following statement 0
-
- v4.push(&'y');
- //~^ ERROR borrowed value does not live long enough
- //~| NOTE ...but borrowed value is only valid for the statement
- //~| HELP consider using a `let` binding to increase its lifetime
-
- } // (statement 7)
-
- let mut v5 = Vec::new(); // statement 8
- //~^ NOTE reference must be valid for the block suffix following statement 8
-
- v5.push(&'z');
- //~^ ERROR borrowed value does not live long enough
- //~| NOTE ...but borrowed value is only valid for the statement
- //~| HELP consider using a `let` binding to increase its lifetime
-
- v1.push(&old[0]);
-}
-
-fn main() {
- f();
-}
+++ /dev/null
-// 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.
-//
-// 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() {
- let x = [1].iter(); //~ ERROR borrowed value does not live long enough
- //~^ NOTE reference must be valid for the block suffix following statement
- //~^^ HELP consider using a `let` binding to increase its lifetime
-}
-
-fn main() {
- f();
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-use std::thread;
-
-fn borrow<F>(v: &isize, f: F) where F: FnOnce(&isize) {
- f(v);
-}
-
-fn box_imm() {
- let v: Box<_> = box 3;
- let _w = &v;
- thread::spawn(move|| {
- println!("v={}", *v);
- //~^ ERROR cannot move `v` into closure
- });
-}
-
-fn box_imm_explicit() {
- let v: Box<_> = box 3;
- let _w = &v;
- thread::spawn(move|| {
- println!("v={}", *v);
- //~^ ERROR cannot move
- });
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-fn take(_v: Box<isize>) {
-}
-
-fn box_imm() {
- let v = box 3;
- let _w = &v;
- take(v); //~ ERROR cannot move out of `v` because it is borrowed
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-fn borrow<F>(v: &isize, f: F) where F: FnOnce(&isize) {
- f(v);
-}
-
-fn box_imm() {
- let mut v: Box<_> = box 3;
- borrow(&*v,
- |w| { //~ ERROR cannot borrow `v` as mutable
- v = box 4;
- assert_eq!(*v, 3);
- assert_eq!(*w, 4);
- })
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-#![feature(box_patterns)]
-#![feature(box_syntax)]
-
-use std::ops::Add;
-
-#[derive(Clone)]
-struct foo(Box<usize>);
-
-impl Add for foo {
- type Output = foo;
-
- fn add(self, f: foo) -> foo {
- let foo(box i) = self;
- let foo(box j) = f;
- foo(box (i + j))
- }
-}
-
-fn main() {
- let x = foo(box 3);
- let _y = {x} + x.clone(); // the `{x}` forces a move to occur
- //~^ ERROR use of moved value: `x`
-}
+++ /dev/null
-// Copyright 2015 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.
-
-use std::mem;
-
-fn leak<T>(mut b: Box<T>) -> &'static mut T {
- // isn't this supposed to be safe?
- let inner = &mut *b as *mut _;
- mem::forget(b);
- unsafe { &mut *inner }
-}
-
-fn evil(mut s: &'static mut String)
-{
- // create alias
- let alias: &'static mut String = s;
- let inner: &str = &alias;
- // free value
- *s = String::new(); //~ ERROR cannot assign
- let _spray = "0wned".to_owned();
- // ... and then use it
- println!("{}", inner);
-}
-
-fn main() {
- evil(leak(Box::new("hello".to_owned())));
-}
+++ /dev/null
-// 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.
-
-use std::ops::Add;
-
-#[derive(Copy, Clone)]
-struct Point {
- x: isize,
- y: isize,
-}
-
-impl Add<isize> for Point {
- type Output = isize;
-
- fn add(self, z: isize) -> isize {
- self.x + self.y + z
- }
-}
-
-impl Point {
- pub fn times(&self, z: isize) -> isize {
- self.x * self.y * z
- }
-}
-
-fn a() {
- let mut p = Point {x: 3, y: 4};
-
- // ok (we can loan out rcvr)
- p + 3;
- p.times(3);
-}
-
-fn b() {
- let mut p = Point {x: 3, y: 4};
-
- // Here I create an outstanding loan and check that we get conflicts:
-
- let q = &mut p;
-
- p + 3; //~ ERROR cannot use `p`
- p.times(3); //~ ERROR cannot borrow `p`
-
- *q + 3; // OK to use the new alias `q`
- q.x += 1; // and OK to mutate it
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-
-struct point { x: isize, y: isize }
-
-trait methods {
- fn impurem(&self);
- fn blockm<F>(&self, f: F) where F: FnOnce();
-}
-
-impl methods for point {
- fn impurem(&self) {
- }
-
- fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
-}
-
-fn a() {
- let mut p = point {x: 3, y: 4};
-
- // Here: it's ok to call even though receiver is mutable, because we
- // can loan it out.
- p.impurem();
-
- // But in this case we do not honor the loan:
- p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
- p.x = 10;
- })
-}
-
-fn b() {
- let mut p = point {x: 3, y: 4};
-
- // Here I create an outstanding loan and check that we get conflicts:
-
- let l = &mut p;
- p.impurem(); //~ ERROR cannot borrow
-
- l.x += 1;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-
-// Here we check that it is allowed to lend out an element of a
-// (locally rooted) mutable, unique vector, and that we then prevent
-// modifications to the contents.
-
-fn takes_imm_elt<F>(_v: &isize, f: F) where F: FnOnce() {
- f();
-}
-
-fn has_mut_vec_and_does_not_try_to_change_it() {
- let mut v: Vec<isize> = vec!(1, 2, 3);
- takes_imm_elt(&v[0], || {})
-}
-
-fn has_mut_vec_but_tries_to_change_it() {
- let mut v: Vec<isize> = vec!(1, 2, 3);
- takes_imm_elt(
- &v[0],
- || { //~ ERROR cannot borrow `v` as mutable
- v[1] = 4;
- })
-}
-
-fn main() {
-}
+++ /dev/null
-// Copyright 2012-2014 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.
-
-// Test that immutable pattern bindings cannot be reassigned.
-
-#![feature(slice_patterns)]
-
-enum E {
- Foo(isize)
-}
-
-struct S {
- bar: isize,
-}
-
-pub fn main() {
- match 1 {
- x => {
- x += 1; //~ ERROR re-assignment of immutable variable `x`
- }
- }
-
- match E::Foo(1) {
- E::Foo(x) => {
- x += 1; //~ ERROR re-assignment of immutable variable `x`
- }
- }
-
- match (S { bar: 1 }) {
- S { bar: x } => {
- x += 1; //~ ERROR re-assignment of immutable variable `x`
- }
- }
-
- match (1,) {
- (x,) => {
- x += 1; //~ ERROR re-assignment of immutable variable `x`
- }
- }
-
- match [1,2,3] {
- [x,_,_] => {
- x += 1; //~ ERROR re-assignment of immutable variable `x`
- }
- }
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax,unboxed_closures)]
-
-fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
-fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
-
-pub fn main() {
- let bar: Box<_> = box 3;
- let _g = to_fn_mut(|| {
- let _h = to_fn_once(move || -> isize { *bar }); //~ ERROR cannot move out of
- });
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-enum Foo {
- Foo1(Box<u32>, Box<u32>),
- Foo2(Box<u32>),
- Foo3,
-}
-
-fn blah() {
- let f = &Foo::Foo1(box 1, box 2);
- match *f { //~ ERROR cannot move out of
- Foo::Foo1(num1, //~ NOTE attempting to move value to here
- num2) => (), //~ NOTE and here
- Foo::Foo2(num) => (), //~ NOTE and here
- Foo::Foo3 => ()
- }
-}
-
-struct S {
- f: String,
- g: String
-}
-impl Drop for S {
- fn drop(&mut self) { println!("{}", self.f); }
-}
-
-fn move_in_match() {
- match (S {f: "foo".to_string(), g: "bar".to_string()}) {
- S { //~ ERROR cannot move out of type `S`, which defines the `Drop` trait
- f: _s, //~ NOTE attempting to move value to here
- g: _t //~ NOTE and here
- } => {}
- }
-}
-
-// from issue-8064
-struct A {
- a: Box<isize>,
-}
-
-fn free<T>(_: T) {}
-
-fn blah2() {
- let a = &A { a: box 1 };
- match a.a { //~ ERROR cannot move out of
- n => { //~ NOTE attempting to move value to here
- free(n)
- }
- }
- free(a)
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// verify that an error is raised when trying to move out of a
-// borrowed path.
-
-#![feature(box_syntax)]
-
-fn main() {
- let a: Box<Box<_>> = box box 2;
- let b = &a;
-
- let z = *a; //~ ERROR: cannot move out of `*a` because it is borrowed
-}
+++ /dev/null
-// 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 foo(x: *const Box<isize>) -> Box<isize> {
- let y = *x; //~ ERROR dereference of raw pointer requires unsafe function or block
- return y;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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 with<F>(f: F) where F: FnOnce(&String) {}
-
-fn arg_item(&_x: &String) {}
- //~^ ERROR cannot move out of borrowed content
-
-fn arg_closure() {
- with(|&_x| ())
- //~^ ERROR cannot move out of borrowed content
-}
-
-fn let_pat() {
- let &_x = &"hi".to_string();
- //~^ ERROR cannot move out of borrowed content
-}
-
-pub fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-fn call_f<F:FnOnce() -> isize>(f: F) -> isize {
- f()
-}
-
-fn main() {
- let t: Box<_> = box 3;
-
- call_f(move|| { *t + 1 });
- call_f(move|| { *t + 1 }); //~ ERROR capture of moved value
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that attempt to move `&mut` pointer while pointee is borrowed
-// yields an error.
-//
-// Example from src/librustc_borrowck/borrowck/README.md
-
-fn foo(t0: &mut isize) {
- let p: &isize = &*t0; // Freezes `*t0`
- let t1 = t0; //~ ERROR cannot move out of `t0`
- *t1 = 22;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-use std::rc::Rc;
-
-pub fn main() {
- let _x = Rc::new(vec!(1, 2)).into_iter();
- //~^ ERROR cannot move out of borrowed content
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-use std::rc::Rc;
-
-pub fn main() {
- let _x = *Rc::new("hi".to_string());
- //~^ ERROR cannot move out of borrowed content
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Ensure that moves out of static items is forbidden
-
-struct Foo {
- foo: isize,
-}
-
-static BAR: Foo = Foo { foo: 5 };
-
-
-fn test(f: Foo) {
- let _f = Foo{foo: 4, ..f};
-}
-
-fn main() {
- test(BAR); //~ ERROR cannot move out of static item
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-struct S {f:String}
-impl Drop for S {
- fn drop(&mut self) { println!("{}", self.f); }
-}
-
-fn move_in_match() {
- match (S {f:"foo".to_string()}) {
- S {f:_s} => {}
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
- }
-}
-
-fn move_in_let() {
- let S {f:_s} = S {f:"foo".to_string()};
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
-}
-
-fn move_in_fn_arg(S {f:_s}: S) {
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-struct S(String);
-impl Drop for S {
- fn drop(&mut self) { }
-}
-
-fn move_in_match() {
- match S("foo".to_string()) {
- S(_s) => {}
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
- }
-}
-
-fn move_in_let() {
- let S(_s) = S("foo".to_string());
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
-}
-
-fn move_in_fn_arg(S(_s): S) {
- //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that we do not permit moves from &[] matched by a vec pattern.
-
-#![feature(slice_patterns)]
-
-#[derive(Clone, Debug)]
-struct Foo {
- string: String
-}
-
-pub fn main() {
- let x = vec!(
- Foo { string: "foo".to_string() },
- Foo { string: "bar".to_string() },
- Foo { string: "baz".to_string() }
- );
- let x: &[Foo] = &x;
- match x {
- [_, tail..] => {
- match tail {
- [Foo { string: a }, //~ ERROR cannot move out of borrowed content
- Foo { string: b }] => {
- //~^^ NOTE attempting to move value to here
- //~^^ NOTE and here
- }
- _ => {
- unreachable!();
- }
- }
- let z = tail[0].clone();
- println!("{:?}", z);
- }
- _ => {
- unreachable!();
- }
- }
-}
+++ /dev/null
-// Copyright 2012-2013 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.
-
-// Tests that the borrow checker checks all components of a path when moving
-// out.
-
-#![feature(box_syntax)]
-
-struct S {
- x : Box<isize>
-}
-
-fn f<T>(_: T) {}
-
-fn main() {
- let a : S = S { x : box 1 };
- let pb = &a;
- let S { x: ax } = a; //~ ERROR cannot move out
- f(pb);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-use std::thread;
-
-fn borrow<T>(_: &T) { }
-
-fn different_vars_after_borrows() {
- let x1: Box<_> = box 1;
- let p1 = &x1;
- let x2: Box<_> = box 2;
- let p2 = &x2;
- thread::spawn(move|| {
- drop(x1); //~ ERROR cannot move `x1` into closure because it is borrowed
- drop(x2); //~ ERROR cannot move `x2` into closure because it is borrowed
- });
- borrow(&*p1);
- borrow(&*p2);
-}
-
-fn different_vars_after_moves() {
- let x1: Box<_> = box 1;
- drop(x1);
- let x2: Box<_> = box 2;
- drop(x2);
- thread::spawn(move|| {
- drop(x1); //~ ERROR capture of moved value: `x1`
- drop(x2); //~ ERROR capture of moved value: `x2`
- });
-}
-
-fn same_var_after_borrow() {
- let x: Box<_> = box 1;
- let p = &x;
- thread::spawn(move|| {
- drop(x); //~ ERROR cannot move `x` into closure because it is borrowed
- drop(x); //~ ERROR use of moved value: `x`
- });
- borrow(&*p);
-}
-
-fn same_var_after_move() {
- let x: Box<_> = box 1;
- drop(x);
- thread::spawn(move|| {
- drop(x); //~ ERROR capture of moved value: `x`
- drop(x); //~ ERROR use of moved value: `x`
- });
-}
-
-fn main() {
- different_vars_after_borrows();
- different_vars_after_moves();
- same_var_after_borrow();
- same_var_after_move();
-}
+++ /dev/null
-// 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 main() {
- let x: isize = 3;
- let y: &mut isize = &mut x; //~ ERROR cannot borrow
- *y = 5;
- println!("{}", *y);
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Test to ensure we only report an error for the first issued loan that
-// conflicts with a new loan, as opposed to every issued loan. This keeps us
-// down to O(n) errors (for n problem lines), instead of O(n^2) errors.
-
-fn main() {
- let mut x = 1;
- let mut addr;
- loop {
- match 1 {
- 1 => { addr = &mut x; }
- //~^ ERROR cannot borrow `x` as mutable more than once at a time
- 2 => { addr = &mut x; }
- //~^ ERROR cannot borrow `x` as mutable more than once at a time
- _ => { addr = &mut x; }
- //~^ ERROR cannot borrow `x` as mutable more than once at a time
- }
- }
-}
+++ /dev/null
-// Copyright 2013 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.
-
-// Test that attempt to mutably borrow `&mut` pointer while pointee is
-// borrowed yields an error.
-//
-// Example from src/librustc_borrowck/borrowck/README.md
-
-fn foo<'a>(mut t0: &'a mut isize,
- mut t1: &'a mut isize) {
- let p: &isize = &*t0; // Freezes `*t0`
- let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
- **t2 += 1; // Mutates `*t0`
-}
-
-fn bar<'a>(mut t0: &'a mut isize,
- mut t1: &'a mut isize) {
- let p: &mut isize = &mut *t0; // Claims `*t0`
- let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
- **t2 += 1; // Mutates `*t0` but not through `*p`
-}
-
-fn main() {
-}
+++ /dev/null
-// 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 write(v: &mut [isize]) {
- v[0] += 1;
-}
-
-fn main() {
- let v = vec!(1, 2, 3);
- write(&mut v); //~ ERROR cannot borrow
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-enum Enum<'a> {
- A(&'a isize),
- B(bool),
-}
-
-fn foo() -> isize {
- let mut n = 42;
- let mut x = Enum::A(&mut n);
- match x {
- Enum::A(_) if { x = Enum::B(false); false } => 1,
- //~^ ERROR cannot assign in a pattern guard
- Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
- //~^ ERROR cannot mutably borrow in a pattern guard
- //~^^ ERROR cannot assign in a pattern guard
- Enum::A(p) => *p,
- Enum::B(_) => 2,
- }
-}
-
-fn main() {
- foo();
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-struct node_ {
- a: Box<cycle>
-}
-
-enum cycle {
- node(node_),
- empty
-}
-fn main() {
- let mut x: Box<_> = box cycle::node(node_ {a: box cycle::empty});
- // Create a cycle!
- match *x {
- cycle::node(ref mut y) => {
- y.a = x; //~ ERROR cannot move out of
- }
- cycle::empty => {}
- };
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that borrows that occur due to calls to object methods
-// properly "claim" the object path.
-
-trait Foo {
- fn borrowed(&self) -> &();
- fn mut_borrowed(&mut self) -> &();
-}
-
-fn borrowed_receiver(x: &Foo) {
- let _y = x.borrowed();
- let _z = x.borrowed();
-}
-
-fn mut_borrowed_receiver(x: &mut Foo) {
- let _y = x.borrowed();
- let _z = x.mut_borrowed(); //~ ERROR cannot borrow
-}
-
-fn mut_owned_receiver(mut x: Box<Foo>) {
- let _y = x.borrowed();
- let _z = &mut x; //~ ERROR cannot borrow
-}
-
-fn imm_owned_receiver(mut x: Box<Foo>) {
- let _y = x.borrowed();
- let _z = &x;
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-
-
-trait Foo {
- fn borrowed(&self);
- fn borrowed_mut(&mut self);
-}
-
-fn borrowed_receiver(x: &Foo) {
- x.borrowed();
- x.borrowed_mut(); //~ ERROR cannot borrow
-}
-
-fn borrowed_mut_receiver(x: &mut Foo) {
- x.borrowed();
- x.borrowed_mut();
-}
-
-fn owned_receiver(x: Box<Foo>) {
- x.borrowed();
- x.borrowed_mut(); //~ ERROR cannot borrow
-}
-
-fn mut_owned_receiver(mut x: Box<Foo>) {
- x.borrowed();
- x.borrowed_mut();
-}
-
-fn main() {}
+++ /dev/null
-// 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 main() {
- let i: isize;
-
- println!("{}", false || { i = 5; true });
- println!("{}", i); //~ ERROR use of possibly uninitialized variable: `i`
-}
+++ /dev/null
-// 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.
-
-#![feature(unboxed_closures)]
-
-use std::ops::{Fn, FnMut, FnOnce};
-
-struct SFn {
- x: isize,
- y: isize,
-}
-
-impl Fn<(isize,)> for SFn {
- extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
- self.x * self.y * z
- }
-}
-
-impl FnMut<(isize,)> for SFn {
- extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) }
-}
-
-impl FnOnce<(isize,)> for SFn {
- type Output = isize;
- extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) }
-}
-
-struct SFnMut {
- x: isize,
- y: isize,
-}
-
-impl FnMut<(isize,)> for SFnMut {
- extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
- self.x * self.y * z
- }
-}
-
-impl FnOnce<(isize,)> for SFnMut {
- type Output = isize;
- extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) }
-}
-
-struct SFnOnce {
- x: String,
-}
-
-impl FnOnce<(String,)> for SFnOnce {
- type Output = usize;
-
- extern "rust-call" fn call_once(self, (z,): (String,)) -> usize {
- self.x.len() + z.len()
- }
-}
-
-fn f() {
- let mut s = SFn {
- x: 1,
- y: 2,
- };
- let sp = &mut s;
- s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
- //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
-}
-
-fn g() {
- let s = SFnMut {
- x: 1,
- y: 2,
- };
- s(3); //~ ERROR cannot borrow immutable local variable `s` as mutable
-}
-
-fn h() {
- let s = SFnOnce {
- x: "hello".to_string(),
- };
- s(" world".to_string());
- s(" world".to_string()); //~ ERROR use of moved value: `s`
-}
-
-fn main() {}
+++ /dev/null
-// Copyright 2015 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.
-
-// Check that we properly record borrows when we are doing an
-// overloaded, autoderef of a value obtained via an overloaded index
-// operator. The accounting of the all the implicit things going on
-// here is rather subtle. Issue #20232.
-
-use std::ops::{Deref, Index};
-
-struct MyVec<T> { x: T }
-
-impl<T> Index<usize> for MyVec<T> {
- type Output = T;
- fn index(&self, _: usize) -> &T {
- &self.x
- }
-}
-
-struct MyPtr<T> { x: T }
-
-impl<T> Deref for MyPtr<T> {
- type Target = T;
- fn deref(&self) -> &T {
- &self.x
- }
-}
-
-struct Foo { f: usize }
-
-fn main() {
- let mut v = MyVec { x: MyPtr { x: Foo { f: 22 } } };
- let i = &v[0].f;
- v = MyVec { x: MyPtr { x: Foo { f: 23 } } };
- //~^ ERROR cannot assign to `v`
- read(*i);
-}
-
-fn read(_: usize) { }
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that we still see borrowck errors of various kinds when using
-// indexing and autoderef in combination.
-
-use std::ops::{Index, IndexMut};
-
-struct Foo {
- x: isize,
- y: isize,
-}
-
-impl<'a> Index<&'a String> for Foo {
- type Output = isize;
-
- fn index(&self, z: &String) -> &isize {
- if *z == "x" {
- &self.x
- } else {
- &self.y
- }
- }
-}
-
-impl<'a> IndexMut<&'a String> for Foo {
- fn index_mut(&mut self, z: &String) -> &mut isize {
- if *z == "x" {
- &mut self.x
- } else {
- &mut self.y
- }
- }
-}
-
-fn test1(mut f: Box<Foo>, s: String) {
- let _p = &mut f[&s];
- let _q = &f[&s]; //~ ERROR cannot borrow
-}
-
-fn test2(mut f: Box<Foo>, s: String) {
- let _p = &mut f[&s];
- let _q = &mut f[&s]; //~ ERROR cannot borrow
-}
-
-struct Bar {
- foo: Foo
-}
-
-fn test3(mut f: Box<Bar>, s: String) {
- let _p = &mut f.foo[&s];
- let _q = &mut f.foo[&s]; //~ ERROR cannot borrow
-}
-
-fn test4(mut f: Box<Bar>, s: String) {
- let _p = &f.foo[&s];
- let _q = &f.foo[&s];
-}
-
-fn test5(mut f: Box<Bar>, s: String) {
- let _p = &f.foo[&s];
- let _q = &mut f.foo[&s]; //~ ERROR cannot borrow
-}
-
-fn test6(mut f: Box<Bar>, g: Foo, s: String) {
- let _p = &f.foo[&s];
- f.foo = g; //~ ERROR cannot assign
-}
-
-fn test7(mut f: Box<Bar>, g: Bar, s: String) {
- let _p = &f.foo[&s];
- *f = g; //~ ERROR cannot assign
-}
-
-fn test8(mut f: Box<Bar>, g: Foo, s: String) {
- let _p = &mut f.foo[&s];
- f.foo = g; //~ ERROR cannot assign
-}
-
-fn test9(mut f: Box<Bar>, g: Bar, s: String) {
- let _p = &mut f.foo[&s];
- *f = g; //~ ERROR cannot assign
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-use std::ops::Index;
-
-struct MyVec<T> {
- data: Vec<T>,
-}
-
-impl<T> Index<usize> for MyVec<T> {
- type Output = T;
-
- fn index(&self, i: usize) -> &T {
- &self.data[i]
- }
-}
-
-fn main() {
- let v = MyVec::<Box<_>> { data: vec!(box 1, box 2, box 3) };
- let good = &v[0]; // Shouldn't fail here
- let bad = v[0];
- //~^ ERROR cannot move out of indexed content
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-use std::ops::{Index, IndexMut};
-
-struct Foo {
- x: isize,
- y: isize,
-}
-
-impl Index<String> for Foo {
- type Output = isize;
-
- fn index(&self, z: String) -> &isize {
- if z == "x" {
- &self.x
- } else {
- &self.y
- }
- }
-}
-
-impl IndexMut<String> for Foo {
- fn index_mut(&mut self, z: String) -> &mut isize {
- if z == "x" {
- &mut self.x
- } else {
- &mut self.y
- }
- }
-}
-
-struct Bar {
- x: isize,
-}
-
-impl Index<isize> for Bar {
- type Output = isize;
-
- fn index<'a>(&'a self, z: isize) -> &'a isize {
- &self.x
- }
-}
-
-fn main() {
- let mut f = Foo {
- x: 1,
- y: 2,
- };
- let mut s = "hello".to_string();
- let rs = &mut s;
-
- println!("{}", f[s]);
- //~^ ERROR cannot move out of `s` because it is borrowed
-
- f[s] = 10;
- //~^ ERROR cannot move out of `s` because it is borrowed
- //~| ERROR use of moved value: `s`
-
- let s = Bar {
- x: 1,
- };
- let i = 2;
- let _j = &i;
- println!("{}", s[i]); // no error, i is copy
- println!("{}", s[i]);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-use std::ops::{Index, IndexMut};
-
-struct Foo {
- x: isize,
- y: isize,
-}
-
-impl<'a> Index<&'a String> for Foo {
- type Output = isize;
-
- fn index(&self, z: &String) -> &isize {
- if *z == "x" {
- &self.x
- } else {
- &self.y
- }
- }
-}
-
-impl<'a> IndexMut<&'a String> for Foo {
- fn index_mut(&mut self, z: &String) -> &mut isize {
- if *z == "x" {
- &mut self.x
- } else {
- &mut self.y
- }
- }
-}
-
-struct Bar {
- x: isize,
-}
-
-impl Index<isize> for Bar {
- type Output = isize;
-
- fn index<'a>(&'a self, z: isize) -> &'a isize {
- &self.x
- }
-}
-
-fn main() {
- let mut f = Foo {
- x: 1,
- y: 2,
- };
- let mut s = "hello".to_string();
- let rs = &mut s;
- println!("{}", f[&s]);
- //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
- f[&s] = 10;
- //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
- let s = Bar {
- x: 1,
- };
- s[2] = 20;
- //~^ ERROR cannot assign to immutable indexed content
-}
+++ /dev/null
-// Copyright 2014-2015 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.
-
-struct Test;
-
-struct Test2 {
- b: Option<Test>,
-}
-
-struct Test3(Option<Test>);
-
-impl Drop for Test {
- fn drop(&mut self) {
- println!("dropping!");
- }
-}
-
-impl Drop for Test2 {
- fn drop(&mut self) {}
-}
-
-impl Drop for Test3 {
- fn drop(&mut self) {}
-}
-
-fn stuff() {
- let mut t = Test2 { b: None };
- let u = Test;
- drop(t);
- t.b = Some(u);
- //~^ ERROR partial reinitialization of uninitialized structure `t`
-
- let mut t = Test3(None);
- let u = Test;
- drop(t);
- t.0 = Some(u);
- //~^ ERROR partial reinitialization of uninitialized structure `t`
-}
-
-fn main() {
- stuff()
-}
+++ /dev/null
-// Copyright 2014-2015 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.
-
-struct Test {
- a: isize,
- b: Option<Box<Test>>,
-}
-
-impl Drop for Test {
- fn drop(&mut self) {
- println!("Dropping {}", self.a);
- }
-}
-
-fn stuff() {
- let mut t = Test { a: 1, b: None};
- let mut u = Test { a: 2, b: Some(Box::new(t))};
- t.b = Some(Box::new(u));
- //~^ ERROR partial reinitialization of uninitialized structure `t`
- println!("done");
-}
-
-fn main() {
- stuff();
- println!("Hello, world!")
-}
+++ /dev/null
-// Copyright 2015 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.
-use std::mem;
-
-struct Test { f: usize }
-impl Drop for Test {
- fn drop(&mut self) {}
-}
-
-fn main() {
- let mut x = (Test { f: 2 }, Test { f: 4 });
- mem::drop(x.0);
- x.0.f = 3;
- //~^ ERROR partial reinitialization of uninitialized structure `x.0`
-}
+++ /dev/null
-// Copyright 2015 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.
-
-struct Test;
-
-struct Test2(Option<Test>);
-
-impl Drop for Test {
- fn drop(&mut self) {
- println!("dropping!");
- }
-}
-
-impl Drop for Test2 {
- fn drop(&mut self) {}
-}
-
-fn stuff() {
- let mut x : (Test2, Test2);
- (x.0).0 = Some(Test);
- //~^ ERROR partial reinitialization of uninitialized structure `x.0`
-}
-
-fn main() {
- stuff()
-}
+++ /dev/null
-// Copyright 2012-2014 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 main() {
- let mut x: Option<isize> = None;
- match x {
- None => {
- // Note: on this branch, no borrow has occurred.
- x = Some(0);
- }
- Some(ref i) => {
- // But on this branch, `i` is an outstanding borrow
- x = Some(*i+1); //~ ERROR cannot assign to `x`
- }
- }
- x.clone(); // just to prevent liveness warnings
-}
+++ /dev/null
-// 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.
-
-struct Foo {
- bar1: Bar,
- bar2: Bar
-}
-
-struct Bar {
- int1: isize,
- int2: isize,
-}
-
-fn borrow_same_field_twice_mut_mut(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_same_field_twice_mut_imm(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1;
- let _bar2 = &foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_same_field_twice_imm_mut(foo: &mut Foo) {
- let _bar1 = &foo.bar1;
- let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_same_field_twice_imm_imm(foo: &mut Foo) {
- let _bar1 = &foo.bar1;
- let _bar2 = &foo.bar1;
-}
-
-fn borrow_both_mut(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1;
- let _bar2 = &mut foo.bar2;
-}
-
-fn borrow_both_mut_pattern(foo: &mut Foo) {
- match *foo {
- Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
- }
-}
-
-fn borrow_var_and_pattern(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1;
- match *foo {
- Foo { bar1: ref mut _bar1, bar2: _ } => {}
- //~^ ERROR cannot borrow
- }
-}
-
-fn borrow_mut_and_base_imm(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1.int1;
- let _foo1 = &foo.bar1; //~ ERROR cannot borrow
- let _foo2 = &*foo; //~ ERROR cannot borrow
-}
-
-fn borrow_mut_and_base_mut(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_mut_and_base_mut2(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1.int1;
- let _foo2 = &mut *foo; //~ ERROR cannot borrow
-}
-
-fn borrow_imm_and_base_mut(foo: &mut Foo) {
- let _bar1 = &foo.bar1.int1;
- let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_imm_and_base_mut2(foo: &mut Foo) {
- let _bar1 = &foo.bar1.int1;
- let _foo2 = &mut *foo; //~ ERROR cannot borrow
-}
-
-fn borrow_imm_and_base_imm(foo: &mut Foo) {
- let _bar1 = &foo.bar1.int1;
- let _foo1 = &foo.bar1;
- let _foo2 = &*foo;
-}
-
-fn borrow_mut_and_imm(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1;
- let _foo1 = &foo.bar2;
-}
-
-fn borrow_mut_from_imm(foo: &Foo) {
- let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow
-}
-
-fn borrow_long_path_both_mut(foo: &mut Foo) {
- let _bar1 = &mut foo.bar1.int1;
- let _foo1 = &mut foo.bar2.int2;
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that assignments to an `&mut` pointer which is found in a
-// borrowed (but otherwise non-aliasable) location is illegal.
-
-struct S<'a> {
- pointer: &'a mut isize
-}
-
-fn copy_borrowed_ptr<'a,'b>(p: &'a mut S<'b>) -> S<'b> {
- S { pointer: &mut *p.pointer }
- //~^ ERROR cannot infer
-}
-
-fn main() {
- let mut x = 1;
-
- {
- let mut y = S { pointer: &mut x };
- let z = copy_borrowed_ptr(&mut y);
- *y.pointer += 1;
- *z.pointer += 1;
- }
-}
+++ /dev/null
-// 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 main() {
- let msg;
- match Some("Hello".to_string()) {
- Some(ref m) => { //~ ERROR borrowed value does not live long enough
- msg = m;
- },
- None => { panic!() }
- }
- println!("{}", *msg);
-}
+++ /dev/null
-// 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 destructure(x: Option<isize>) -> isize {
- match x {
- None => 0,
- Some(ref mut v) => *v //~ ERROR cannot borrow
- }
-}
-
-fn main() {
- assert_eq!(destructure(Some(22)), 22);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![allow(dead_code)]
-fn main() {
- // Original borrow ends at end of function
- let mut x = 1;
- let y = &mut x;
- let z = &x; //~ ERROR cannot borrow
-}
-//~^ NOTE previous borrow ends here
-
-fn foo() {
- match true {
- true => {
- // Original borrow ends at end of match arm
- let mut x = 1;
- let y = &x;
- let z = &mut x; //~ ERROR cannot borrow
- }
- //~^ NOTE previous borrow ends here
- false => ()
- }
-}
-
-fn bar() {
- // Original borrow ends at end of closure
- || {
- let mut x = 1;
- let y = &mut x;
- let z = &mut x; //~ ERROR cannot borrow
- };
- //~^ NOTE previous borrow ends here
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Check that when we clone a `&T` pointer we properly relate the
-// lifetime of the pointer which results to the pointer being cloned.
-// Bugs in method resolution have sometimes broken this connection.
-// Issue #19261.
-
-fn leak<'a, T>(x: T) -> &'a T {
- (&x).clone() //~ ERROR `x` does not live long enough
-}
-
-fn main() { }
+++ /dev/null
-// 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() -> isize {
- let x: isize;
- return x; //~ ERROR use of possibly uninitialized variable: `x`
-}
-
-fn main() { f(); }
+++ /dev/null
-// Copyright 2012-2013 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.
-
-// Issue 4691: Ensure that functional-struct-update can only copy, not
-// move, when the struct implements Drop.
-
-struct B;
-struct S { a: isize, b: B }
-impl Drop for S { fn drop(&mut self) { } }
-
-struct T { a: isize, mv: Box<isize> }
-impl Drop for T { fn drop(&mut self) { } }
-
-fn f(s0:S) {
- let _s2 = S{a: 2, ..s0}; //~error: cannot move out of type `S`, which defines the `Drop` trait
-}
-
-fn g(s0:T) {
- let _s2 = T{a: 2, ..s0}; //~error: cannot move out of type `T`, which defines the `Drop` trait
-}
-
-fn main() { }
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Test that attempt to swap `&mut` pointer while pointee is borrowed
-// yields an error.
-//
-// Example from src/librustc_borrowck/borrowck/README.md
-
-use std::mem::swap;
-
-fn foo<'a>(mut t0: &'a mut isize,
- mut t1: &'a mut isize) {
- let p: &isize = &*t0; // Freezes `*t0`
- swap(&mut t0, &mut t1); //~ ERROR cannot borrow `t0`
- *t1 = 22;
-}
-
-fn main() {
-}
+++ /dev/null
-// 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 foo(x: Box<isize>) -> isize {
- let y = &*x;
- free(x); //~ ERROR cannot move out of `x` because it is borrowed
- *y
-}
-
-fn free(_x: Box<isize>) {
-}
-
-fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(overloaded_calls, unboxed_closures)]
-
-fn a<F:Fn(isize, isize) -> isize>(mut f: F) {
- let g = &mut f;
- f(1, 2); //~ ERROR cannot borrow `f` as immutable
- //~^ ERROR cannot borrow `f` as immutable
-}
-
-fn b<F:FnMut(isize, isize) -> isize>(f: F) {
- f(1, 2); //~ ERROR cannot borrow immutable argument
-}
-
-fn c<F:FnOnce(isize, isize) -> isize>(f: F) {
- f(1, 2);
- f(1, 2); //~ ERROR use of moved value
-}
-
-fn main() {}
+++ /dev/null
-// 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 main() {
- let bar;
- fn baz(_x: isize) { }
- baz(bar); //~ ERROR use of possibly uninitialized variable: `bar`
-}
+++ /dev/null
-// Copyright 2012-2014 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.
-
-// Tests that the use of uninitialized variable in assignment operator
-// expression is detected.
-
-pub fn main() {
- let x: isize;
- x += 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x -= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x *= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x /= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x %= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x ^= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x &= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x |= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x <<= 1; //~ ERROR use of possibly uninitialized variable: `x`
-
- let x: isize;
- x >>= 1; //~ ERROR use of possibly uninitialized variable: `x`
-}
+++ /dev/null
-// 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 foo(x: isize) { println!("{}", x); }
-
-fn main() {
- let x: isize;
- foo(x); //~ ERROR use of possibly uninitialized variable: `x`
-}
+++ /dev/null
-// 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.
-
-#![feature(box_syntax)]
-
-fn borrow(_v: &isize) {}
-
-fn local() {
- let mut v: Box<_> = box 3;
- borrow(&*v);
-}
-
-fn local_rec() {
- struct F { f: Box<isize> }
- let mut v = F {f: box 3};
- borrow(&*v.f);
-}
-
-fn local_recs() {
- struct F { f: G }
- struct G { g: H }
- struct H { h: Box<isize> }
- let mut v = F {f: G {g: H {h: box 3}}};
- borrow(&*v.f.g.h);
-}
-
-fn aliased_imm() {
- let mut v: Box<_> = box 3;
- let _w = &v;
- borrow(&*v);
-}
-
-fn aliased_mut() {
- let mut v: Box<_> = box 3;
- let _w = &mut v;
- borrow(&*v); //~ ERROR cannot borrow `*v`
-}
-
-fn aliased_other() {
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let _x = &mut w;
- borrow(&*v);
-}
-
-fn aliased_other_reassign() {
- let mut v: Box<_> = box 3;
- let mut w: Box<_> = box 4;
- let mut _x = &mut w;
- _x = &mut v;
- borrow(&*v); //~ ERROR cannot borrow `*v`
-}
-
-fn main() {
-}
+++ /dev/null
-// 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 test() {
- let w: &mut [isize];
- w[5] = 0; //~ ERROR use of possibly uninitialized variable: `*w`
-
- let mut w: &mut [isize];
- w[5] = 0; //~ ERROR use of possibly uninitialized variable: `*w`
-}
-
-fn main() { test(); }
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(box_syntax)]
-
-#[derive(Copy, Clone)]
-struct A { a: isize, b: isize }
-
-struct B { a: isize, b: Box<isize> }
-
-fn var_copy_after_var_borrow() {
- let mut x: isize = 1;
- let p = &mut x;
- drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
- *p = 2;
-}
-
-fn var_copy_after_field_borrow() {
- let mut x = A { a: 1, b: 2 };
- let p = &mut x.a;
- drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
- *p = 3;
-}
-
-fn field_copy_after_var_borrow() {
- let mut x = A { a: 1, b: 2 };
- let p = &mut x;
- drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
- p.a = 3;
-}
-
-fn field_copy_after_field_borrow() {
- let mut x = A { a: 1, b: 2 };
- let p = &mut x.a;
- drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
- *p = 3;
-}
-
-fn fu_field_copy_after_var_borrow() {
- let mut x = A { a: 1, b: 2 };
- let p = &mut x;
- let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
- drop(y);
- p.a = 4;
-}
-
-fn fu_field_copy_after_field_borrow() {
- let mut x = A { a: 1, b: 2 };
- let p = &mut x.a;
- let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
- drop(y);
- *p = 4;
-}
-
-fn var_deref_after_var_borrow() {
- let mut x: Box<isize> = box 1;
- let p = &mut x;
- drop(*x); //~ ERROR cannot use `*x` because it was mutably borrowed
- **p = 2;
-}
-
-fn field_deref_after_var_borrow() {
- let mut x = B { a: 1, b: box 2 };
- let p = &mut x;
- drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
- p.a = 3;
-}
-
-fn field_deref_after_field_borrow() {
- let mut x = B { a: 1, b: box 2 };
- let p = &mut x.b;
- drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
- **p = 3;
-}
-
-fn main() {
- var_copy_after_var_borrow();
- var_copy_after_field_borrow();
-
- field_copy_after_var_borrow();
- field_copy_after_field_borrow();
-
- fu_field_copy_after_var_borrow();
- fu_field_copy_after_field_borrow();
-
- var_deref_after_var_borrow();
- field_deref_after_var_borrow();
- field_deref_after_field_borrow();
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Variation on `borrowck-use-uninitialized-in-cast` in which we do a
-// trait cast from an uninitialized source. Issue #20791.
-
-trait Foo { fn dummy(&self) { } }
-impl Foo for i32 { }
-
-fn main() {
- let x: &i32;
- let y = x as *const Foo; //~ ERROR use of possibly uninitialized variable: `*x`
-}
+++ /dev/null
-// Copyright 2015 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.
-
-// Check that we detect unused values that are cast to other things.
-// The problem was specified to casting to `*`, as creating unsafe
-// pointers was not being fully checked. Issue #20791.
-
-// pretty-expanded FIXME #23616
-
-fn main() {
- let x: &i32;
- let y = x as *const i32; //~ ERROR use of possibly uninitialized variable: `*x`
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(advanced_slice_patterns)]
-#![feature(slice_patterns)]
-
-fn a<'a>() -> &'a [isize] {
- let vec = vec!(1, 2, 3, 4);
- let vec: &[isize] = &vec; //~ ERROR does not live long enough
- let tail = match vec {
- [_, tail..] => tail,
- _ => panic!("a")
- };
- tail
-}
-
-fn b<'a>() -> &'a [isize] {
- let vec = vec!(1, 2, 3, 4);
- let vec: &[isize] = &vec; //~ ERROR does not live long enough
- let init = match vec {
- [init.., _] => init,
- _ => panic!("b")
- };
- init
-}
-
-fn c<'a>() -> &'a [isize] {
- let vec = vec!(1, 2, 3, 4);
- let vec: &[isize] = &vec; //~ ERROR does not live long enough
- let slice = match vec {
- [_, slice.., _] => slice,
- _ => panic!("c")
- };
- slice
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(slice_patterns)]
-
-fn a() {
- let mut v = vec!(1, 2, 3);
- let vb: &mut [isize] = &mut v;
- match vb {
- [_a, tail..] => {
- v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
- }
- _ => {}
- };
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(slice_patterns)]
-
-fn main() {
- let mut a = [1, 2, 3, 4];
- let t = match a {
- [1, 2, tail..] => tail,
- _ => unreachable!()
- };
- println!("t[0]: {}", t[0]);
- a[2] = 0; //~ ERROR cannot assign to `a[..]` because it is borrowed
- println!("t[0]: {}", t[0]);
- t[0];
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(advanced_slice_patterns)]
-#![feature(box_patterns)]
-#![feature(box_syntax)]
-#![feature(slice_patterns)]
-
-fn a() {
- let mut vec = [box 1, box 2, box 3];
- match vec {
- [box ref _a, _, _] => {
- vec[0] = box 4; //~ ERROR cannot assign
- }
- }
-}
-
-fn b() {
- let mut vec = vec!(box 1, box 2, box 3);
- let vec: &mut [Box<isize>] = &mut vec;
- match vec {
- [_b..] => {
- vec[0] = box 4; //~ ERROR cannot assign
- }
- }
-}
-
-fn c() {
- let mut vec = vec!(box 1, box 2, box 3);
- let vec: &mut [Box<isize>] = &mut vec;
- match vec {
- [_a, //~ ERROR cannot move out
- _b..] => { //~^ NOTE attempting to move value to here
-
- // Note: `_a` is *moved* here, but `b` is borrowing,
- // hence illegal.
- //
- // See comment in middle/borrowck/gather_loans/mod.rs
- // in the case covering these sorts of vectors.
- }
- _ => {}
- }
- let a = vec[0]; //~ ERROR cannot move out
-}
-
-fn d() {
- let mut vec = vec!(box 1, box 2, box 3);
- let vec: &mut [Box<isize>] = &mut vec;
- match vec {
- [_a.., //~ ERROR cannot move out
- _b] => {} //~ NOTE attempting to move value to here
- _ => {}
- }
- let a = vec[0]; //~ ERROR cannot move out
-}
-
-fn e() {
- let mut vec = vec!(box 1, box 2, box 3);
- let vec: &mut [Box<isize>] = &mut vec;
- match vec {
- [_a, _b, _c] => {} //~ ERROR cannot move out
- //~^ NOTE attempting to move value to here
- //~^^ NOTE and here
- //~^^^ NOTE and here
- _ => {}
- }
- let a = vec[0]; //~ ERROR cannot move out
- //~^ NOTE attempting to move value to here
-}
-
-fn main() {}
+++ /dev/null
-// 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.
-//
-// 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.
-
-#![feature(slice_patterns)]
-
-fn a<'a>() -> &'a isize {
- let vec = vec!(1, 2, 3, 4);
- let vec: &[isize] = &vec; //~ ERROR `vec` does not live long enough
- let tail = match vec {
- [_a, tail..] => &tail[0],
- _ => panic!("foo")
- };
- tail
-}
-
-fn main() {
- let fifth = a();
- println!("{}", *fifth);
-}
+++ /dev/null
-// 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 test(cond: bool) {
- let v;
- while cond {
- v = 3;
- break;
- }
- println!("{}", v); //~ ERROR use of possibly uninitialized variable: `v`
-}
-
-fn main() {
- test(true);
-}
+++ /dev/null
-// 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 main() {
- let x: bool;
- while x { } //~ ERROR use of possibly uninitialized variable: `x`
-}
+++ /dev/null
-// 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() -> isize {
- let mut x: isize;
- while 1 == 1 { x = 10; }
- return x; //~ ERROR use of possibly uninitialized variable: `x`
-}
-
-fn main() { f(); }
--- /dev/null
+// 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 main() {
+ let i: isize;
+
+ println!("{}", false && { i = 5; true });
+ println!("{}", i); //~ ERROR use of possibly uninitialized variable: `i`
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that we are able to distinguish when loans borrow different
+// anonymous fields of a tuple vs the same anonymous field.
+
+struct Y(usize, usize);
+
+fn distinct_variant() {
+ let mut y = Y(1, 2);
+
+ let a = match y {
+ Y(ref mut a, _) => a
+ };
+
+ let b = match y {
+ Y(_, ref mut b) => b
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn same_variant() {
+ let mut y = Y(1, 2);
+
+ let a = match y {
+ Y(ref mut a, _) => a
+ };
+
+ let b = match y {
+ Y(ref mut b, _) => b //~ ERROR cannot borrow
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that we are able to distinguish when loans borrow different
+// anonymous fields of a tuple vs the same anonymous field.
+
+fn distinct_variant() {
+ let mut y = (1, 2);
+
+ let a = match y {
+ (ref mut a, _) => a
+ };
+
+ let b = match y {
+ (_, ref mut b) => b
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn same_variant() {
+ let mut y = (1, 2);
+
+ let a = match y {
+ (ref mut a, _) => a
+ };
+
+ let b = match y {
+ (ref mut b, _) => b //~ ERROR cannot borrow
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that we are able to distinguish when loans borrow different
+// anonymous fields of an enum variant vs the same anonymous field.
+
+enum Foo {
+ X, Y(usize, usize)
+}
+
+fn distinct_variant() {
+ let mut y = Foo::Y(1, 2);
+
+ let a = match y {
+ Foo::Y(ref mut a, _) => a,
+ Foo::X => panic!()
+ };
+
+ let b = match y {
+ Foo::Y(_, ref mut b) => b,
+ Foo::X => panic!()
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn same_variant() {
+ let mut y = Foo::Y(1, 2);
+
+ let a = match y {
+ Foo::Y(ref mut a, _) => a,
+ Foo::X => panic!()
+ };
+
+ let b = match y {
+ Foo::Y(ref mut b, _) => b, //~ ERROR cannot borrow
+ Foo::X => panic!()
+ };
+
+ *a += 1;
+ *b += 1;
+}
+
+fn main() {
+}
--- /dev/null
+// Copyright 2015 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.
+
+#[derive(Copy, Clone)]
+struct S;
+
+impl S {
+ fn mutate(&mut self) {
+ }
+}
+
+fn func(arg: S) {
+ arg.mutate(); //~ ERROR: cannot borrow immutable argument
+}
+
+impl S {
+ fn method(&self, arg: S) {
+ arg.mutate(); //~ ERROR: cannot borrow immutable argument
+ }
+}
+
+trait T {
+ fn default(&self, arg: S) {
+ arg.mutate(); //~ ERROR: cannot borrow immutable argument
+ }
+}
+
+impl T for S {}
+
+fn main() {
+ let s = S;
+ func(s);
+ s.method(s);
+ s.default(s);
+ (|arg: S| { arg.mutate() })(s); //~ ERROR: cannot borrow immutable argument
+}
--- /dev/null
+// 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.
+
+struct Point {
+ x: isize,
+ y: isize,
+}
+
+fn a() {
+ let mut p = vec!(1);
+
+ // Create an immutable pointer into p's contents:
+ let q: &isize = &p[0];
+
+ p[0] = 5; //~ ERROR cannot borrow
+
+ println!("{}", *q);
+}
+
+fn borrow<F>(_x: &[isize], _f: F) where F: FnOnce() {}
+
+fn b() {
+ // here we alias the mutable vector into an imm slice and try to
+ // modify the original:
+
+ let mut p = vec!(1);
+
+ borrow(
+ &p,
+ || p[0] = 5); //~ ERROR cannot borrow `p` as mutable
+}
+
+fn c() {
+ // Legal because the scope of the borrow does not include the
+ // modification:
+ let mut p = vec!(1);
+ borrow(&p, ||{});
+ p[0] = 5;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+struct point { x: isize, y: isize }
+
+fn a() {
+ let mut p = point {x: 3, y: 4};
+ let q = &p;
+
+ // This assignment is illegal because the field x is not
+ // inherently mutable; since `p` was made immutable, `p.x` is now
+ // immutable. Otherwise the type of &_q.x (&isize) would be wrong.
+ p.x = 5; //~ ERROR cannot assign to `p.x`
+ q.x;
+}
+
+fn c() {
+ // this is sort of the opposite. We take a loan to the interior of `p`
+ // and then try to overwrite `p` as a whole.
+
+ let mut p = point {x: 3, y: 4};
+ let q = &p.y;
+ p = point {x: 5, y: 7};//~ ERROR cannot assign to `p`
+ p.x; // silence warning
+ *q; // stretch loan
+}
+
+fn d() {
+ // just for completeness's sake, the easy case, where we take the
+ // address of a subcomponent and then modify that subcomponent:
+
+ let mut p = point {x: 3, y: 4};
+ let q = &p.y;
+ p.y = 5; //~ ERROR cannot assign to `p.y`
+ *q;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+// Test that assignments to an `&mut` pointer which is found in a
+// borrowed (but otherwise non-aliasable) location is illegal.
+
+struct S<'a> {
+ pointer: &'a mut isize
+}
+
+fn a(s: &S) {
+ *s.pointer += 1; //~ ERROR cannot assign
+}
+
+fn b(s: &mut S) {
+ *s.pointer += 1;
+}
+
+fn c(s: & &mut S) {
+ *s.pointer += 1; //~ ERROR cannot assign
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+// Test that assignments to an `&mut` pointer which is found in a
+// borrowed (but otherwise non-aliasable) location is illegal.
+
+struct S<'a> {
+ pointer: &'a mut isize
+}
+
+fn copy_borrowed_ptr<'a>(p: &'a mut S<'a>) -> S<'a> {
+ S { pointer: &mut *p.pointer }
+}
+
+fn main() {
+ let mut x = 1;
+
+ {
+ let mut y = S { pointer: &mut x };
+ let z = copy_borrowed_ptr(&mut y);
+ *y.pointer += 1; //~ ERROR cannot assign
+ *z.pointer += 1;
+ }
+}
--- /dev/null
+// 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.
+
+static foo: isize = 5;
+
+fn main() {
+ // assigning to various global constants
+ foo = 6; //~ ERROR cannot assign to immutable static item
+}
--- /dev/null
+// 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.
+
+// Tests that auto-ref can't create mutable aliases to immutable memory.
+
+struct Foo {
+ x: isize
+}
+
+impl Foo {
+ pub fn printme(&mut self) {
+ println!("{}", self.x);
+ }
+}
+
+fn main() {
+ let x = Foo { x: 3 };
+ x.printme(); //~ ERROR cannot borrow
+}
--- /dev/null
+// 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.
+
+enum Either<T, U> { Left(T), Right(U) }
+
+struct X(Either<(usize,usize), fn()>);
+
+impl X {
+ pub fn with<F>(&self, blk: F) where F: FnOnce(&Either<(usize, usize), fn()>) {
+ let X(ref e) = *self;
+ blk(e)
+ }
+}
+
+fn main() {
+ let mut x = X(Either::Right(main as fn()));
+ (&mut x).with(
+ |opt| { //~ ERROR cannot borrow `x` as mutable more than once at a time
+ match opt {
+ &Either::Right(ref f) => {
+ x = X(Either::Left((0, 0)));
+ (*f)()
+ },
+ _ => panic!()
+ }
+ })
+}
--- /dev/null
+// 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.
+
+// Test that we detect nested calls that could free pointers evaluated
+// for earlier arguments.
+
+#![feature(box_syntax)]
+
+fn rewrite(v: &mut Box<usize>) -> usize {
+ *v = box 22;
+ **v
+}
+
+fn add(v: &usize, w: usize) -> usize {
+ *v + w
+}
+
+fn implicit() {
+ let mut a: Box<_> = box 1;
+
+ // Note the danger here:
+ //
+ // the pointer for the first argument has already been
+ // evaluated, but it gets freed when evaluating the second
+ // argument!
+ add(
+ &*a,
+ rewrite(&mut a)); //~ ERROR cannot borrow
+}
+
+fn explicit() {
+ let mut a: Box<_> = box 1;
+ add(
+ &*a,
+ rewrite(&mut a)); //~ ERROR cannot borrow
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+// Test that we detect nested calls that could free pointers evaluated
+// for earlier arguments.
+
+#![feature(box_syntax)]
+
+fn rewrite(v: &mut Box<usize>) -> usize {
+ *v = box 22;
+ **v
+}
+
+fn add(v: &usize, w: Box<usize>) -> usize {
+ *v + *w
+}
+
+fn implicit() {
+ let mut a: Box<_> = box 1;
+
+ // Note the danger here:
+ //
+ // the pointer for the first argument has already been
+ // evaluated, but it gets moved when evaluating the second
+ // argument!
+ add(
+ &*a,
+ a); //~ ERROR cannot move
+}
+
+fn explicit() {
+ let mut a: Box<_> = box 1;
+ add(
+ &*a,
+ a); //~ ERROR cannot move
+}
+
+fn main() {}
--- /dev/null
+// 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 force<F>(f: F) where F: FnOnce() { f(); }
+fn main() {
+ let x: isize;
+ force(|| { //~ ERROR capture of possibly uninitialized variable: `x`
+ println!("{}", x);
+ });
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+
+#[derive(Copy, Clone)]
+struct Foo {
+ bar1: Bar,
+ bar2: Bar
+}
+
+#[derive(Copy, Clone)]
+struct Bar {
+ int1: isize,
+ int2: isize,
+}
+
+fn make_foo() -> Box<Foo> { panic!() }
+
+fn borrow_same_field_twice_mut_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_mut_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _bar2 = &foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_imm_mut() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_imm_imm() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1;
+ let _bar2 = &foo.bar1;
+ *bar1;
+}
+
+fn borrow_both_fields_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ 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
+ }
+}
+
+fn borrow_var_and_pattern() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ match *foo {
+ Foo { bar1: ref mut _bar1, bar2: _ } => {}
+ //~^ ERROR cannot borrow
+ }
+ *bar1;
+}
+
+fn borrow_mut_and_base_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo1 = &foo.bar1; //~ ERROR cannot borrow
+ let _foo2 = &*foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_mut_and_base_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_mut_and_base_mut2() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo2 = &mut *foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_mut() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_mut2() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo2 = &mut *foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_imm() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo1 = &foo.bar1;
+ let _foo2 = &*foo;
+ *bar1;
+}
+
+fn borrow_mut_and_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _foo1 = &foo.bar2; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_mut_from_imm() {
+ let foo = make_foo();
+ let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_long_path_both_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let foo1 = &mut foo.bar2.int2; //~ ERROR cannot borrow
+ *bar1;
+ *foo1;
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+#[derive(Copy, Clone)]
+struct Foo {
+ bar1: Bar,
+ bar2: Bar
+}
+
+#[derive(Copy, Clone)]
+struct Bar {
+ int1: isize,
+ int2: isize,
+}
+
+fn make_foo() -> Foo { panic!() }
+
+fn borrow_same_field_twice_mut_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_mut_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _bar2 = &foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_imm_mut() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_same_field_twice_imm_imm() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1;
+ let _bar2 = &foo.bar1;
+ *bar1;
+}
+
+fn borrow_both_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _bar2 = &mut foo.bar2;
+ *bar1;
+}
+
+fn borrow_both_mut_pattern() {
+ let mut foo = make_foo();
+ match foo {
+ Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
+ }
+}
+
+fn borrow_var_and_pattern() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ match foo {
+ Foo { bar1: ref mut _bar1, bar2: _ } => {} //
+ //~^ ERROR cannot borrow
+ }
+ *bar1;
+}
+
+fn borrow_mut_and_base_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo1 = &foo.bar1; //~ ERROR cannot borrow
+ let _foo2 = &foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_mut_and_base_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_mut_and_base_mut2() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo2 = &mut foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_mut() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_mut2() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo2 = &mut foo; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_imm_and_base_imm() {
+ let mut foo = make_foo();
+ let bar1 = &foo.bar1.int1;
+ let _foo1 = &foo.bar1;
+ let _foo2 = &foo;
+ *bar1;
+}
+
+fn borrow_mut_and_imm() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1;
+ let _foo1 = &foo.bar2;
+ *bar1;
+}
+
+fn borrow_mut_from_imm() {
+ let foo = make_foo();
+ let bar1 = &mut foo.bar1; //~ ERROR cannot borrow
+ *bar1;
+}
+
+fn borrow_long_path_both_mut() {
+ let mut foo = make_foo();
+ let bar1 = &mut foo.bar1.int1;
+ let _foo1 = &mut foo.bar2.int2;
+ *bar1;
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test lifetimes are linked properly when we take reference
+// to interior.
+
+struct Foo(isize);
+
+fn foo<'a>() -> &'a isize {
+ let &Foo(ref x) = &Foo(3); //~ ERROR borrowed value does not live long enough
+ x
+}
+
+pub fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+struct A;
+
+impl A {
+ fn foo(&mut self) {
+ }
+}
+
+pub fn main() {
+ let a: Box<_> = box A;
+ a.foo();
+ //~^ ERROR cannot borrow immutable `Box` content `*a` as mutable
+}
--- /dev/null
+// 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.
+
+// Test that attempt to reborrow an `&mut` pointer in an aliasable
+// location yields an error.
+//
+// Example from src/librustc_borrowck/borrowck/README.md
+
+fn foo(t0: & &mut isize) {
+ let t1 = t0;
+ let p: &isize = &**t0;
+ **t1 = 22; //~ ERROR cannot assign
+}
+
+fn foo3(t0: &mut &mut isize) {
+ let t1 = &mut *t0;
+ let p: &isize = &**t0; //~ ERROR cannot borrow
+ **t1 = 22;
+}
+
+fn foo4(t0: & &mut isize) {
+ let x: &mut isize = &mut **t0; //~ ERROR cannot borrow
+ *x += 1;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+// Check that `&mut` objects cannot be borrowed twice, just like
+// other `&mut` pointers.
+
+trait Foo {
+ fn f1(&mut self) -> &();
+ fn f2(&mut self);
+}
+
+fn test(x: &mut Foo) {
+ let _y = x.f1();
+ x.f2(); //~ ERROR cannot borrow `*x` as mutable
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test how overloaded deref interacts with borrows when DerefMut
+// is implemented.
+
+use std::ops::{Deref, DerefMut};
+
+struct Own<T> {
+ value: *mut T
+}
+
+impl<T> Deref for Own<T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.value }
+ }
+}
+
+impl<T> DerefMut for Own<T> {
+ fn deref_mut(&mut self) -> &mut T {
+ unsafe { &mut *self.value }
+ }
+}
+
+struct Point {
+ x: isize,
+ y: isize
+}
+
+impl Point {
+ fn get(&self) -> (isize, isize) {
+ (self.x, self.y)
+ }
+
+ fn set(&mut self, x: isize, y: isize) {
+ self.x = x;
+ self.y = y;
+ }
+
+ fn x_ref(&self) -> &isize {
+ &self.x
+ }
+
+ fn y_mut(&mut self) -> &mut isize {
+ &mut self.y
+ }
+}
+
+fn deref_imm_field(x: Own<Point>) {
+ let __isize = &x.y;
+}
+
+fn deref_mut_field1(x: Own<Point>) {
+ let __isize = &mut x.y; //~ ERROR cannot borrow
+}
+
+fn deref_mut_field2(mut x: Own<Point>) {
+ let __isize = &mut x.y;
+}
+
+fn deref_extend_field(x: &Own<Point>) -> &isize {
+ &x.y
+}
+
+fn deref_extend_mut_field1(x: &Own<Point>) -> &mut isize {
+ &mut x.y //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut_field2(x: &mut Own<Point>) -> &mut isize {
+ &mut x.y
+}
+
+fn deref_extend_mut_field3(x: &mut Own<Point>) {
+ // Hmm, this is unfortunate, because with box it would work,
+ // but it's presently the expected outcome. See `deref_extend_mut_field4`
+ // for the workaround.
+
+ let _x = &mut x.x;
+ let _y = &mut x.y; //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut_field4<'a>(x: &'a mut Own<Point>) {
+ let p = &mut **x;
+ let _x = &mut p.x;
+ let _y = &mut p.y;
+}
+
+fn assign_field1<'a>(x: Own<Point>) {
+ x.y = 3; //~ ERROR cannot borrow
+}
+
+fn assign_field2<'a>(x: &'a Own<Point>) {
+ x.y = 3; //~ ERROR cannot assign
+}
+
+fn assign_field3<'a>(x: &'a mut Own<Point>) {
+ x.y = 3;
+}
+
+fn assign_field4<'a>(x: &'a mut Own<Point>) {
+ let _p: &mut Point = &mut **x;
+ x.y = 3; //~ ERROR cannot borrow
+}
+
+fn deref_imm_method(x: Own<Point>) {
+ let __isize = x.get();
+}
+
+fn deref_mut_method1(x: Own<Point>) {
+ x.set(0, 0); //~ ERROR cannot borrow
+}
+
+fn deref_mut_method2(mut x: Own<Point>) {
+ x.set(0, 0);
+}
+
+fn deref_extend_method(x: &Own<Point>) -> &isize {
+ x.x_ref()
+}
+
+fn deref_extend_mut_method1(x: &Own<Point>) -> &mut isize {
+ x.y_mut() //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut_method2(x: &mut Own<Point>) -> &mut isize {
+ x.y_mut()
+}
+
+fn assign_method1<'a>(x: Own<Point>) {
+ *x.y_mut() = 3; //~ ERROR cannot borrow
+}
+
+fn assign_method2<'a>(x: &'a Own<Point>) {
+ *x.y_mut() = 3; //~ ERROR cannot borrow
+}
+
+fn assign_method3<'a>(x: &'a mut Own<Point>) {
+ *x.y_mut() = 3;
+}
+
+pub fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test how overloaded deref interacts with borrows when only
+// Deref and not DerefMut is implemented.
+
+use std::ops::Deref;
+
+struct Rc<T> {
+ value: *const T
+}
+
+impl<T> Deref for Rc<T> {
+ type Target = T;
+
+ fn deref(&self) -> &T {
+ unsafe { &*self.value }
+ }
+}
+
+struct Point {
+ x: isize,
+ y: isize
+}
+
+impl Point {
+ fn get(&self) -> (isize, isize) {
+ (self.x, self.y)
+ }
+
+ fn set(&mut self, x: isize, y: isize) {
+ self.x = x;
+ self.y = y;
+ }
+
+ fn x_ref(&self) -> &isize {
+ &self.x
+ }
+
+ fn y_mut(&mut self) -> &mut isize {
+ &mut self.y
+ }
+}
+
+fn deref_imm_field(x: Rc<Point>) {
+ let __isize = &x.y;
+}
+
+fn deref_mut_field1(x: Rc<Point>) {
+ let __isize = &mut x.y; //~ ERROR cannot borrow
+}
+
+fn deref_mut_field2(mut x: Rc<Point>) {
+ let __isize = &mut x.y; //~ ERROR cannot borrow
+}
+
+fn deref_extend_field(x: &Rc<Point>) -> &isize {
+ &x.y
+}
+
+fn deref_extend_mut_field1(x: &Rc<Point>) -> &mut isize {
+ &mut x.y //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut_field2(x: &mut Rc<Point>) -> &mut isize {
+ &mut x.y //~ ERROR cannot borrow
+}
+
+fn assign_field1<'a>(x: Rc<Point>) {
+ x.y = 3; //~ ERROR cannot assign
+}
+
+fn assign_field2<'a>(x: &'a Rc<Point>) {
+ x.y = 3; //~ ERROR cannot assign
+}
+
+fn assign_field3<'a>(x: &'a mut Rc<Point>) {
+ x.y = 3; //~ ERROR cannot assign
+}
+
+fn deref_imm_method(x: Rc<Point>) {
+ let __isize = x.get();
+}
+
+fn deref_mut_method1(x: Rc<Point>) {
+ x.set(0, 0); //~ ERROR cannot borrow
+}
+
+fn deref_mut_method2(mut x: Rc<Point>) {
+ x.set(0, 0); //~ ERROR cannot borrow
+}
+
+fn deref_extend_method(x: &Rc<Point>) -> &isize {
+ x.x_ref()
+}
+
+fn deref_extend_mut_method1(x: &Rc<Point>) -> &mut isize {
+ x.y_mut() //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut_method2(x: &mut Rc<Point>) -> &mut isize {
+ x.y_mut() //~ ERROR cannot borrow
+}
+
+fn assign_method1<'a>(x: Rc<Point>) {
+ *x.y_mut() = 3; //~ ERROR cannot borrow
+}
+
+fn assign_method2<'a>(x: &'a Rc<Point>) {
+ *x.y_mut() = 3; //~ ERROR cannot borrow
+}
+
+fn assign_method3<'a>(x: &'a mut Rc<Point>) {
+ *x.y_mut() = 3; //~ ERROR cannot borrow
+}
+
+pub fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test how overloaded deref interacts with borrows when DerefMut
+// is implemented.
+
+use std::ops::{Deref, DerefMut};
+
+struct Own<T> {
+ value: *mut T
+}
+
+impl<T> Deref for Own<T> {
+ type Target = T;
+
+ fn deref<'a>(&'a self) -> &'a T {
+ unsafe { &*self.value }
+ }
+}
+
+impl<T> DerefMut for Own<T> {
+ fn deref_mut<'a>(&'a mut self) -> &'a mut T {
+ unsafe { &mut *self.value }
+ }
+}
+
+fn deref_imm(x: Own<isize>) {
+ let __isize = &*x;
+}
+
+fn deref_mut1(x: Own<isize>) {
+ let __isize = &mut *x; //~ ERROR cannot borrow
+}
+
+fn deref_mut2(mut x: Own<isize>) {
+ let __isize = &mut *x;
+}
+
+fn deref_extend<'a>(x: &'a Own<isize>) -> &'a isize {
+ &**x
+}
+
+fn deref_extend_mut1<'a>(x: &'a Own<isize>) -> &'a mut isize {
+ &mut **x //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut2<'a>(x: &'a mut Own<isize>) -> &'a mut isize {
+ &mut **x
+}
+
+fn assign1<'a>(x: Own<isize>) {
+ *x = 3; //~ ERROR cannot borrow
+}
+
+fn assign2<'a>(x: &'a Own<isize>) {
+ **x = 3; //~ ERROR cannot borrow
+}
+
+fn assign3<'a>(x: &'a mut Own<isize>) {
+ **x = 3;
+}
+
+pub fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test how overloaded deref interacts with borrows when only
+// Deref and not DerefMut is implemented.
+
+use std::ops::Deref;
+
+struct Rc<T> {
+ value: *const T
+}
+
+impl<T> Deref for Rc<T> {
+ type Target = T;
+
+ fn deref<'a>(&'a self) -> &'a T {
+ unsafe { &*self.value }
+ }
+}
+
+fn deref_imm(x: Rc<isize>) {
+ let __isize = &*x;
+}
+
+fn deref_mut1(x: Rc<isize>) {
+ let __isize = &mut *x; //~ ERROR cannot borrow
+}
+
+fn deref_mut2(mut x: Rc<isize>) {
+ let __isize = &mut *x; //~ ERROR cannot borrow
+}
+
+fn deref_extend<'a>(x: &'a Rc<isize>) -> &'a isize {
+ &**x
+}
+
+fn deref_extend_mut1<'a>(x: &'a Rc<isize>) -> &'a mut isize {
+ &mut **x //~ ERROR cannot borrow
+}
+
+fn deref_extend_mut2<'a>(x: &'a mut Rc<isize>) -> &'a mut isize {
+ &mut **x //~ ERROR cannot borrow
+}
+
+fn assign1<'a>(x: Rc<isize>) {
+ *x = 3; //~ ERROR cannot assign
+}
+
+fn assign2<'a>(x: &'a Rc<isize>) {
+ **x = 3; //~ ERROR cannot assign
+}
+
+fn assign3<'a>(x: &'a mut Rc<isize>) {
+ **x = 3; //~ ERROR cannot assign
+}
+
+pub fn main() {}
--- /dev/null
+// 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.
+
+struct defer<'a> {
+ x: &'a [&'a str],
+}
+
+impl<'a> Drop for defer<'a> {
+ fn drop(&mut self) {
+ unsafe {
+ println!("{:?}", self.x);
+ }
+ }
+}
+
+fn defer<'r>(x: &'r [&'r str]) -> defer<'r> {
+ defer {
+ x: x
+ }
+}
+
+fn main() {
+ let x = defer(&vec!("Goodbye", "world!"));
+ //~^ ERROR borrowed value does not live long enough
+ x.x[0];
+}
--- /dev/null
+// 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.
+
+//buggy.rs
+
+#![feature(box_syntax)]
+
+extern crate collections;
+use std::collections::HashMap;
+
+fn main() {
+ let tmp: Box<_>;
+ let mut buggy_map: HashMap<usize, &usize> = HashMap::new();
+ // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
+ buggy_map.insert(42, &*Box::new(1)); //~ ERROR borrowed value does not live long enough
+
+ // but it is ok if we use a temporary
+ tmp = box 2;
+ buggy_map.insert(43, &*tmp);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+struct A {
+ x: Box<isize>,
+ y: isize,
+}
+
+struct B {
+ x: Box<isize>,
+ y: Box<isize>,
+}
+
+struct C {
+ x: Box<A>,
+ y: isize,
+}
+
+struct D {
+ x: Box<A>,
+ y: Box<isize>,
+}
+
+fn copy_after_move() {
+ let a: Box<_> = box A { x: box 0, y: 1 };
+ let _x = a.x;
+ let _y = a.y; //~ ERROR use of moved
+ //~^^ NOTE `a` moved here (through moving `a.x`)
+}
+
+fn move_after_move() {
+ let a: Box<_> = box B { x: box 0, y: box 1 };
+ let _x = a.x;
+ let _y = a.y; //~ ERROR use of moved
+ //~^^ NOTE `a` moved here (through moving `a.x`)
+}
+
+fn borrow_after_move() {
+ let a: Box<_> = box A { x: box 0, y: 1 };
+ let _x = a.x;
+ let _y = &a.y; //~ ERROR use of moved
+ //~^^ NOTE `a` moved here (through moving `a.x`)
+}
+
+fn move_after_borrow() {
+ let a: Box<_> = box B { x: box 0, y: box 1 };
+ let _x = &a.x;
+ let _y = a.y; //~ ERROR cannot move
+}
+
+fn copy_after_mut_borrow() {
+ let mut a: Box<_> = box A { x: box 0, y: 1 };
+ let _x = &mut a.x;
+ let _y = a.y; //~ ERROR cannot use
+}
+
+fn move_after_mut_borrow() {
+ let mut a: Box<_> = box B { x: box 0, y: box 1 };
+ let _x = &mut a.x;
+ let _y = a.y; //~ ERROR cannot move
+}
+
+fn borrow_after_mut_borrow() {
+ let mut a: Box<_> = box A { x: box 0, y: 1 };
+ let _x = &mut a.x;
+ let _y = &a.y; //~ ERROR cannot borrow
+}
+
+fn mut_borrow_after_borrow() {
+ let mut a: Box<_> = box A { x: box 0, y: 1 };
+ let _x = &a.x;
+ let _y = &mut a.y; //~ ERROR cannot borrow
+}
+
+fn copy_after_move_nested() {
+ let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
+ let _x = a.x.x;
+ let _y = a.y; //~ ERROR use of collaterally moved
+}
+
+fn move_after_move_nested() {
+ let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
+ let _x = a.x.x;
+ let _y = a.y; //~ ERROR use of collaterally moved
+}
+
+fn borrow_after_move_nested() {
+ let a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
+ let _x = a.x.x;
+ let _y = &a.y; //~ ERROR use of collaterally moved
+}
+
+fn move_after_borrow_nested() {
+ let a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
+ let _x = &a.x.x;
+ let _y = a.y; //~ ERROR cannot move
+}
+
+fn copy_after_mut_borrow_nested() {
+ let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
+ let _x = &mut a.x.x;
+ let _y = a.y; //~ ERROR cannot use
+}
+
+fn move_after_mut_borrow_nested() {
+ let mut a: Box<_> = box D { x: box A { x: box 0, y: 1 }, y: box 2 };
+ let _x = &mut a.x.x;
+ let _y = a.y; //~ ERROR cannot move
+}
+
+fn borrow_after_mut_borrow_nested() {
+ let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
+ let _x = &mut a.x.x;
+ let _y = &a.y; //~ ERROR cannot borrow
+}
+
+fn mut_borrow_after_borrow_nested() {
+ let mut a: Box<_> = box C { x: box A { x: box 0, y: 1 }, y: 2 };
+ let _x = &a.x.x;
+ let _y = &mut a.y; //~ ERROR cannot borrow
+}
+
+fn main() {
+ copy_after_move();
+ move_after_move();
+ borrow_after_move();
+
+ move_after_borrow();
+
+ copy_after_mut_borrow();
+ move_after_mut_borrow();
+ borrow_after_mut_borrow();
+ mut_borrow_after_borrow();
+
+ copy_after_move_nested();
+ move_after_move_nested();
+ borrow_after_move_nested();
+
+ move_after_borrow_nested();
+
+ copy_after_mut_borrow_nested();
+ move_after_mut_borrow_nested();
+ borrow_after_mut_borrow_nested();
+ mut_borrow_after_borrow_nested();
+}
--- /dev/null
+// 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 foo() -> isize {
+ let x: isize;
+
+ while 1 != 2 {
+ break;
+ x = 0;
+ }
+
+ println!("{}", x); //~ ERROR use of possibly uninitialized variable: `x`
+
+ return 17;
+}
+
+fn main() { println!("{}", foo()); }
--- /dev/null
+// 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 foo() -> isize {
+ let x: isize;
+
+ loop {
+ break;
+ x = 0;
+ }
+
+ println!("{}", x); //~ ERROR use of possibly uninitialized variable: `x`
+
+ return 17;
+}
+
+fn main() { println!("{}", foo()); }
--- /dev/null
+// 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.
+//
+// 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.
+
+// Ensure that invoking a closure counts as a unique immutable borrow
+
+#![feature(unboxed_closures)]
+
+type Fn<'a> = Box<FnMut() + 'a>;
+
+struct Test<'a> {
+ f: Box<FnMut() + 'a>
+}
+
+// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
+fn call<F>(mut f: F) where F: FnMut(Fn) {
+ f(Box::new(|| {
+ //~^ ERROR: cannot borrow `f` as mutable more than once
+ f((Box::new(|| {})))
+ }));
+}
+
+fn test1() {
+ call(|mut a| {
+ a.call_mut(());
+ });
+}
+
+fn test2<F>(f: &F) where F: FnMut() {
+ (*f)(); //~ ERROR: cannot borrow immutable borrowed content `*f` as mutable
+}
+
+fn test3<F>(f: &mut F) where F: FnMut() {
+ (*f)();
+}
+
+fn test4(f: &Test) {
+ f.f.call_mut(()) //~ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
+}
+
+fn test5(f: &mut Test) {
+ f.f.call_mut(())
+}
+
+fn test6() {
+ let mut f = || {};
+ (|| {
+ f();
+ })();
+}
+
+fn test7() {
+ fn foo<F>(_: F) where F: FnMut(Box<FnMut(isize)>, isize) {}
+ let mut f = |g: Box<FnMut(isize)>, b: isize| {};
+ // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
+ f(Box::new(|a| {
+ foo(f);
+ //~^ ERROR cannot move `f` into closure because it is borrowed
+ //~| ERROR cannot move out of captured outer variable in an `FnMut` closure
+ }), 3);
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+struct Foo {
+ x: isize,
+}
+
+impl Foo {
+ pub fn f(&self) {}
+ pub fn h(&mut self) {}
+}
+
+fn a(x: &mut Foo) {
+ x.f();
+ x.h();
+}
+
+fn b(x: &Foo) {
+ x.f();
+ x.h(); //~ ERROR cannot borrow
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that two closures cannot simultaneously have mutable
+// and immutable access to the variable. Issue #6801.
+
+#![feature(box_syntax)]
+
+fn get(x: &isize) -> isize {
+ *x
+}
+
+fn set(x: &mut isize) {
+ *x = 4;
+}
+
+fn a() {
+ let mut x = 3;
+ let c1 = || x = 4;
+ let c2 = || x * 5; //~ ERROR cannot borrow `x`
+}
+
+fn b() {
+ let mut x = 3;
+ let c1 = || set(&mut x);
+ let c2 = || get(&x); //~ ERROR cannot borrow `x`
+}
+
+fn c() {
+ let mut x = 3;
+ let c1 = || set(&mut x);
+ let c2 = || x * 5; //~ ERROR cannot borrow `x`
+}
+
+fn d() {
+ let mut x = 3;
+ let c2 = || x * 5;
+ x = 5; //~ ERROR cannot assign
+}
+
+fn e() {
+ let mut x = 3;
+ let c1 = || get(&x);
+ x = 5; //~ ERROR cannot assign
+}
+
+fn f() {
+ let mut x: Box<_> = box 3;
+ let c1 = || get(&*x);
+ *x = 5; //~ ERROR cannot assign
+}
+
+fn g() {
+ struct Foo {
+ f: Box<isize>
+ }
+
+ let mut x: Box<_> = box Foo { f: box 3 };
+ let c1 = || get(&*x.f);
+ *x.f = 5; //~ ERROR cannot assign to `*x.f`
+}
+
+fn h() {
+ struct Foo {
+ f: Box<isize>
+ }
+
+ let mut x: Box<_> = box Foo { f: box 3 };
+ let c1 = || get(&*x.f);
+ let c2 = || *x.f = 5; //~ ERROR cannot borrow `x` as mutable
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that two closures cannot simultaneously have mutable
+// and immutable access to the variable. Issue #6801.
+
+fn get(x: &isize) -> isize {
+ *x
+}
+
+fn set(x: &mut isize) {
+ *x = 4;
+}
+
+fn a(x: &isize) {
+ let c1 = || set(&mut *x);
+ //~^ ERROR cannot borrow
+ let c2 = || set(&mut *x);
+ //~^ ERROR cannot borrow
+ //~| ERROR closure requires unique access
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that two closures cannot simultaneously have mutable
+// access to the variable, whether that mutable access be used
+// for direct assignment or for taking mutable ref. Issue #6801.
+
+#![feature(box_syntax)]
+
+fn to_fn_mut<F: FnMut()>(f: F) -> F { f }
+
+fn a() {
+ let mut x = 3;
+ let c1 = to_fn_mut(|| x = 4);
+ let c2 = to_fn_mut(|| x = 5); //~ ERROR cannot borrow `x` as mutable more than once
+}
+
+fn set(x: &mut isize) {
+ *x = 4;
+}
+
+fn b() {
+ let mut x = 3;
+ let c1 = to_fn_mut(|| set(&mut x));
+ let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
+}
+
+fn c() {
+ let mut x = 3;
+ let c1 = to_fn_mut(|| x = 5);
+ let c2 = to_fn_mut(|| set(&mut x)); //~ ERROR cannot borrow `x` as mutable more than once
+}
+
+fn d() {
+ let mut x = 3;
+ let c1 = to_fn_mut(|| x = 5);
+ let c2 = to_fn_mut(|| { let _y = to_fn_mut(|| set(&mut x)); }); // (nested closure)
+ //~^ ERROR cannot borrow `x` as mutable more than once
+}
+
+fn g() {
+ struct Foo {
+ f: Box<isize>
+ }
+
+ let mut x: Box<_> = box Foo { f: box 3 };
+ let c1 = to_fn_mut(|| set(&mut *x.f));
+ let c2 = to_fn_mut(|| set(&mut *x.f));
+ //~^ ERROR cannot borrow `x` as mutable more than once
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+struct Foo {
+ x: isize,
+}
+
+pub fn main() {
+ let mut this = &mut Foo {
+ x: 1,
+ };
+ let mut r = || {
+ let p = &this.x;
+ &mut this.x; //~ ERROR cannot borrow
+ };
+ r()
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that a closure which requires mutable access to the referent
+// of an `&mut` requires a "unique" borrow -- that is, the variable to
+// be borrowed (here, `x`) will not be borrowed *mutably*, but
+// may be *immutable*, but we cannot allow
+// multiple borrows.
+
+fn get(x: &isize) -> isize {
+ *x
+}
+
+fn set(x: &mut isize) -> isize {
+ *x
+}
+
+fn a(x: &mut isize) {
+ let c1 = || get(x);
+ let c2 = || get(x);
+}
+
+fn b(x: &mut isize) {
+ let c1 = || get(x);
+ let c2 = || set(x); //~ ERROR closure requires unique access to `x`
+}
+
+fn c(x: &mut isize) {
+ let c1 = || get(x);
+ let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
+}
+
+fn d(x: &mut isize) {
+ let c1 = || set(x);
+ let c2 = || set(x); //~ ERROR closure requires unique access to `x`
+}
+
+fn e(x: &mut isize) {
+ let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that a closure which mutates a local variable
+// cannot also be supplied a borrowed version of that
+// variable's contents. Issue #11192.
+
+#![feature(box_syntax)]
+
+struct Foo {
+ x: isize
+}
+
+impl Drop for Foo {
+ fn drop(&mut self) {
+ println!("drop {}", self.x);
+ }
+}
+
+fn main() {
+ let mut ptr: Box<_> = box Foo { x: 0 };
+ let mut test = |foo: &Foo| {
+ ptr = box Foo { x: ptr.x + 1 };
+ };
+ test(&*ptr); //~ ERROR cannot borrow `*ptr`
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Check that we report an error if an upcast box is moved twice.
+
+fn consume(_: Box<[i32]>) {
+}
+
+fn foo(b: Box<[i32;5]>) {
+ consume(b);
+ consume(b); //~ ERROR use of moved value
+}
+
+fn main() {
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Check that we report an error if an upcast box is moved twice.
+
+trait Foo { fn dummy(&self); }
+
+fn consume(_: Box<Foo>) {
+}
+
+fn foo(b: Box<Foo+Send>) {
+ consume(b);
+ consume(b); //~ ERROR use of moved value
+}
+
+fn main() {
+}
--- /dev/null
+// Copyright 2015 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.
+
+use std::thread::spawn;
+
+// Test that we give a custom error (E0373) for the case where a
+// closure is escaping current frame, and offer a suggested code edit.
+// I refrained from including the precise message here, but the
+// original text as of the time of this writing is:
+//
+// closure may outlive the current function, but it borrows `books`,
+// which is owned by the current function
+
+fn main() {
+ let mut books = vec![1,2,3];
+ spawn(|| books.push(4));
+ //~^ ERROR E0373
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Test that we give a custom error (E0373) for the case where a
+// closure is escaping current frame, and offer a suggested code edit.
+// I refrained from including the precise message here, but the
+// original text as of the time of this writing is:
+//
+// closure may outlive the current function, but it borrows `books`,
+// which is owned by the current function
+
+fn foo<'a>(x: &'a i32) -> Box<FnMut()+'a> {
+ let mut books = vec![1,2,3];
+ Box::new(|| books.push(4))
+ //~^ ERROR E0373
+}
+
+fn main() { }
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+struct A { a: isize, b: Box<isize> }
+
+fn deref_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ drop(*x.b); //~ ERROR use of moved value: `*x.b`
+}
+
+fn deref_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let y = A { a: 3, .. x };
+ drop(*x.b); //~ ERROR use of moved value: `*x.b`
+}
+
+fn borrow_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ let p = &x.b; //~ ERROR use of moved value: `x.b`
+ drop(**p);
+}
+
+fn borrow_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ let p = &x.b; //~ ERROR use of moved value: `x.b`
+ drop(**p);
+}
+
+fn move_after_borrow() {
+ let x = A { a: 1, b: box 2 };
+ let p = &x.b;
+ drop(x.b); //~ ERROR cannot move out of `x.b` because it is borrowed
+ drop(**p);
+}
+
+fn fu_move_after_borrow() {
+ let x = A { a: 1, b: box 2 };
+ let p = &x.b;
+ let _y = A { a: 3, .. x }; //~ ERROR cannot move out of `x.b` because it is borrowed
+ drop(**p);
+}
+
+fn mut_borrow_after_mut_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.a;
+ let q = &mut x.a; //~ ERROR cannot borrow `x.a` as mutable more than once at a time
+ drop(*p);
+ drop(*q);
+}
+
+fn move_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ drop(x.b); //~ ERROR use of moved value: `x.b`
+}
+
+fn move_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ drop(x.b); //~ ERROR use of moved value: `x.b`
+}
+
+fn fu_move_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ let _z = A { a: 3, .. x }; //~ ERROR use of moved value: `x.b`
+}
+
+fn fu_move_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ let _z = A { a: 4, .. x }; //~ ERROR use of moved value: `x.b`
+}
+
+// The following functions aren't yet accepted, but they should be.
+
+fn copy_after_field_assign_after_uninit() {
+ let mut x: A;
+ x.a = 1;
+ drop(x.a); //~ ERROR use of possibly uninitialized variable: `x.a`
+}
+
+fn borrow_after_field_assign_after_uninit() {
+ let mut x: A;
+ x.a = 1;
+ let p = &x.a; //~ ERROR use of possibly uninitialized variable: `x.a`
+ drop(*p);
+}
+
+fn move_after_field_assign_after_uninit() {
+ let mut x: A;
+ x.b = box 1;
+ drop(x.b); //~ ERROR use of possibly uninitialized variable: `x.b`
+}
+
+fn main() {
+ deref_after_move();
+ deref_after_fu_move();
+
+ borrow_after_move();
+ borrow_after_fu_move();
+ move_after_borrow();
+ fu_move_after_borrow();
+ mut_borrow_after_mut_borrow();
+
+ move_after_move();
+ move_after_fu_move();
+ fu_move_after_move();
+ fu_move_after_fu_move();
+
+ copy_after_field_assign_after_uninit();
+ borrow_after_field_assign_after_uninit();
+ move_after_field_assign_after_uninit();
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Check that we check fns appearing in constant declarations.
+// Issue #22382.
+
+const MOVE: fn(&String) -> String = {
+ fn broken(x: &String) -> String {
+ return *x //~ ERROR cannot move
+ }
+ broken
+};
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Check that we check fns appearing in constant declarations.
+// Issue #22382.
+
+// How about mutating an immutable vector?
+const MUTATE: fn(&Vec<String>) = {
+ fn broken(x: &Vec<String>) {
+ x.push(format!("this is broken"));
+ //~^ ERROR cannot borrow
+ }
+ broken
+};
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Check that we check fns appearing in constant declarations.
+// Issue #22382.
+
+// Returning local references?
+struct DropString {
+ inner: String
+}
+impl Drop for DropString {
+ fn drop(&mut self) {
+ self.inner.clear();
+ self.inner.push_str("dropped");
+ }
+}
+const LOCAL_REF: fn() -> &'static str = {
+ fn broken() -> &'static str {
+ let local = DropString { inner: format!("Some local string") };
+ return &local.inner; //~ ERROR does not live long enough
+ }
+ broken
+};
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Issue #16205.
+
+#![feature(box_syntax)]
+
+struct Foo {
+ a: [Box<isize>; 3],
+}
+
+fn main() {
+ let mut y = 1;
+ let x = Some(&mut y);
+ for &a in x.iter() { //~ ERROR cannot move out
+ }
+
+ let f = Foo {
+ a: [box 3, box 4, box 5],
+ };
+ for &a in &f.a { //~ ERROR cannot move out
+ }
+
+ let x: Option<Box<_>> = Some(box 1);
+ for &a in x.iter() { //~ ERROR cannot move out
+ }
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+use std::iter::repeat;
+
+fn main() {
+ let mut vector = vec![1, 2];
+ for &x in &vector {
+ let cap = vector.capacity();
+ vector.extend(repeat(0)); //~ ERROR cannot borrow
+ vector[1] = 5; //~ ERROR cannot borrow
+ }
+}
--- /dev/null
+// 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 foo(x: isize) { println!("{}", x); }
+
+fn main() {
+ let x: isize; if 1 > 2 { x = 10; }
+ foo(x); //~ ERROR use of possibly uninitialized variable: `x`
+}
--- /dev/null
+// 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 foo(x: isize) { println!("{}", x); }
+
+fn main() {
+ let x: isize;
+ if 1 > 2 {
+ println!("whoops");
+ } else {
+ x = 10;
+ }
+ foo(x); //~ ERROR use of possibly uninitialized variable: `x`
+}
--- /dev/null
+// 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 main() {
+ let mut _a = 3;
+ let _b = &mut _a;
+ {
+ let _c = &*_b;
+ _a = 4; //~ ERROR cannot assign to `_a`
+ }
+}
--- /dev/null
+// Copyright 2015 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.
+
+// check that borrowck looks inside consts/statics
+
+static FN : &'static (Fn() -> (Box<Fn()->Box<i32>>) + Sync) = &|| {
+ let x = Box::new(0);
+ Box::new(|| x) //~ ERROR cannot move out of captured outer variable
+};
+
+fn main() {
+ let f = (FN)();
+ f();
+ f();
+}
--- /dev/null
+// 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 main() {
+ let j = || -> isize {
+ let i: isize;
+ i //~ ERROR use of possibly uninitialized variable: `i`
+ };
+ j();
+}
--- /dev/null
+// 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 main() {
+ let f = || -> isize {
+ let i: isize;
+ i //~ ERROR use of possibly uninitialized variable: `i`
+ };
+ println!("{}", f());
+}
--- /dev/null
+// 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.
+
+#[derive(Clone)]
+struct point {
+ x: isize,
+ y: isize,
+}
+
+fn main() {
+ let mut origin: point;
+ origin = point {x: 10,.. origin}; //~ ERROR use of possibly uninitialized variable: `origin.y`
+ origin.clone();
+}
--- /dev/null
+// 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 test() {
+ let v: isize;
+ v += 1; //~ ERROR use of possibly uninitialized variable: `v`
+ v.clone();
+}
+
+fn main() {
+}
--- /dev/null
+// 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 test() {
+ let mut v: isize;
+ v = v + 1; //~ ERROR use of possibly uninitialized variable: `v`
+ v.clone();
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+extern crate collections;
+use std::collections::HashSet;
+
+struct Foo {
+ n: HashSet<isize>,
+}
+
+impl Foo {
+ pub fn foo<F>(&mut self, mut fun: F) where F: FnMut(&isize) {
+ for f in &self.n {
+ fun(f);
+ }
+ }
+}
+
+fn bar(f: &mut Foo) {
+ f.foo(
+ |a| { //~ ERROR closure requires unique access to `f`
+ f.n.insert(*a);
+ })
+}
+
+fn main() {
+ let mut f = Foo { n: HashSet::new() };
+ bar(&mut f);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// This tests that we can't modify Box<&mut T> contents while they
+// are borrowed (#14498).
+//
+// Also includes tests of the errors reported when the Box in question
+// is immutable (#14270).
+
+#![feature(box_syntax)]
+
+struct A { a: isize }
+struct B<'a> { a: Box<&'a mut isize> }
+
+fn indirect_write_to_imm_box() {
+ let mut x: isize = 1;
+ let y: Box<_> = box &mut x;
+ let p = &y;
+ ***p = 2; //~ ERROR cannot assign to data in an immutable container
+ drop(p);
+}
+
+fn borrow_in_var_from_var() {
+ let mut x: isize = 1;
+ let mut y: Box<_> = box &mut x;
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_var_via_imm_box() {
+ let mut x: isize = 1;
+ let y: Box<_> = box &mut x;
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ //~^ ERROR cannot assign to data in an immutable container
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_field() {
+ let mut x = A { a: 1 };
+ let mut y: Box<_> = box &mut x.a;
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_var_from_field_via_imm_box() {
+ let mut x = A { a: 1 };
+ let y: Box<_> = box &mut x.a;
+ let p = &y;
+ let q = &***p;
+ **y = 2; //~ ERROR cannot assign to `**y` because it is borrowed
+ //~^ ERROR cannot assign to data in an immutable container
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_var() {
+ let mut x: isize = 1;
+ let mut y = B { a: box &mut x };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_var_via_imm_box() {
+ let mut x: isize = 1;
+ let y = B { a: box &mut x };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ //~^ ERROR cannot assign to data in an immutable container
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_field() {
+ let mut x = A { a: 1 };
+ let mut y = B { a: box &mut x.a };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ drop(p);
+ drop(q);
+}
+
+fn borrow_in_field_from_field_via_imm_box() {
+ let mut x = A { a: 1 };
+ let y = B { a: box &mut x.a };
+ let p = &y.a;
+ let q = &***p;
+ **y.a = 2; //~ ERROR cannot assign to `**y.a` because it is borrowed
+ //~^ ERROR cannot assign to data in an immutable container
+ drop(p);
+ drop(q);
+}
+
+fn main() {
+ indirect_write_to_imm_box();
+ borrow_in_var_from_var();
+ borrow_in_var_from_var_via_imm_box();
+ borrow_in_var_from_field();
+ borrow_in_var_from_field_via_imm_box();
+ borrow_in_field_from_var();
+ borrow_in_field_from_var_via_imm_box();
+ borrow_in_field_from_field();
+ borrow_in_field_from_field_via_imm_box();
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+fn main() {
+ let x: Option<Box<_>> = Some(box 1);
+ match x {
+ Some(ref _y) => {
+ let _a = x; //~ ERROR cannot move
+ }
+ _ => {}
+ }
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+fn main() {
+ let x: Option<Box<_>> = Some(box 1);
+ match x {
+ Some(ref y) => {
+ let _b = *y; //~ ERROR cannot move out
+ }
+ _ => {}
+ }
+}
--- /dev/null
+// 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.
+
+// Note: the borrowck analysis is currently flow-insensitive.
+// Therefore, some of these errors are marked as spurious and could be
+// corrected by a simple change to the analysis. The others are
+// either genuine or would require more advanced changes. The latter
+// cases are noted.
+
+#![feature(box_syntax)]
+
+fn borrow(_v: &isize) {}
+fn borrow_mut(_v: &mut isize) {}
+fn cond() -> bool { panic!() }
+fn for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
+fn produce<T>() -> T { panic!(); }
+
+fn inc(v: &mut Box<isize>) {
+ *v = box (**v + 1);
+}
+
+fn pre_freeze_cond() {
+ // In this instance, the freeze is conditional and starts before
+ // the mut borrow.
+
+ let mut v: Box<_> = box 3;
+ let _w;
+ if cond() {
+ _w = &v;
+ }
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+}
+
+fn pre_freeze_else() {
+ // In this instance, the freeze and mut borrow are on separate sides
+ // of the if.
+
+ let mut v: Box<_> = box 3;
+ let _w;
+ if cond() {
+ _w = &v;
+ } else {
+ borrow_mut(&mut *v);
+ }
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+// Note: the borrowck analysis is currently flow-insensitive.
+// Therefore, some of these errors are marked as spurious and could be
+// corrected by a simple change to the analysis. The others are
+// either genuine or would require more advanced changes. The latter
+// cases are noted.
+
+#![feature(box_syntax)]
+
+fn borrow(_v: &isize) {}
+fn borrow_mut(_v: &mut isize) {}
+fn cond() -> bool { panic!() }
+fn produce<T>() -> T { panic!(); }
+
+fn inc(v: &mut Box<isize>) {
+ *v = box (**v + 1);
+}
+
+fn loop_overarching_alias_mut() {
+ // In this instance, the borrow encompasses the entire loop.
+
+ let mut v: Box<_> = box 3;
+ let mut x = &mut v;
+ **x += 1;
+ loop {
+ borrow(&*v); //~ ERROR cannot borrow
+ }
+}
+
+fn block_overarching_alias_mut() {
+ // In this instance, the borrow encompasses the entire closure call.
+
+ let mut v: Box<_> = box 3;
+ let mut x = &mut v;
+ for _ in 0..3 {
+ borrow(&*v); //~ ERROR cannot borrow
+ }
+ *x = box 5;
+}
+
+fn loop_aliased_mut() {
+ // In this instance, the borrow is carried through the loop.
+
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut _x = &w;
+ loop {
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+ _x = &v;
+ }
+}
+
+fn while_aliased_mut() {
+ // In this instance, the borrow is carried through the loop.
+
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut _x = &w;
+ while cond() {
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+ _x = &v;
+ }
+}
+
+
+fn loop_aliased_mut_break() {
+ // In this instance, the borrow is carried through the loop.
+
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut _x = &w;
+ loop {
+ borrow_mut(&mut *v);
+ _x = &v;
+ break;
+ }
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+}
+
+fn while_aliased_mut_break() {
+ // In this instance, the borrow is carried through the loop.
+
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut _x = &w;
+ while cond() {
+ borrow_mut(&mut *v);
+ _x = &v;
+ break;
+ }
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+}
+
+fn while_aliased_mut_cond(cond: bool, cond2: bool) {
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut x = &mut w;
+ while cond {
+ **x += 1;
+ borrow(&*v); //~ ERROR cannot borrow
+ if cond2 {
+ x = &mut v; //~ ERROR cannot borrow
+ }
+ }
+}
+
+fn loop_break_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F) where
+ F: FnMut(&'r mut usize) -> bool,
+{
+ // Here we check that when you break out of an inner loop, the
+ // borrows that go out of scope as you exit the inner loop are
+ // removed from the bitset.
+
+ while cond() {
+ while cond() {
+ // this borrow is limited to the scope of `r`...
+ let r: &'r mut usize = produce();
+ if !f(&mut *r) {
+ break; // ...so it is not live as exit the `while` loop here
+ }
+ }
+ }
+}
+
+fn loop_loop_pops_scopes<'r, F>(_v: &'r mut [usize], mut f: F)
+ where F: FnMut(&'r mut usize) -> bool
+{
+ // Similar to `loop_break_pops_scopes` but for the `loop` keyword
+
+ while cond() {
+ while cond() {
+ // this borrow is limited to the scope of `r`...
+ let r: &'r mut usize = produce();
+ if !f(&mut *r) {
+ continue; // ...so it is not live as exit (and re-enter) the `while` loop here
+ }
+ }
+ }
+}
+
+fn main() {}
--- /dev/null
+// Copyright 2012-2014 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.
+
+#![allow(unused_variables)]
+#![allow(unused_assignments)]
+
+fn separate_arms() {
+ // Here both arms perform assignments, but only is illegal.
+
+ let mut x = None;
+ match x {
+ None => {
+ // It is ok to reassign x here, because there is in
+ // fact no outstanding loan of x!
+ x = Some(0);
+ }
+ Some(ref __isize) => {
+ x = Some(1); //~ ERROR cannot assign
+ }
+ }
+ x.clone(); // just to prevent liveness warnings
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+// Note: the borrowck analysis is currently flow-insensitive.
+// Therefore, some of these errors are marked as spurious and could be
+// corrected by a simple change to the analysis. The others are
+// either genuine or would require more advanced changes. The latter
+// cases are noted.
+
+#![feature(box_syntax)]
+
+fn borrow(_v: &isize) {}
+fn borrow_mut(_v: &mut isize) {}
+fn cond() -> bool { panic!() }
+fn for_func<F>(_f: F) where F: FnOnce() -> bool { panic!() }
+fn produce<T>() -> T { panic!(); }
+
+fn inc(v: &mut Box<isize>) {
+ *v = box (**v + 1);
+}
+
+fn pre_freeze() {
+ // In this instance, the freeze starts before the mut borrow.
+
+ let mut v: Box<_> = box 3;
+ let _w = &v;
+ borrow_mut(&mut *v); //~ ERROR cannot borrow
+}
+
+fn post_freeze() {
+ // In this instance, the const alias starts after the borrow.
+
+ let mut v: Box<_> = box 3;
+ borrow_mut(&mut *v);
+ let _w = &v;
+}
+
+fn main() {}
--- /dev/null
+// Copyright 2015 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() {
+ let old = ['o']; // statement 0
+ let mut v1 = Vec::new(); // statement 1
+
+ let mut v2 = Vec::new(); // statement 2
+ //~^ NOTE reference must be valid for the block suffix following statement 2
+
+ let young = ['y']; // statement 3
+ //~^ NOTE ...but borrowed value is only valid for the block suffix following statement 3
+
+ v2.push(&young[0]); // statement 4
+ //~^ ERROR `young[..]` does not live long enough
+
+ let mut v3 = Vec::new(); // statement 5
+ //~^ NOTE reference must be valid for the block suffix following statement 5
+
+ v3.push(&'x'); // statement 6
+ //~^ ERROR borrowed value does not live long enough
+ //~| NOTE ...but borrowed value is only valid for the statement
+ //~| HELP consider using a `let` binding to increase its lifetime
+
+ {
+
+ let mut v4 = Vec::new(); // (sub) statement 0
+ //~^ NOTE reference must be valid for the block suffix following statement 0
+
+ v4.push(&'y');
+ //~^ ERROR borrowed value does not live long enough
+ //~| NOTE ...but borrowed value is only valid for the statement
+ //~| HELP consider using a `let` binding to increase its lifetime
+
+ } // (statement 7)
+
+ let mut v5 = Vec::new(); // statement 8
+ //~^ NOTE reference must be valid for the block suffix following statement 8
+
+ v5.push(&'z');
+ //~^ ERROR borrowed value does not live long enough
+ //~| NOTE ...but borrowed value is only valid for the statement
+ //~| HELP consider using a `let` binding to increase its lifetime
+
+ v1.push(&old[0]);
+}
+
+fn main() {
+ f();
+}
--- /dev/null
+// 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.
+//
+// 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() {
+ let x = [1].iter(); //~ ERROR borrowed value does not live long enough
+ //~^ NOTE reference must be valid for the block suffix following statement
+ //~^^ HELP consider using a `let` binding to increase its lifetime
+}
+
+fn main() {
+ f();
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+use std::thread;
+
+fn borrow<F>(v: &isize, f: F) where F: FnOnce(&isize) {
+ f(v);
+}
+
+fn box_imm() {
+ let v: Box<_> = box 3;
+ let _w = &v;
+ thread::spawn(move|| {
+ println!("v={}", *v);
+ //~^ ERROR cannot move `v` into closure
+ });
+}
+
+fn box_imm_explicit() {
+ let v: Box<_> = box 3;
+ let _w = &v;
+ thread::spawn(move|| {
+ println!("v={}", *v);
+ //~^ ERROR cannot move
+ });
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+fn take(_v: Box<isize>) {
+}
+
+fn box_imm() {
+ let v = box 3;
+ let _w = &v;
+ take(v); //~ ERROR cannot move out of `v` because it is borrowed
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+fn borrow<F>(v: &isize, f: F) where F: FnOnce(&isize) {
+ f(v);
+}
+
+fn box_imm() {
+ let mut v: Box<_> = box 3;
+ borrow(&*v,
+ |w| { //~ ERROR cannot borrow `v` as mutable
+ v = box 4;
+ assert_eq!(*v, 3);
+ assert_eq!(*w, 4);
+ })
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+#![feature(box_patterns)]
+#![feature(box_syntax)]
+
+use std::ops::Add;
+
+#[derive(Clone)]
+struct foo(Box<usize>);
+
+impl Add for foo {
+ type Output = foo;
+
+ fn add(self, f: foo) -> foo {
+ let foo(box i) = self;
+ let foo(box j) = f;
+ foo(box (i + j))
+ }
+}
+
+fn main() {
+ let x = foo(box 3);
+ let _y = {x} + x.clone(); // the `{x}` forces a move to occur
+ //~^ ERROR use of moved value: `x`
+}
--- /dev/null
+// Copyright 2015 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.
+
+use std::mem;
+
+fn leak<T>(mut b: Box<T>) -> &'static mut T {
+ // isn't this supposed to be safe?
+ let inner = &mut *b as *mut _;
+ mem::forget(b);
+ unsafe { &mut *inner }
+}
+
+fn evil(mut s: &'static mut String)
+{
+ // create alias
+ let alias: &'static mut String = s;
+ let inner: &str = &alias;
+ // free value
+ *s = String::new(); //~ ERROR cannot assign
+ let _spray = "0wned".to_owned();
+ // ... and then use it
+ println!("{}", inner);
+}
+
+fn main() {
+ evil(leak(Box::new("hello".to_owned())));
+}
--- /dev/null
+// 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.
+
+use std::ops::Add;
+
+#[derive(Copy, Clone)]
+struct Point {
+ x: isize,
+ y: isize,
+}
+
+impl Add<isize> for Point {
+ type Output = isize;
+
+ fn add(self, z: isize) -> isize {
+ self.x + self.y + z
+ }
+}
+
+impl Point {
+ pub fn times(&self, z: isize) -> isize {
+ self.x * self.y * z
+ }
+}
+
+fn a() {
+ let mut p = Point {x: 3, y: 4};
+
+ // ok (we can loan out rcvr)
+ p + 3;
+ p.times(3);
+}
+
+fn b() {
+ let mut p = Point {x: 3, y: 4};
+
+ // Here I create an outstanding loan and check that we get conflicts:
+
+ let q = &mut p;
+
+ p + 3; //~ ERROR cannot use `p`
+ p.times(3); //~ ERROR cannot borrow `p`
+
+ *q + 3; // OK to use the new alias `q`
+ q.x += 1; // and OK to mutate it
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+
+struct point { x: isize, y: isize }
+
+trait methods {
+ fn impurem(&self);
+ fn blockm<F>(&self, f: F) where F: FnOnce();
+}
+
+impl methods for point {
+ fn impurem(&self) {
+ }
+
+ fn blockm<F>(&self, f: F) where F: FnOnce() { f() }
+}
+
+fn a() {
+ let mut p = point {x: 3, y: 4};
+
+ // Here: it's ok to call even though receiver is mutable, because we
+ // can loan it out.
+ p.impurem();
+
+ // But in this case we do not honor the loan:
+ p.blockm(|| { //~ ERROR cannot borrow `p` as mutable
+ p.x = 10;
+ })
+}
+
+fn b() {
+ let mut p = point {x: 3, y: 4};
+
+ // Here I create an outstanding loan and check that we get conflicts:
+
+ let l = &mut p;
+ p.impurem(); //~ ERROR cannot borrow
+
+ l.x += 1;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+
+// Here we check that it is allowed to lend out an element of a
+// (locally rooted) mutable, unique vector, and that we then prevent
+// modifications to the contents.
+
+fn takes_imm_elt<F>(_v: &isize, f: F) where F: FnOnce() {
+ f();
+}
+
+fn has_mut_vec_and_does_not_try_to_change_it() {
+ let mut v: Vec<isize> = vec!(1, 2, 3);
+ takes_imm_elt(&v[0], || {})
+}
+
+fn has_mut_vec_but_tries_to_change_it() {
+ let mut v: Vec<isize> = vec!(1, 2, 3);
+ takes_imm_elt(
+ &v[0],
+ || { //~ ERROR cannot borrow `v` as mutable
+ v[1] = 4;
+ })
+}
+
+fn main() {
+}
--- /dev/null
+// Copyright 2012-2014 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.
+
+// Test that immutable pattern bindings cannot be reassigned.
+
+#![feature(slice_patterns)]
+
+enum E {
+ Foo(isize)
+}
+
+struct S {
+ bar: isize,
+}
+
+pub fn main() {
+ match 1 {
+ x => {
+ x += 1; //~ ERROR re-assignment of immutable variable `x`
+ }
+ }
+
+ match E::Foo(1) {
+ E::Foo(x) => {
+ x += 1; //~ ERROR re-assignment of immutable variable `x`
+ }
+ }
+
+ match (S { bar: 1 }) {
+ S { bar: x } => {
+ x += 1; //~ ERROR re-assignment of immutable variable `x`
+ }
+ }
+
+ match (1,) {
+ (x,) => {
+ x += 1; //~ ERROR re-assignment of immutable variable `x`
+ }
+ }
+
+ match [1,2,3] {
+ [x,_,_] => {
+ x += 1; //~ ERROR re-assignment of immutable variable `x`
+ }
+ }
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax,unboxed_closures)]
+
+fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
+fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
+
+pub fn main() {
+ let bar: Box<_> = box 3;
+ let _g = to_fn_mut(|| {
+ let _h = to_fn_once(move || -> isize { *bar }); //~ ERROR cannot move out of
+ });
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+enum Foo {
+ Foo1(Box<u32>, Box<u32>),
+ Foo2(Box<u32>),
+ Foo3,
+}
+
+fn blah() {
+ let f = &Foo::Foo1(box 1, box 2);
+ match *f { //~ ERROR cannot move out of
+ Foo::Foo1(num1, //~ NOTE attempting to move value to here
+ num2) => (), //~ NOTE and here
+ Foo::Foo2(num) => (), //~ NOTE and here
+ Foo::Foo3 => ()
+ }
+}
+
+struct S {
+ f: String,
+ g: String
+}
+impl Drop for S {
+ fn drop(&mut self) { println!("{}", self.f); }
+}
+
+fn move_in_match() {
+ match (S {f: "foo".to_string(), g: "bar".to_string()}) {
+ S { //~ ERROR cannot move out of type `S`, which defines the `Drop` trait
+ f: _s, //~ NOTE attempting to move value to here
+ g: _t //~ NOTE and here
+ } => {}
+ }
+}
+
+// from issue-8064
+struct A {
+ a: Box<isize>,
+}
+
+fn free<T>(_: T) {}
+
+fn blah2() {
+ let a = &A { a: box 1 };
+ match a.a { //~ ERROR cannot move out of
+ n => { //~ NOTE attempting to move value to here
+ free(n)
+ }
+ }
+ free(a)
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// verify that an error is raised when trying to move out of a
+// borrowed path.
+
+#![feature(box_syntax)]
+
+fn main() {
+ let a: Box<Box<_>> = box box 2;
+ let b = &a;
+
+ let z = *a; //~ ERROR: cannot move out of `*a` because it is borrowed
+}
--- /dev/null
+// 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 foo(x: *const Box<isize>) -> Box<isize> {
+ let y = *x; //~ ERROR dereference of raw pointer requires unsafe function or block
+ return y;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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 with<F>(f: F) where F: FnOnce(&String) {}
+
+fn arg_item(&_x: &String) {}
+ //~^ ERROR cannot move out of borrowed content
+
+fn arg_closure() {
+ with(|&_x| ())
+ //~^ ERROR cannot move out of borrowed content
+}
+
+fn let_pat() {
+ let &_x = &"hi".to_string();
+ //~^ ERROR cannot move out of borrowed content
+}
+
+pub fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+fn call_f<F:FnOnce() -> isize>(f: F) -> isize {
+ f()
+}
+
+fn main() {
+ let t: Box<_> = box 3;
+
+ call_f(move|| { *t + 1 });
+ call_f(move|| { *t + 1 }); //~ ERROR capture of moved value
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that attempt to move `&mut` pointer while pointee is borrowed
+// yields an error.
+//
+// Example from src/librustc_borrowck/borrowck/README.md
+
+fn foo(t0: &mut isize) {
+ let p: &isize = &*t0; // Freezes `*t0`
+ let t1 = t0; //~ ERROR cannot move out of `t0`
+ *t1 = 22;
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+use std::rc::Rc;
+
+pub fn main() {
+ let _x = Rc::new(vec!(1, 2)).into_iter();
+ //~^ ERROR cannot move out of borrowed content
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+use std::rc::Rc;
+
+pub fn main() {
+ let _x = *Rc::new("hi".to_string());
+ //~^ ERROR cannot move out of borrowed content
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Ensure that moves out of static items is forbidden
+
+struct Foo {
+ foo: isize,
+}
+
+static BAR: Foo = Foo { foo: 5 };
+
+
+fn test(f: Foo) {
+ let _f = Foo{foo: 4, ..f};
+}
+
+fn main() {
+ test(BAR); //~ ERROR cannot move out of static item
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+struct S {f:String}
+impl Drop for S {
+ fn drop(&mut self) { println!("{}", self.f); }
+}
+
+fn move_in_match() {
+ match (S {f:"foo".to_string()}) {
+ S {f:_s} => {}
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+ }
+}
+
+fn move_in_let() {
+ let S {f:_s} = S {f:"foo".to_string()};
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+}
+
+fn move_in_fn_arg(S {f:_s}: S) {
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+struct S(String);
+impl Drop for S {
+ fn drop(&mut self) { }
+}
+
+fn move_in_match() {
+ match S("foo".to_string()) {
+ S(_s) => {}
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+ }
+}
+
+fn move_in_let() {
+ let S(_s) = S("foo".to_string());
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+}
+
+fn move_in_fn_arg(S(_s): S) {
+ //~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that we do not permit moves from &[] matched by a vec pattern.
+
+#![feature(slice_patterns)]
+
+#[derive(Clone, Debug)]
+struct Foo {
+ string: String
+}
+
+pub fn main() {
+ let x = vec!(
+ Foo { string: "foo".to_string() },
+ Foo { string: "bar".to_string() },
+ Foo { string: "baz".to_string() }
+ );
+ let x: &[Foo] = &x;
+ match x {
+ [_, tail..] => {
+ match tail {
+ [Foo { string: a }, //~ ERROR cannot move out of borrowed content
+ Foo { string: b }] => {
+ //~^^ NOTE attempting to move value to here
+ //~^^ NOTE and here
+ }
+ _ => {
+ unreachable!();
+ }
+ }
+ let z = tail[0].clone();
+ println!("{:?}", z);
+ }
+ _ => {
+ unreachable!();
+ }
+ }
+}
--- /dev/null
+// Copyright 2012-2013 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.
+
+// Tests that the borrow checker checks all components of a path when moving
+// out.
+
+#![feature(box_syntax)]
+
+struct S {
+ x : Box<isize>
+}
+
+fn f<T>(_: T) {}
+
+fn main() {
+ let a : S = S { x : box 1 };
+ let pb = &a;
+ let S { x: ax } = a; //~ ERROR cannot move out
+ f(pb);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+use std::thread;
+
+fn borrow<T>(_: &T) { }
+
+fn different_vars_after_borrows() {
+ let x1: Box<_> = box 1;
+ let p1 = &x1;
+ let x2: Box<_> = box 2;
+ let p2 = &x2;
+ thread::spawn(move|| {
+ drop(x1); //~ ERROR cannot move `x1` into closure because it is borrowed
+ drop(x2); //~ ERROR cannot move `x2` into closure because it is borrowed
+ });
+ borrow(&*p1);
+ borrow(&*p2);
+}
+
+fn different_vars_after_moves() {
+ let x1: Box<_> = box 1;
+ drop(x1);
+ let x2: Box<_> = box 2;
+ drop(x2);
+ thread::spawn(move|| {
+ drop(x1); //~ ERROR capture of moved value: `x1`
+ drop(x2); //~ ERROR capture of moved value: `x2`
+ });
+}
+
+fn same_var_after_borrow() {
+ let x: Box<_> = box 1;
+ let p = &x;
+ thread::spawn(move|| {
+ drop(x); //~ ERROR cannot move `x` into closure because it is borrowed
+ drop(x); //~ ERROR use of moved value: `x`
+ });
+ borrow(&*p);
+}
+
+fn same_var_after_move() {
+ let x: Box<_> = box 1;
+ drop(x);
+ thread::spawn(move|| {
+ drop(x); //~ ERROR capture of moved value: `x`
+ drop(x); //~ ERROR use of moved value: `x`
+ });
+}
+
+fn main() {
+ different_vars_after_borrows();
+ different_vars_after_moves();
+ same_var_after_borrow();
+ same_var_after_move();
+}
--- /dev/null
+// 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 main() {
+ let x: isize = 3;
+ let y: &mut isize = &mut x; //~ ERROR cannot borrow
+ *y = 5;
+ println!("{}", *y);
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Test to ensure we only report an error for the first issued loan that
+// conflicts with a new loan, as opposed to every issued loan. This keeps us
+// down to O(n) errors (for n problem lines), instead of O(n^2) errors.
+
+fn main() {
+ let mut x = 1;
+ let mut addr;
+ loop {
+ match 1 {
+ 1 => { addr = &mut x; }
+ //~^ ERROR cannot borrow `x` as mutable more than once at a time
+ 2 => { addr = &mut x; }
+ //~^ ERROR cannot borrow `x` as mutable more than once at a time
+ _ => { addr = &mut x; }
+ //~^ ERROR cannot borrow `x` as mutable more than once at a time
+ }
+ }
+}
--- /dev/null
+// Copyright 2013 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.
+
+// Test that attempt to mutably borrow `&mut` pointer while pointee is
+// borrowed yields an error.
+//
+// Example from src/librustc_borrowck/borrowck/README.md
+
+fn foo<'a>(mut t0: &'a mut isize,
+ mut t1: &'a mut isize) {
+ let p: &isize = &*t0; // Freezes `*t0`
+ let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
+ **t2 += 1; // Mutates `*t0`
+}
+
+fn bar<'a>(mut t0: &'a mut isize,
+ mut t1: &'a mut isize) {
+ let p: &mut isize = &mut *t0; // Claims `*t0`
+ let mut t2 = &mut t0; //~ ERROR cannot borrow `t0`
+ **t2 += 1; // Mutates `*t0` but not through `*p`
+}
+
+fn main() {
+}
--- /dev/null
+// 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 write(v: &mut [isize]) {
+ v[0] += 1;
+}
+
+fn main() {
+ let v = vec!(1, 2, 3);
+ write(&mut v); //~ ERROR cannot borrow
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+enum Enum<'a> {
+ A(&'a isize),
+ B(bool),
+}
+
+fn foo() -> isize {
+ let mut n = 42;
+ let mut x = Enum::A(&mut n);
+ match x {
+ Enum::A(_) if { x = Enum::B(false); false } => 1,
+ //~^ ERROR cannot assign in a pattern guard
+ Enum::A(_) if { let y = &mut x; *y = Enum::B(false); false } => 1,
+ //~^ ERROR cannot mutably borrow in a pattern guard
+ //~^^ ERROR cannot assign in a pattern guard
+ Enum::A(p) => *p,
+ Enum::B(_) => 2,
+ }
+}
+
+fn main() {
+ foo();
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+struct node_ {
+ a: Box<cycle>
+}
+
+enum cycle {
+ node(node_),
+ empty
+}
+fn main() {
+ let mut x: Box<_> = box cycle::node(node_ {a: box cycle::empty});
+ // Create a cycle!
+ match *x {
+ cycle::node(ref mut y) => {
+ y.a = x; //~ ERROR cannot move out of
+ }
+ cycle::empty => {}
+ };
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that borrows that occur due to calls to object methods
+// properly "claim" the object path.
+
+trait Foo {
+ fn borrowed(&self) -> &();
+ fn mut_borrowed(&mut self) -> &();
+}
+
+fn borrowed_receiver(x: &Foo) {
+ let _y = x.borrowed();
+ let _z = x.borrowed();
+}
+
+fn mut_borrowed_receiver(x: &mut Foo) {
+ let _y = x.borrowed();
+ let _z = x.mut_borrowed(); //~ ERROR cannot borrow
+}
+
+fn mut_owned_receiver(mut x: Box<Foo>) {
+ let _y = x.borrowed();
+ let _z = &mut x; //~ ERROR cannot borrow
+}
+
+fn imm_owned_receiver(mut x: Box<Foo>) {
+ let _y = x.borrowed();
+ let _z = &x;
+}
+
+fn main() {}
--- /dev/null
+// 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.
+
+
+trait Foo {
+ fn borrowed(&self);
+ fn borrowed_mut(&mut self);
+}
+
+fn borrowed_receiver(x: &Foo) {
+ x.borrowed();
+ x.borrowed_mut(); //~ ERROR cannot borrow
+}
+
+fn borrowed_mut_receiver(x: &mut Foo) {
+ x.borrowed();
+ x.borrowed_mut();
+}
+
+fn owned_receiver(x: Box<Foo>) {
+ x.borrowed();
+ x.borrowed_mut(); //~ ERROR cannot borrow
+}
+
+fn mut_owned_receiver(mut x: Box<Foo>) {
+ x.borrowed();
+ x.borrowed_mut();
+}
+
+fn main() {}
--- /dev/null
+// 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 main() {
+ let i: isize;
+
+ println!("{}", false || { i = 5; true });
+ println!("{}", i); //~ ERROR use of possibly uninitialized variable: `i`
+}
--- /dev/null
+// 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.
+
+#![feature(unboxed_closures)]
+
+use std::ops::{Fn, FnMut, FnOnce};
+
+struct SFn {
+ x: isize,
+ y: isize,
+}
+
+impl Fn<(isize,)> for SFn {
+ extern "rust-call" fn call(&self, (z,): (isize,)) -> isize {
+ self.x * self.y * z
+ }
+}
+
+impl FnMut<(isize,)> for SFn {
+ extern "rust-call" fn call_mut(&mut self, args: (isize,)) -> isize { self.call(args) }
+}
+
+impl FnOnce<(isize,)> for SFn {
+ type Output = isize;
+ extern "rust-call" fn call_once(self, args: (isize,)) -> isize { self.call(args) }
+}
+
+struct SFnMut {
+ x: isize,
+ y: isize,
+}
+
+impl FnMut<(isize,)> for SFnMut {
+ extern "rust-call" fn call_mut(&mut self, (z,): (isize,)) -> isize {
+ self.x * self.y * z
+ }
+}
+
+impl FnOnce<(isize,)> for SFnMut {
+ type Output = isize;
+ extern "rust-call" fn call_once(mut self, args: (isize,)) -> isize { self.call_mut(args) }
+}
+
+struct SFnOnce {
+ x: String,
+}
+
+impl FnOnce<(String,)> for SFnOnce {
+ type Output = usize;
+
+ extern "rust-call" fn call_once(self, (z,): (String,)) -> usize {
+ self.x.len() + z.len()
+ }
+}
+
+fn f() {
+ let mut s = SFn {
+ x: 1,
+ y: 2,
+ };
+ let sp = &mut s;
+ s(3); //~ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+ //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+}
+
+fn g() {
+ let s = SFnMut {
+ x: 1,
+ y: 2,
+ };
+ s(3); //~ ERROR cannot borrow immutable local variable `s` as mutable
+}
+
+fn h() {
+ let s = SFnOnce {
+ x: "hello".to_string(),
+ };
+ s(" world".to_string());
+ s(" world".to_string()); //~ ERROR use of moved value: `s`
+}
+
+fn main() {}
--- /dev/null
+// Copyright 2015 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.
+
+// Check that we properly record borrows when we are doing an
+// overloaded, autoderef of a value obtained via an overloaded index
+// operator. The accounting of the all the implicit things going on
+// here is rather subtle. Issue #20232.
+
+use std::ops::{Deref, Index};
+
+struct MyVec<T> { x: T }
+
+impl<T> Index<usize> for MyVec<T> {
+ type Output = T;
+ fn index(&self, _: usize) -> &T {
+ &self.x
+ }
+}
+
+struct MyPtr<T> { x: T }
+
+impl<T> Deref for MyPtr<T> {
+ type Target = T;
+ fn deref(&self) -> &T {
+ &self.x
+ }
+}
+
+struct Foo { f: usize }
+
+fn main() {
+ let mut v = MyVec { x: MyPtr { x: Foo { f: 22 } } };
+ let i = &v[0].f;
+ v = MyVec { x: MyPtr { x: Foo { f: 23 } } };
+ //~^ ERROR cannot assign to `v`
+ read(*i);
+}
+
+fn read(_: usize) { }
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that we still see borrowck errors of various kinds when using
+// indexing and autoderef in combination.
+
+use std::ops::{Index, IndexMut};
+
+struct Foo {
+ x: isize,
+ y: isize,
+}
+
+impl<'a> Index<&'a String> for Foo {
+ type Output = isize;
+
+ fn index(&self, z: &String) -> &isize {
+ if *z == "x" {
+ &self.x
+ } else {
+ &self.y
+ }
+ }
+}
+
+impl<'a> IndexMut<&'a String> for Foo {
+ fn index_mut(&mut self, z: &String) -> &mut isize {
+ if *z == "x" {
+ &mut self.x
+ } else {
+ &mut self.y
+ }
+ }
+}
+
+fn test1(mut f: Box<Foo>, s: String) {
+ let _p = &mut f[&s];
+ let _q = &f[&s]; //~ ERROR cannot borrow
+}
+
+fn test2(mut f: Box<Foo>, s: String) {
+ let _p = &mut f[&s];
+ let _q = &mut f[&s]; //~ ERROR cannot borrow
+}
+
+struct Bar {
+ foo: Foo
+}
+
+fn test3(mut f: Box<Bar>, s: String) {
+ let _p = &mut f.foo[&s];
+ let _q = &mut f.foo[&s]; //~ ERROR cannot borrow
+}
+
+fn test4(mut f: Box<Bar>, s: String) {
+ let _p = &f.foo[&s];
+ let _q = &f.foo[&s];
+}
+
+fn test5(mut f: Box<Bar>, s: String) {
+ let _p = &f.foo[&s];
+ let _q = &mut f.foo[&s]; //~ ERROR cannot borrow
+}
+
+fn test6(mut f: Box<Bar>, g: Foo, s: String) {
+ let _p = &f.foo[&s];
+ f.foo = g; //~ ERROR cannot assign
+}
+
+fn test7(mut f: Box<Bar>, g: Bar, s: String) {
+ let _p = &f.foo[&s];
+ *f = g; //~ ERROR cannot assign
+}
+
+fn test8(mut f: Box<Bar>, g: Foo, s: String) {
+ let _p = &mut f.foo[&s];
+ f.foo = g; //~ ERROR cannot assign
+}
+
+fn test9(mut f: Box<Bar>, g: Bar, s: String) {
+ let _p = &mut f.foo[&s];
+ *f = g; //~ ERROR cannot assign
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+use std::ops::Index;
+
+struct MyVec<T> {
+ data: Vec<T>,
+}
+
+impl<T> Index<usize> for MyVec<T> {
+ type Output = T;
+
+ fn index(&self, i: usize) -> &T {
+ &self.data[i]
+ }
+}
+
+fn main() {
+ let v = MyVec::<Box<_>> { data: vec!(box 1, box 2, box 3) };
+ let good = &v[0]; // Shouldn't fail here
+ let bad = v[0];
+ //~^ ERROR cannot move out of indexed content
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+use std::ops::{Index, IndexMut};
+
+struct Foo {
+ x: isize,
+ y: isize,
+}
+
+impl Index<String> for Foo {
+ type Output = isize;
+
+ fn index(&self, z: String) -> &isize {
+ if z == "x" {
+ &self.x
+ } else {
+ &self.y
+ }
+ }
+}
+
+impl IndexMut<String> for Foo {
+ fn index_mut(&mut self, z: String) -> &mut isize {
+ if z == "x" {
+ &mut self.x
+ } else {
+ &mut self.y
+ }
+ }
+}
+
+struct Bar {
+ x: isize,
+}
+
+impl Index<isize> for Bar {
+ type Output = isize;
+
+ fn index<'a>(&'a self, z: isize) -> &'a isize {
+ &self.x
+ }
+}
+
+fn main() {
+ let mut f = Foo {
+ x: 1,
+ y: 2,
+ };
+ let mut s = "hello".to_string();
+ let rs = &mut s;
+
+ println!("{}", f[s]);
+ //~^ ERROR cannot move out of `s` because it is borrowed
+
+ f[s] = 10;
+ //~^ ERROR cannot move out of `s` because it is borrowed
+ //~| ERROR use of moved value: `s`
+
+ let s = Bar {
+ x: 1,
+ };
+ let i = 2;
+ let _j = &i;
+ println!("{}", s[i]); // no error, i is copy
+ println!("{}", s[i]);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+use std::ops::{Index, IndexMut};
+
+struct Foo {
+ x: isize,
+ y: isize,
+}
+
+impl<'a> Index<&'a String> for Foo {
+ type Output = isize;
+
+ fn index(&self, z: &String) -> &isize {
+ if *z == "x" {
+ &self.x
+ } else {
+ &self.y
+ }
+ }
+}
+
+impl<'a> IndexMut<&'a String> for Foo {
+ fn index_mut(&mut self, z: &String) -> &mut isize {
+ if *z == "x" {
+ &mut self.x
+ } else {
+ &mut self.y
+ }
+ }
+}
+
+struct Bar {
+ x: isize,
+}
+
+impl Index<isize> for Bar {
+ type Output = isize;
+
+ fn index<'a>(&'a self, z: isize) -> &'a isize {
+ &self.x
+ }
+}
+
+fn main() {
+ let mut f = Foo {
+ x: 1,
+ y: 2,
+ };
+ let mut s = "hello".to_string();
+ let rs = &mut s;
+ println!("{}", f[&s]);
+ //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+ f[&s] = 10;
+ //~^ ERROR cannot borrow `s` as immutable because it is also borrowed as mutable
+ let s = Bar {
+ x: 1,
+ };
+ s[2] = 20;
+ //~^ ERROR cannot assign to immutable indexed content
+}
--- /dev/null
+// Copyright 2014-2015 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.
+
+struct Test;
+
+struct Test2 {
+ b: Option<Test>,
+}
+
+struct Test3(Option<Test>);
+
+impl Drop for Test {
+ fn drop(&mut self) {
+ println!("dropping!");
+ }
+}
+
+impl Drop for Test2 {
+ fn drop(&mut self) {}
+}
+
+impl Drop for Test3 {
+ fn drop(&mut self) {}
+}
+
+fn stuff() {
+ let mut t = Test2 { b: None };
+ let u = Test;
+ drop(t);
+ t.b = Some(u);
+ //~^ ERROR partial reinitialization of uninitialized structure `t`
+
+ let mut t = Test3(None);
+ let u = Test;
+ drop(t);
+ t.0 = Some(u);
+ //~^ ERROR partial reinitialization of uninitialized structure `t`
+}
+
+fn main() {
+ stuff()
+}
--- /dev/null
+// Copyright 2014-2015 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.
+
+struct Test {
+ a: isize,
+ b: Option<Box<Test>>,
+}
+
+impl Drop for Test {
+ fn drop(&mut self) {
+ println!("Dropping {}", self.a);
+ }
+}
+
+fn stuff() {
+ let mut t = Test { a: 1, b: None};
+ let mut u = Test { a: 2, b: Some(Box::new(t))};
+ t.b = Some(Box::new(u));
+ //~^ ERROR partial reinitialization of uninitialized structure `t`
+ println!("done");
+}
+
+fn main() {
+ stuff();
+ println!("Hello, world!")
+}
--- /dev/null
+// Copyright 2015 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.
+use std::mem;
+
+struct Test { f: usize }
+impl Drop for Test {
+ fn drop(&mut self) {}
+}
+
+fn main() {
+ let mut x = (Test { f: 2 }, Test { f: 4 });
+ mem::drop(x.0);
+ x.0.f = 3;
+ //~^ ERROR partial reinitialization of uninitialized structure `x.0`
+}
--- /dev/null
+// Copyright 2015 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.
+
+struct Test;
+
+struct Test2(Option<Test>);
+
+impl Drop for Test {
+ fn drop(&mut self) {
+ println!("dropping!");
+ }
+}
+
+impl Drop for Test2 {
+ fn drop(&mut self) {}
+}
+
+fn stuff() {
+ let mut x : (Test2, Test2);
+ (x.0).0 = Some(Test);
+ //~^ ERROR partial reinitialization of uninitialized structure `x.0`
+}
+
+fn main() {
+ stuff()
+}
--- /dev/null
+// Copyright 2012-2014 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 main() {
+ let mut x: Option<isize> = None;
+ match x {
+ None => {
+ // Note: on this branch, no borrow has occurred.
+ x = Some(0);
+ }
+ Some(ref i) => {
+ // But on this branch, `i` is an outstanding borrow
+ x = Some(*i+1); //~ ERROR cannot assign to `x`
+ }
+ }
+ x.clone(); // just to prevent liveness warnings
+}
--- /dev/null
+// 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.
+
+struct Foo {
+ bar1: Bar,
+ bar2: Bar
+}
+
+struct Bar {
+ int1: isize,
+ int2: isize,
+}
+
+fn borrow_same_field_twice_mut_mut(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_same_field_twice_mut_imm(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1;
+ let _bar2 = &foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_same_field_twice_imm_mut(foo: &mut Foo) {
+ let _bar1 = &foo.bar1;
+ let _bar2 = &mut foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_same_field_twice_imm_imm(foo: &mut Foo) {
+ let _bar1 = &foo.bar1;
+ let _bar2 = &foo.bar1;
+}
+
+fn borrow_both_mut(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1;
+ let _bar2 = &mut foo.bar2;
+}
+
+fn borrow_both_mut_pattern(foo: &mut Foo) {
+ match *foo {
+ Foo { bar1: ref mut _bar1, bar2: ref mut _bar2 } => {}
+ }
+}
+
+fn borrow_var_and_pattern(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1;
+ match *foo {
+ Foo { bar1: ref mut _bar1, bar2: _ } => {}
+ //~^ ERROR cannot borrow
+ }
+}
+
+fn borrow_mut_and_base_imm(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1.int1;
+ let _foo1 = &foo.bar1; //~ ERROR cannot borrow
+ let _foo2 = &*foo; //~ ERROR cannot borrow
+}
+
+fn borrow_mut_and_base_mut(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_mut_and_base_mut2(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1.int1;
+ let _foo2 = &mut *foo; //~ ERROR cannot borrow
+}
+
+fn borrow_imm_and_base_mut(foo: &mut Foo) {
+ let _bar1 = &foo.bar1.int1;
+ let _foo1 = &mut foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_imm_and_base_mut2(foo: &mut Foo) {
+ let _bar1 = &foo.bar1.int1;
+ let _foo2 = &mut *foo; //~ ERROR cannot borrow
+}
+
+fn borrow_imm_and_base_imm(foo: &mut Foo) {
+ let _bar1 = &foo.bar1.int1;
+ let _foo1 = &foo.bar1;
+ let _foo2 = &*foo;
+}
+
+fn borrow_mut_and_imm(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1;
+ let _foo1 = &foo.bar2;
+}
+
+fn borrow_mut_from_imm(foo: &Foo) {
+ let _bar1 = &mut foo.bar1; //~ ERROR cannot borrow
+}
+
+fn borrow_long_path_both_mut(foo: &mut Foo) {
+ let _bar1 = &mut foo.bar1.int1;
+ let _foo1 = &mut foo.bar2.int2;
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that assignments to an `&mut` pointer which is found in a
+// borrowed (but otherwise non-aliasable) location is illegal.
+
+struct S<'a> {
+ pointer: &'a mut isize
+}
+
+fn copy_borrowed_ptr<'a,'b>(p: &'a mut S<'b>) -> S<'b> {
+ S { pointer: &mut *p.pointer }
+ //~^ ERROR cannot infer
+}
+
+fn main() {
+ let mut x = 1;
+
+ {
+ let mut y = S { pointer: &mut x };
+ let z = copy_borrowed_ptr(&mut y);
+ *y.pointer += 1;
+ *z.pointer += 1;
+ }
+}
--- /dev/null
+// 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 main() {
+ let msg;
+ match Some("Hello".to_string()) {
+ Some(ref m) => { //~ ERROR borrowed value does not live long enough
+ msg = m;
+ },
+ None => { panic!() }
+ }
+ println!("{}", *msg);
+}
--- /dev/null
+// 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 destructure(x: Option<isize>) -> isize {
+ match x {
+ None => 0,
+ Some(ref mut v) => *v //~ ERROR cannot borrow
+ }
+}
+
+fn main() {
+ assert_eq!(destructure(Some(22)), 22);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![allow(dead_code)]
+fn main() {
+ // Original borrow ends at end of function
+ let mut x = 1;
+ let y = &mut x;
+ let z = &x; //~ ERROR cannot borrow
+}
+//~^ NOTE previous borrow ends here
+
+fn foo() {
+ match true {
+ true => {
+ // Original borrow ends at end of match arm
+ let mut x = 1;
+ let y = &x;
+ let z = &mut x; //~ ERROR cannot borrow
+ }
+ //~^ NOTE previous borrow ends here
+ false => ()
+ }
+}
+
+fn bar() {
+ // Original borrow ends at end of closure
+ || {
+ let mut x = 1;
+ let y = &mut x;
+ let z = &mut x; //~ ERROR cannot borrow
+ };
+ //~^ NOTE previous borrow ends here
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Check that when we clone a `&T` pointer we properly relate the
+// lifetime of the pointer which results to the pointer being cloned.
+// Bugs in method resolution have sometimes broken this connection.
+// Issue #19261.
+
+fn leak<'a, T>(x: T) -> &'a T {
+ (&x).clone() //~ ERROR `x` does not live long enough
+}
+
+fn main() { }
--- /dev/null
+// 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() -> isize {
+ let x: isize;
+ return x; //~ ERROR use of possibly uninitialized variable: `x`
+}
+
+fn main() { f(); }
--- /dev/null
+// Copyright 2012-2013 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.
+
+// Issue 4691: Ensure that functional-struct-update can only copy, not
+// move, when the struct implements Drop.
+
+struct B;
+struct S { a: isize, b: B }
+impl Drop for S { fn drop(&mut self) { } }
+
+struct T { a: isize, mv: Box<isize> }
+impl Drop for T { fn drop(&mut self) { } }
+
+fn f(s0:S) {
+ let _s2 = S{a: 2, ..s0}; //~error: cannot move out of type `S`, which defines the `Drop` trait
+}
+
+fn g(s0:T) {
+ let _s2 = T{a: 2, ..s0}; //~error: cannot move out of type `T`, which defines the `Drop` trait
+}
+
+fn main() { }
--- /dev/null
+// 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.
+//
+// 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.
+
+// Test that attempt to swap `&mut` pointer while pointee is borrowed
+// yields an error.
+//
+// Example from src/librustc_borrowck/borrowck/README.md
+
+use std::mem::swap;
+
+fn foo<'a>(mut t0: &'a mut isize,
+ mut t1: &'a mut isize) {
+ let p: &isize = &*t0; // Freezes `*t0`
+ swap(&mut t0, &mut t1); //~ ERROR cannot borrow `t0`
+ *t1 = 22;
+}
+
+fn main() {
+}
--- /dev/null
+// 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 foo(x: Box<isize>) -> isize {
+ let y = &*x;
+ free(x); //~ ERROR cannot move out of `x` because it is borrowed
+ *y
+}
+
+fn free(_x: Box<isize>) {
+}
+
+fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(overloaded_calls, unboxed_closures)]
+
+fn a<F:Fn(isize, isize) -> isize>(mut f: F) {
+ let g = &mut f;
+ f(1, 2); //~ ERROR cannot borrow `f` as immutable
+ //~^ ERROR cannot borrow `f` as immutable
+}
+
+fn b<F:FnMut(isize, isize) -> isize>(f: F) {
+ f(1, 2); //~ ERROR cannot borrow immutable argument
+}
+
+fn c<F:FnOnce(isize, isize) -> isize>(f: F) {
+ f(1, 2);
+ f(1, 2); //~ ERROR use of moved value
+}
+
+fn main() {}
--- /dev/null
+// 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 main() {
+ let bar;
+ fn baz(_x: isize) { }
+ baz(bar); //~ ERROR use of possibly uninitialized variable: `bar`
+}
--- /dev/null
+// Copyright 2012-2014 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.
+
+// Tests that the use of uninitialized variable in assignment operator
+// expression is detected.
+
+pub fn main() {
+ let x: isize;
+ x += 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x -= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x *= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x /= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x %= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x ^= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x &= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x |= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x <<= 1; //~ ERROR use of possibly uninitialized variable: `x`
+
+ let x: isize;
+ x >>= 1; //~ ERROR use of possibly uninitialized variable: `x`
+}
--- /dev/null
+// 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 foo(x: isize) { println!("{}", x); }
+
+fn main() {
+ let x: isize;
+ foo(x); //~ ERROR use of possibly uninitialized variable: `x`
+}
--- /dev/null
+// 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.
+
+#![feature(box_syntax)]
+
+fn borrow(_v: &isize) {}
+
+fn local() {
+ let mut v: Box<_> = box 3;
+ borrow(&*v);
+}
+
+fn local_rec() {
+ struct F { f: Box<isize> }
+ let mut v = F {f: box 3};
+ borrow(&*v.f);
+}
+
+fn local_recs() {
+ struct F { f: G }
+ struct G { g: H }
+ struct H { h: Box<isize> }
+ let mut v = F {f: G {g: H {h: box 3}}};
+ borrow(&*v.f.g.h);
+}
+
+fn aliased_imm() {
+ let mut v: Box<_> = box 3;
+ let _w = &v;
+ borrow(&*v);
+}
+
+fn aliased_mut() {
+ let mut v: Box<_> = box 3;
+ let _w = &mut v;
+ borrow(&*v); //~ ERROR cannot borrow `*v`
+}
+
+fn aliased_other() {
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let _x = &mut w;
+ borrow(&*v);
+}
+
+fn aliased_other_reassign() {
+ let mut v: Box<_> = box 3;
+ let mut w: Box<_> = box 4;
+ let mut _x = &mut w;
+ _x = &mut v;
+ borrow(&*v); //~ ERROR cannot borrow `*v`
+}
+
+fn main() {
+}
--- /dev/null
+// 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 test() {
+ let w: &mut [isize];
+ w[5] = 0; //~ ERROR use of possibly uninitialized variable: `*w`
+
+ let mut w: &mut [isize];
+ w[5] = 0; //~ ERROR use of possibly uninitialized variable: `*w`
+}
+
+fn main() { test(); }
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(box_syntax)]
+
+#[derive(Copy, Clone)]
+struct A { a: isize, b: isize }
+
+struct B { a: isize, b: Box<isize> }
+
+fn var_copy_after_var_borrow() {
+ let mut x: isize = 1;
+ let p = &mut x;
+ drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
+ *p = 2;
+}
+
+fn var_copy_after_field_borrow() {
+ let mut x = A { a: 1, b: 2 };
+ let p = &mut x.a;
+ drop(x); //~ ERROR cannot use `x` because it was mutably borrowed
+ *p = 3;
+}
+
+fn field_copy_after_var_borrow() {
+ let mut x = A { a: 1, b: 2 };
+ let p = &mut x;
+ drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
+ p.a = 3;
+}
+
+fn field_copy_after_field_borrow() {
+ let mut x = A { a: 1, b: 2 };
+ let p = &mut x.a;
+ drop(x.a); //~ ERROR cannot use `x.a` because it was mutably borrowed
+ *p = 3;
+}
+
+fn fu_field_copy_after_var_borrow() {
+ let mut x = A { a: 1, b: 2 };
+ let p = &mut x;
+ let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
+ drop(y);
+ p.a = 4;
+}
+
+fn fu_field_copy_after_field_borrow() {
+ let mut x = A { a: 1, b: 2 };
+ let p = &mut x.a;
+ let y = A { b: 3, .. x }; //~ ERROR cannot use `x.a` because it was mutably borrowed
+ drop(y);
+ *p = 4;
+}
+
+fn var_deref_after_var_borrow() {
+ let mut x: Box<isize> = box 1;
+ let p = &mut x;
+ drop(*x); //~ ERROR cannot use `*x` because it was mutably borrowed
+ **p = 2;
+}
+
+fn field_deref_after_var_borrow() {
+ let mut x = B { a: 1, b: box 2 };
+ let p = &mut x;
+ drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
+ p.a = 3;
+}
+
+fn field_deref_after_field_borrow() {
+ let mut x = B { a: 1, b: box 2 };
+ let p = &mut x.b;
+ drop(*x.b); //~ ERROR cannot use `*x.b` because it was mutably borrowed
+ **p = 3;
+}
+
+fn main() {
+ var_copy_after_var_borrow();
+ var_copy_after_field_borrow();
+
+ field_copy_after_var_borrow();
+ field_copy_after_field_borrow();
+
+ fu_field_copy_after_var_borrow();
+ fu_field_copy_after_field_borrow();
+
+ var_deref_after_var_borrow();
+ field_deref_after_var_borrow();
+ field_deref_after_field_borrow();
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Variation on `borrowck-use-uninitialized-in-cast` in which we do a
+// trait cast from an uninitialized source. Issue #20791.
+
+trait Foo { fn dummy(&self) { } }
+impl Foo for i32 { }
+
+fn main() {
+ let x: &i32;
+ let y = x as *const Foo; //~ ERROR use of possibly uninitialized variable: `*x`
+}
--- /dev/null
+// Copyright 2015 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.
+
+// Check that we detect unused values that are cast to other things.
+// The problem was specified to casting to `*`, as creating unsafe
+// pointers was not being fully checked. Issue #20791.
+
+// pretty-expanded FIXME #23616
+
+fn main() {
+ let x: &i32;
+ let y = x as *const i32; //~ ERROR use of possibly uninitialized variable: `*x`
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(advanced_slice_patterns)]
+#![feature(slice_patterns)]
+
+fn a<'a>() -> &'a [isize] {
+ let vec = vec!(1, 2, 3, 4);
+ let vec: &[isize] = &vec; //~ ERROR does not live long enough
+ let tail = match vec {
+ [_, tail..] => tail,
+ _ => panic!("a")
+ };
+ tail
+}
+
+fn b<'a>() -> &'a [isize] {
+ let vec = vec!(1, 2, 3, 4);
+ let vec: &[isize] = &vec; //~ ERROR does not live long enough
+ let init = match vec {
+ [init.., _] => init,
+ _ => panic!("b")
+ };
+ init
+}
+
+fn c<'a>() -> &'a [isize] {
+ let vec = vec!(1, 2, 3, 4);
+ let vec: &[isize] = &vec; //~ ERROR does not live long enough
+ let slice = match vec {
+ [_, slice.., _] => slice,
+ _ => panic!("c")
+ };
+ slice
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(slice_patterns)]
+
+fn a() {
+ let mut v = vec!(1, 2, 3);
+ let vb: &mut [isize] = &mut v;
+ match vb {
+ [_a, tail..] => {
+ v.push(tail[0] + tail[1]); //~ ERROR cannot borrow
+ }
+ _ => {}
+ };
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(slice_patterns)]
+
+fn main() {
+ let mut a = [1, 2, 3, 4];
+ let t = match a {
+ [1, 2, tail..] => tail,
+ _ => unreachable!()
+ };
+ println!("t[0]: {}", t[0]);
+ a[2] = 0; //~ ERROR cannot assign to `a[..]` because it is borrowed
+ println!("t[0]: {}", t[0]);
+ t[0];
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(advanced_slice_patterns)]
+#![feature(box_patterns)]
+#![feature(box_syntax)]
+#![feature(slice_patterns)]
+
+fn a() {
+ let mut vec = [box 1, box 2, box 3];
+ match vec {
+ [box ref _a, _, _] => {
+ vec[0] = box 4; //~ ERROR cannot assign
+ }
+ }
+}
+
+fn b() {
+ let mut vec = vec!(box 1, box 2, box 3);
+ let vec: &mut [Box<isize>] = &mut vec;
+ match vec {
+ [_b..] => {
+ vec[0] = box 4; //~ ERROR cannot assign
+ }
+ }
+}
+
+fn c() {
+ let mut vec = vec!(box 1, box 2, box 3);
+ let vec: &mut [Box<isize>] = &mut vec;
+ match vec {
+ [_a, //~ ERROR cannot move out
+ _b..] => { //~^ NOTE attempting to move value to here
+
+ // Note: `_a` is *moved* here, but `b` is borrowing,
+ // hence illegal.
+ //
+ // See comment in middle/borrowck/gather_loans/mod.rs
+ // in the case covering these sorts of vectors.
+ }
+ _ => {}
+ }
+ let a = vec[0]; //~ ERROR cannot move out
+}
+
+fn d() {
+ let mut vec = vec!(box 1, box 2, box 3);
+ let vec: &mut [Box<isize>] = &mut vec;
+ match vec {
+ [_a.., //~ ERROR cannot move out
+ _b] => {} //~ NOTE attempting to move value to here
+ _ => {}
+ }
+ let a = vec[0]; //~ ERROR cannot move out
+}
+
+fn e() {
+ let mut vec = vec!(box 1, box 2, box 3);
+ let vec: &mut [Box<isize>] = &mut vec;
+ match vec {
+ [_a, _b, _c] => {} //~ ERROR cannot move out
+ //~^ NOTE attempting to move value to here
+ //~^^ NOTE and here
+ //~^^^ NOTE and here
+ _ => {}
+ }
+ let a = vec[0]; //~ ERROR cannot move out
+ //~^ NOTE attempting to move value to here
+}
+
+fn main() {}
--- /dev/null
+// 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.
+//
+// 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.
+
+#![feature(slice_patterns)]
+
+fn a<'a>() -> &'a isize {
+ let vec = vec!(1, 2, 3, 4);
+ let vec: &[isize] = &vec; //~ ERROR `vec` does not live long enough
+ let tail = match vec {
+ [_a, tail..] => &tail[0],
+ _ => panic!("foo")
+ };
+ tail
+}
+
+fn main() {
+ let fifth = a();
+ println!("{}", *fifth);
+}
--- /dev/null
+// 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 test(cond: bool) {
+ let v;
+ while cond {
+ v = 3;
+ break;
+ }
+ println!("{}", v); //~ ERROR use of possibly uninitialized variable: `v`
+}
+
+fn main() {
+ test(true);
+}
--- /dev/null
+// 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 main() {
+ let x: bool;
+ while x { } //~ ERROR use of possibly uninitialized variable: `x`
+}
--- /dev/null
+// 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() -> isize {
+ let mut x: isize;
+ while 1 == 1 { x = 10; }
+ return x; //~ ERROR use of possibly uninitialized variable: `x`
+}
+
+fn main() { f(); }
+++ /dev/null
-// 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.
-
-// pretty-expanded FIXME #23616
-
-pub fn main() {
- struct A {
- a: isize,
- w: B,
- }
- struct B {
- a: isize
- }
- let mut p = A {
- a: 1,
- w: B {a: 1},
- };
-
- // even though `x` is not declared as a mutable field,
- // `p` as a whole is mutable, so it can be modified.
- p.a = 2;
-
- // this is true for an interior field too
- p.w.a = 2;
-}
+++ /dev/null
-// 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.
-
-
-// pretty-expanded FIXME #23616
-
-struct F { f: Vec<isize> }
-
-fn impure(_v: &[isize]) {
-}
-
-pub fn main() {
- let mut x = F {f: vec!(3)};
-
- match x {
- F {f: ref mut v} => {
- impure(v);
- }
- }
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-
-fn borrow<F>(x: &isize, f: F) where F: FnOnce(&isize) {
- f(x)
-}
-
-fn test1(x: &Box<isize>) {
- borrow(&*(*x).clone(), |p| {
- let x_a = &**x as *const isize;
- assert!((x_a as usize) != (p as *const isize as usize));
- assert_eq!(unsafe{*x_a}, *p);
- })
-}
-
-pub fn main() {
- test1(&box 22);
-}
+++ /dev/null
-// Copyright 2013 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.
-
-// Test that freezing an `&mut` pointer while referent is
-// frozen is legal.
-//
-// Example from src/librustc_borrowck/borrowck/README.md
-
-// pretty-expanded FIXME #23616
-
-fn foo<'a>(mut t0: &'a mut isize,
- mut t1: &'a mut isize) {
- let p: &isize = &*t0; // Freezes `*t0`
- let mut t2 = &t0;
- let q: &isize = &**t2; // Freezes `*t0`, but that's ok...
- let r: &isize = &*t0; // ...after all, could do same thing directly.
-}
-
-pub fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Tests that two closures can simultaneously have immutable
-// access to the variable, whether that immutable access be used
-// for direct reads or for taking immutable ref. Also check
-// that the main function can read the variable too while
-// the closures are in scope. Issue #6801.
-
-
-fn a() -> i32 {
- let mut x = 3;
- x += 1;
- let c1 = || x * 4;
- let c2 = || x * 5;
- c1() * c2() * x
-}
-
-fn get(x: &i32) -> i32 {
- *x * 4
-}
-
-fn b() -> i32 {
- let mut x = 3;
- x += 1;
- let c1 = || get(&x);
- let c2 = || get(&x);
- c1() * c2() * x
-}
-
-fn c() -> i32 {
- let mut x = 3;
- x += 1;
- let c1 = || x * 5;
- let c2 = || get(&x);
- c1() * c2() * x
-}
-
-pub fn main() {
- assert_eq!(a(), 1280);
- assert_eq!(b(), 1024);
- assert_eq!(c(), 1280);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// pretty-expanded FIXME #23616
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-
-struct A { a: isize, b: Box<isize> }
-struct B { a: Box<isize>, b: Box<isize> }
-
-fn move_after_copy() {
- let x = A { a: 1, b: box 2 };
- drop(x.a);
- drop(x.b);
-}
-
-fn move_after_fu_copy() {
- let x = A { a: 1, b: box 2 };
- let _y = A { b: box 3, .. x };
- drop(x.b);
-}
-
-fn fu_move_after_copy() {
- let x = A { a: 1, b: box 2 };
- drop(x.a);
- let _y = A { a: 3, .. x };
-}
-
-fn fu_move_after_fu_copy() {
- let x = A { a: 1, b: box 2 };
- let _y = A { b: box 3, .. x };
- let _z = A { a: 4, .. x };
-}
-
-fn copy_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- drop(x.a);
-}
-
-fn copy_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let y = A { a: 3, .. x };
- drop(x.a);
-}
-
-fn fu_copy_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- let _y = A { b: box 3, .. x };
-}
-
-fn fu_copy_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- let _z = A { b: box 3, .. x };
-}
-
-fn borrow_after_move() {
- let x = A { a: 1, b: box 2 };
- drop(x.b);
- let p = &x.a;
- drop(*p);
-}
-
-fn borrow_after_fu_move() {
- let x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- let p = &x.a;
- drop(*p);
-}
-
-fn move_after_borrow() {
- let x = A { a: 1, b: box 2 };
- let p = &x.a;
- drop(x.b);
- drop(*p);
-}
-
-fn fu_move_after_borrow() {
- let x = A { a: 1, b: box 2 };
- let p = &x.a;
- let _y = A { a: 3, .. x };
- drop(*p);
-}
-
-fn mut_borrow_after_mut_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.a;
- let q = &mut x.b;
- drop(*p);
- drop(**q);
-}
-
-fn move_after_move() {
- let x = B { a: box 1, b: box 2 };
- drop(x.a);
- drop(x.b);
-}
-
-fn move_after_fu_move() {
- let x = B { a: box 1, b: box 2 };
- let y = B { a: box 3, .. x };
- drop(x.a);
-}
-
-fn fu_move_after_move() {
- let x = B { a: box 1, b: box 2 };
- drop(x.a);
- let z = B { a: box 3, .. x };
- drop(z.b);
-}
-
-fn fu_move_after_fu_move() {
- let x = B { a: box 1, b: box 2 };
- let _y = B { b: box 3, .. x };
- let _z = B { a: box 4, .. x };
-}
-
-fn copy_after_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- drop(x.b);
- x = A { a: 3, b: box 4 };
- drop(*x.b);
-}
-
-fn copy_after_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x = A { a: 3, b: box 4 };
- drop(*x.b);
-}
-
-fn copy_after_field_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- drop(x.b);
- x.b = box 3;
- drop(*x.b);
-}
-
-fn copy_after_field_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x.b = box 3;
- drop(*x.b);
-}
-
-fn borrow_after_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- drop(x.b);
- x = A { a: 3, b: box 4 };
- let p = &x.b;
- drop(**p);
-}
-
-fn borrow_after_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x = A { a: 3, b: box 4 };
- let p = &x.b;
- drop(**p);
-}
-
-fn borrow_after_field_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- drop(x.b);
- x.b = box 3;
- let p = &x.b;
- drop(**p);
-}
-
-fn borrow_after_field_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x.b = box 3;
- let p = &x.b;
- drop(**p);
-}
-
-fn move_after_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = x.b;
- x = A { a: 3, b: box 4 };
- drop(x.b);
-}
-
-fn move_after_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x = A { a: 3, b: box 4 };
- drop(x.b);
-}
-
-fn move_after_field_assign_after_move() {
- let mut x = A { a: 1, b: box 2 };
- drop(x.b);
- x.b = box 3;
- drop(x.b);
-}
-
-fn move_after_field_assign_after_fu_move() {
- let mut x = A { a: 1, b: box 2 };
- let _y = A { a: 3, .. x };
- x.b = box 3;
- drop(x.b);
-}
-
-fn copy_after_assign_after_uninit() {
- let mut x: A;
- x = A { a: 1, b: box 2 };
- drop(x.a);
-}
-
-fn borrow_after_assign_after_uninit() {
- let mut x: A;
- x = A { a: 1, b: box 2 };
- let p = &x.a;
- drop(*p);
-}
-
-fn move_after_assign_after_uninit() {
- let mut x: A;
- x = A { a: 1, b: box 2 };
- drop(x.b);
-}
-
-fn main() {
- move_after_copy();
- move_after_fu_copy();
- fu_move_after_copy();
- fu_move_after_fu_copy();
- copy_after_move();
- copy_after_fu_move();
- fu_copy_after_move();
- fu_copy_after_fu_move();
-
- borrow_after_move();
- borrow_after_fu_move();
- move_after_borrow();
- fu_move_after_borrow();
- mut_borrow_after_mut_borrow();
-
- move_after_move();
- move_after_fu_move();
- fu_move_after_move();
- fu_move_after_fu_move();
-
- copy_after_assign_after_move();
- copy_after_assign_after_fu_move();
- copy_after_field_assign_after_move();
- copy_after_field_assign_after_fu_move();
-
- borrow_after_assign_after_move();
- borrow_after_assign_after_fu_move();
- borrow_after_field_assign_after_move();
- borrow_after_field_assign_after_fu_move();
-
- move_after_assign_after_move();
- move_after_assign_after_fu_move();
- move_after_field_assign_after_move();
- move_after_field_assign_after_fu_move();
-
- copy_after_assign_after_uninit();
- borrow_after_assign_after_uninit();
- move_after_assign_after_uninit();
-}
+++ /dev/null
-// 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.
-
-
-pub fn main() {
- let x = [22];
- let y = &x[0];
- assert_eq!(*y, 22);
-}
+++ /dev/null
-// Copyright 2012-2013 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.
-
-// Test that a `&mut` inside of an `&` is freezable.
-
-
-struct MutSlice<'a, T:'a> {
- data: &'a mut [T]
-}
-
-fn get<'a, T>(ms: &'a MutSlice<'a, T>, index: usize) -> &'a T {
- &ms.data[index]
-}
-
-pub fn main() {
- let mut data = [1, 2, 3];
- {
- let slice = MutSlice { data: &mut data };
- slice.data[0] += 4;
- let index0 = get(&slice, 0);
- let index1 = get(&slice, 1);
- let index2 = get(&slice, 2);
- assert_eq!(*index0, 5);
- assert_eq!(*index1, 2);
- assert_eq!(*index2, 3);
- }
- assert_eq!(data[0], 5);
- assert_eq!(data[1], 2);
- assert_eq!(data[2], 3);
-}
+++ /dev/null
-// 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.
-
-
-// pretty-expanded FIXME #23616
-
-fn borrow(_v: &isize) {}
-
-fn borrow_from_arg_imm_ref(v: Box<isize>) {
- borrow(&*v);
-}
-
-fn borrow_from_arg_mut_ref(v: &mut Box<isize>) {
- borrow(&**v);
-}
-
-fn borrow_from_arg_copy(v: Box<isize>) {
- borrow(&*v);
-}
-
-pub fn main() {
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// Check that we do not ICE when compiling this
-// macro, which reuses the expression `$id`
-
-
-#![feature(box_patterns)]
-#![feature(box_syntax)]
-
-struct Foo {
- a: isize
-}
-
-pub enum Bar {
- Bar1, Bar2(isize, Box<Bar>),
-}
-
-impl Foo {
- fn elaborate_stm(&mut self, s: Box<Bar>) -> Box<Bar> {
- macro_rules! declare {
- ($id:expr, $rest:expr) => ({
- self.check_id($id);
- box Bar::Bar2($id, $rest)
- })
- }
- match s {
- box Bar::Bar2(id, rest) => declare!(id, self.elaborate_stm(rest)),
- _ => panic!()
- }
- }
-
- fn check_id(&mut self, s: isize) { panic!() }
-}
-
-pub fn main() { }
+++ /dev/null
-// 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.
-//
-// 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.
-
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-#![feature(unboxed_closures)]
-
-pub fn main() {
- let bar: Box<_> = box 3;
- let h = || -> isize { *bar };
- assert_eq!(h(), 3);
-}
+++ /dev/null
-// 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.
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-
-use std::mem::swap;
-
-#[derive(Debug)]
-struct Ints {sum: Box<isize>, values: Vec<isize> }
-
-fn add_int(x: &mut Ints, v: isize) {
- *x.sum += v;
- let mut values = Vec::new();
- swap(&mut values, &mut x.values);
- values.push(v);
- swap(&mut values, &mut x.values);
-}
-
-fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&isize) -> bool {
- let l = x.values.len();
- (0..l).all(|i| f(&x.values[i]))
-}
-
-pub fn main() {
- let mut ints: Box<_> = box Ints {sum: box 0, values: Vec::new()};
- add_int(&mut *ints, 22);
- add_int(&mut *ints, 44);
-
- iter_ints(&*ints, |i| {
- println!("isize = {:?}", *i);
- true
- });
-
- println!("ints={:?}", ints);
-}
+++ /dev/null
-// 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 want_slice(v: &[isize]) -> isize {
- let mut sum = 0;
- for i in v { sum += *i; }
- sum
-}
-
-fn has_mut_vec(v: Vec<isize> ) -> isize {
- want_slice(&v)
-}
-
-pub fn main() {
- assert_eq!(has_mut_vec(vec!(1, 2, 3)), 6);
-}
+++ /dev/null
-// Copyright 2012-2014 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.
-
-// ignore-pretty
-
-fn match_ref(v: Option<isize>) -> isize {
- match v {
- Some(ref i) => {
- *i
- }
- None => {0}
- }
-}
-
-fn match_ref_unused(v: Option<isize>) {
- match v {
- Some(_) => {}
- None => {}
- }
-}
-
-fn impure(_i: isize) {
-}
-
-fn match_imm_reg(v: &Option<isize>) {
- match *v {
- Some(ref i) => {impure(*i)} // OK because immutable
- None => {}
- }
-}
-
-fn match_mut_reg(v: &mut Option<isize>) {
- match *v {
- Some(ref i) => {impure(*i)} // OK, frozen
- None => {}
- }
-}
-
-pub fn main() {
-}
+++ /dev/null
-// 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.
-
-
-pub fn main() {
- let mut x = None;
- match x {
- None => {
- // It is ok to reassign x here, because there is in
- // fact no outstanding loan of x!
- x = Some(0);
- }
- Some(_) => { }
- }
- assert_eq!(x, Some(0));
-}
+++ /dev/null
-// 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.
-
-
-struct Counter {
- value: usize
-}
-
-impl Counter {
- fn new(v: usize) -> Counter {
- Counter {value: v}
- }
-
- fn inc<'a>(&'a mut self) -> &'a mut Counter {
- self.value += 1;
- self
- }
-
- fn get(&self) -> usize {
- self.value
- }
-
- fn get_and_inc(&mut self) -> usize {
- let v = self.value;
- self.value += 1;
- v
- }
-}
-
-pub fn main() {
- let v = Counter::new(22).get_and_inc();
- assert_eq!(v, 22);
-
- let v = Counter::new(22).inc().inc().get();
- assert_eq!(v, 24);
-}
+++ /dev/null
-// Copyright 2012-2013 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.
-
-// Tests that the scope of the pointer returned from `get()` is
-// limited to the deref operation itself, and does not infect the
-// block as a whole.
-
-
-struct Box {
- x: usize
-}
-
-impl Box {
- fn get(&self) -> &usize {
- &self.x
- }
- fn set(&mut self, x: usize) {
- self.x = x;
- }
-}
-
-fn fun1() {
- // in the past, borrow checker behaved differently when
- // init and decl of `v` were distinct
- let v;
- let mut a_box = Box {x: 0};
- a_box.set(22);
- v = *a_box.get();
- a_box.set(v+1);
- assert_eq!(23, *a_box.get());
-}
-
-fn fun2() {
- let mut a_box = Box {x: 0};
- a_box.set(22);
- let v = *a_box.get();
- a_box.set(v+1);
- assert_eq!(23, *a_box.get());
-}
-
-pub fn main() {
- fun1();
- fun2();
-}
+++ /dev/null
-// Copyright 2013 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.
-
-// Regression test for issue #7740
-
-// pretty-expanded FIXME #23616
-
-pub fn main() {
- static A: &'static char = &'A';
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// This test verifies that casting from the same lifetime on a value
-// to the same lifetime on a trait succeeds. See issue #10766.
-
-// pretty-expanded FIXME #23616
-
-#![allow(dead_code)]
-
-use std::marker;
-
-fn main() {
- trait T { fn foo(&self) {} }
-
- fn f<'a, V: T>(v: &'a V) -> &'a T {
- v as &'a T
- }
-}
+++ /dev/null
-// 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.
-
-
-// pretty-expanded FIXME #23616
-
-struct Rec {
- f: Box<isize>,
-}
-
-struct Outer {
- f: Inner
-}
-
-struct Inner {
- g: Innermost
-}
-
-struct Innermost {
- h: Box<isize>,
-}
-
-fn borrow(_v: &isize) {}
-
-fn box_mut(v: &mut Box<isize>) {
- borrow(&**v); // OK: &mut -> &imm
-}
-
-fn box_mut_rec(v: &mut Rec) {
- borrow(&*v.f); // OK: &mut -> &imm
-}
-
-fn box_mut_recs(v: &mut Outer) {
- borrow(&*v.f.g.h); // OK: &mut -> &imm
-}
-
-fn box_imm(v: &Box<isize>) {
- borrow(&**v); // OK
-}
-
-fn box_imm_rec(v: &Rec) {
- borrow(&*v.f); // OK
-}
-
-fn box_imm_recs(v: &Outer) {
- borrow(&*v.f.g.h); // OK
-}
-
-pub fn main() {
-}
+++ /dev/null
-// 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.
-
-
-
-use std::cell::Cell;
-
-#[derive(Copy, Clone)]
-enum newtype {
- newvar(isize)
-}
-
-pub fn main() {
-
- // Test that borrowck treats enums with a single variant
- // specially.
-
- let x = &Cell::new(5);
- let y = &Cell::new(newtype::newvar(3));
- let z = match y.get() {
- newtype::newvar(b) => {
- x.set(x.get() + 1);
- x.get() * b
- }
- };
- assert_eq!(z, 18);
-}
+++ /dev/null
-// 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.
-//
-// 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.
-
-// pretty-expanded FIXME #23616
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-
-struct A { a: isize, b: Box<isize> }
-
-fn field_copy_after_field_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.b;
- drop(x.a);
- **p = 3;
-}
-
-fn fu_field_copy_after_field_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.b;
- let y = A { b: box 3, .. x };
- drop(y);
- **p = 4;
-}
-
-fn field_deref_after_field_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.a;
- drop(*x.b);
- *p = 3;
-}
-
-fn field_move_after_field_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.a;
- drop(x.b);
- *p = 3;
-}
-
-fn fu_field_move_after_field_borrow() {
- let mut x = A { a: 1, b: box 2 };
- let p = &mut x.a;
- let y = A { a: 3, .. x };
- drop(y);
- *p = 4;
-}
-
-fn main() {
- field_copy_after_field_borrow();
- fu_field_copy_after_field_borrow();
- field_deref_after_field_borrow();
- field_move_after_field_borrow();
- fu_field_move_after_field_borrow();
-}
--- /dev/null
+// 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.
+
+// pretty-expanded FIXME #23616
+
+pub fn main() {
+ struct A {
+ a: isize,
+ w: B,
+ }
+ struct B {
+ a: isize
+ }
+ let mut p = A {
+ a: 1,
+ w: B {a: 1},
+ };
+
+ // even though `x` is not declared as a mutable field,
+ // `p` as a whole is mutable, so it can be modified.
+ p.a = 2;
+
+ // this is true for an interior field too
+ p.w.a = 2;
+}
--- /dev/null
+// 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.
+
+
+// pretty-expanded FIXME #23616
+
+struct F { f: Vec<isize> }
+
+fn impure(_v: &[isize]) {
+}
+
+pub fn main() {
+ let mut x = F {f: vec!(3)};
+
+ match x {
+ F {f: ref mut v} => {
+ impure(v);
+ }
+ }
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+
+#![allow(unknown_features)]
+#![feature(box_syntax)]
+
+fn borrow<F>(x: &isize, f: F) where F: FnOnce(&isize) {
+ f(x)
+}
+
+fn test1(x: &Box<isize>) {
+ borrow(&*(*x).clone(), |p| {
+ let x_a = &**x as *const isize;
+ assert!((x_a as usize) != (p as *const isize as usize));
+ assert_eq!(unsafe{*x_a}, *p);
+ })
+}
+
+pub fn main() {
+ test1(&box 22);
+}
--- /dev/null
+// Copyright 2013 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.
+
+// Test that freezing an `&mut` pointer while referent is
+// frozen is legal.
+//
+// Example from src/librustc_borrowck/borrowck/README.md
+
+// pretty-expanded FIXME #23616
+
+fn foo<'a>(mut t0: &'a mut isize,
+ mut t1: &'a mut isize) {
+ let p: &isize = &*t0; // Freezes `*t0`
+ let mut t2 = &t0;
+ let q: &isize = &**t2; // Freezes `*t0`, but that's ok...
+ let r: &isize = &*t0; // ...after all, could do same thing directly.
+}
+
+pub fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Tests that two closures can simultaneously have immutable
+// access to the variable, whether that immutable access be used
+// for direct reads or for taking immutable ref. Also check
+// that the main function can read the variable too while
+// the closures are in scope. Issue #6801.
+
+
+fn a() -> i32 {
+ let mut x = 3;
+ x += 1;
+ let c1 = || x * 4;
+ let c2 = || x * 5;
+ c1() * c2() * x
+}
+
+fn get(x: &i32) -> i32 {
+ *x * 4
+}
+
+fn b() -> i32 {
+ let mut x = 3;
+ x += 1;
+ let c1 = || get(&x);
+ let c2 = || get(&x);
+ c1() * c2() * x
+}
+
+fn c() -> i32 {
+ let mut x = 3;
+ x += 1;
+ let c1 = || x * 5;
+ let c2 = || get(&x);
+ c1() * c2() * x
+}
+
+pub fn main() {
+ assert_eq!(a(), 1280);
+ assert_eq!(b(), 1024);
+ assert_eq!(c(), 1280);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// pretty-expanded FIXME #23616
+
+#![allow(unknown_features)]
+#![feature(box_syntax)]
+
+struct A { a: isize, b: Box<isize> }
+struct B { a: Box<isize>, b: Box<isize> }
+
+fn move_after_copy() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.a);
+ drop(x.b);
+}
+
+fn move_after_fu_copy() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { b: box 3, .. x };
+ drop(x.b);
+}
+
+fn fu_move_after_copy() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.a);
+ let _y = A { a: 3, .. x };
+}
+
+fn fu_move_after_fu_copy() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { b: box 3, .. x };
+ let _z = A { a: 4, .. x };
+}
+
+fn copy_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ drop(x.a);
+}
+
+fn copy_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let y = A { a: 3, .. x };
+ drop(x.a);
+}
+
+fn fu_copy_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ let _y = A { b: box 3, .. x };
+}
+
+fn fu_copy_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ let _z = A { b: box 3, .. x };
+}
+
+fn borrow_after_move() {
+ let x = A { a: 1, b: box 2 };
+ drop(x.b);
+ let p = &x.a;
+ drop(*p);
+}
+
+fn borrow_after_fu_move() {
+ let x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ let p = &x.a;
+ drop(*p);
+}
+
+fn move_after_borrow() {
+ let x = A { a: 1, b: box 2 };
+ let p = &x.a;
+ drop(x.b);
+ drop(*p);
+}
+
+fn fu_move_after_borrow() {
+ let x = A { a: 1, b: box 2 };
+ let p = &x.a;
+ let _y = A { a: 3, .. x };
+ drop(*p);
+}
+
+fn mut_borrow_after_mut_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.a;
+ let q = &mut x.b;
+ drop(*p);
+ drop(**q);
+}
+
+fn move_after_move() {
+ let x = B { a: box 1, b: box 2 };
+ drop(x.a);
+ drop(x.b);
+}
+
+fn move_after_fu_move() {
+ let x = B { a: box 1, b: box 2 };
+ let y = B { a: box 3, .. x };
+ drop(x.a);
+}
+
+fn fu_move_after_move() {
+ let x = B { a: box 1, b: box 2 };
+ drop(x.a);
+ let z = B { a: box 3, .. x };
+ drop(z.b);
+}
+
+fn fu_move_after_fu_move() {
+ let x = B { a: box 1, b: box 2 };
+ let _y = B { b: box 3, .. x };
+ let _z = B { a: box 4, .. x };
+}
+
+fn copy_after_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ drop(x.b);
+ x = A { a: 3, b: box 4 };
+ drop(*x.b);
+}
+
+fn copy_after_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x = A { a: 3, b: box 4 };
+ drop(*x.b);
+}
+
+fn copy_after_field_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ drop(x.b);
+ x.b = box 3;
+ drop(*x.b);
+}
+
+fn copy_after_field_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x.b = box 3;
+ drop(*x.b);
+}
+
+fn borrow_after_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ drop(x.b);
+ x = A { a: 3, b: box 4 };
+ let p = &x.b;
+ drop(**p);
+}
+
+fn borrow_after_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x = A { a: 3, b: box 4 };
+ let p = &x.b;
+ drop(**p);
+}
+
+fn borrow_after_field_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ drop(x.b);
+ x.b = box 3;
+ let p = &x.b;
+ drop(**p);
+}
+
+fn borrow_after_field_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x.b = box 3;
+ let p = &x.b;
+ drop(**p);
+}
+
+fn move_after_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = x.b;
+ x = A { a: 3, b: box 4 };
+ drop(x.b);
+}
+
+fn move_after_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x = A { a: 3, b: box 4 };
+ drop(x.b);
+}
+
+fn move_after_field_assign_after_move() {
+ let mut x = A { a: 1, b: box 2 };
+ drop(x.b);
+ x.b = box 3;
+ drop(x.b);
+}
+
+fn move_after_field_assign_after_fu_move() {
+ let mut x = A { a: 1, b: box 2 };
+ let _y = A { a: 3, .. x };
+ x.b = box 3;
+ drop(x.b);
+}
+
+fn copy_after_assign_after_uninit() {
+ let mut x: A;
+ x = A { a: 1, b: box 2 };
+ drop(x.a);
+}
+
+fn borrow_after_assign_after_uninit() {
+ let mut x: A;
+ x = A { a: 1, b: box 2 };
+ let p = &x.a;
+ drop(*p);
+}
+
+fn move_after_assign_after_uninit() {
+ let mut x: A;
+ x = A { a: 1, b: box 2 };
+ drop(x.b);
+}
+
+fn main() {
+ move_after_copy();
+ move_after_fu_copy();
+ fu_move_after_copy();
+ fu_move_after_fu_copy();
+ copy_after_move();
+ copy_after_fu_move();
+ fu_copy_after_move();
+ fu_copy_after_fu_move();
+
+ borrow_after_move();
+ borrow_after_fu_move();
+ move_after_borrow();
+ fu_move_after_borrow();
+ mut_borrow_after_mut_borrow();
+
+ move_after_move();
+ move_after_fu_move();
+ fu_move_after_move();
+ fu_move_after_fu_move();
+
+ copy_after_assign_after_move();
+ copy_after_assign_after_fu_move();
+ copy_after_field_assign_after_move();
+ copy_after_field_assign_after_fu_move();
+
+ borrow_after_assign_after_move();
+ borrow_after_assign_after_fu_move();
+ borrow_after_field_assign_after_move();
+ borrow_after_field_assign_after_fu_move();
+
+ move_after_assign_after_move();
+ move_after_assign_after_fu_move();
+ move_after_field_assign_after_move();
+ move_after_field_assign_after_fu_move();
+
+ copy_after_assign_after_uninit();
+ borrow_after_assign_after_uninit();
+ move_after_assign_after_uninit();
+}
--- /dev/null
+// 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.
+
+
+pub fn main() {
+ let x = [22];
+ let y = &x[0];
+ assert_eq!(*y, 22);
+}
--- /dev/null
+// Copyright 2012-2013 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.
+
+// Test that a `&mut` inside of an `&` is freezable.
+
+
+struct MutSlice<'a, T:'a> {
+ data: &'a mut [T]
+}
+
+fn get<'a, T>(ms: &'a MutSlice<'a, T>, index: usize) -> &'a T {
+ &ms.data[index]
+}
+
+pub fn main() {
+ let mut data = [1, 2, 3];
+ {
+ let slice = MutSlice { data: &mut data };
+ slice.data[0] += 4;
+ let index0 = get(&slice, 0);
+ let index1 = get(&slice, 1);
+ let index2 = get(&slice, 2);
+ assert_eq!(*index0, 5);
+ assert_eq!(*index1, 2);
+ assert_eq!(*index2, 3);
+ }
+ assert_eq!(data[0], 5);
+ assert_eq!(data[1], 2);
+ assert_eq!(data[2], 3);
+}
--- /dev/null
+// 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.
+
+
+// pretty-expanded FIXME #23616
+
+fn borrow(_v: &isize) {}
+
+fn borrow_from_arg_imm_ref(v: Box<isize>) {
+ borrow(&*v);
+}
+
+fn borrow_from_arg_mut_ref(v: &mut Box<isize>) {
+ borrow(&**v);
+}
+
+fn borrow_from_arg_copy(v: Box<isize>) {
+ borrow(&*v);
+}
+
+pub fn main() {
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// Check that we do not ICE when compiling this
+// macro, which reuses the expression `$id`
+
+
+#![feature(box_patterns)]
+#![feature(box_syntax)]
+
+struct Foo {
+ a: isize
+}
+
+pub enum Bar {
+ Bar1, Bar2(isize, Box<Bar>),
+}
+
+impl Foo {
+ fn elaborate_stm(&mut self, s: Box<Bar>) -> Box<Bar> {
+ macro_rules! declare {
+ ($id:expr, $rest:expr) => ({
+ self.check_id($id);
+ box Bar::Bar2($id, $rest)
+ })
+ }
+ match s {
+ box Bar::Bar2(id, rest) => declare!(id, self.elaborate_stm(rest)),
+ _ => panic!()
+ }
+ }
+
+ fn check_id(&mut self, s: isize) { panic!() }
+}
+
+pub fn main() { }
--- /dev/null
+// 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.
+//
+// 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.
+
+
+#![allow(unknown_features)]
+#![feature(box_syntax)]
+#![feature(unboxed_closures)]
+
+pub fn main() {
+ let bar: Box<_> = box 3;
+ let h = || -> isize { *bar };
+ assert_eq!(h(), 3);
+}
--- /dev/null
+// 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.
+
+#![allow(unknown_features)]
+#![feature(box_syntax)]
+
+use std::mem::swap;
+
+#[derive(Debug)]
+struct Ints {sum: Box<isize>, values: Vec<isize> }
+
+fn add_int(x: &mut Ints, v: isize) {
+ *x.sum += v;
+ let mut values = Vec::new();
+ swap(&mut values, &mut x.values);
+ values.push(v);
+ swap(&mut values, &mut x.values);
+}
+
+fn iter_ints<F>(x: &Ints, mut f: F) -> bool where F: FnMut(&isize) -> bool {
+ let l = x.values.len();
+ (0..l).all(|i| f(&x.values[i]))
+}
+
+pub fn main() {
+ let mut ints: Box<_> = box Ints {sum: box 0, values: Vec::new()};
+ add_int(&mut *ints, 22);
+ add_int(&mut *ints, 44);
+
+ iter_ints(&*ints, |i| {
+ println!("isize = {:?}", *i);
+ true
+ });
+
+ println!("ints={:?}", ints);
+}
--- /dev/null
+// 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 want_slice(v: &[isize]) -> isize {
+ let mut sum = 0;
+ for i in v { sum += *i; }
+ sum
+}
+
+fn has_mut_vec(v: Vec<isize> ) -> isize {
+ want_slice(&v)
+}
+
+pub fn main() {
+ assert_eq!(has_mut_vec(vec!(1, 2, 3)), 6);
+}
--- /dev/null
+// Copyright 2012-2014 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.
+
+// ignore-pretty
+
+fn match_ref(v: Option<isize>) -> isize {
+ match v {
+ Some(ref i) => {
+ *i
+ }
+ None => {0}
+ }
+}
+
+fn match_ref_unused(v: Option<isize>) {
+ match v {
+ Some(_) => {}
+ None => {}
+ }
+}
+
+fn impure(_i: isize) {
+}
+
+fn match_imm_reg(v: &Option<isize>) {
+ match *v {
+ Some(ref i) => {impure(*i)} // OK because immutable
+ None => {}
+ }
+}
+
+fn match_mut_reg(v: &mut Option<isize>) {
+ match *v {
+ Some(ref i) => {impure(*i)} // OK, frozen
+ None => {}
+ }
+}
+
+pub fn main() {
+}
--- /dev/null
+// 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.
+
+
+pub fn main() {
+ let mut x = None;
+ match x {
+ None => {
+ // It is ok to reassign x here, because there is in
+ // fact no outstanding loan of x!
+ x = Some(0);
+ }
+ Some(_) => { }
+ }
+ assert_eq!(x, Some(0));
+}
--- /dev/null
+// 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.
+
+
+struct Counter {
+ value: usize
+}
+
+impl Counter {
+ fn new(v: usize) -> Counter {
+ Counter {value: v}
+ }
+
+ fn inc<'a>(&'a mut self) -> &'a mut Counter {
+ self.value += 1;
+ self
+ }
+
+ fn get(&self) -> usize {
+ self.value
+ }
+
+ fn get_and_inc(&mut self) -> usize {
+ let v = self.value;
+ self.value += 1;
+ v
+ }
+}
+
+pub fn main() {
+ let v = Counter::new(22).get_and_inc();
+ assert_eq!(v, 22);
+
+ let v = Counter::new(22).inc().inc().get();
+ assert_eq!(v, 24);
+}
--- /dev/null
+// Copyright 2012-2013 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.
+
+// Tests that the scope of the pointer returned from `get()` is
+// limited to the deref operation itself, and does not infect the
+// block as a whole.
+
+
+struct Box {
+ x: usize
+}
+
+impl Box {
+ fn get(&self) -> &usize {
+ &self.x
+ }
+ fn set(&mut self, x: usize) {
+ self.x = x;
+ }
+}
+
+fn fun1() {
+ // in the past, borrow checker behaved differently when
+ // init and decl of `v` were distinct
+ let v;
+ let mut a_box = Box {x: 0};
+ a_box.set(22);
+ v = *a_box.get();
+ a_box.set(v+1);
+ assert_eq!(23, *a_box.get());
+}
+
+fn fun2() {
+ let mut a_box = Box {x: 0};
+ a_box.set(22);
+ let v = *a_box.get();
+ a_box.set(v+1);
+ assert_eq!(23, *a_box.get());
+}
+
+pub fn main() {
+ fun1();
+ fun2();
+}
--- /dev/null
+// Copyright 2013 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.
+
+// Regression test for issue #7740
+
+// pretty-expanded FIXME #23616
+
+pub fn main() {
+ static A: &'static char = &'A';
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// This test verifies that casting from the same lifetime on a value
+// to the same lifetime on a trait succeeds. See issue #10766.
+
+// pretty-expanded FIXME #23616
+
+#![allow(dead_code)]
+
+use std::marker;
+
+fn main() {
+ trait T { fn foo(&self) {} }
+
+ fn f<'a, V: T>(v: &'a V) -> &'a T {
+ v as &'a T
+ }
+}
--- /dev/null
+// 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.
+
+
+// pretty-expanded FIXME #23616
+
+struct Rec {
+ f: Box<isize>,
+}
+
+struct Outer {
+ f: Inner
+}
+
+struct Inner {
+ g: Innermost
+}
+
+struct Innermost {
+ h: Box<isize>,
+}
+
+fn borrow(_v: &isize) {}
+
+fn box_mut(v: &mut Box<isize>) {
+ borrow(&**v); // OK: &mut -> &imm
+}
+
+fn box_mut_rec(v: &mut Rec) {
+ borrow(&*v.f); // OK: &mut -> &imm
+}
+
+fn box_mut_recs(v: &mut Outer) {
+ borrow(&*v.f.g.h); // OK: &mut -> &imm
+}
+
+fn box_imm(v: &Box<isize>) {
+ borrow(&**v); // OK
+}
+
+fn box_imm_rec(v: &Rec) {
+ borrow(&*v.f); // OK
+}
+
+fn box_imm_recs(v: &Outer) {
+ borrow(&*v.f.g.h); // OK
+}
+
+pub fn main() {
+}
--- /dev/null
+// 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.
+
+
+
+use std::cell::Cell;
+
+#[derive(Copy, Clone)]
+enum newtype {
+ newvar(isize)
+}
+
+pub fn main() {
+
+ // Test that borrowck treats enums with a single variant
+ // specially.
+
+ let x = &Cell::new(5);
+ let y = &Cell::new(newtype::newvar(3));
+ let z = match y.get() {
+ newtype::newvar(b) => {
+ x.set(x.get() + 1);
+ x.get() * b
+ }
+ };
+ assert_eq!(z, 18);
+}
--- /dev/null
+// 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.
+//
+// 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.
+
+// pretty-expanded FIXME #23616
+
+#![allow(unknown_features)]
+#![feature(box_syntax)]
+
+struct A { a: isize, b: Box<isize> }
+
+fn field_copy_after_field_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.b;
+ drop(x.a);
+ **p = 3;
+}
+
+fn fu_field_copy_after_field_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.b;
+ let y = A { b: box 3, .. x };
+ drop(y);
+ **p = 4;
+}
+
+fn field_deref_after_field_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.a;
+ drop(*x.b);
+ *p = 3;
+}
+
+fn field_move_after_field_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.a;
+ drop(x.b);
+ *p = 3;
+}
+
+fn fu_field_move_after_field_borrow() {
+ let mut x = A { a: 1, b: box 2 };
+ let p = &mut x.a;
+ let y = A { a: 3, .. x };
+ drop(y);
+ *p = 4;
+}
+
+fn main() {
+ field_copy_after_field_borrow();
+ fu_field_copy_after_field_borrow();
+ field_deref_after_field_borrow();
+ field_move_after_field_borrow();
+ fu_field_move_after_field_borrow();
+}