Skip to content

Transmuting fn pointer to a usize in const somehow causes UB? #142230

Open
@theemathas

Description

@theemathas

I tried this code:

fn foo() {}

const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };

I expected to see this happen: Either a normal compile error before consteval, a normal consteval error, or the program successfully compiles.

Instead, this happened:

error[E0080]: constructing invalid value at .<deref>: encountered a pointer, but expected an integer
 --> src/lib.rs:3:1
  |
3 | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };
  | ^^^^^^^^^^^^^^^ it is undefined behavior to use this value
  |
  = help: this code performed an operation that depends on the underlying bytes representing a pointer
  = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
  = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
  = note: the raw bytes of the constant (size: 8, align: 8) {
              ╾─────alloc4<imm>─────╼                         │ ╾──────╼
          }

note: erroneous constant encountered
 --> src/lib.rs:3:28
  |
3 | const _: &usize = unsafe { &std::mem::transmute(foo as fn()) };
  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

I find it rather strange that adding a reference would cause this code to be undefined behavior. For comparison, the following code produces a normal consteval error.

fn foo() {}

const _: usize = unsafe { std::mem::transmute(foo as fn()) };
error[E0080]: unable to turn pointer into integer
 --> src/lib.rs:3:1
  |
3 | const _: usize = unsafe { std::mem::transmute(foo as fn()) };
  | ^^^^^^^^^^^^^^ evaluation of constant value failed here
  |
  = help: this code performed an operation that depends on the underlying bytes representing a pointer
  = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Meta

Reproducible on the playground with 1.89.0-nightly (2025-06-08 6ccd4476036edfce364e)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsC-discussionCategory: Discussion or questions that doesn't represent real issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions