2016-11-10 11:08:21 -06:00
|
|
|
#![feature(fn_traits)]
|
|
|
|
|
2014-04-22 00:02:19 -05:00
|
|
|
// Ensure that invoking a closure counts as a unique immutable borrow
|
|
|
|
|
2019-05-28 13:46:13 -05:00
|
|
|
type Fn<'a> = Box<dyn FnMut() + 'a>;
|
2014-04-22 00:02:19 -05:00
|
|
|
|
|
|
|
struct Test<'a> {
|
2019-05-28 13:46:13 -05:00
|
|
|
f: Box<dyn FnMut() + 'a>
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-04-24 22:25:30 -05:00
|
|
|
(*f)();
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `*f` as mutable, as it is behind a `&` reference
|
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) {
|
2017-04-24 22:25:30 -05:00
|
|
|
f.f.call_mut(())
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR: cannot borrow `f.f` as mutable, as it is behind a `&` reference
|
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() {
|
2019-05-28 13:46:13 -05:00
|
|
|
fn foo<F>(_: F) where F: FnMut(Box<dyn FnMut(isize)>, isize) {}
|
2018-03-23 04:57:28 -05:00
|
|
|
let s = String::new(); // Capture to make f !Copy
|
2019-05-28 13:46:13 -05:00
|
|
|
let mut f = move |g: Box<dyn FnMut(isize)>, b: isize| {
|
2018-03-23 04:57:28 -05:00
|
|
|
let _ = s.len();
|
|
|
|
};
|
2015-02-15 02:52:21 -06:00
|
|
|
f(Box::new(|a| {
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot move out of `f` because it is borrowed
|
2015-01-24 14:54:52 -06:00
|
|
|
foo(f);
|
2019-05-05 06:02:32 -05:00
|
|
|
//~^ ERROR cannot move out of `f`, a captured variable in an `FnMut` closure
|
2015-02-15 02:52:21 -06:00
|
|
|
}), 3);
|
2014-04-22 00:02:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|