Skip to content

Function pointer primitive cast not working #140491

Closed
@Swiiz

Description

@Swiiz

When running the following Rust code:

fn my_fn(event: &Event<'_>) {}

struct Event<'a>(&'a ());

fn main() {
    const ptr: &fn(&Event<'_>) = &my_fn as _;
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b78156a025e81a9f78584c14d4ebe1ab

The cast using as _ should succeed, or the compiler should give a clearer reason for rejection if it is truly invalid. Since the types are compatible under HRLB, i guess this coercion should ideally be allowed.
The issue arises from using a reference before the function pointer. (It works when removed)

However, the compiler currently emits the following error:

error[E0605]: non-primitive cast: `&for<'a, 'b> fn(&'a Event<'b>) {my_fn}` as `&for<'a, 'b> fn(&'a Event<'b>)`
 --> src/main.rs:8:43
  |
8 |     const ptr: &fn(&Event<'_>) = &my_fn as _;
  |                                           ^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

This happens on stable, beta, and nightly Rust.

Workaround:

fn my_fn(event: &Event<'_>) {}

struct Event<'a>(&'a ());

const fn force<F: Fn(&Event<'_>)>(x: F) -> F {
    x
}

fn main() {
    const ptr: &fn(&Event<'_>) = &force(|event| { my_fn(event) });
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=56b65567bbb6f55043168f3a4d0cf411

Thanks to @_madfrog on Discord for the help

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Apr 29, 2025
zachs18

zachs18 commented on Apr 30, 2025

@zachs18
Contributor

Note that unary prefix & binds tigher than as casting, so &foo as bar is (&foo) as bar, not &(foo as bar). Changing &my_fn as _ in the OP to &(my_fn as _) makes it compile.

Swiiz

Swiiz commented on Apr 30, 2025

@Swiiz
Author

Maybe the compiler diagnostic could be improved to explain this.

added
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 30, 2025
added
A-diagnosticsArea: Messages for errors, warnings, and lints
and removed
C-bugCategory: This is a bug.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Apr 30, 2025
xizheyin

xizheyin commented on May 8, 2025

@xizheyin
Contributor

@rustbot claim

Perhaps note pointing only to &my_fn instead of &my_fn as _ would be clearer.

added a commit that references this issue on May 31, 2025

Rollup merge of rust-lang#140787 - xizheyin:issue-140491, r=nnethercote

05debb0
added a commit that references this issue on May 31, 2025
39e831e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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

    Participants

    @zachs18@lolbinarycat@Swiiz@jieyouxu@rustbot

    Issue actions

      Function pointer primitive cast not working · Issue #140491 · rust-lang/rust