2015-01-24 14:54:52 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Test that we cannot mutate an outer variable that is not declared
|
|
|
|
// as `mut` through a closure. Also test that we CAN mutate a moved copy,
|
|
|
|
// unless this is a `Fn` closure. Issue #16749.
|
|
|
|
|
2015-02-03 10:32:26 -06:00
|
|
|
#![feature(unboxed_closures)]
|
|
|
|
|
2015-01-24 14:54:52 -06:00
|
|
|
use std::mem;
|
|
|
|
|
2015-02-03 10:32:26 -06:00
|
|
|
fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
|
|
|
|
fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
|
|
|
|
|
2015-01-24 14:54:52 -06:00
|
|
|
fn a() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn_mut(|| { //~ ERROR closure cannot assign
|
2015-01-24 14:54:52 -06:00
|
|
|
n += 1;
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn_mut(|| {
|
2015-01-24 14:54:52 -06:00
|
|
|
n += 1; // OK
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn_mut(move || {
|
2015-01-24 14:54:52 -06:00
|
|
|
// If we just did a straight-forward desugaring, this would
|
|
|
|
// compile, but we do something a bit more subtle, and hence
|
|
|
|
// we get an error.
|
|
|
|
n += 1; //~ ERROR cannot assign
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn d() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn_mut(move || {
|
2015-01-24 14:54:52 -06:00
|
|
|
n += 1; // OK
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn e() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn(move || {
|
2015-01-24 14:54:52 -06:00
|
|
|
n += 1; //~ ERROR cannot assign
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
2015-03-03 02:42:26 -06:00
|
|
|
let mut n = 0;
|
2015-02-03 10:32:26 -06:00
|
|
|
let mut f = to_fn(move || {
|
2015-01-24 14:54:52 -06:00
|
|
|
n += 1; //~ ERROR cannot assign
|
2015-02-03 10:32:26 -06:00
|
|
|
});
|
2015-01-24 14:54:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() { }
|