rust-lang / rust-lang/rust

Const generic type inference works when it should not

Open
#142,378 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference 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:

#![expect(incomplete_features, reason = "generic_const_exprs")]
#![feature(generic_const_exprs)]

pub fn compute_root_only<const N: u64>() -> u64
where
    [(); N.ilog2() as usize]:,
{
    N
}

#[test]
fn test() {
    test_impl::<99>();
}

#[cfg(test)]
fn test_impl<const N: u64>()
where
    [(); N.ilog2() as usize]:,
{
    // Const generic is inferred, but it shouldn't be!
    assert_eq!(compute_root_only(), N);
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=1b2f4edef4886032e1f9391f671112f4

I expected to see this happen:

error[E0284]: type annotations needed
  --> src/lib.rs:22:16
   |
22 |     assert_eq!(compute_root_only(), N);
   |                ^^^^^^^^^^^^^^^^^ cannot infer the value of the const parameter `N` declared on the function `compute_root_only`
   |
note: required by a const generic parameter in `compute_root_only`
  --> src/lib.rs:4:26
   |
4  | pub fn compute_root_only<const N: u64>() -> u64
   |                          ^^^^^^^^^^^^ required by this const generic parameter in `compute_root_only`
help: consider specifying the generic argument
   |
22 |     assert_eq!(compute_root_only::<N>(), N);
   |                                 +++++

Instead, this happened: It compiles successfully 🤯

The key thing is where bound. If it is simplified to something simpler like this:

where
    [(); N]:,

Then I do get expected compiler error.

This is basically a miscompilation, compiler should not pick arbitrary matching where bound from the context of the caller to infer types of the called function unless corresponding constant is used explicitly.

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (cdd545be1 2025-06-07)
binary: rustc
commit-hash: cdd545be1b4f024d38360aa9f000dcb782fbc81b
commit-date: 2025-06-07
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

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 running the Rust Playground reproduction with rustc 1.89.0-nightly and compare the generic_const_exprs bound using N.ilog2() with the simplified [(); N]: bound. Investigate how the caller's where bound affects const generic inference, then add a regression test showing that compute_root_only() must not infer N implicitly and verify the expected error.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.