]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-call-is-borrow-issue-12224.rs
002ae5a7d28b418ab394d40247ff8aa100f4bf42
[rust.git] / src / test / compile-fail / borrowck-call-is-borrow-issue-12224.rs
1 // Copyright 2014 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 that invoking a closure counts as a unique immutable borrow
12
13
14 type Fn<'a> = ||:'a;
15
16 struct Test<'a> {
17     f: ||: 'a
18 }
19
20 fn call(f: |Fn|) {
21     f(|| {
22     //~^ ERROR: closure requires unique access to `f` but it is already borrowed
23         f(|| {})
24     });
25 }
26
27 fn test1() {
28     call(|a| {
29         a();
30     });
31 }
32
33 fn test2(f: &||) {
34     (*f)(); //~ ERROR: closure invocation in a `&` reference
35 }
36
37 fn test3(f: &mut ||) {
38     (*f)();
39 }
40
41 fn test4(f: &Test) {
42     (f.f)() //~ ERROR: closure invocation in a `&` reference
43 }
44
45 fn test5(f: &mut Test) {
46     (f.f)()
47 }
48
49 fn test6() {
50     let f = || {};
51     (|| {
52         f();
53     })();
54 }
55
56 fn test7() {
57     fn foo(_: |g: |int|, b: int|) {}
58     let f = |g: |int|, b: int| {};
59     f(|a| { //~ ERROR: cannot borrow `f` as immutable because previous closure
60         foo(f); //~ ERROR: cannot move out of captured outer variable
61     }, 3);
62 }
63
64 fn main() {}