Can't call `unreachable!()` in const functions
Open
Nobody has claimed this yet.
C-feature-request
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I tried this code:
const fn foo() {
unreachable!("Hello");
}
I expected to see this happen:
Code compiles
Instead, this happened:
error[E0015]: cannot call non-const formatting macro in constant functions
--> src/lib.rs:2:5
|
2 | unreachable!("Hello");
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
This happens on both stable and nightly. Almost identical panic!() macro works fine, so I expect unreachable!() and todo!() to work as well, as long as no formatting is applied to the provided string.
Meta
rustc --version --verbose:
rustc 1.99.0-nightly (d0babd8b6 2026-07-15)
binary: rustc
commit-hash: d0babd8b6b05ef9bb65d42f928cef4129d64cf65
commit-date: 2026-07-15
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 22.1.8
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 reported snippet in src/lib.rs using the linked Rust Playground, then trace how unreachable!() is expanded and checked in const functions. Done means the no-formatting invocation compiles like the analogous panic!() case, with regression coverage for the reported behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100