Skip to content

Closure-to-fn coercion does not work for unsafe fn #58478

Closed
@RalfJung

Description

@RalfJung

Given that non-capturing closures coerce to fn, and fn coerces to unsafe fn, I would expect the transitive step to also happen -- but it does not:

fn coerce(_x: fn()) {}
fn coerce_unsafe(_x: unsafe fn()) {}

fn foo() {}

fn main() {
    coerce(|| panic!()); // works
    coerce_unsafe(foo); // works
    coerce_unsafe(|| panic!()); // fails
}
error[E0308]: mismatched types
 --> src/main.rs:9:19
  |
9 |     coerce_unsafe(|| panic!());
  |                   ^^^^^^^^^^^ expected unsafe fn, found normal fn
  |
  = note: expected type `unsafe fn()`
             found type `[closure@src/main.rs:9:19: 9:30]`

This is particularly annoying when instantiating RawWakerVTable, because it means one cannot just use closure syntax there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions