2015-01-31 06:29:04 -05:00
|
|
|
// Various unsuccessful attempts to put the unboxed closure kind
|
|
|
|
// inference into an awkward position that might require fixed point
|
|
|
|
// iteration (basically where inferring the kind of a closure `c`
|
|
|
|
// would require knowing the kind of `c`). I currently believe this is
|
|
|
|
// impossible.
|
|
|
|
|
|
|
|
fn a() {
|
|
|
|
// This case of recursion wouldn't even require fixed-point
|
|
|
|
// iteration, but it still doesn't work. The weird structure with
|
|
|
|
// the `Option` is to avoid giving any useful hints about the `Fn`
|
|
|
|
// kind via the expected type.
|
2019-05-28 14:46:13 -04:00
|
|
|
let mut factorial: Option<Box<dyn Fn(u32) -> u32>> = None;
|
2015-01-31 06:29:04 -05:00
|
|
|
|
|
|
|
let f = |x: u32| -> u32 {
|
2015-05-08 16:08:59 +02:00
|
|
|
let g = factorial.as_ref().unwrap();
|
2016-08-16 15:02:20 -07:00
|
|
|
//~^ ERROR `factorial` does not live long enough
|
2015-05-08 16:08:59 +02:00
|
|
|
if x == 0 {1} else {x * g(x-1)}
|
|
|
|
};
|
|
|
|
|
|
|
|
factorial = Some(Box::new(f));
|
2019-04-22 08:40:08 +01:00
|
|
|
//~^ ERROR cannot assign to `factorial` because it is borrowed
|
2015-05-08 16:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn b() {
|
2019-05-28 14:46:13 -04:00
|
|
|
let mut factorial: Option<Box<dyn Fn(u32) -> u32 + 'static>> = None;
|
2015-05-08 16:08:59 +02:00
|
|
|
|
|
|
|
let f = |x: u32| -> u32 {
|
2015-01-31 06:29:04 -05:00
|
|
|
let g = factorial.as_ref().unwrap();
|
2019-04-22 08:40:08 +01:00
|
|
|
//~^ ERROR `factorial` does not live long enough
|
2015-01-31 06:29:04 -05:00
|
|
|
if x == 0 {1} else {x * g(x-1)}
|
|
|
|
};
|
|
|
|
|
|
|
|
factorial = Some(Box::new(f));
|
2019-04-22 08:40:08 +01:00
|
|
|
//~^ ERROR cannot assign to `factorial` because it is borrowed
|
2015-01-31 06:29:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() { }
|