]> git.lizzy.rs Git - rust.git/blob - tests/ui/unboxed-closures/unboxed-closures-failed-recursive-fn-1.rs
Rollup merge of #106856 - vadorovsky:fix-atomic-annotations, r=joshtriplett
[rust.git] / tests / ui / unboxed-closures / unboxed-closures-failed-recursive-fn-1.rs
1 // Various unsuccessful attempts to put the unboxed closure kind
2 // inference into an awkward position that might require fixed point
3 // iteration (basically where inferring the kind of a closure `c`
4 // would require knowing the kind of `c`). I currently believe this is
5 // impossible.
6
7 fn a() {
8     // This case of recursion wouldn't even require fixed-point
9     // iteration, but it still doesn't work. The weird structure with
10     // the `Option` is to avoid giving any useful hints about the `Fn`
11     // kind via the expected type.
12     let mut factorial: Option<Box<dyn Fn(u32) -> u32>> = None;
13
14     let f = |x: u32| -> u32 {
15         let g = factorial.as_ref().unwrap();
16         //~^ ERROR `factorial` does not live long enough
17         if x == 0 {1} else {x * g(x-1)}
18     };
19
20     factorial = Some(Box::new(f));
21     //~^ ERROR cannot assign to `factorial` because it is borrowed
22 }
23
24 fn b() {
25     let mut factorial: Option<Box<dyn Fn(u32) -> u32 + 'static>> = None;
26
27     let f = |x: u32| -> u32 {
28         let g = factorial.as_ref().unwrap();
29         //~^ ERROR `factorial` does not live long enough
30         if x == 0 {1} else {x * g(x-1)}
31     };
32
33     factorial = Some(Box::new(f));
34     //~^ ERROR cannot assign to `factorial` because it is borrowed
35 }
36
37 fn main() { }