impr: block scopes in the nameRegistry#2177
Conversation
|
pkg.pr.new packages benchmark commit |
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:No major changes. 📋 All resultsClick to reveal the results table (343 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
|
After merging this PR, we should be able to change how we get unique index in the for of loop. EDIT: |
There was a problem hiding this comment.
Pull request overview
This PR refines identifier generation by adding block-level scoping to nameRegistry, reducing shadowing and improving determinism in preparation for upcoming bundler changes.
Changes:
- Add block-scope tracking to
NameRegistryand wire it intoResolutionCtxblock scope lifecycle. - Update WGSL generation for
for ... of ...to use a dedicated block scope and add a test for invalid loop variable identifiers. - Update example snapshot tests (variable renames like
i→i_1) and minor docs formatting cleanup.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/src/nameRegistry.ts | Implements a scope stack with function + block scopes and adds push/pop block scope APIs. |
| packages/typegpu/src/resolutionCtx.ts | Ensures block scope pushes/pops also advance the name registry scope stack. |
| packages/typegpu/src/tgsl/wgslGenerator.ts | Wraps for...of loop variable naming/definition in a block scope. |
| packages/typegpu/tests/tgsl/wgslGenerator.test.ts | Updates comptime usage and adds a test asserting invalid loop variable names throw. |
| packages/typegpu/tests/examples/individual/jelly-switch.test.ts | Snapshot update reflecting new scoped renaming (i → i_1). |
| packages/typegpu/tests/examples/individual/jelly-slider.test.ts | Snapshot update reflecting new scoped renaming (i → i_1). |
| packages/typegpu/tests/examples/individual/blur.test.ts | Snapshot update reflecting new scoped renaming (r → r_1) and downstream references. |
| packages/typegpu/tests/examples/individual/3d-fish.test.ts | Snapshot update reflecting new scoped renaming (i → i_1) and downstream references. |
| apps/typegpu-docs/src/examples/algorithms/mnist-inference/index.ts | Minor expression formatting cleanup (removes unnecessary parentheses). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 'use gpu'; | ||
| const arr = [1, 2, 3]; | ||
| let res = 0; | ||
| for (const i of arr) { |
There was a problem hiding this comment.
This was my mystery test case that, haha. Nice!
| } | ||
| var distanceFromOrigin = max(0f, intersection.tMin); | ||
| for (var i = 0; (i < 64i); i++) { | ||
| for (var i_1 = 0; (i_1 < 64i); i_1++) { |
There was a problem hiding this comment.
Why is this name changed?
There was a problem hiding this comment.
This change is related to how we generate regular for loops. Currently, we generate the loop header first and then create a new block for the body. Because of this, two consecutive loops cause a name collision
for (let i = 0; ...) {
...
}
for (let i = 7; ...) {
...
}I’ll fix that first thing in the morning.
packages/typegpu/tests/examples/individual/jelly-slider.test.ts
Outdated
Show resolved
Hide resolved
…n/TypeGPU into impr/better-nameRegistry
This PR introduces finer-grained scoping to our
nameRegistryby implementing block scopes.It aims to:
esbuildtorolldownin upcoming Vite 8Additionally:
Below, I attach diagram of new

nameRegistry