diff options
| author | Matthew Jasper <mjjasper1@gmail.com> | 2018-08-01 20:40:06 +0100 |
|---|---|---|
| committer | Matthew Jasper <mjjasper1@gmail.com> | 2018-08-03 23:00:27 +0100 |
| commit | 5639e2173b2506ed540ca4a563f0e63731e56bcb (patch) | |
| tree | c5d5f0126f22cf071e95039dc7d1ad74a81f9eae /src/test | |
| parent | 903851f785aaa01b5ae2e9648a2fd1256eea2ba5 (diff) | |
| download | rust-5639e2173b2506ed540ca4a563f0e63731e56bcb.tar.gz rust-5639e2173b2506ed540ca4a563f0e63731e56bcb.zip | |
Tests for closure spans
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/ui/nll/closure-access-spans.rs | 68 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-access-spans.stderr | 110 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-borrow-spans.rs | 112 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-borrow-spans.stderr | 172 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-move-spans.rs | 33 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-move-spans.stderr | 39 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-use-spans.rs | 33 | ||||
| -rw-r--r-- | src/test/ui/nll/closure-use-spans.stderr | 33 | ||||
| -rw-r--r-- | src/test/ui/nll/closures-in-loops.rs | 36 | ||||
| -rw-r--r-- | src/test/ui/nll/closures-in-loops.stderr | 30 |
10 files changed, 666 insertions, 0 deletions
diff --git a/src/test/ui/nll/closure-access-spans.rs b/src/test/ui/nll/closure-access-spans.rs new file mode 100644 index 00000000000..b49436fabcf --- /dev/null +++ b/src/test/ui/nll/closure-access-spans.rs @@ -0,0 +1,68 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-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 accesses due to a closure capture give a special note + +#![feature(nll)] + +fn closure_imm_capture_conflict(mut x: i32) { + let r = &mut x; + || x; //~ ERROR + r.use_mut(); +} + +fn closure_mut_capture_conflict(mut x: i32) { + let r = &mut x; + || x = 2; //~ ERROR + r.use_mut(); +} + +fn closure_unique_capture_conflict(mut x: &mut i32) { + let r = &mut x; + || *x = 2; //~ ERROR + r.use_mut(); +} + +fn closure_copy_capture_conflict(mut x: i32) { + let r = &mut x; + move || x; //~ ERROR + r.use_ref(); +} + +fn closure_move_capture_conflict(mut x: String) { + let r = &x; + || x; //~ ERROR + r.use_ref(); +} + +fn closure_imm_capture_moved(mut x: String) { + let r = x; + || x.len(); //~ ERROR +} + +fn closure_mut_capture_moved(mut x: String) { + let r = x; + || x = String::new(); //~ ERROR +} + +fn closure_unique_capture_moved(x: &mut String) { + let r = x; + || *x = String::new(); //~ ERROR +} + +fn closure_move_capture_moved(x: &mut String) { + let r = x; + || x; //~ ERROR +} + +fn main() {} + +trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { } } +impl<T> Fake for T { } diff --git a/src/test/ui/nll/closure-access-spans.stderr b/src/test/ui/nll/closure-access-spans.stderr new file mode 100644 index 00000000000..0042b5d7d52 --- /dev/null +++ b/src/test/ui/nll/closure-access-spans.stderr @@ -0,0 +1,110 @@ +error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable + --> $DIR/closure-access-spans.rs:17:5 + | +LL | let r = &mut x; + | ------ mutable borrow occurs here +LL | || x; //~ ERROR + | ^^ - second borrow occurs due to use of `x` in closure + | | + | immutable borrow occurs here +LL | r.use_mut(); + | - borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/closure-access-spans.rs:23:5 + | +LL | let r = &mut x; + | ------ first mutable borrow occurs here +LL | || x = 2; //~ ERROR + | ^^ - second borrow occurs due to use of `x` in closure + | | + | second mutable borrow occurs here +LL | r.use_mut(); + | - borrow later used here + +error[E0500]: closure requires unique access to `x` but it is already borrowed + --> $DIR/closure-access-spans.rs:29:5 + | +LL | let r = &mut x; + | ------ borrow occurs here +LL | || *x = 2; //~ ERROR + | ^^ - second borrow occurs due to use of `x` in closure + | | + | closure construction occurs here +LL | r.use_mut(); + | - borrow later used here + +error[E0503]: cannot use `x` because it was mutably borrowed + --> $DIR/closure-access-spans.rs:35:13 + | +LL | let r = &mut x; + | ------ borrow of `x` occurs here +LL | move || x; //~ ERROR + | ^ use of borrowed `x` +LL | r.use_ref(); + | - borrow later used here + +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/closure-access-spans.rs:41:5 + | +LL | let r = &x; + | -- borrow of `x` occurs here +LL | || x; //~ ERROR + | ^^ - move occurs due to use in closure + | | + | move out of `x` occurs here +LL | r.use_ref(); + | - borrow later used here + +error[E0382]: borrow of moved value: `x` + --> $DIR/closure-access-spans.rs:47:5 + | +LL | let r = x; + | - value moved here +LL | || x.len(); //~ ERROR + | ^^ - borrow occurs due to use in closure + | | + | value borrowed here after move + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error[E0382]: borrow of moved value: `x` + --> $DIR/closure-access-spans.rs:52:5 + | +LL | let r = x; + | - value moved here +LL | || x = String::new(); //~ ERROR + | ^^ - borrow occurs due to use in closure + | | + | value borrowed here after move + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error[E0382]: borrow of moved value: `x` + --> $DIR/closure-access-spans.rs:57:5 + | +LL | let r = x; + | - value moved here +LL | || *x = String::new(); //~ ERROR + | ^^ - borrow occurs due to use in closure + | | + | value borrowed here after move + | + = note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait + +error[E0382]: use of moved value: `x` + --> $DIR/closure-access-spans.rs:62:5 + | +LL | let r = x; + | - value moved here +LL | || x; //~ ERROR + | ^^ - use occurs due to use in closure + | | + | value used here after move + | + = note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait + +error: aborting due to 9 previous errors + +Some errors occurred: E0382, E0499, E0500, E0502, E0503, E0505. +For more information about an error, try `rustc --explain E0382`. diff --git a/src/test/ui/nll/closure-borrow-spans.rs b/src/test/ui/nll/closure-borrow-spans.rs new file mode 100644 index 00000000000..d62dc27dade --- /dev/null +++ b/src/test/ui/nll/closure-borrow-spans.rs @@ -0,0 +1,112 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-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 existing borrows due to a closure capture give a special note + +#![feature(nll)] + +fn move_while_borrowed(x: String) { + let f = || x.len(); + let y = x; //~ ERROR + f.use_ref(); +} + +fn borrow_mut_while_borrowed(mut x: i32) { + let f = || x; + let y = &mut x; //~ ERROR + f.use_ref(); +} + +fn drop_while_borrowed() { + let f; + { + let x = 1; + f = || x; //~ ERROR + } + f.use_ref(); +} + +fn assign_while_borrowed(mut x: i32) { + let f = || x; + x = 1; //~ ERROR + f.use_ref(); +} + +fn copy_while_borrowed_mut(mut x: i32) { + let f = || x = 0; + let y = x; //~ ERROR + f.use_ref(); +} + +fn borrow_while_borrowed_mut(mut x: i32) { + let f = || x = 0; + let y = &x; //~ ERROR + f.use_ref(); +} + +fn borrow_mut_while_borrowed_mut(mut x: i32) { + let f = || x = 0; + let y = &mut x; //~ ERROR + f.use_ref(); +} + +fn drop_while_borrowed_mut() { + let f; + { + let mut x = 1; + f = || x = 0; //~ ERROR + } + f.use_ref(); +} + +fn assign_while_borrowed_mut(mut x: i32) { + let f = || x = 0; + x = 1; //~ ERROR + f.use_ref(); +} + +fn copy_while_borrowed_unique(x: &mut i32) { + let f = || *x = 0; + let y = x; //~ ERROR + f.use_ref(); +} + +fn borrow_while_borrowed_unique(x: &mut i32) { + let f = || *x = 0; + let y = &x; //~ ERROR + f.use_ref(); +} + +fn borrow_mut_while_borrowed_unique(mut x: &mut i32) { + let f = || *x = 0; + let y = &mut x; //~ ERROR + f.use_ref(); +} + +fn drop_while_borrowed_unique() { + let mut z = 1; + let f; + { + let x = &mut z; + f = || *x = 0; //~ ERROR + } + f.use_ref(); +} + +fn assign_while_borrowed_unique(x: &mut i32) { + let f = || *x = 0; + *x = 1; //~ ERROR + f.use_ref(); +} + +fn main() {} + +trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { } } +impl<T> Fake for T { } diff --git a/src/test/ui/nll/closure-borrow-spans.stderr b/src/test/ui/nll/closure-borrow-spans.stderr new file mode 100644 index 00000000000..1b9420b3c0b --- /dev/null +++ b/src/test/ui/nll/closure-borrow-spans.stderr @@ -0,0 +1,172 @@ +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/closure-borrow-spans.rs:17:13 + | +LL | let f = || x.len(); + | -- - borrow occurs due to use in closure + | | + | borrow of `x` occurs here +LL | let y = x; //~ ERROR + | ^ move out of `x` occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immutable + --> $DIR/closure-borrow-spans.rs:23:13 + | +LL | let f = || x; + | -- - first borrow occurs due to use of `x` in closure + | | + | immutable borrow occurs here +LL | let y = &mut x; //~ ERROR + | ^^^^^^ mutable borrow occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0597]: `x` does not live long enough + --> $DIR/closure-borrow-spans.rs:31:16 + | +LL | f = || x; //~ ERROR + | -- ^ borrowed value does not live long enough + | | + | value captured here +LL | } + | - `x` dropped here while still borrowed +LL | f.use_ref(); + | - borrow later used here + +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/closure-borrow-spans.rs:38:5 + | +LL | let f = || x; + | -- - borrow occurs due to use in closure + | | + | borrow of `x` occurs here +LL | x = 1; //~ ERROR + | ^^^^^ assignment to borrowed `x` occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0503]: cannot use `x` because it was mutably borrowed + --> $DIR/closure-borrow-spans.rs:44:13 + | +LL | let f = || x = 0; + | -- - borrow occurs due to use of `x` in closure + | | + | borrow of `x` occurs here +LL | let y = x; //~ ERROR + | ^ use of borrowed `x` +LL | f.use_ref(); + | - borrow later used here + +error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable + --> $DIR/closure-borrow-spans.rs:50:13 + | +LL | let f = || x = 0; + | -- - first borrow occurs due to use of `x` in closure + | | + | mutable borrow occurs here +LL | let y = &x; //~ ERROR + | ^^ immutable borrow occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/closure-borrow-spans.rs:56:13 + | +LL | let f = || x = 0; + | -- - first borrow occurs due to use of `x` in closure + | | + | first mutable borrow occurs here +LL | let y = &mut x; //~ ERROR + | ^^^^^^ second mutable borrow occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0597]: `x` does not live long enough + --> $DIR/closure-borrow-spans.rs:64:16 + | +LL | f = || x = 0; //~ ERROR + | -- ^ borrowed value does not live long enough + | | + | value captured here +LL | } + | - `x` dropped here while still borrowed +LL | f.use_ref(); + | - borrow later used here + +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/closure-borrow-spans.rs:71:5 + | +LL | let f = || x = 0; + | -- - borrow occurs due to use in closure + | | + | borrow of `x` occurs here +LL | x = 1; //~ ERROR + | ^^^^^ assignment to borrowed `x` occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0505]: cannot move out of `x` because it is borrowed + --> $DIR/closure-borrow-spans.rs:77:13 + | +LL | let f = || *x = 0; + | -- - borrow occurs due to use in closure + | | + | borrow of `x` occurs here +LL | let y = x; //~ ERROR + | ^ move out of `x` occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0501]: cannot borrow `x` as immutable because previous closure requires unique access + --> $DIR/closure-borrow-spans.rs:83:13 + | +LL | let f = || *x = 0; + | -- - first borrow occurs due to use of `x` in closure + | | + | closure construction occurs here +LL | let y = &x; //~ ERROR + | ^^ borrow occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0501]: cannot borrow `x` as mutable because previous closure requires unique access + --> $DIR/closure-borrow-spans.rs:89:13 + | +LL | let f = || *x = 0; + | -- - first borrow occurs due to use of `x` in closure + | | + | closure construction occurs here +LL | let y = &mut x; //~ ERROR + | ^^^^^^ borrow occurs here +LL | f.use_ref(); + | - borrow later used here + +error[E0597]: `x` does not live long enough + --> $DIR/closure-borrow-spans.rs:98:17 + | +LL | f = || *x = 0; //~ ERROR + | -- ^ borrowed value does not live long enough + | | + | value captured here +LL | } + | - `x` dropped here while still borrowed +LL | f.use_ref(); + | - borrow later used here + +error[E0506]: cannot assign to `*x` because it is borrowed + --> $DIR/closure-borrow-spans.rs:105:5 + | +LL | let f = || *x = 0; + | -- - borrow occurs due to use in closure + | | + | borrow of `*x` occurs here +LL | *x = 1; //~ ERROR + | ^^^^^^ assignment to borrowed `*x` occurs here +LL | f.use_ref(); + | - borrow later used here + +error: aborting due to 14 previous errors + +Some errors occurred: E0499, E0501, E0502, E0503, E0505, E0506, E0597. +For more information about an error, try `rustc --explain E0499`. diff --git a/src/test/ui/nll/closure-move-spans.rs b/src/test/ui/nll/closure-move-spans.rs new file mode 100644 index 00000000000..7e836275c8b --- /dev/null +++ b/src/test/ui/nll/closure-move-spans.rs @@ -0,0 +1,33 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-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 moves due to a closure capture give a special note + +#![feature(nll)] + +fn move_after_move(x: String) { + || x; + let y = x; //~ ERROR +} + +fn borrow_after_move(x: String) { + || x; + let y = &x; //~ ERROR +} + +fn borrow_mut_after_move(mut x: String) { + || x; + let y = &mut x; //~ ERROR +} + +fn fn_ref<F: Fn()>(f: F) -> F { f } +fn fn_mut<F: FnMut()>(f: F) -> F { f } + +fn main() {} diff --git a/src/test/ui/nll/closure-move-spans.stderr b/src/test/ui/nll/closure-move-spans.stderr new file mode 100644 index 00000000000..80b7b4246a7 --- /dev/null +++ b/src/test/ui/nll/closure-move-spans.stderr @@ -0,0 +1,39 @@ +error[E0382]: use of moved value: `x` + --> $DIR/closure-move-spans.rs:17:13 + | +LL | || x; + | -- - variable moved due to use in closure + | | + | value moved into closure here +LL | let y = x; //~ ERROR + | ^ value used here after move + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error[E0382]: borrow of moved value: `x` + --> $DIR/closure-move-spans.rs:22:13 + | +LL | || x; + | -- - variable moved due to use in closure + | | + | value moved into closure here +LL | let y = &x; //~ ERROR + | ^^ value borrowed here after move + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error[E0382]: borrow of moved value: `x` + --> $DIR/closure-move-spans.rs:27:13 + | +LL | || x; + | -- - variable moved due to use in closure + | | + | value moved into closure here +LL | let y = &mut x; //~ ERROR + | ^^^^^^ value borrowed here after move + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0382`. diff --git a/src/test/ui/nll/closure-use-spans.rs b/src/test/ui/nll/closure-use-spans.rs new file mode 100644 index 00000000000..7ab382a96bb --- /dev/null +++ b/src/test/ui/nll/closure-use-spans.rs @@ -0,0 +1,33 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-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 liveness due to a closure capture gives a special note + +#![feature(nll)] + +fn use_as_borrow_capture(mut x: i32) { + let y = &x; + x = 0; //~ ERROR + || *y; +} + +fn use_as_borrow_mut_capture(mut x: i32) { + let y = &mut x; + x = 0; //~ ERROR + || *y = 1; +} + +fn use_as_move_capture(mut x: i32) { + let y = &x; + x = 0; //~ ERROR + move || *y; +} + +fn main() {} diff --git a/src/test/ui/nll/closure-use-spans.stderr b/src/test/ui/nll/closure-use-spans.stderr new file mode 100644 index 00000000000..7e5f9621d09 --- /dev/null +++ b/src/test/ui/nll/closure-use-spans.stderr @@ -0,0 +1,33 @@ +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/closure-use-spans.rs:17:5 + | +LL | let y = &x; + | -- borrow of `x` occurs here +LL | x = 0; //~ ERROR + | ^^^^^ assignment to borrowed `x` occurs here +LL | || *y; + | - borrow later captured here by closure + +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/closure-use-spans.rs:23:5 + | +LL | let y = &mut x; + | ------ borrow of `x` occurs here +LL | x = 0; //~ ERROR + | ^^^^^ assignment to borrowed `x` occurs here +LL | || *y = 1; + | - borrow later captured here by closure + +error[E0506]: cannot assign to `x` because it is borrowed + --> $DIR/closure-use-spans.rs:29:5 + | +LL | let y = &x; + | -- borrow of `x` occurs here +LL | x = 0; //~ ERROR + | ^^^^^ assignment to borrowed `x` occurs here +LL | move || *y; + | - borrow later captured here by closure + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0506`. diff --git a/src/test/ui/nll/closures-in-loops.rs b/src/test/ui/nll/closures-in-loops.rs new file mode 100644 index 00000000000..c6113f3a9c5 --- /dev/null +++ b/src/test/ui/nll/closures-in-loops.rs @@ -0,0 +1,36 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-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 messages where a closure capture conflicts with itself because it's in +// a loop. + +#![feature(nll)] + +fn repreated_move(x: String) { + for i in 0..10 { + || x; //~ ERROR + } +} + +fn repreated_mut_borrow(mut x: String) { + let mut v = Vec::new(); + for i in 0..10 { + v.push(|| x = String::new()); //~ ERROR + } +} + +fn repreated_unique_borrow(x: &mut String) { + let mut v = Vec::new(); + for i in 0..10 { + v.push(|| *x = String::new()); //~ ERROR + } +} + +fn main() {} diff --git a/src/test/ui/nll/closures-in-loops.stderr b/src/test/ui/nll/closures-in-loops.stderr new file mode 100644 index 00000000000..9758a803620 --- /dev/null +++ b/src/test/ui/nll/closures-in-loops.stderr @@ -0,0 +1,30 @@ +error[E0382]: use of moved value: `x` + --> $DIR/closures-in-loops.rs:18:9 + | +LL | || x; //~ ERROR + | ^^ - use occurs due to use in closure + | | + | value moved into closure here in previous iteration of loop + | + = note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait + +error[E0499]: cannot borrow `x` as mutable more than once at a time + --> $DIR/closures-in-loops.rs:25:16 + | +LL | v.push(|| x = String::new()); //~ ERROR + | ^^ - borrows occur due to use of `x` in closure + | | + | mutable borrow starts here in previous iteration of loop + +error[E0524]: two closures require unique access to `x` at the same time + --> $DIR/closures-in-loops.rs:32:16 + | +LL | v.push(|| *x = String::new()); //~ ERROR + | ^^ - borrows occur due to use of `x` in closure + | | + | closures are constructed here in different iterations of loop + +error: aborting due to 3 previous errors + +Some errors occurred: E0382, E0499, E0524. +For more information about an error, try `rustc --explain E0382`. |
