-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(hir): same-name classes get their own ClassId per scope — the inner body is no longer silently dropped (#9466) #9527
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
026ecb9
test(9466): gap fixture — same-name classes at different lexical depths
d484568
test(9466): cover every block-kind scope — switch cases and loop bodies
b483399
test(9466): instanceof arms that actually discriminate — block bounda…
25c5016
fix(hir): disambiguate same-name classes per SCOPE, not per name (#9466)
8ab5438
test(9466): drop the loop-capture sub-arm — it discriminates a differ…
0bd261b
test(9466): wording — the capture gap is reported, not yet filed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| ### Fixed | ||
|
|
||
| - **Same-name `class` declarations at different lexical depths are distinct | ||
| classes again — the inner body is no longer silently dropped.** Perry keeps | ||
| two same-named classes apart by registering the second under a uniquified key | ||
| (`M$0`); the key was minted once per **name** instead of once per **scope**, so | ||
| the third and every later `class M` aliased onto the second's ClassId. Whichever | ||
| body registered first won, and the program ran the wrong methods with no | ||
| diagnostic: | ||
|
|
||
| ```ts | ||
| class M { v() { return "top"; } } | ||
| function h() { | ||
| class M { v() { return "outer"; } } | ||
| function h2() { class M { v() { return "inner"; } } return new M().v(); } | ||
| return [new M().v(), h2()].join(","); | ||
| } | ||
| console.log(new M().v(), h()); // node: top outer,inner perry: top outer,outer | ||
| ``` | ||
|
|
||
| Two defects, one symptom: | ||
|
|
||
| 1. **The "already renamed" guard was per name, not per scope.** | ||
| `maybe_rename_colliding_class` returned early on | ||
| `class_renames.contains_key(name)` — but `class_renames` is inherited by | ||
| nested bodies (it is saved and restored per body, so an enclosing body's | ||
| alias is live while the nested one lowers). A nested body declaring the same | ||
| name therefore took the early return and registered its `class X` under the | ||
| **outer** body's key. The map now carries the source span of the scope that | ||
| minted each alias, so the guard means "this scope already renamed it" — the | ||
| idempotence the guard existed for — and every nested scope mints its own. | ||
|
|
||
| 2. **Block scopes never ran the disambiguation scan at all.** Only function | ||
| bodies did, so two sibling `{ class Blk { … } }` blocks shared one ClassId | ||
| and the second ran the first's body: | ||
|
|
||
| ```ts | ||
| { class Blk { v() { return "b1"; } } console.log(new Blk().v()); } // b1 | ||
| { class Blk { v() { return "b2"; } } console.log(new Blk().v()); } // node b2, perry b1 | ||
| ``` | ||
|
|
||
| `class` is block-scoped, so the scan is now bracketed at every `{ … }`-shaped | ||
| scope — bare block, `if` / `else` branch, loop body, `try` / `catch` / | ||
| `finally`, and `switch` — mirroring `register_block_forward_lexicals` | ||
| (#6062), which brackets the same boundary for TDZ names: record only what the | ||
| scope changed, undo exactly that, so an alias owned by an enclosing scope | ||
| survives. | ||
|
|
||
| This is an **identity** fix, not a naming one: each declaration now gets its own | ||
| ClassId, so `instanceof` across the shadowing boundary is correct in both | ||
| directions (an inner instance is not `instanceof` the outer class, and vice | ||
| versa), `Object.getPrototypeOf(inner) !== Outer.prototype`, and `class Sub | ||
| extends M` inside the inner scope extends the **inner** `M`. `.name` keeps | ||
| reporting the source name for all of them — the display-name override #9413 | ||
| (PR #9465) installed on this exact path is what carries it, and every newly | ||
| minted alias goes through the same `lower_class_decl` site that records it. | ||
|
|
||
| Validated by `test-files/test_gap_9466_shadowed_class_identity.ts`, byte-identical | ||
| to `node --experimental-strip-types`. |
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Reserve enclosing class identities before nested scopes lower.
lookup_class(name)isNonewhen a nested block appears before a same-named direct class in its enclosing function body. Phase-1.5 records that outer declaration only inforward_class_names; it does not register a ClassId. The nestedclass Xthen remains unaliased and registers asX. The later enclosingclass Xcollides with that registration and can share the wrong ClassId.Track unresolved enclosing declarations by lexical scope, or pre-register their distinct identities before lowering nested scopes.
🤖 Prompt for AI Agents