-
Notifications
You must be signed in to change notification settings - Fork 4.6k
parser: preserve TDZ for const declared in switch case #30936
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
robobun
wants to merge
2
commits into
main
Choose a base branch
from
farm/05559398/switch-case-const-tdz
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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
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,101 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/30932 (and #18477) | ||
| // | ||
| // Every case in a switch shares one lexical scope but each case is entered | ||
| // conditionally, so a `const` declared in one case does not dominate references | ||
| // to the same name in sibling cases. Two inlining passes in the parser used to | ||
| // ignore this and produced code that silently bypassed the spec-required TDZ | ||
| // `ReferenceError`: | ||
| // | ||
| // 1. const-local-prefix inlining — `const X = literal` in the first case was | ||
| // recorded and every later reference to `X` in the switch body was | ||
| // rewritten to `literal`. | ||
| // 2. single-use substitution — `visit_stmts` ran once per case body but | ||
| // `use_count_estimate` is global, so when visiting `case "*"` the | ||
| // counter had only seen `case "*"`s references. `const X = expr` + | ||
| // `return X` read as single-use and the decl was deleted, leaving | ||
| // sibling-case references dangling. | ||
|
robobun marked this conversation as resolved.
|
||
| describe("switch-case const does not leak across cases", () => { | ||
| test("literal-initialized const is not inlined into sibling case", () => { | ||
| function test(action: string) { | ||
| switch (action) { | ||
| case "*": | ||
| const CONSTANT = 2; | ||
| return "matched " + CONSTANT; | ||
| case "a": | ||
| return "a=" + CONSTANT; | ||
| } | ||
| } | ||
| expect(() => test("a")).toThrow(ReferenceError); | ||
| expect(test("*")).toBe("matched 2"); | ||
| }); | ||
|
|
||
| test("non-foldable const is not substituted out of sibling case", () => { | ||
| function test(action: string) { | ||
| switch (action) { | ||
| case "*": | ||
| const X = Math.random(); | ||
| return "* " + X; | ||
| case "a": | ||
| return "a " + X; | ||
| } | ||
| } | ||
| // Post-fix JSC raises "Cannot access 'X' before initialization" (TDZ). | ||
| // Pre-fix, Bun deleted the `const X` decl entirely as a single-use | ||
| // substitution while visiting case "*", turning the case "a" reference | ||
| // into an unbound "X is not defined" — also a ReferenceError, but with | ||
| // different semantics — so match on name + message shape. | ||
| expect(() => test("a")).toThrow( | ||
| expect.objectContaining({ | ||
| name: "ReferenceError", | ||
| message: expect.stringContaining("before initialization"), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| test("outer const is shadowed by inner const, not leaked through", () => { | ||
| const CONSTANT = 1; | ||
| function test(action: string) { | ||
| switch (action) { | ||
| case "*": | ||
| const CONSTANT = 2; | ||
| return "* " + CONSTANT; | ||
| case "a": | ||
| return "a " + CONSTANT; | ||
| } | ||
| return "outer=" + CONSTANT; | ||
| } | ||
| // The inner `const CONSTANT` hoists over the entire switch body and | ||
| // shadows the outer one, so touching it from `case "a"` before the | ||
| // `case "*"` declaration has evaluated is TDZ. | ||
| expect(() => test("a")).toThrow(ReferenceError); | ||
| expect(test("*")).toBe("* 2"); | ||
| // Outside the switch body the outer `CONSTANT` is still visible. | ||
| expect(test("other")).toBe("outer=1"); | ||
| }); | ||
|
|
||
| test("declaration + use in the same case still works", () => { | ||
| function test() { | ||
| switch ("a") { | ||
| case "a": { | ||
| const X = 42; | ||
| return X; | ||
| } | ||
| } | ||
| } | ||
| expect(test()).toBe(42); | ||
| }); | ||
|
|
||
| test("fall-through from declaring case still initializes binding", () => { | ||
| function test() { | ||
| switch ("a") { | ||
| case "a": | ||
| const X = 100; | ||
| case "b": | ||
| return "X=" + X; | ||
| } | ||
| } | ||
| expect(test()).toBe("X=100"); | ||
| }); | ||
| }); | ||
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.