Skip to content

consts in dead monomorphic async fn are never evaluated #140655

Open
@moxian

Description

@moxian

Split out from #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-monomorphizationArea: MonomorphizationC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions