rust/tests/ui/span/borrowck-call-is-borrow-issue-12224.rs

63 lines
1.3 KiB
Rust
Raw Normal View History

#![feature(fn_traits)]
// 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>;
struct Test<'a> {
2019-05-28 13:46:13 -05:00
f: Box<dyn FnMut() + 'a>
}
2015-01-03 09:45:00 -06:00
fn call<F>(mut f: F) where F: FnMut(Fn) {
f(Box::new(|| {
2015-01-03 09:45:00 -06:00
//~^ ERROR: cannot borrow `f` as mutable more than once
f((Box::new(|| {})))
}));
}
fn test1() {
2015-01-03 09:45:00 -06:00
call(|mut a| {
a.call_mut(());
});
}
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)();
//~^ ERROR cannot borrow `*f` as mutable, as it is behind a `&` reference
}
2015-01-03 09:45:00 -06:00
fn test3<F>(f: &mut F) where F: FnMut() {
(*f)();
}
fn test4(f: &Test) {
2017-04-24 22:25:30 -05:00
f.f.call_mut(())
//~^ ERROR: cannot borrow `f.f` as mutable, as it is behind a `&` reference
}
fn test5(f: &mut Test) {
2015-01-03 09:45:00 -06:00
f.f.call_mut(())
}
fn test6() {
let mut f = || {};
(|| {
f();
})();
}
fn test7() {
2019-05-28 13:46:13 -05:00
fn foo<F>(_: F) where F: FnMut(Box<dyn FnMut(isize)>, isize) {}
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| {
let _ = s.len();
};
f(Box::new(|a| {
//~^ ERROR cannot move out of `f` because it is borrowed
foo(f);
//~^ ERROR cannot move out of `f`, a captured variable in an `FnMut` closure
}), 3);
}
fn main() {}