]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/unboxed-closures-failed-recursive-fn-1.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / test / compile-fail / unboxed-closures-failed-recursive-fn-1.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Various unsuccessful attempts to put the unboxed closure kind
12 // inference into an awkward position that might require fixed point
13 // iteration (basically where inferring the kind of a closure `c`
14 // would require knowing the kind of `c`). I currently believe this is
15 // impossible.
16
17 fn a() {
18     // This case of recursion wouldn't even require fixed-point
19     // iteration, but it still doesn't work. The weird structure with
20     // the `Option` is to avoid giving any useful hints about the `Fn`
21     // kind via the expected type.
22     let mut factorial: Option<Box<Fn(u32) -> u32>> = None;
23
24     let f = |x: u32| -> u32 {
25         let g = factorial.as_ref().unwrap();
26         //~^ ERROR `factorial` does not live long enough
27         if x == 0 {1} else {x * g(x-1)}
28     };
29
30     factorial = Some(Box::new(f));
31 }
32
33 fn b() {
34     let mut factorial: Option<Box<Fn(u32) -> u32 + 'static>> = None;
35
36     let f = |x: u32| -> u32 {
37         //~^ ERROR closure may outlive the current function, but it borrows `factorial`
38         let g = factorial.as_ref().unwrap();
39         if x == 0 {1} else {x * g(x-1)}
40     };
41
42     factorial = Some(Box::new(f));
43 }
44
45 fn main() { }