Skip to content

Direct coercion from closures to unsafe fn pointers #57883

Closed
@Centril

Description

@Centril

While you can coerce closures to fn pointers, it is currently not possible to coerce closures to unsafe fn pointers since that requires a transitive coercion from fn pointers to unsafe fn pointers. We do not allow transitive coercions. Example:

static A: fn() = || {}; // OK
static B: unsafe fn() = A; // OK
static C: unsafe fn() = || {}; // ERROR; would require the two coercions above transitively.

This behavior of current Rust was unexpected in rust-lang/rfcs#2592 (comment).

@cramertj had the idea to implement an immediately attainable direct coercion between closures and unsafe fn pointers rather than go the more complex and contentious route of transitive coercions (which may have unforeseen consequences). This idea seems reasonable to me.

cc @nikomatsakis @arielb1

PS: should we fcp the PR once made?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-inferenceArea: Type inferenceA-type-systemArea: Type systemC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions