rust-lang / rust-lang/rust

Inline const errors are opt-level-dependent

Open
#160,964 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-eval A-monomorphization C-bug I-lang-radar T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

fn inline_const() {
    const { panic!() }
}
pub fn main() {
    inline_const();
}
fn inline_const_with_const_generics_and_println<const N: i32>() {
    const { panic!() }
    println!();
}
pub fn main() {
    inline_const_with_const_generics_and_println::<0>();
}
fn inline_const_with_const_generics<const N: i32>() {
    const { panic!() }
}
pub fn main() {
    inline_const_with_const_generics::<0>();
}
fn inline_const_with_type_generics<T>() {
    const { panic!() }
}
pub fn main() {
    inline_const_with_type_generics::<i32>();
}
fn inline_const_with_const_generics_and_return<const N: i32>() -> Result<(), Box<dyn std::error::Error>> {
    const { panic!() }
    Ok(())
}
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
    inline_const_with_const_generics_and_return::<0>()
}

I expected to see this happen:

The compile error should always occur, regardless of opt-level

error[E0080]: evaluation panicked: explicit panic
 --> repro.rs:3:13
  |
3 |     const { panic!() }
  |             ^^^^^^^^ evaluation of `inline_const::{constant#0}` failed here

note: erroneous constant encountered
 --> repro.rs:3:5
  |
3 |     const { panic!() }
  |     ^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.

Instead, this happened:

opt=0 opt=1 opt=2 opt=3 opt=3 (bin) opt=3 (inline-mir=no)
inline_const ERROR ERROR ERROR ERROR ERROR ERROR
inline_const_with_const_generics_and_println ERROR ERROR ERROR ERROR ERROR ERROR
inline_const_with_const_generics ERROR ERROR pass pass ERROR ERROR
inline_const_with_type_generics ERROR ERROR pass pass ERROR ERROR
inline_const_with_const_generics_and_return ERROR ERROR pass pass ERROR ERROR
rustc --edition 2024 -C opt-level=0 --crate-type lib --emit=obj -o out.o repro.rs
rustc --edition 2024 -C opt-level=1 --crate-type lib --emit=obj -o out.o repro.rs
rustc --edition 2024 -C opt-level=2 --crate-type lib --emit=obj -o out.o repro.rs
rustc --edition 2024 -C opt-level=3 --crate-type lib --emit=obj -o out.o repro.rs
rustc --edition 2024 -C opt-level=3 --crate-type bin --emit=obj -o out.o repro.rs
rustc +nightly --edition 2024 -C opt-level=3 -Z inline-mir=no --crate-type lib --emit=obj -o out.o repro.rs
Meta

rustc --version --verbose:

rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: x86_64-pc-windows-msvc
release: 1.97.1
LLVM version: 22.1.6

The issue also reproduces with rustc 1.99.0-nightly (ad3d0bc14 2026-07-31).

Related
Note

While I used an LLM to investigate this issue and perform machine translation, I myself conducted the issue reproduction and original text creation, as required by the Rust LLM Usage Policy, which I note here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied rustc commands across opt-levels, crate types, and -Z inline-mir=no, then compare the differing compilation paths. Read related issue #107503 and pull request #122568 for context. Done means every listed example consistently reports the inline-const panic instead of passing at optimization levels 2 and 3.

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
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.