fix: Keep generated identifiers off the template-parameter enumerants - #3151
Open
dchaudhari7177 wants to merge 1 commit into
Open
dchaudhari7177 wants to merge 1 commit into
dchaudhari7177 wants to merge 1 commit into
Conversation
A module-scope or local declaration named like an address space, access mode or texel format shadows that enumerant, so a later template such as var<storage, read_write> would refer to the declaration. Reserve them in the namespace so such names get a suffix. They stay valid as struct member names, which shadow nothing. Closes software-mansion#3065
Contributor
There was a problem hiding this comment.
Important
The new set misses the immediate address space — one TypeGPU itself emits as var<immediate> — so the shadowing bug this PR fixes is still reachable there.
Reviewed changes
templateEnumerants(packages/typegpu/src/nameUtils.ts) — adds the five core address spaces, three access modes, and 40 texel formats as reserved enumerants, deliberately kept out ofbannedTokensso struct-member names stay legal.- Namespace wiring (
packages/typegpu/src/core/resolve/namespace.ts) — seedstakenGlobalIdentifierswith the new set;makeUniqueIdentifierconsults it for both global and block scopes. - Tests (
packages/typegpu/tests/namespace.test.ts) — a snapshot that fails onmain(mutableread_write, private varrgba8unorm, localwrite), plus a guard that struct members namedread/writeare untouched.
ℹ️ Nitpicks
- The texel-format half of
templateEnumerantsre-lists the 40 members ofStorageTextureFormats(packages/typegpu/src/core/texture/textureFormats.ts:14); a format added there in future won't be picked up here, silently reintroducing this bug for that format.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
Comment on lines
+190
to
+194
| 'function', | ||
| 'private', | ||
| 'workgroup', | ||
| 'uniform', | ||
| 'storage', |
Contributor
There was a problem hiding this comment.
The address-space block omits immediate, which TypeGPU itself emits as var<immediate> (packages/typegpu/src/tgsl/wgslGenerator.ts:221, packages/typegpu/src/core/immediate/immediateVar.ts:197). immediate is not a WGSL keyword, reserved word, or builtin, so a resource or local named immediate still shadows the enumerant — the exact bug this PR fixes for the other five address spaces.
Suggested change
| 'function', | |
| 'private', | |
| 'workgroup', | |
| 'uniform', | |
| 'storage', | |
| 'function', | |
| 'private', | |
| 'workgroup', | |
| 'uniform', | |
| 'storage', | |
| 'immediate', |
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 #3065.
Cause
WGSL's address spaces (
function,private,workgroup,uniform,storage), access modes (read,write,read_write) and texel formats (rgba8unorm, …) are predeclared enumerants, and a declaration can shadow them. Template parameters refer to them by name:var<storage, read_write>,texture_storage_2d<rgba8unorm, write>. Before this change, a buffer namedread_writecame out asand any later
read_writein a template resolves to that variable. A locallet write = ...does the same inside its function.uniformandstoragewere already inbannedTokens. The other enumerants weren't reserved anywhere.Fix
nameUtils.tsgets atemplateEnumerantsset: the five address spaces, the three access modes, and every texel format inStorageTextureFormats(the 17 core WGSL formats plus thetexture-formats-tier1ones).takenGlobalIdentifiers, next tobannedTokensandbuiltins.makeUniqueIdentifierchecks the set for both global and block scope, so module-scope and local names get a_1suffix like the other reserved words.I kept them out of
bannedTokenson purpose.bannedTokensis also whatvalidatePropchecks for struct members and what function argument names are checked against, so adding them there would maked.struct({ read: d.u32 })throw. A struct member never shadows a module-scope name, soread/writethere are valid WGSL and keep working. If you'd rather have them inbannedTokenslikeuniformandstorage, that's a one-line move.Tests
tests/namespace.test.ts:read_write, a private var namedrgba8unormand a localwriteresolve toread_write_1,rgba8unorm_1andwrite_1. Onmainthis test fails: the names come out unsuffixed.d.struct({ read: d.u32, write: d.u32 })still resolves with its member names.packages/typegpu:vitest rungives 192 files, 2733 passed. oxfmt is clean.