File tree Expand file tree Collapse file tree 3 files changed +101
-0
lines changed
src/test/compile-fail/borrowck Expand file tree Collapse file tree 3 files changed +101
-0
lines changed Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments