2015-01-24 20:54:52 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-07-30 05:37:48 +00:00
|
|
|
#![feature(unboxed_closures, tuple_trait)]
|
2015-02-03 16:32:26 +00:00
|
|
|
|
2015-01-24 20:54:52 +00:00
|
|
|
use std::mem;
|
|
|
|
|
2022-07-30 05:37:48 +00:00
|
|
|
fn to_fn<A:std::marker::Tuple,F:Fn<A>>(f: F) -> F { f }
|
|
|
|
fn to_fn_mut<A:std::marker::Tuple,F:FnMut<A>>(f: F) -> F { f }
|
2015-02-03 16:32:26 +00:00
|
|
|
|
2015-01-24 20:54:52 +00:00
|
|
|
fn a() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let n = 0;
|
2019-04-22 07:40:08 +00:00
|
|
|
let mut f = to_fn_mut(|| {
|
|
|
|
n += 1; //~ ERROR cannot assign to `n`, as it is not declared as mutable
|
2015-02-03 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let mut n = 0;
|
2015-02-03 16:32:26 +00:00
|
|
|
let mut f = to_fn_mut(|| {
|
2015-01-24 20:54:52 +00:00
|
|
|
n += 1; // OK
|
2015-02-03 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn c() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let n = 0;
|
2015-02-03 16:32:26 +00:00
|
|
|
let mut f = to_fn_mut(move || {
|
2015-01-24 20:54:52 +00: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 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn d() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let mut n = 0;
|
2015-02-03 16:32:26 +00:00
|
|
|
let mut f = to_fn_mut(move || {
|
2015-01-24 20:54:52 +00:00
|
|
|
n += 1; // OK
|
2015-02-03 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn e() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let n = 0;
|
2015-02-03 16:32:26 +00:00
|
|
|
let mut f = to_fn(move || {
|
2015-01-24 20:54:52 +00:00
|
|
|
n += 1; //~ ERROR cannot assign
|
2015-02-03 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn f() {
|
2015-03-03 08:42:26 +00:00
|
|
|
let mut n = 0;
|
2015-02-03 16:32:26 +00:00
|
|
|
let mut f = to_fn(move || {
|
2015-01-24 20:54:52 +00:00
|
|
|
n += 1; //~ ERROR cannot assign
|
2015-02-03 16:32:26 +00:00
|
|
|
});
|
2015-01-24 20:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() { }
|