Skip to content

Commit 6888520

Browse files
committed
Added tests for new loop borrow message
One set of tests is to ensure the current message is correct. The other set is to check the messages are generated correctly for every type of loop.
1 parent c802fc7 commit 6888520

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2017 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+
// produce special borrowck message inside all kinds of loops
12+
13+
struct FuncWrapper<'a, T : 'a> {
14+
func : fn(&'a mut T) -> ()
15+
}
16+
17+
impl<'a, T : 'a> FuncWrapper<'a, T> {
18+
fn in_loop(self, arg : &'a mut T) {
19+
loop {
20+
(self.func)(arg)
21+
}
22+
}
23+
24+
fn in_while(self, arg : &'a mut T) {
25+
while true {
26+
(self.func)(arg)
27+
}
28+
}
29+
30+
fn in_for(self, arg : &'a mut T) {
31+
let v : Vec<()> = vec![];
32+
for _ in v.iter() {
33+
(self.func)(arg)
34+
}
35+
}
36+
}
37+
38+
fn main() {
39+
}
40+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0499]: cannot borrow `*arg` as mutable more than once at a time
2+
--> $DIR/mut-borrow-in-loop.rs:20:25
3+
|
4+
20 | (self.func)(arg)
5+
| ^^^ mutable borrow starts here in previous iteration of loop
6+
21 | }
7+
22 | }
8+
| - mutable borrow ends here
9+
10+
error[E0499]: cannot borrow `*arg` as mutable more than once at a time
11+
--> $DIR/mut-borrow-in-loop.rs:26:25
12+
|
13+
26 | (self.func)(arg)
14+
| ^^^ mutable borrow starts here in previous iteration of loop
15+
27 | }
16+
28 | }
17+
| - mutable borrow ends here
18+
19+
error[E0499]: cannot borrow `*arg` as mutable more than once at a time
20+
--> $DIR/mut-borrow-in-loop.rs:33:25
21+
|
22+
33 | (self.func)(arg)
23+
| ^^^ mutable borrow starts here in previous iteration of loop
24+
34 | }
25+
35 | }
26+
| - mutable borrow ends here
27+
28+
error: aborting due to 3 previous errors
29+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 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+
// ensure borrowck messages are correct outside special case
12+
13+
fn main() {
14+
let mut void = ();
15+
16+
let first = &mut void;
17+
let second = &mut void;
18+
19+
loop {
20+
let mut inner_void = ();
21+
22+
let inner_first = &mut inner_void;
23+
let inner_second = &mut inner_void;
24+
}
25+
}
26+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0499]: cannot borrow `void` as mutable more than once at a time
2+
--> $DIR/mut-borrow-outside-loop.rs:17:23
3+
|
4+
16 | let first = &mut void;
5+
| ---- first mutable borrow occurs here
6+
17 | let second = &mut void;
7+
| ^^^^ second mutable borrow occurs here
8+
...
9+
25 | }
10+
| - first borrow ends here
11+
12+
error[E0499]: cannot borrow `inner_void` as mutable more than once at a time
13+
--> $DIR/mut-borrow-outside-loop.rs:23:33
14+
|
15+
22 | let inner_first = &mut inner_void;
16+
| ---------- first mutable borrow occurs here
17+
23 | let inner_second = &mut inner_void;
18+
| ^^^^^^^^^^ second mutable borrow occurs here
19+
24 | }
20+
| - first borrow ends here
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)