]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/borrowck-closures-two-imm.rs
Rollup merge of #106644 - alexcrichton:update-wasi-toolchain, r=cuviper
[rust.git] / tests / ui / borrowck / borrowck-closures-two-imm.rs
1 // run-pass
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
8
9 fn a() -> i32 {
10     let mut x = 3;
11     x += 1;
12     let c1 = || x * 4;
13     let c2 = || x * 5;
14     c1() * c2() * x
15 }
16
17 fn get(x: &i32) -> i32 {
18     *x * 4
19 }
20
21 fn b() -> i32 {
22     let mut x = 3;
23     x += 1;
24     let c1 = || get(&x);
25     let c2 = || get(&x);
26     c1() * c2() * x
27 }
28
29 fn c() -> i32 {
30     let mut x = 3;
31     x += 1;
32     let c1 = || x * 5;
33     let c2 = || get(&x);
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 }