-
-
Notifications
You must be signed in to change notification settings - Fork 145
fix(symbol): resolve well-known symbols via optional chaining Symbol?.iterator (#6719)
#6729
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
Merged
proggeramlug
merged 2 commits into
PerryTS:main
from
proggeramlug:fix/6719-optional-chain-symbol
Jul 21, 2026
+209
−76
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // #6719: reading a well-known symbol off the `Symbol` constructor through | ||
| // OPTIONAL chaining (`Symbol?.iterator`) must return the same symbol as dot | ||
| // access (`Symbol.iterator`). #6676 fixed the computed bracket form | ||
| // (`Symbol["iterator"]`); the optional-chain form lowers through a different | ||
| // arm (`arm_optchain.rs`) that never routed through the well-known-symbol | ||
| // resolver, so it fell through to a generic property read on the `Symbol` | ||
| // constructor and returned `undefined`. `Symbol` is a non-nullish global, so | ||
| // `Symbol?.iterator` is exactly `Symbol.iterator`. | ||
| // | ||
| // Downstream impact: a class/object keyed with `[Symbol?.iterator]` was keyed | ||
| // under `undefined` and so was not iterable. | ||
| // | ||
| // Validated byte-for-byte against `node --experimental-strip-types`. | ||
|
|
||
| // ── optional dot (`Symbol?.iterator`) === dot access ──────────────────────── | ||
| console.log(Symbol?.iterator === Symbol.iterator, typeof Symbol?.iterator); | ||
| console.log( | ||
| Symbol?.asyncIterator === Symbol.asyncIterator, | ||
| typeof Symbol?.asyncIterator, | ||
| ); | ||
| console.log( | ||
| Symbol?.toPrimitive === Symbol.toPrimitive, | ||
| typeof Symbol?.toPrimitive, | ||
| ); | ||
| console.log( | ||
| Symbol?.hasInstance === Symbol.hasInstance, | ||
| typeof Symbol?.hasInstance, | ||
| ); | ||
| console.log( | ||
| Symbol?.toStringTag === Symbol.toStringTag, | ||
| typeof Symbol?.toStringTag, | ||
| ); | ||
|
|
||
| // ── optional computed key (`Symbol?.["iterator"]`) — string literal ───────── | ||
| console.log( | ||
| Symbol?.["iterator"] === Symbol.iterator, | ||
| typeof Symbol?.["iterator"], | ||
| ); | ||
| console.log( | ||
| Symbol?.["species"] === Symbol.species, | ||
| typeof Symbol?.["species"], | ||
| ); | ||
|
|
||
| // ── optional computed key with a runtime key (`Symbol?.[name]`) ───────────── | ||
| // Routes through `js_symbol_computed_member`, identity-equal to dot access. | ||
| const grab = (name: string): any => Symbol?.[name as any]; | ||
| for (const k of ["iterator", "asyncIterator", "toPrimitive", "match"]) { | ||
| console.log(k, grab(k) === (Symbol as any)[k], typeof grab(k)); | ||
| } | ||
|
|
||
| // ── a non-well-known key stays undefined ──────────────────────────────────── | ||
| console.log(typeof Symbol?.["definitelyNotAWellKnownSymbol" as any]); | ||
| console.log(typeof grab("notAKnownSymbol")); | ||
|
|
||
| // ── the real manifestation: a class computed method key via `?.` ──────────── | ||
| class R { | ||
| *[Symbol?.iterator]() { | ||
| yield 1; | ||
| yield 2; | ||
| } | ||
| } | ||
| console.log(JSON.stringify([...new R()])); // spread → [1,2] | ||
| const collected: number[] = []; | ||
| for (const v of new R()) collected.push(v); // for-of | ||
| console.log(collected.join(",")); | ||
|
|
||
| // an object literal keyed with `[Symbol?.iterator]` is iterable too | ||
| const obj = { | ||
| *[Symbol?.iterator]() { | ||
| yield 10; | ||
| yield 20; | ||
| yield 30; | ||
| }, | ||
| }; | ||
| console.log([...obj].join(",")); | ||
| console.log(Math.max(...obj)); // spread into a call | ||
|
|
||
| // ── a locally-shadowed `Symbol` must NOT fold — normal scoping wins ───────── | ||
| function shadowed(): string { | ||
| const Symbol = { iterator: "local-value" } as any; | ||
| return typeof Symbol?.iterator + ":" + Symbol?.iterator; | ||
| } | ||
| console.log(shadowed()); // node: "string:local-value" | ||
|
|
||
| // ── `?.` still short-circuits a genuinely nullish receiver ────────────────── | ||
| const maybe: any = null; | ||
| console.log(maybe?.iterator); // undefined |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.