rust-lang / rust-lang/rust

`const` in `async fn` is evaluated twice when used across crates.

Open
#148,335 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

dep/src/lib.rs

#![warn(long_running_const_eval)]

pub async fn has_const() {
    const CONST: () = {
        let mut i = 0;
        while i < 10_000_000 {
            i += 1;
        }
    };
    CONST
}

src/lib.rs

#![warn(long_running_const_eval)]

pub async fn call() {
    dep::has_const().await;
}

I expected rust to evaluate the const only once.

Instead, the long_running_const_eval fires when compiling the dep crate, and also fires again when compiling the dependent crate. This means that the const is evaluated at least twice, wasting compilation time.

(For some reason, it fired twice per crate, for a total of 4 times. I don't know why.)

This bug was discovered in https://github.com/rust-lang/rust/issues/148328, and filed here as a separate issue.

Meta

rustc --version --verbose:

rustc 1.93.0-nightly (d5419f1e9 2025-10-30)
binary: rustc
commit-hash: d5419f1e97b90741d51841f800d3c697c662567d  
commit-date: 2025-10-30
host: x86_64-pc-windows-msvc
release: 1.93.0-nightly
LLVM version: 21.1.3

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 two-crate example using dep/src/lib.rs and src/lib.rs, then compare the long_running_const_eval warnings and evaluation counts in each crate. Trace how const evaluation results are handled across the crate boundary; done means the const is evaluated once rather than repeatedly while preserving the warning behavior.

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.