Skip to content

Fn item/fn pointer distinction breaks fn pointer generics #20178

Closed
@pmsanford

Description

@pmsanford

This is broken now:

fn accepts_optional_fn_ptr(_test: Option<fn(int) -> int>) {}

fn test_int(_: int) -> int { 0i }

fn main() {
    accepts_optional_fn_ptr(Some(test_int));
}

because it expects Option<fn(int) -> int> and gets Option<fn(int) -> int {test_int}>

This is an issue for FFI specifically because Option<fn(...) -> ...> is the idiomatic way (mentioned in the FFI guide) to represent C function pointers as it takes advantage of nullable pointer optimization.

I'll add that for anyone encountering this issue, an explicit cast fixes it:

accepts_optional_fn_ptr(Some(test_int as fn(int) -> int));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions