Skip to content

consts in dead monomorphic async fn are never evaluated #140655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
moxian opened this issue May 5, 2025 · 1 comment
Open

consts in dead monomorphic async fn are never evaluated #140655

moxian opened this issue May 5, 2025 · 1 comment
Labels
A-async-await Area: Async & Await A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@moxian
Copy link
Contributor

moxian commented May 5, 2025

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

@moxian moxian added the C-bug Category: This is a bug. label May 5, 2025
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-async-await Area: Async & Await A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 5, 2025
@RalfJung
Copy link
Member

RalfJung commented May 5, 2025

Note that these are not "const checks", this is standard const evaluation. "const checks" refers to checking whether some operations are even legal inside const fn; this is a form of static analysis. I'll rename the issue.

@RalfJung RalfJung changed the title const checks are not performed in async functions consts in dead monomorphic async fn are never evaluated May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-async-await Area: Async & Await A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants