rust-lang / rust-lang/rust

consts in dead monomorphic async fn are never evaluated

Open
#140,655 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Split out from https://github.com/rust-lang/rust/issues/140600

I tried this code:

pub fn this_errors() {
    const { panic!("in fn") } //<~ ERROR: evaluation of `this_errors::{constant#0}` failed
}

pub async fn this_compiles() {
   const { panic!("in async") } // compiles
}

I expected to see this happen: both functions raise a const eval error (or at least both functions don't)

Instead, this happened: sync function raises const eval error, but the same function with an async keyword doesn't.

This is surprising from a perspective of a language user who is not familiar with intricacies of async internals. I didn't expect that making function async would "disable" const checks.

And even though i kinda sorta know some details of async desugaring, it is still surprising that the following "sorta desugared" code does fail to compile (unlike non-desugared version) (I realize that this desugaring is very far from perfect, I'm only dabbling)

fn desugared() -> impl Fn() {
    || const { panic!("in closure") } //<~ ERROR
}
Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-pc-windows-msvc
release: 1.86.0
LLVM version: 19.1.7

I'm not sure if this current behavior is a bug per se or if it's only just a mildly surprising behaviour, but thought it's still worth making an issue either way.

@rustbot label: +T-compiler +T-lang +A-async-await +A-const-eval

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 two Rust examples in the issue with the stated rustc version, then compare their const-evaluation behavior with the closure example. Determine whether async functions should match synchronous functions or whether the behavior is intentional; done means the language and compiler behavior are consistently defined and covered by appropriate compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.