Skip to content

Commit

Permalink
Tests for closure spans
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Aug 3, 2018
1 parent 903851f commit 5639e21
Show file tree
Hide file tree
Showing 10 changed files with 666 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 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 { }
110 changes: 110 additions & 0 deletions 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`.
112 changes: 112 additions & 0 deletions 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 { }

0 comments on commit 5639e21

Please sign in to comment.