Skip to content

Commit 51d47e8

Browse files
committed
test closure-to-fn-ptr coercions a bit more
1 parent 35e1fe1 commit 51d47e8

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

tests/run-pass/non_capture_closure_to_fn_ptr.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ static FOO: fn() = || { assert_ne!(42, 43) };
55
static BAR: fn(i32, i32) = |a, b| { assert_ne!(a, b) };
66

77
// use to first make the closure FnOnce() before making it fn()
8-
fn magic<F: FnOnce()>(f: F) -> F { f }
8+
fn magic0<R, F: FnOnce() -> R>(f: F) -> F { f }
9+
fn magic1<T, R, F: FnOnce(T) -> R>(f: F) -> F { f }
910

1011
fn main() {
1112
FOO();
@@ -15,6 +16,20 @@ fn main() {
1516
let boo: &dyn Fn(i32, i32) = &BAR;
1617
boo(48, 49);
1718

18-
let f = magic(||{}) as fn();
19+
let f: fn() = ||{};
1920
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();
2035
}

0 commit comments

Comments
 (0)