]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/borrowck-closures-unique.rs
rollup merge of #20518: nagisa/weighted-bool
[rust.git] / src / test / compile-fail / borrowck-closures-unique.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 // Tests that a closure which requires mutable access to the referent
12 // of an `&mut` requires a "unique" borrow -- that is, the variable to
13 // be borrowed (here, `x`) will not be borrowed *mutably*, but
14 //  may be *immutable*, but we cannot allow
15 // multiple borrows.
16
17 fn get(x: &int) -> int {
18     *x
19 }
20
21 fn set(x: &mut int) -> int {
22     *x
23 }
24
25 fn a(x: &mut int) {
26     let c1 = |&mut:| get(x);
27     let c2 = |&mut:| get(x);
28 }
29
30 fn b(x: &mut int) {
31     let c1 = |&mut:| get(x);
32     let c2 = |&mut:| set(x); //~ ERROR closure requires unique access to `x`
33 }
34
35 fn c(x: &mut int) {
36     let c1 = |&mut:| get(x);
37     let c2 = |&mut:| { get(x); set(x); }; //~ ERROR closure requires unique access to `x`
38 }
39
40 fn d(x: &mut int) {
41     let c1 = |&mut:| set(x);
42     let c2 = |&mut:| set(x); //~ ERROR closure requires unique access to `x`
43 }
44
45 fn e(x: &mut int) {
46     let c1 = |&mut:| x = panic!(); //~ ERROR closure cannot assign to immutable local variable
47 }
48
49 fn main() {
50 }