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.
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.
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
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;
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)}
30 factorial = Some(Box::new(f));
34 let mut factorial: Option<Box<Fn(u32) -> u32 + 'static>> = None;
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)}
42 factorial = Some(Box::new(f));