Transmuting fn pointer to a usize in const somehow causes UB?
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the two minimal reproductions in src/lib.rs and compare the E0080 diagnostics for the referenced and unreferenced transmute cases. Investigate the const-evaluation handling of function-pointer-to-integer transmutation and determine whether the differing behavior is intended; done means the behavior or diagnostic is consistent with the intended rules.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100