]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/non_capture_closure_to_fn_ptr.rs
test closure-to-fn-ptr coercions a bit more
[rust.git] / tests / run-pass / non_capture_closure_to_fn_ptr.rs
1 // allow(const_err) to work around a bug in warnings
2 #[allow(const_err)]
3 static FOO: fn() = || { assert_ne!(42, 43) };
4 #[allow(const_err)]
5 static BAR: fn(i32, i32) = |a, b| { assert_ne!(a, b) };
6
7 // use to first make the closure FnOnce() before making it fn()
8 fn magic0<R, F: FnOnce() -> R>(f: F) -> F { f }
9 fn magic1<T, R, F: FnOnce(T) -> R>(f: F) -> F { f }
10
11 fn main() {
12     FOO();
13     BAR(44, 45);
14     let bar: unsafe fn(i32, i32) = BAR;
15     unsafe { bar(46, 47) };
16     let boo: &dyn Fn(i32, i32) = &BAR;
17     boo(48, 49);
18
19     let f: fn() = ||{};
20     f();
21     let f = magic0(||{}) as fn();
22     f();
23
24     let g: fn(i32) = |i| assert_eq!(i, 2);
25     g(2);
26     let g = magic1(|i| assert_eq!(i, 2)) as fn(i32);
27     g(2);
28
29     // FIXME: This fails with "invalid use of NULL pointer"
30     //let h: fn() -> ! = || std::process::exit(0);
31     //h();
32     // FIXME: This does not even compile?!?
33     //let h = magic0(|| std::process::exit(0)) as fn() -> !;
34     //h();
35 }