tc39 / proposal-explicit-resource-management

ECMAScript Explicit Resource Management
https://arai-a.github.io/ecma262-compare/?pr=3000
BSD 3-Clause "New" or "Revised" License
758 stars 30 forks source link

typo `const x = { [Symbol.dispose]() {} }` #181

Closed loynoir closed 1 year ago

loynoir commented 1 year ago

Related https://github.com/typescript-eslint/typescript-eslint/issues/7160

I think current implement might be insecure when

It might better if can avoid this at language scope.

bradzacher commented 1 year ago

The difficult thing is that a runtime error for const x = <disposable> would be wholely backwards incompatible - it means you can't pass a disposable anywhere.

For example imagine this code:

function incrementCounter(arg) {
  const foo = arg;
  foo.counter = (foo.counter ?? 0) + 1;
}

using disposable = {[Symbol.dispose]() {}};
incrementCounter(disposable);
incrementCounter(disposable);

On the second line the alias is completely harmless to the program running and harmless to the conceptual flow of the disposable - so the runtime preventing it would make the feature really hard to use without crashing!

rixtox commented 1 year ago

This is a dupe to #159

After more in-depth discussion on that thread, it started to be apparent to me the reason behind the choice of returning Disposable type is to maintain drop-in level compatibility for existing functions. You can upgrade your existing function to return a Disposable without affecting any old code using the old dispose mechanism.

This compatibility however, created a unique challenge in linting code for disposal mistake like forgetting to write using. Read more in the "Linter failure" section of this thread. I'd like to hear from @rbuckton on a solution to this problem.

loynoir commented 1 year ago

Closed as seems duplicated.

Wow, that thread is really long.