Skip to content

Commit 9df75a8

Browse files
committed
tests transcribed from nikos blog post.
1 parent 7cadf7e commit 9df75a8

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// revisions: lxl nll
12+
//[lxl]compile-flags: -Z borrowck=mir -Z two-phase-borrows
13+
//[nll]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll
14+
15+
// This is the second counter-example from Niko's blog post
16+
// smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
17+
//
18+
// It is "artificial". It is meant to illustrate directly that we
19+
// should allow an aliasing access during reservation, but *not* while
20+
// the mutable borrow is active.
21+
22+
fn main() {
23+
/*0*/ let mut i = 0;
24+
25+
/*1*/ let p = &mut i; // (reservation of `i` starts here)
26+
27+
/*2*/ let j = i; // OK: `i` is only reserved here
28+
29+
/*3*/ *p += 1; // (mutable borrow of `i` starts here, since `p` is used)
30+
31+
/*4*/ let k = i; //[lxl]~ ERROR cannot use `i` because it was mutably borrowed [E0503]
32+
//[nll]~^ ERROR cannot use `i` because it was mutably borrowed [E0503]
33+
34+
/*5*/ *p += 1;
35+
36+
let _ = (j, k, p);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
// revisions: lxl nll
12+
//[lxl]compile-flags: -Z borrowck=mir -Z two-phase-borrows
13+
//[nll]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll
14+
15+
// This is the third counter-example from Niko's blog post
16+
// smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
17+
//
18+
// It shows that not all nested method calls on `self` are magically
19+
// allowed by this change. In particular, a nested `&mut` borrow is
20+
// still disallowed.
21+
22+
fn main() {
23+
24+
25+
let mut vec = vec![0, 1];
26+
vec.get({
27+
28+
vec.push(2);
29+
//[lxl]~^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
30+
//[nll]~^^ ERROR cannot borrow `vec` as mutable because it is also borrowed as immutable
31+
32+
0
33+
});
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
// revisions: lxl nll
12+
//[lxl]compile-flags: -Z borrowck=mir -Z two-phase-borrows
13+
//[nll]compile-flags: -Z borrowck=mir -Z two-phase-borrows -Z nll
14+
15+
// This is the first counter-example from Niko's blog post
16+
// smallcultfollowing.com/babysteps/blog/2017/03/01/nested-method-calls-via-two-phase-borrowing/
17+
// of a danger for code to crash if we just turned off the check for whether
18+
// a mutable-borrow aliases another borrow.
19+
20+
fn main() {
21+
let mut v: Vec<String> = vec![format!("Hello, ")];
22+
v[0].push_str({
23+
24+
v.push(format!("foo"));
25+
//[lxl]~^ ERROR cannot borrow `v` as mutable more than once at a time [E0499]
26+
//[nll]~^^ ERROR cannot borrow `v` as mutable more than once at a time [E0499]
27+
28+
"World!"
29+
});
30+
}

0 commit comments

Comments
 (0)