diff --git a/tests/compile-fail/cast_fn_ptr.rs b/tests/compile-fail/cast_fn_ptr.rs index 0a8f5ef752a6..0add977bf97b 100644 --- a/tests/compile-fail/cast_fn_ptr.rs +++ b/tests/compile-fail/cast_fn_ptr.rs @@ -5,6 +5,5 @@ fn f() {} std::mem::transmute::(f) }; - g(42) //~ ERROR constant evaluation error - //~^ NOTE tried to call a function with sig fn() through a function pointer of type fn(i32) + g(42) //~ ERROR tried to call a function with incorrect number of arguments } diff --git a/tests/compile-fail/cast_fn_ptr2.rs b/tests/compile-fail/cast_fn_ptr2.rs index cb80521c60ee..5af527016fb6 100644 --- a/tests/compile-fail/cast_fn_ptr2.rs +++ b/tests/compile-fail/cast_fn_ptr2.rs @@ -5,6 +5,5 @@ fn f(_ : (i32,i32)) {} std::mem::transmute::(f) }; - g(42) //~ ERROR constant evaluation error - //~^ NOTE tried to call a function with sig fn((i32, i32)) through a function pointer of type fn(i32) + g(42) //~ ERROR tried to call a function with argument of type (i32, i32) passing data of type i32 } diff --git a/tests/compile-fail/cast_fn_ptr3.rs b/tests/compile-fail/cast_fn_ptr3.rs new file mode 100644 index 000000000000..29507e7c7cf5 --- /dev/null +++ b/tests/compile-fail/cast_fn_ptr3.rs @@ -0,0 +1,10 @@ +fn main() { + fn f(_ : (i32,i32)) {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g() //~ ERROR tried to call a function with incorrect number of arguments +} + diff --git a/tests/compile-fail/cast_fn_ptr4.rs b/tests/compile-fail/cast_fn_ptr4.rs new file mode 100644 index 000000000000..f9a2cf9f6965 --- /dev/null +++ b/tests/compile-fail/cast_fn_ptr4.rs @@ -0,0 +1,9 @@ +fn main() { + fn f(_ : *const [i32]) {} + + let g = unsafe { + std::mem::transmute::(f) + }; + + g(&42 as *const i32) //~ ERROR tried to call a function with argument of type *const [i32] passing data of type *const i32 +} diff --git a/tests/run-pass/non_capture_closure_to_fn_ptr.rs b/tests/run-pass/non_capture_closure_to_fn_ptr.rs index c9daff9c9f46..d48c4df45944 100644 --- a/tests/run-pass/non_capture_closure_to_fn_ptr.rs +++ b/tests/run-pass/non_capture_closure_to_fn_ptr.rs @@ -4,6 +4,9 @@ #[allow(const_err)] static BAR: fn(i32, i32) = |a, b| { assert_ne!(a, b) }; +// use to first make the closure FnOnce() before making it fn() +fn magic(f: F) -> F { f } + fn main() { FOO(); BAR(44, 45); @@ -11,4 +14,7 @@ fn main() { unsafe { bar(46, 47) }; let boo: &Fn(i32, i32) = &BAR; boo(48, 49); + + let f = magic(||{}) as fn(); + f(); }