Skip to content

Add using and await using Declarations, SuppressedError, DisposableStack, and AsyncDisposableStack#3000

Open
rbuckton wants to merge 92 commits intotc39:mainfrom
rbuckton:explicit-resource-management
Open

Add using and await using Declarations, SuppressedError, DisposableStack, and AsyncDisposableStack#3000
rbuckton wants to merge 92 commits intotc39:mainfrom
rbuckton:explicit-resource-management

Conversation

@rbuckton
Copy link
Copy Markdown
Contributor

@rbuckton rbuckton commented Jan 23, 2023

This PR contains the Stage 3 specification text for Explicit Resource Management.

@rbuckton rbuckton force-pushed the explicit-resource-management branch from b7b82d5 to 17e0bc0 Compare January 23, 2023 23:33
@rbuckton
Copy link
Copy Markdown
Contributor Author

I'm not quite sure how to address the remaining esmeta issues.

@ljharb ljharb added normative change Affects behavior required to correctly evaluate some ECMAScript source text needs test262 tests The proposal should specify how to test an implementation. Ideally via github.com/tc39/test262 pending stage 4 This proposal has not yet achieved stage 4, but may otherwise be ready to merge. proposal This is related to a specific proposal, and will be closed/merged when the proposal reaches stage 4. labels Jan 24, 2023
@jmdyck
Copy link
Copy Markdown
Collaborator

jmdyck commented Jan 24, 2023

Rather than suggest changes here, I've made a PR against this PR's branch: rbuckton#2

@rbuckton
Copy link
Copy Markdown
Contributor Author

This last commit adds a few UpdateEmpty calls that aren't strictly necessary, but esmeta doesn't quite understand what's happening without it. I also had to switch AO parameters typed : a function object to : an ECMAScript language value since IsCallable doesn't seem to act as a type guard.

@ljharb ljharb marked this pull request as draft January 25, 2023 22:30
@bakkot
Copy link
Copy Markdown
Member

bakkot commented Jan 25, 2023

@rbuckton Sorry about that - esmeta is still a little imprecise. You should feel free to write the spec text you want and not worry about esmeta's errors, and we can update esmeta or the ignore file.

Comment thread spec.html Outdated
Comment thread spec.html Outdated
@rbuckton
Copy link
Copy Markdown
Contributor Author

@rbuckton Sorry about that - esmeta is still a little imprecise. You should feel free to write the spec text you want and not worry about esmeta's errors, and we can update esmeta or the ignore file.

I replaced the UpdateEmpty calls with a few asserts that seem to be accurate. For example, in ForIn/OfBodyEvaluation, if status is abrupt going in to DisposeResources, it should still be abrupt on the way out. Likewise, ForBodyEvaluation won't return a normal completion for empty, so DisposeResources shouldn't produce one either.

@rbuckton
Copy link
Copy Markdown
Contributor Author

Please note, I've created rbuckton#3 as a separate PR against this one containing the additional specification changes that would be necessary to support https://github.com/tc39/proposal-async-explicit-resource-management, should it advance to Stage 3.

Comment thread spec.html Outdated
@rbuckton
Copy link
Copy Markdown
Contributor Author

I found an issue with the change in e4f2d05 to use an EE to restrict binding patterns, in that it results in an ambiguous parse of using[x] before the EE can be applied, which was not intended.

I've filed tc39/proposal-explicit-resource-management#152 against the proposal spec, and will update this PR in due time.

@rbuckton
Copy link
Copy Markdown
Contributor Author

I found an issue with the change in e4f2d05 to use an EE to restrict binding patterns, in that it results in an ambiguous parse of using[x] before the EE can be applied, which was not intended.

I've filed tc39/proposal-explicit-resource-management#152 against the proposal spec, and will update this PR in due time.

tc39/proposal-explicit-resource-management#152 has merged, and I've amended this PR to match as part of 3044fd9.

Comment thread spec.html Outdated
@rbalicki2

This comment was marked as duplicate.

Comment thread spec.html Outdated
@rbuckton
Copy link
Copy Markdown
Contributor Author

This now includes the await using declaration and AsyncDisposableStack, per prior consensus (incl. the March 2023 plenary).

@rbuckton
Copy link
Copy Markdown
Contributor Author

Note to implementers: There is a typo in GetDisposeMethod that will be addressed by tc39/proposal-explicit-resource-management#217. The typo was introduced as part of a normative change that reached consensus in July 2023, but fixing it is also a potentially a normative change and may require additional consensus.

@rbuckton
Copy link
Copy Markdown
Contributor Author

This is also missing the consensus change to use await using as the syntactic head for the asynchronous form of the using declaration. That will be up shortly.

rbuckton and others added 20 commits April 27, 2026 18:35
I suggested `variants="Disposable object"` because I suggested
putting <dfn> around "Disposable interface"
(similar to the dfn for other interfaces).

If instead you put <dfn> around only "Disposable",
then you don't need the variants attribute,
because any occurrence of "Disposable object"
will get the "Disposable" auto-linked.

