rust-lang / rust-lang/rust

Using a closure in array repeat expr count causes "constant expression depends on a generic parameter" error

Open
#153,083 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Using an (unevaluated) closure to size an array results in a "constant expression depends on a generic parameter" error inside async or generic functions. Assigning to a constant and then using the constant to size the array works:

pub const fn fn_size(_f: fn() -> ()) -> usize {
    0
}

// broken
fn generic1<T>() {
    let _ = [0u8; fn_size(|| ())];
}

// works
fn generic2<T>() {
    const N: usize = fn_size(|| ());
    let _ = [0u8; N];
}

This error happens inside generic functions, async functions and async blocks:

async fn asynchronous() {
    let _ = [0u8; fn_size(|| ())];
}

fn generic1<T>() {
    let _ = [0u8; fn_size(|| ())];
}

fn async_block() {
    async {
        let _ = [0u8; fn_size(|| ())];
    };
}

Both stable (1.93.1) and nightly are affected.

playground with a larger set of working and broken examples.

Reddit post

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 generic, async function, and async block examples from the issue, using the linked Rust Playground and comparing stable with nightly. Investigate the compiler path handling array repeat expression counts involving closures and generic parameters. Done means the reported examples compile without the constant workaround while existing working examples remain valid.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.