]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-closures-unique.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / borrowck-closures-unique.rs
1 // Tests that a closure which requires mutable access to the referent
2 // of an `&mut` requires a "unique" borrow -- that is, the variable to
3 // be borrowed (here, `x`) will not be borrowed *mutably*, but
4 //  may be *immutable*, but we cannot allow
5 // multiple borrows.
6
7
8
9 fn get(x: &isize) -> isize {
10     *x
11 }
12
13 fn set(x: &mut isize) -> isize {
14     *x
15 }
16
17 fn a(x: &mut isize) {
18     let c1 = || get(x);
19     let c2 = || get(x);
20     c1();
21     c2();
22 }
23
24 fn b(x: &mut isize) {
25     let c1 = || get(x);
26     let c2 = || set(x); //~ ERROR closure requires unique access to `x`
27     c1;
28 }
29
30 fn c(x: &mut isize) {
31     let c1 = || get(x);
32     let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
33     c1;
34 }
35
36 fn d(x: &mut isize) {
37     let c1 = || set(x);
38     let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time
39     c1;
40 }
41
42 fn e(x: &'static mut isize) {
43     let c1 = |y: &'static mut isize| x = y;
44     //~^ ERROR cannot assign to `x`, as it is not declared as mutable
45     c1;
46 }
47
48 fn f(x: &'static mut isize) {
49     let c1 = || x = panic!(); // OK assignment is unreachable.
50     c1;
51 }
52
53 fn main() {
54 }