Skip to content
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

Allow yield inside of try / catch and catch #8413

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jaredpar
Copy link
Member

@jaredpar jaredpar commented Sep 6, 2024

This proposal expands the use of yield to

  • try portion of a try / catch block
  • catch portion of a try / catch block with some restrictions around finally

This proposal expands the use of `yield` to

- `try` portion of a `try / catch` block
- `catch` portion of a `try / catch` block with some restrictions around
  `finally`
@jaredpar jaredpar requested a review from a team as a code owner September 6, 2024 22:05
@timcassell
Copy link

Regarding yield-inside-catch-with-nested-finally, could that be resolved by generating 2 separate code paths, one for MoveNext and another for Dispose?

@jaredpar
Copy link
Member Author

jaredpar commented Sep 6, 2024

Regarding yield-inside-catch-with-nested-finally, could that be resolved by generating 2 separate code paths, one for MoveNext and another for Dispose?

I think the big problem is agreeing on what behavior yield should have when it's encountered in the Dispose path? Is it ignored? Do we throw an exception? etc ... My suspicion is that there won't be broad agreement on what is to be done there.

Basically I think the problem is more along agreeing what behavior is correct vs. implementing that behavior. Can add this to open issues though in case there is broad agreement on what the best behavior is.


### Code generation yield inside try / catch in async iterators

The code generation for `try / catch` blocks in async iterators will be largely the same as traditional iterators. The difference is that the return type of generated `catch` methods will be `ValueTask` instead of `void`.
Copy link
Member

@jcouv jcouv Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't follow this. Async iterators are more like async state machines than iterator state machines.
Instead of generating a MoveNext and a Dispose method with similar structure, we use a single method (MoveNext) for both normal and disposal execution. A flag indicates if we're in disposal and we use it to skip over non-disposal code as appropriate.
So we shouldn't need to generate catch methods.

Here are some notes on disposal in async iterators. Happy to discuss offline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Maybe i was looking at the wrong code gen here. Will take a look at this.

@jcouv jcouv self-assigned this Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants