forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Tweak the borrow on closure invocations
This alters the borrow checker's requirements on invoking closures from requiring an immutable borrow to requiring a unique immutable borrow. This means that it is illegal to invoke a closure through a `&` pointer because there is no guarantee that is not aliased. This does not mean that a closure is required to be in a mutable location, but rather a location which can be proven to be unique (often through a mutable pointer). For example, the following code is unsound and is no longer allowed: type Fn<'a> = ||:'a; fn call(f: |Fn|) { f(|| { f(|| {}) }); } fn main() { call(|a| { a(); }); } There is no replacement for this pattern. For all closures which are stored in structures, it was previously allowed to invoke the closure through `&self` but it now requires invocation through `&mut self`. The standard library has a good number of violations of this new rule, but the fixes will be separated into multiple breaking change commits. Closes rust-lang#12224 [breaking-change]
- Loading branch information
1 parent
1e33589
commit 159a10d
Showing
5 changed files
with
125 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/compile-fail/borrowck-call-is-borrow-issue-12224.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2014 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. | ||
|
||
// Ensure that invoking a closure counts as a unique immutable borrow | ||
|
||
|
||
type Fn<'a> = ||:'a; | ||
|
||
struct Test<'a> { | ||
f: ||: 'a | ||
} | ||
|
||
fn call(f: |Fn|) { | ||
f(|| { | ||
//~^ ERROR: closure requires unique access to `f` but it is already borrowed | ||
f(|| {}) | ||
}); | ||
} | ||
|
||
fn test1() { | ||
call(|a| { | ||
a(); | ||
}); | ||
} | ||
|
||
fn test2(f: &||) { | ||
(*f)(); //~ ERROR: closure invocation in a `&` reference | ||
} | ||
|
||
fn test3(f: &mut ||) { | ||
(*f)(); | ||
} | ||
|
||
fn test4(f: &Test) { | ||
(f.f)() //~ ERROR: closure invocation in a `&` reference | ||
} | ||
|
||
fn test5(f: &mut Test) { | ||
(f.f)() | ||
} | ||
|
||
fn test6() { | ||
let f = || {}; | ||
(|| { | ||
f(); | ||
})(); | ||
} | ||
|
||
fn test7() { | ||
fn foo(_: |g: |int|, b: int|) {} | ||
let f = |g: |int|, b: int| {}; | ||
f(|a| { //~ ERROR: cannot borrow `f` as immutable because previous closure | ||
foo(f); //~ ERROR: cannot move out of captured outer variable | ||
}, 3); | ||
} | ||
|
||
fn main() {} |