Skip to content

fix(tgpu-gen): Sort structs used in nested arrays before the structs that hold them - #3317

Open
RaphaelFakhri wants to merge 1 commit into
software-mansion:mainfrom
RaphaelFakhri:fix/tgpu-gen-nested-array-struct-order
Open

RaphaelFakhri wants to merge 1 commit into
software-mansion:mainfrom
RaphaelFakhri:fix/tgpu-gen-nested-array-struct-order

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

tgpu-gen sorts structs topologically so that each struct is generated after the structs it uses. topologicalSort only looks at the member type and, for arrays, the element type. When a struct is used inside an array of arrays, such as array<array<Cell, 4>, 4>, and is declared later in the WGSL, it is not recorded as a dependency, and generation fails with Unknown data type: Cell.

Changes

  • topologicalSort unwraps nested arrays down to their innermost element type before checking for a struct dependency. This also covers the single-level array case that the old code handled separately.
  • Adds a structs generator test for a struct used in a nested array and declared after its parent.

Testing

The packages/tgpu-gen tests pass: 33 tests. Without the gen.mjs change, the new test fails with Error: Unknown data type: Cell.

Fixes #3316

Copilot AI balanced review requested due to automatic review settings November 3, 2026 16:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Nested-array dependency unwrapping — topologicalSort now walks every ArrayInfo.format layer down to the innermost element type before checking allStructNames, so a struct used in array<array<Cell, 4>, 4> (or a runtime-sized array<array<Cell, 2>>) is recorded as a dependency of its holder and emitted first.
  • Regression test — structs.test.ts adds a Grid/Cell case that asserts Cell is generated before Grid and that the member renders as d.arrayOf(d.arrayOf(Cell, 4), 4).

The change is a strict superset of the previous dependency detection: it adds only real array-element edges, so ordering changes only for inputs that previously threw Unknown data type. I ran the tgpu-gen suite (33 tests pass) and also generated runtime-sized nested arrays, three-level nesting, and an aliased nested-array member locally — all order correctly.

( ◦°^°◦) 🏴‍☠️ Arr, I read the whole diff, ran the tgpu-gen tests, and found no leaks in this here nested-array fix.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tgpu-gen: struct used inside a nested array throws "Unknown data type" when declared later

2 participants