rust-lang / rust-lang/rust

`generic_const_exprs` causes code to fail compilation even if the feature is not used

Open
#152,905 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-generic_const_exprs P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
fn main() {
    println!("Hello, world!");
    let a = foo::<1>();
    println!("{}", a.0);
}
const fn foo<const A: usize>() -> (usize, usize) {
    const { assert!(A > 0); }
    (0, 0)
}

And it fails with:

error: overly complex generic constant
 --> src/main.rs:9:11
  |
9 |     const { assert!(A > 0); }
  |           ^^^^^^^^^^^^^^^^^^^ blocks are not supported in generic constants
  |
  = help: consider moving this anonymous constant into a `const` function
  = note: this operation may be supported in the future

error: could not compile `testconst` (bin "testconst") due to 1 previous error

However if the feature(generic_const_exprs) is removed it works fine:

fn main() {
    println!("Hello, world!");
    let a = foo::<1>();
    println!("{}", a.0);
}
const fn foo<const A: usize>() -> (usize, usize) {
    const { assert!(A > 0); }
    (0, 0)
}

Output from cargo run

Hello, world!
0
Meta

rustc --version --verbose:

rustc 1.95.0-nightly (838709580 2026-02-17)
binary: rustc
commit-hash: 8387095803f21a256a9a772ac1f9b41ed4d5aa0a
commit-date: 2026-02-17
host: aarch64-apple-darwin
release: 1.95.0-nightly
LLVM version: 22.1.0
Backtrace

error: overly complex generic constant
 --> src/main.rs:9:11
  |
9 |     const { assert!(A > 0); }
  |           ^^^^^^^^^^^^^^^^^^^ blocks are not supported in generic constants
  |
  = help: consider moving this anonymous constant into a `const` function
  = note: this operation may be supported in the future

error: could not compile `testconst` (bin "testconst") due to 1 previous error

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

Reproduce the failure from the example in src/main.rs with cargo run using the listed nightly rustc. Trace handling of the const block with generic_const_exprs enabled; done means the example compiles and prints Hello, world! and 0 with the feature enabled, without breaking the no-feature case.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.