Tests for closure spans

This commit is contained in:
Matthew Jasper 2018-08-01 20:40:06 +01:00
parent 903851f785
commit 5639e2173b
10 changed files with 666 additions and 0 deletions

View File

@ -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 { }

View File

@ -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`.

View File

@ -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 { }

View File

@ -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`.

View File

@ -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() {}

View File

@ -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`.

View File

@ -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() {}

View File

@ -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`.

View File

@ -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() {}

View File

@ -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`.