Allow const fn pointers to arbitrary memory address
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
const FOO: unsafe extern "C" fn() = unsafe { core::mem::transmute(0x2123210 as *const ())};
fn main() {}
Current output
error[E0080]: it is undefined behavior to use this value
--> src/main.rs:1:1
|
1 | const FOO: unsafe extern "C" fn() = unsafe { core::mem::transmute(0x2123210 as *const ())};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x2123210[noalloc], but expected a function pointer
|
= 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) {
10 32 12 02 00 00 00 00 │ .2......
}
Desired output
successful build
Rationale and extra context
I'm developing a library for embedded environment. The microcontroller I'm working with features ROM filled with preflashed code.
I'd like to create a listing of the functions there, similarly to what the C library does, and put them as consts. This is arguably not UB at all, because it's absolutely certain that such functions exist at given addresses.
Other cases
No response
Anything else?
No response
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
Reproduce the example in src/main.rs and inspect the compiler's handling of const evaluation for function pointers and transmute. The work is complete when the shown constant compiles successfully for an arbitrary address without introducing an invalid-value diagnostic.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100