Repository navigation
Fix downlevel emit for private names in for-in/for-of assignment targets - #65427
Open
Thanh Nguyen (RealThanhNguyxn) wants to merge 1 commit into
Open
Thanh Nguyen (RealThanhNguyxn) wants to merge 1 commit into
Thanh Nguyen (RealThanhNguyxn) wants to merge 1 commit into
Conversation
The assignment target of a for-in/for-of statement is evaluated on each iteration, so a private name cannot be lowered to a __classPrivateFieldGet call in the initializer; the helper returns a value, not a Reference. When the initializer contains a private name that must be transformed, bind the iteration value to a temporary and perform the (transformed) assignment at the start of the loop body. The original body is kept in its own block so that bindings declared in it cannot shadow identifiers used by the target. This also fixes destructuring assignment targets such as `for ([this.#x] of ...)`, which previously emitted the get helper as an element of the pattern.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Destructuring private-target lowering can evaluate receivers too early and update the wrong object, and for-await private-target regression coverage is missing across compilation targets.
2 open issues
What changed in this PR
Addresses #65413 by lowering private-name loop assignment targets into assignments at the start of each iteration, rather than emitting invalid helper calls as loop targets.
Changes:
- Introduces temporary loop bindings while preserving the original body’s scope.
- Adds conformance cases for fields, accessors, static members, destructuring, receiver side effects, and shadowing.
- Adds JavaScript, type, and symbol baselines.
| File | Description |
|---|---|
tsc/testdata/tests/cases/conformance/classes/members/privateNames/privateNameForInOfTarget.ts |
Adds private-name loop regression cases. |
tsc/testdata/baselines/reference/conformance/privateNameForInOfTarget.types |
Records expected types. |
tsc/testdata/baselines/reference/conformance/privateNameForInOfTarget.symbols |
Records expected symbol bindings. |
tsc/testdata/baselines/reference/conformance/privateNameForInOfTarget.js |
Records expected downlevel JavaScript. |
tsc/internal/transformers/estransforms/classfields.go |
Rewrites affected loop targets into body assignments. |
Review effort: Balanced
| ) | ||
| declarationList.Loc = statement.Initializer.Loc | ||
|
|
||
| assignmentExpression := f.NewAssignmentExpression(statement.Initializer, temp) |
|
|
||
| return f.UpdateForInOrOfStatement( | ||
| statement, | ||
| statement.AwaitModifier, |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Fixes #65413
When downleveling
for (this.#x of ...)/for (this.#x in ...), the private name was lowered to__classPrivateFieldGet(...)directly in the loop initializer. The get helper returns a value rather than a Reference, so the emitted code fails at runtime withReferenceError: Invalid left-hand side in for-loop. The same happened for destructuring targets such asfor ([this.#x] of ...), which emitted aSyntaxErrorinstead.This changes the class fields transformer to bind the iteration value to a temporary and perform the (already lowered) assignment at the start of the loop body when the initializer contains a private name that needs transforming. This keeps the assignment target evaluation per iteration, which matters for side-effecting receivers (e.g.
for (getReceiver().#x of ...)must callgetReceiver()for every value). The original body is kept in its own block so that bindings declared in it cannot shadow identifiers used by the assignment target.The existing destructuring wrapper (
wrapPrivateIdentifierForDestructuringTarget) is not reused here because it pushes the receiver assignment intopendingExpressions, which is hoisted out of the loop.The fix covers private fields, private accessors, static private members,
for...in,for...of,for await...of, and destructuring assignment targets.Tests
privateNameForInOfTarget.tswith baselines.go test ./internal/testrunnergo test ./internal/compiler ./internal/transformers/...getReceiver().#xcalling the receiver twice and body bindings that would otherwise shadow the target receiver.