Ditto AsyncDisposable.
An ExportDeclaration is a ModuleItem, not a Declaration,
so I don't think IsUsingDeclaration or IsAwaitUsingDeclaration
would ever be applied to an ExportDeclaration.
IsFunctionDefinition, AssignmentTargetType, and HasCallInTailPosition
already have explicit rules for CoverAwaitExpressionAndAwaitUsingDeclarationHead,
and AwaitExpression only arises by reparsing CoverAwaitExpressionAndAwaitUsingDeclarationHead,
so those SDOs would never reach AwaitExpression.
At this point, you know that _method_ is present,
and therefore must be a function object,
and so IsCallable(_method_) must be *true*,
so this step can't do anything.
The use of _env_.[[DisposeCapability]]
assumes that _env_ is a Declarative Environment Record,
though I'm not sure that's the case.
@rbuckton rbuckton force-pushed the explicit-resource-management branch from bf962d5 to 878e362 Compare April 27, 2026 22:38
@linusg
Copy link
Copy Markdown
Member

linusg commented Apr 28, 2026

Thanks for the changes, LGTM except for the explicit [[DisposableResourceStack]] unrooting in DisposeResources. There is precedent in 262 for informing implementors about collectable data structures, all in form of a NOTE step:

https://tc39.es/ecma262/#sec-%iteratorhelperprototype%.return

NOTE: Once a generator enters the completed state it never leaves it and its associated execution context is never resumed. Any execution state associated with obj can be discarded at this point.

https://tc39.es/ecma262/#sec-generatorstart

NOTE: Once a generator enters the completed state it never leaves it and its associated execution context is never resumed. Any execution state associated with acGenerator can be discarded at this point.

https://tc39.es/ecma262/#sec-generatorresumeabrupt

NOTE: Once a generator enters the completed state it never leaves it and its associated execution context is never resumed. Any execution state associated with generator can be discarded at this point.

I believe this is sufficiently clear and we should not assign values that are impossible to observe.

@rbuckton
Copy link
Copy Markdown
Contributor Author

rbuckton commented Apr 30, 2026

Thanks for the changes, LGTM except for the explicit [[DisposableResourceStack]] unrooting in DisposeResources. There is precedent in 262 for informing implementors about collectable data structures, all in form of a NOTE step:

I don't have a strong preference, though I'm wary of the hand-wavyness of just using a NOTE for a feature explicitly focused on resource/memory cleanup.

@michaelficarra
Copy link
Copy Markdown
Member

@rbuckton I made a handy flowchart

image

@jmdyck
Copy link
Copy Markdown
Collaborator

jmdyck commented Apr 30, 2026

Re Assert vs Note in the diagram: I'm not sure how I'd discern whether something is an invariant. My preference is that an Assert's condition should be something that, if you were working out an example and got to that step, you could actually check and confirm its truth at that point. (So, e.g., a statement about the future should be a Note, not an Assert.)

@michaelficarra
Copy link
Copy Markdown
Member

@jmdyck The point was just that "you have an opportunity to do cleanup here" is definitely not an invariant, and it also doesn't impose a normative requirement, so it's either fluff or it's a note.

@jmdyck
Copy link
Copy Markdown
Collaborator

jmdyck commented Apr 30, 2026

You should add type="built-in function" for

  • sec-suppressederror
  • sec-iterator.prototype-%symbol.dispose%
  • sec-%asynciteratorprototype%-%symbol.asyncdispose%
  • sec-disposablestack
  • sec-disposablestack.prototype.adopt
  • sec-disposablestack.prototype.defer
  • sec-disposablestack.prototype.dispose
  • sec-get-disposablestack.prototype.disposed
  • sec-disposablestack.prototype.move
  • sec-disposablestack.prototype.use
  • sec-asyncdisposablestack
  • sec-asyncdisposablestack.prototype.adopt
  • sec-asyncdisposablestack.prototype.defer
  • sec-asyncdisposablestack.prototype.disposeAsync
  • sec-get-asyncdisposablestack.prototype.disposed
  • sec-asyncdisposablestack.prototype.move
  • sec-asyncdisposablestack.prototype.use

(I had a commit to do this, but there was a rebase problem.)

@jmdyck
Copy link
Copy Markdown
Collaborator

jmdyck commented Apr 30, 2026

I suggest changing "This function" to "This method" for:

  • sec-iterator.prototype-%symbol.dispose%
  • sec-iterator.prototype-%symbol.iterator%
  • sec-%asynciteratorprototype%-%symbol.asyncdispose%
  • sec-%asynciteratorprototype%-%symbol.asynciterator%

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

Labels

has test262 tests normative change Affects behavior required to correctly evaluate some ECMAScript source text pending stage 4 This proposal has not yet achieved stage 4, but may otherwise be ready to merge. proposal This is related to a specific proposal, and will be closed/merged when the proposal reaches stage 4.

Projects

None yet

Development

Successfully merging this pull request may close these issues.