]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
1a4d82fc JJ |
2 | // Tests that two closures can simultaneously have immutable |
3 | // access to the variable, whether that immutable access be used | |
4 | // for direct reads or for taking immutable ref. Also check | |
5 | // that the main function can read the variable too while | |
6 | // the closures are in scope. Issue #6801. | |
7 | ||
c34b1796 | 8 | |
85aaf69f | 9 | fn a() -> i32 { |
c34b1796 | 10 | let mut x = 3; |
1a4d82fc | 11 | x += 1; |
85aaf69f SL |
12 | let c1 = || x * 4; |
13 | let c2 = || x * 5; | |
1a4d82fc JJ |
14 | c1() * c2() * x |
15 | } | |
16 | ||
85aaf69f | 17 | fn get(x: &i32) -> i32 { |
1a4d82fc JJ |
18 | *x * 4 |
19 | } | |
20 | ||
85aaf69f | 21 | fn b() -> i32 { |
c34b1796 | 22 | let mut x = 3; |
1a4d82fc | 23 | x += 1; |
85aaf69f SL |
24 | let c1 = || get(&x); |
25 | let c2 = || get(&x); | |
1a4d82fc JJ |
26 | c1() * c2() * x |
27 | } | |
28 | ||
85aaf69f | 29 | fn c() -> i32 { |
c34b1796 | 30 | let mut x = 3; |
1a4d82fc | 31 | x += 1; |
85aaf69f SL |
32 | let c1 = || x * 5; |
33 | let c2 = || get(&x); | |
1a4d82fc JJ |
34 | c1() * c2() * x |
35 | } | |
36 | ||
37 | pub fn main() { | |
38 | assert_eq!(a(), 1280); | |
39 | assert_eq!(b(), 1024); | |
40 | assert_eq!(c(), 1280); | |
41 | } |