2014-04-22 00:02:19 -05:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
// Ensure that invoking a closure counts as a unique immutable borrow
|
|
|
|
|
2015-01-03 09:45:00 -06:00
|
|
|
#![feature(unboxed_closures)]
|
2014-04-22 00:02:19 -05:00
|
|
|
|
2015-01-03 09:45:00 -06:00
|
|
|
type Fn<'a> = Box<FnMut() + 'a>;
|
2014-04-22 00:02:19 -05:00
|
|
|
|
|
|
|
struct Test<'a> {
|
2015-01-03 09:45:00 -06:00
|
|
|
f: Box<FnMut() + 'a>
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
2015-02-15 02:52:21 -06:00
|
|
|
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
|
2015-01-03 09:45:00 -06:00
|
|
|
fn call<F>(mut f: F) where F: FnMut(Fn) {
|
2015-02-15 02:52:21 -06:00
|
|
|
f(Box::new(|| {
|
2015-01-03 09:45:00 -06:00
|
|
|
//~^ ERROR: cannot borrow `f` as mutable more than once
|
2015-02-15 02:52:21 -06:00
|
|
|
f((Box::new(|| {})))
|
|
|
|
}));
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test1() {
|
2015-01-03 09:45:00 -06:00
|
|
|
call(|mut a| {
|
|
|
|
a.call_mut(());
|
2014-04-22 00:02:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-03 09:45:00 -06:00
|
|
|
fn test2<F>(f: &F) where F: FnMut() {
|
2015-01-08 08:12:06 -06:00
|
|
|
(*f)(); //~ ERROR: cannot borrow immutable borrowed content `*f` as mutable
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 09:45:00 -06:00
|
|
|
fn test3<F>(f: &mut F) where F: FnMut() {
|
2014-04-22 00:02:19 -05:00
|
|
|
(*f)();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test4(f: &Test) {
|
2015-01-08 08:12:06 -06:00
|
|
|
f.f.call_mut(()) //~ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test5(f: &mut Test) {
|
2015-01-03 09:45:00 -06:00
|
|
|
f.f.call_mut(())
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test6() {
|
2015-02-01 11:44:15 -06:00
|
|
|
let mut f = || {};
|
|
|
|
(|| {
|
2014-04-22 00:02:19 -05:00
|
|
|
f();
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test7() {
|
2015-01-08 04:54:35 -06:00
|
|
|
fn foo<F>(_: F) where F: FnMut(Box<FnMut(isize)>, isize) {}
|
2015-02-01 11:44:15 -06:00
|
|
|
let mut f = |g: Box<FnMut(isize)>, b: isize| {};
|
2015-02-15 02:52:21 -06:00
|
|
|
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
|
|
|
|
f(Box::new(|a| {
|
2015-01-24 14:54:52 -06:00
|
|
|
foo(f);
|
|
|
|
//~^ ERROR cannot move `f` into closure because it is borrowed
|
|
|
|
//~| ERROR cannot move out of captured outer variable in an `FnMut` closure
|
2015-02-15 02:52:21 -06:00
|
|
|
}), 3);
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|