]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/unboxed-closures-mutate-upvar.rs
650bb17bb7758f9ce60d81d2dcc7ec38ff7cab7b
[rustc.git] / src / test / compile-fail / unboxed-closures-mutate-upvar.rs
1 // Copyright 2015 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 // Test that we cannot mutate an outer variable that is not declared
12 // as `mut` through a closure. Also test that we CAN mutate a moved copy,
13 // unless this is a `Fn` closure. Issue #16749.
14
15 #![feature(unboxed_closures)]
16
17 use std::mem;
18
19 fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
20 fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
21
22 fn a() {
23 let n = 0u8;
24 let mut f = to_fn_mut(|| { //~ ERROR closure cannot assign
25 n += 1;
26 });
27 }
28
29 fn b() {
30 let mut n = 0u8;
31 let mut f = to_fn_mut(|| {
32 n += 1; // OK
33 });
34 }
35
36 fn c() {
37 let n = 0u8;
38 let mut f = to_fn_mut(move || {
39 // If we just did a straight-forward desugaring, this would
40 // compile, but we do something a bit more subtle, and hence
41 // we get an error.
42 n += 1; //~ ERROR cannot assign
43 });
44 }
45
46 fn d() {
47 let mut n = 0u8;
48 let mut f = to_fn_mut(move || {
49 n += 1; // OK
50 });
51 }
52
53 fn e() {
54 let n = 0u8;
55 let mut f = to_fn(move || {
56 n += 1; //~ ERROR cannot assign
57 });
58 }
59
60 fn f() {
61 let mut n = 0u8;
62 let mut f = to_fn(move || {
63 n += 1; //~ ERROR cannot assign
64 });
65 }
66
67 fn main() { }