Open
Description
Usually, non-capturing closures can be coerced to fn
ptrs. But somehow that seems to fail when the return type is !
:
fn magic<R, F: FnOnce() -> R>(f: F) -> F { f }
fn main() {
let f1 = magic(|| {}) as fn() -> (); // works fine
let f2 = magic(|| loop {}) as fn() -> !; // errors
}
The error is
error[E0605]: non-primitive cast: `[closure@src/main.rs:5:21: 5:31]` as `fn() -> !`
--> src/main.rs:5:14
|
5 | let f2 = magic(|| loop {}) as fn() -> !;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait
This fails even on nightly, where !
should be stable as a type.
Curiously, this works:
let f2: fn() -> ! = || loop {};
Cc @Centril