Skip to content

Functions pointers with associated type parameters can't cast to pointers #54094

Closed
@michaelwu

Description

@michaelwu
trait Zoo {
    type X;
}

impl Zoo for u16 {
    type X = usize;
}

fn foo(abc: <u16 as Zoo>::X) {}

fn main() {
    let x: *const u8 = foo as _;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (file:///playground)
error[E0605]: non-primitive cast: `fn(<u16 as Zoo>::X) {foo}` as `*const u8`
  --> src/main.rs:13:24
   |
13 |     let x: *const u8 = foo as _;
   |                        ^^^^^^^^
   |
   = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

error: aborting due to previous error

For more information about this error, try `rustc --explain E0605`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Seems like this should work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler 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