]> git.proxmox.com Git - rustc.git/blame - src/test/ui/borrowck/borrowck-closures-unique.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / test / ui / borrowck / borrowck-closures-unique.rs
CommitLineData
1a4d82fc
JJ
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
b7449926
XL
7
8
1a4d82fc
JJ
9fn get(x: &isize) -> isize {
10 *x
11}
12
13fn set(x: &mut isize) -> isize {
14 *x
15}
16
17fn a(x: &mut isize) {
85aaf69f
SL
18 let c1 = || get(x);
19 let c2 = || get(x);
b7449926
XL
20 c1();
21 c2();
1a4d82fc
JJ
22}
23
24fn b(x: &mut isize) {
85aaf69f
SL
25 let c1 = || get(x);
26 let c2 = || set(x); //~ ERROR closure requires unique access to `x`
b7449926 27 c1;
1a4d82fc
JJ
28}
29
30fn c(x: &mut isize) {
85aaf69f
SL
31 let c1 = || get(x);
32 let c2 = || { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
b7449926 33 c1;
1a4d82fc
JJ
34}
35
36fn d(x: &mut isize) {
85aaf69f 37 let c1 = || set(x);
a7813a04 38 let c2 = || set(x); //~ ERROR two closures require unique access to `x` at the same time
b7449926
XL
39 c1;
40}
41
b7449926 42fn e(x: &'static mut isize) {
48663c56
XL
43 let c1 = |y: &'static mut isize| x = y;
44 //~^ ERROR cannot assign to `x`, as it is not declared as mutable
b7449926 45 c1;
1a4d82fc
JJ
46}
47
b7449926 48fn f(x: &'static mut isize) {
48663c56 49 let c1 = || x = panic!(); // OK assignment is unreachable.
b7449926 50 c1;
1a4d82fc
JJ
51}
52
53fn main() {
54}