Implement deterministic collapse of 'await' in 'await using' #58929
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This updates the
__disposeResourceshelper to align with a recent change to theDisposeResourcesalgorithm that reached consensus in the June TC39 meeting. It has the effect of reducing extraneous Await operations for cases like the following:It is a requirement that an
await usingdeclaration must trigger an Await when exiting the block such thatHAPPENS_BEFORE()andHAPPENS_AFTER()occur in different turns, even if the resource recorded wasnull/undefined. Prior to this change, theDisposeResourcesalgorithm would Await for eachnull/undefinedin anawait using, which could introduce significant delays to function execution. Rather than Await-ing three times in the above example, we can collapse these down to a single Await instead to preserve the aforementioned requirement.I should note that our downlevel emit is not as efficient as a native implementation since we must
awaitthe result of__disposeResources, which introduces 1-2 turn overhead as it adopts the state of the resultingPromise. The only way we could make that more efficient would be to inline the behavior__disposeResourcesinto thefinallyblock, which would dramatically increase emit size.For more context as to the effects of the DisposeResources algorithm as a result of this change, see tc39/proposal-explicit-resource-management#219 (comment)
As this changes our helpers, you can find a companion PR for tslib here: microsoft/tslib#262
Fixes #58876