Skip to content

Commit 5639e21

Browse files
committed
Tests for closure spans
1 parent 903851f commit 5639e21

10 files changed

+666
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// check that accesses due to a closure capture give a special note
12+
13+
#![feature(nll)]
14+
15+
fn closure_imm_capture_conflict(mut x: i32) {
16+
let r = &mut x;
17+
|| x; //~ ERROR
18+
r.use_mut();
19+
}
20+
21+
fn closure_mut_capture_conflict(mut x: i32) {
22+
let r = &mut x;
23+
|| x = 2; //~ ERROR
24+
r.use_mut();
25+
}
26+
27+
fn closure_unique_capture_conflict(mut x: &mut i32) {
28+
let r = &mut x;
29+
|| *x = 2; //~ ERROR
30+
r.use_mut();
31+
}
32+
33+
fn closure_copy_capture_conflict(mut x: i32) {
34+
let r = &mut x;
35+
move || x; //~ ERROR
36+
r.use_ref();
37+
}
38+
39+
fn closure_move_capture_conflict(mut x: String) {
40+
let r = &x;
41+
|| x; //~ ERROR
42+
r.use_ref();
43+
}
44+
45+
fn closure_imm_capture_moved(mut x: String) {
46+
let r = x;
47+
|| x.len(); //~ ERROR
48+
}
49+
50+
fn closure_mut_capture_moved(mut x: String) {
51+
let r = x;
52+
|| x = String::new(); //~ ERROR
53+
}
54+
55+
fn closure_unique_capture_moved(x: &mut String) {
56+
let r = x;
57+
|| *x = String::new(); //~ ERROR
58+
}
59+
60+
fn closure_move_capture_moved(x: &mut String) {
61+
let r = x;
62+
|| x; //~ ERROR
63+
}
64+
65+
fn main() {}
66+
67+
trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { } }
68+
impl<T> Fake for T { }
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
error[E0502]: cannot borrow `x` as immutable because it is also borrowed as mutable
2+
--> $DIR/closure-access-spans.rs:17:5
3+
|
4+
LL | let r = &mut x;
5+
| ------ mutable borrow occurs here
6+
LL | || x; //~ ERROR
7+
| ^^ - second borrow occurs due to use of `x` in closure
8+
| |
9+
| immutable borrow occurs here
10+
LL | r.use_mut();
11+
| - borrow later used here
12+
13+
error[E0499]: cannot borrow `x` as mutable more than once at a time
14+
--> $DIR/closure-access-spans.rs:23:5
15+
|
16+
LL | let r = &mut x;
17+
| ------ first mutable borrow occurs here
18+
LL | || x = 2; //~ ERROR
19+
| ^^ - second borrow occurs due to use of `x` in closure
20+
| |
21+
| second mutable borrow occurs here
22+
LL | r.use_mut();
23+
| - borrow later used here
24+
25+
error[E0500]: closure requires unique access to `x` but it is already borrowed
26+
--> $DIR/closure-access-spans.rs:29:5
27+
|
28+
LL | let r = &mut x;
29+
| ------ borrow occurs here
30+
LL | || *x = 2; //~ ERROR
31+
| ^^ - second borrow occurs due to use of `x` in closure
32+
| |
33+
| closure construction occurs here
34+
LL | r.use_mut();
35+
| - borrow later used here
36+
37+
error[E0503]: cannot use `x` because it was mutably borrowed
38+
--> $DIR/closure-access-spans.rs:35:13
39+
|
40+
LL | let r = &mut x;
41+
| ------ borrow of `x` occurs here
42+
LL | move || x; //~ ERROR
43+
| ^ use of borrowed `x`
44+
LL | r.use_ref();
45+
| - borrow later used here
46+
47+
error[E0505]: cannot move out of `x` because it is borrowed
48+
--> $DIR/closure-access-spans.rs:41:5
49+
|
50+
LL | let r = &x;
51+
| -- borrow of `x` occurs here
52+
LL | || x; //~ ERROR
53+
| ^^ - move occurs due to use in closure
54+
| |
55+
| move out of `x` occurs here
56+
LL | r.use_ref();
57+
| - borrow later used here
58+
59+
error[E0382]: borrow of moved value: `x`
60+
--> $DIR/closure-access-spans.rs:47:5
61+
|
62+
LL | let r = x;
63+
| - value moved here
64+
LL | || x.len(); //~ ERROR
65+
| ^^ - borrow occurs due to use in closure
66+
| |
67+
| value borrowed here after move
68+
|
69+
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
70+
71+
error[E0382]: borrow of moved value: `x`
72+
--> $DIR/closure-access-spans.rs:52:5
73+
|
74+
LL | let r = x;
75+
| - value moved here
76+
LL | || x = String::new(); //~ ERROR
77+
| ^^ - borrow occurs due to use in closure
78+
| |
79+
| value borrowed here after move
80+
|
81+
= note: move occurs because `x` has type `std::string::String`, which does not implement the `Copy` trait
82+
83+
error[E0382]: borrow of moved value: `x`
84+
--> $DIR/closure-access-spans.rs:57:5
85+
|
86+
LL | let r = x;
87+
| - value moved here
88+
LL | || *x = String::new(); //~ ERROR
89+
| ^^ - borrow occurs due to use in closure
90+
| |
91+
| value borrowed here after move
92+
|
93+
= note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
94+
95+
error[E0382]: use of moved value: `x`
96+
--> $DIR/closure-access-spans.rs:62:5
97+
|
98+
LL | let r = x;
99+
| - value moved here
100+
LL | || x; //~ ERROR
101+
| ^^ - use occurs due to use in closure
102+
| |
103+
| value used here after move
104+
|
105+
= note: move occurs because `x` has type `&mut std::string::String`, which does not implement the `Copy` trait
106+
107+
error: aborting due to 9 previous errors
108+
109+
Some errors occurred: E0382, E0499, E0500, E0502, E0503, E0505.
110+
For more information about an error, try `rustc --explain E0382`.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// check that existing borrows due to a closure capture give a special note
12+
13+
#![feature(nll)]
14+
15+
fn move_while_borrowed(x: String) {
16+
let f = || x.len();
17+
let y = x; //~ ERROR
18+
f.use_ref();
19+
}
20+
21+
fn borrow_mut_while_borrowed(mut x: i32) {
22+
let f = || x;
23+
let y = &mut x; //~ ERROR
24+
f.use_ref();
25+
}
26+
27+
fn drop_while_borrowed() {
28+
let f;
29+
{
30+
let x = 1;
31+
f = || x; //~ ERROR
32+
}
33+
f.use_ref();
34+
}
35+
36+
fn assign_while_borrowed(mut x: i32) {
37+
let f = || x;
38+
x = 1; //~ ERROR
39+
f.use_ref();
40+
}
41+
42+
fn copy_while_borrowed_mut(mut x: i32) {
43+
let f = || x = 0;
44+
let y = x; //~ ERROR
45+
f.use_ref();
46+
}
47+
48+
fn borrow_while_borrowed_mut(mut x: i32) {
49+
let f = || x = 0;
50+
let y = &x; //~ ERROR
51+
f.use_ref();
52+
}
53+
54+
fn borrow_mut_while_borrowed_mut(mut x: i32) {
55+
let f = || x = 0;
56+
let y = &mut x; //~ ERROR
57+
f.use_ref();
58+
}
59+
60+
fn drop_while_borrowed_mut() {
61+
let f;
62+
{
63+
let mut x = 1;
64+
f = || x = 0; //~ ERROR
65+
}
66+
f.use_ref();
67+
}
68+
69+
fn assign_while_borrowed_mut(mut x: i32) {
70+
let f = || x = 0;
71+
x = 1; //~ ERROR
72+
f.use_ref();
73+
}
74+
75+
fn copy_while_borrowed_unique(x: &mut i32) {
76+
let f = || *x = 0;
77+
let y = x; //~ ERROR
78+
f.use_ref();
79+
}
80+
81+
fn borrow_while_borrowed_unique(x: &mut i32) {
82+
let f = || *x = 0;
83+
let y = &x; //~ ERROR
84+
f.use_ref();
85+
}
86+
87+
fn borrow_mut_while_borrowed_unique(mut x: &mut i32) {
88+
let f = || *x = 0;
89+
let y = &mut x; //~ ERROR
90+
f.use_ref();
91+
}
92+
93+
fn drop_while_borrowed_unique() {
94+
let mut z = 1;
95+
let f;
96+
{
97+
let x = &mut z;
98+
f = || *x = 0; //~ ERROR
99+
}
100+
f.use_ref();
101+
}
102+
103+
fn assign_while_borrowed_unique(x: &mut i32) {
104+
let f = || *x = 0;
105+
*x = 1; //~ ERROR
106+
f.use_ref();
107+
}
108+
109+
fn main() {}
110+
111+
trait Fake { fn use_mut(&mut self) { } fn use_ref(&self) { } }
112+
impl<T> Fake for T { }

0 commit comments

Comments
 (0)