Skip to content

[rustc] Recompute required constants for coroutine drop shim - #165972

Draft
jayanth260 wants to merge 1 commit into
rust-lang:mainfrom
jayanth260:fix-coroutine-required-consts
Draft

jayanth260 wants to merge 1 commit into
rust-lang:mainfrom
jayanth260:fix-coroutine-required-consts

Conversation

@jayanth260

@jayanth260 jayanth260 commented Nov 14, 2026 •

Copy link
Copy Markdown

Fixes #148335

Summary

When generating coroutine drop shims, rustc clones the original coroutine MIR body and transforms it into a new body.

However, cloning the body also copies its required_consts metadata. Since the generated drop shim can have a different set of required constants from the original coroutine, this metadata may become stale.

The monomorphization collector evaluates all constants listed in required_consts. As a result, constants required only by the original coroutine can be unnecessarily evaluated again when the generated drop shim is instantiated in a downstream crate. This is particularly noticeable with expensive constants in public async functions used across crate boundaries.

Recompute required_consts after transforming each generated drop-shim body, so that the metadata reflects the resulting MIR rather than the original coroutine body. This recomputation is applied in all three coroutine drop-shim construction paths.

Effect

In the example from #148335, the original compilation produced four warning diagnostics: two in the source crate and two in the downstream crate. However, these corresponded to only two CTFE executions—one in each crate.

With this fix, the constant is evaluated only once, in the source crate. The unnecessary CTFE execution in the downstream crate is eliminated.

LLM Usage: Used Chat-GPT for writing tests.

@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2026
@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @mejrs (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@jayanth260
jayanth260 force-pushed the fix-coroutine-required-consts branch from 89ec53c to 82eeaa1 Compare November 14, 2026 19:57
@Kivooeo

Kivooeo commented Nov 15, 2026

Copy link
Copy Markdown
Member

Hi,

Used Chat-GPT for writing tests

This violates our LLM Policy because LLM-generated tests count as LLM-created code, you need to find an LLM reviewer to review your PR, you can do so in #llm-reviews Zulip channel. I'm going to move this to draft until so.

@Kivooeo
Kivooeo marked this pull request as draft November 15, 2026 07:10
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 15, 2026
@Kivooeo Kivooeo added the llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. label Nov 15, 2026
@oli-obk oli-obk self-assigned this Nov 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-assisted An LLM-assisted PR as defined by the LLM policy. Requires ahead-of-time consent by assignee. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants