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 8 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
@poizan42
Copy link

poizan42 commented Oct 20, 2024

May I suggest that instead of resetting the stack trace, throw; is lowered to System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex).Throw() so the original stack trace is preserved like with a normal throw;?

(It should be .Capture(ex).Throw() and not .Throw(ex) as the latter is a static method and cannot be easily polyfilled if people want to use the new C# version with runtimes earlier than .net core 2.0)

proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved

### Dispose and finally in async iterators

The `DiposeAsync` behavior for async iterators mirrors that of traditional iterators in that it executes the `finally` blocks at the the point the state machine is suspended. However it does this by setting the `state` variable to a value that represents disposing and then calls `MoveNextAsync`. The implementation of `MoveNextAsync` will then execute only the `finally` blocks for the suspend point. The `DisposeAsync` method does not have a mirror copy of the `finally` blocks.
Copy link
Member

@jjonescz jjonescz Oct 23, 2024

Choose a reason for hiding this comment

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

Curious, is there some reason why the codegen of DisposeAsync is different from Dispose? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

I suspect it's mostly an accident of history. The async state machine didn't have to deal with Dispose scenarios initially hence never went through the necessary rewriting process. At the point we added async iterators it was easier to just tweak the state field to represent disposing and using the existing state machine.

@jcouv can confirm or give a better explanation ;)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it was simpler and more elegant/lightweight to use a single state machine with a "disposal" flag for async-iterators. I was aware of the codegen for iterators but didn't see a benefit to the added overhead (second state machine with bunch of logic extracted into helper methods).

proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
proposals/yield-in-try-catch.md Show resolved Hide resolved
proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
2. The contents of the `when` clause will be generated into a parameterless method on the iterator with a `bool` return.
3. The `catch` block will be replaced with a call to the generated method in `MoveNext`.
4. The `when` clause will be replaced with a call to the generated method in `MoveNext`.
5. The `Dispose` method will mirror `catch` and `finally` blocks in the same way it mirrors `finally` blocks today and dispatch to the appropriate method
Copy link
Member

Choose a reason for hiding this comment

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

Could we also do codegen similar to async iterators, i.e., share MoveNext and Dispose implementations?

Copy link
Member Author

Choose a reason for hiding this comment

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

That is definitely an option, essentially changing iterators to favor the async iterators code gen approach. I think that would be fine as I can't see any technical reason why iterators have to be done that way but we'd want to vet it out a bit

proposals/yield-in-try-catch.md Outdated Show resolved Hide resolved
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.

5 participants