]> git.lizzy.rs Git - rust.git/blob - src/test/ui/nll/closure-captures.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / nll / closure-captures.rs
1 // Copyright 2018 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 // Some cases with closures that might be problems
12
13 #![allow(unused)]
14 #![feature(nll)]
15
16 // Should have one error per assignment
17
18 fn one_closure(x: i32) {
19     ||
20     x = 1; //~ ERROR
21     move ||
22     x = 1; //~ ERROR
23 }
24
25 fn two_closures(x: i32) {
26     || {
27         ||
28         x = 1; //~ ERROR
29     };
30     move || {
31         ||
32         x = 1; //~ ERROR
33     };
34 }
35
36 fn fn_ref<F: Fn()>(f: F) -> F { f }
37
38 fn two_closures_ref_mut(mut x: i32) {
39     fn_ref(|| {
40         || //~ ERROR
41          x = 1;}
42     );
43     fn_ref(move || {
44         ||  //~ ERROR
45     x = 1;});
46 }
47
48 // This still gives two messages, but it requires two things to be fixed.
49 fn two_closures_ref(x: i32) {
50     fn_ref(|| {
51         || //~ ERROR
52          x = 1;} //~ ERROR
53     );
54     fn_ref(move || {
55         ||  //~ ERROR
56     x = 1;}); //~ ERROR
57 }
58
59 fn two_closures_two_refs(x: &mut i32) {
60     fn_ref(|| {
61         || //~ ERROR
62         *x = 1;});
63     fn_ref(move || {
64         || //~ ERROR
65         *x = 1;});
66 }
67
68 fn main() {}