]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/borrowck-closures-two-imm.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / borrowck-closures-two-imm.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 two closures can simultaneously have immutable
12 // access to the variable, whether that immutable access be used
13 // for direct reads or for taking immutable ref. Also check
14 // that the main function can read the variable too while
15 // the closures are in scope. Issue #6801.
16
17 fn a() -> int {
18     let mut x = 3i;
19     x += 1;
20     let c1 = |&:| x * 4;
21     let c2 = |&:| x * 5;
22     c1() * c2() * x
23 }
24
25 fn get(x: &int) -> int {
26     *x * 4
27 }
28
29 fn b() -> int {
30     let mut x = 3;
31     x += 1;
32     let c1 = |&:| get(&x);
33     let c2 = |&:| get(&x);
34     c1() * c2() * x
35 }
36
37 fn c() -> int {
38     let mut x = 3;
39     x += 1;
40     let c1 = |&:| x * 5;
41     let c2 = |&:| get(&x);
42     c1() * c2() * x
43 }
44
45 pub fn main() {
46     assert_eq!(a(), 1280);
47     assert_eq!(b(), 1024);
48     assert_eq!(c(), 1280);
49 }