Skip to content

Normative: Add "Late Errors for Function Call Assignment Targets" to Annex B #3568

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

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

Conversation

rkirsling
Copy link
Member

@rkirsling rkirsling commented Apr 18, 2025

Replaces #2193.

This PR specifies the web reality that the following are not early errors in sloppy mode on browser-hosted engines:

f() = 1;
f() *= 1;
f()++;
for (f() in [1]) {}
for (f() of [1]) {}

It aligns with the new Annex B editorial convention (#2952) and amounts to:

  • augmenting AssignmentTargetType with a new value ~web-compat~
  • throwing a ReferenceError immediately after performing the function call in each case

@bakkot
Copy link
Contributor

bakkot commented Apr 18, 2025

Thanks!

Web reality has the error occurring before evaluation of the RHS (try (function(){})() = console.log(0)), and with this PR it would happen after (specifically, Evaluation for assignment expressions will evaluate both sides and then perform PutValue, and only then will the error be thrown). I think we should try to match web reality as closely as possibly.

This is maybe what you're trying to achieve with the "Late Errors" section? But the spec doesn't have a notion of "late errors"; there's just parse failures, early errors which happen at the same time as parse errors, and errors which occur as explicit steps in the evaluation semantics or other AOs. Since this is not a parse failure it has to specified as an actual algorithmic step.

@rkirsling
Copy link
Member Author

Oops, yeah, I see that function f() { print("lhs"); } f() = print("rhs"); prints just lhs before throwing a ReferenceError. I was misunderstanding the precise behavior; will update.

@rkirsling
Copy link
Member Author

rkirsling commented Apr 19, 2025

Interestingly, we don't have agreement among browsers on how far the LHS processing goes.

λ eshost -sx "function f() { print("1"); return { valueOf() { print("2"); } }; } f() += 1;"
#### SpiderMonkey, V8
1
ReferenceError: [...]
λ eshost -sx "function f() { print("1"); return { valueOf() { print("2"); } }; } ++f();"
#### SpiderMonkey
1
2
ReferenceError: cannot assign to function call

#### V8
1
ReferenceError: Invalid left-hand side expression in prefix operation

(JSC omitted as it doesn't currently call f() at all.)

I am inclined to specify V8's behavior here; we should only go as far as web reality obliges us to.


Edit: JSC's behavior has now been updated to match V8's.

@rkirsling
Copy link
Member Author

rkirsling commented Apr 19, 2025

Okay, updated. Technically, nothing outside of AssignmentTargetType needs to be marked normative optional—for now, I've marked every usage of ~web-compat~, just in case we find it clearest for non-browser hosts, but I'm more than happy to undo all that if it's too noisy.

@rkirsling
Copy link
Member Author

Added tests in tc39/test262#4459.

@rkirsling rkirsling added normative change Affects behavior required to correctly evaluate some ECMAScript source text needs consensus This needs committee consensus before it can be eligible to be merged. web reality has test262 tests labels Apr 21, 2025
webkit-commit-queue pushed a commit to rkirsling/WebKit that referenced this pull request Apr 23, 2025
https://bugs.webkit.org/show_bug.cgi?id=291882

Reviewed by Yusuke Suzuki.

In tc39/ecma262#3568, we'll finally be specifying the web reality that `f() = 1` is not an early error in sloppy mode.
JSC's behavior on this was mostly aligned with SM/V8's back in https://commits.webkit.org/212076@main,
but there is one outstanding discrepancy to resolve: SM/V8 both call the function before throwing a ReferenceError.

This patch resolves said discrepancy, which applies to not only =, but also += and kin (excluding logical assignment),
++ / --, and for-in/of.

* JSTests/ChakraCore/test/Function/call1.baseline-jsc:
* JSTests/ChakraCore/test/LetConst/tdz1.baseline-jsc:
* JSTests/stress/sloppy-mode-function-call-assignment-targets.js: Added.
* LayoutTests/js/modify-non-references-expected.txt:
* LayoutTests/js/parser-syntax-check-expected.txt:
* LayoutTests/js/script-tests/modify-non-references.js:
* LayoutTests/svg/dom/SVGTransformList-anim-read-only-expected.txt:
* Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp:
(JSC::PostfixNode::emitBytecode):
(JSC::PrefixNode::emitBytecode):
(JSC::AssignErrorNode::emitBytecode):
(JSC::ForInNode::emitBytecode):
(JSC::ForOfNode::emitBytecode):
* Source/JavaScriptCore/parser/ASTBuilder.h:
(JSC::ASTBuilder::makeAssignNode):
* Source/JavaScriptCore/parser/NodeConstructors.h:
(JSC::AssignErrorNode::AssignErrorNode):
* Source/JavaScriptCore/parser/Nodes.h:
* Source/JavaScriptCore/parser/Parser.cpp:
(JSC::Parser<LexerType>::isSimpleAssignmentTarget):

Canonical link: https://commits.webkit.org/293996@main
@bakkot bakkot added the editor call to be discussed in the next editor call label May 13, 2025
Copy link
Member

@michaelficarra michaelficarra left a comment

Choose a reason for hiding this comment

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

LGTM otherwise!

@michaelficarra michaelficarra added has consensus This has committee consensus. and removed needs consensus This needs committee consensus before it can be eligible to be merged. editor call to be discussed in the next editor call labels May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has consensus This has committee consensus. has test262 tests normative change Affects behavior required to correctly evaluate some ECMAScript source text web reality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants