Skip to content

Desugar async blocks in HIR instead of MIR #997

@cjgillot

Description

@cjgillot

Proposal

In the current implementation, gen/async/async gen blocks and closures have type Coroutine(..) and CoroutineClosure(..). Those types implement Iterator, Future or AsyncIterator depending on the initial desugaring.

This creates a lot of complexity:

  • trait solvers must check which kind of coroutine each time;
  • MIR StateTransform needs to fixup types depending on the coroutine kind.

I propose to change the desugaring for coroutines to:

  • gen { .. } becomes CoroutineIterator::from_coroutine(#[coroutine] { .. });
  • async { .. } becomes CoroutineFuture::from_coroutine(#[coroutine] { .. });
  • async gen { .. } becomes CoroutineAsyncIterator::from_coroutine(#[coroutine] { .. }).

This way, all coroutines implement std::ops::Coroutine and core is responsible for translating this to user-friendly traits. All the complexity is pushed to error-reporting code, which is not soundness-critical.

Coroutine closures are a little more complex, as we need to keep the CoroutineClosure type for borrow-checking.

Mentors or Reviewers

This involves MIR manipulation and trait solving, so @oli-obk and @lcnr need to know.

Process

The main points of the Major Change Process are as follows:

  • File an issue describing the proposal.
  • A compiler team member who is knowledgeable in the area can second by writing @rustbot second or kickoff a team FCP with @rfcbot fcp $RESOLUTION.
  • Once an MCP is seconded, the Final Comment Period begins.
    • Final Comment Period lasts for 10 days after all outstanding concerns are solved.
    • Outstanding concerns will block the Final Comment Period from finishing. Once all concerns are resolved, the 10 day countdown is restarted.
    • If no concerns are raised after 10 days since the resolution of the last outstanding concern, the MCP is considered approved.

You can read more about Major Change Proposals on forge.

Caution

Concerns (1 active)

Managed by @rustbot—see help for details.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-compilerAdd this label so rfcbot knows to poll the compiler teamfinal-comment-periodThe FCP has started, most (if not all) team members are in agreementhas-concernsThere are active concernsmajor-changeA proposal to make a major change to rustc

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions