]> git.lizzy.rs Git - rust.git/blob - src/test/ui/span/borrowck-call-is-borrow-issue-12224.rs
Rollup merge of #87922 - Manishearth:c-enum-target-spec, r=nagisa,eddyb
[rust.git] / src / test / ui / span / borrowck-call-is-borrow-issue-12224.rs
1 #![feature(fn_traits)]
2
3 // Ensure that invoking a closure counts as a unique immutable borrow
4
5 type Fn<'a> = Box<dyn FnMut() + 'a>;
6
7 struct Test<'a> {
8     f: Box<dyn FnMut() + 'a>
9 }
10
11 fn call<F>(mut f: F) where F: FnMut(Fn) {
12     f(Box::new(|| {
13     //~^ ERROR: cannot borrow `f` as mutable more than once
14         f((Box::new(|| {})))
15     }));
16 }
17
18 fn test1() {
19     call(|mut a| {
20         a.call_mut(());
21     });
22 }
23
24 fn test2<F>(f: &F) where F: FnMut() {
25     (*f)();
26     //~^ ERROR cannot borrow `*f` as mutable, as it is behind a `&` reference
27 }
28
29 fn test3<F>(f: &mut F) where F: FnMut() {
30     (*f)();
31 }
32
33 fn test4(f: &Test) {
34     f.f.call_mut(())
35     //~^ ERROR: cannot borrow `f.f` as mutable, as it is behind a `&` reference
36 }
37
38 fn test5(f: &mut Test) {
39     f.f.call_mut(())
40 }
41
42 fn test6() {
43     let mut f = || {};
44     (|| {
45         f();
46     })();
47 }
48
49 fn test7() {
50     fn foo<F>(_: F) where F: FnMut(Box<dyn FnMut(isize)>, isize) {}
51     let s = String::new();  // Capture to make f !Copy
52     let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
53         let _ = s.len();
54     };
55     f(Box::new(|a| {
56         //~^ ERROR cannot move out of `f` because it is borrowed
57         foo(f);
58         //~^ ERROR cannot move out of `f`, a captured variable in an `FnMut` closure
59     }), 3);
60 }
61
62 fn main() {}