Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/typegpu/src/resolutionCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,6 @@ class ItemStateStackImpl implements ItemStateStack {
}

defineBlockVariable(id: string, snippet: Snippet): void {
if (snippet.dataType.type === 'unknown') {
throw Error(`Tried to define variable '${id}' of unknown type`);
}

for (let i = this._stack.length - 1; i >= 0; --i) {
const layer = this._stack[i];

Expand Down
24 changes: 23 additions & 1 deletion packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ ${this.ctx.pre}}`;
ptrType,
'function',
);

if (snippet.dataType.type === 'unknown') {
throw Error(`Tried to define variable '${id}' of unknown type.`);
}

this.ctx.defineVariable(id, snippet);
return varName;
}
Expand Down Expand Up @@ -255,6 +260,7 @@ ${this.ctx.pre}}`;
dataType,
/* origin */ varOrigin,
);

this.ctx.defineVariable(id, snippet);
return snippet;
}
Expand Down Expand Up @@ -1004,7 +1010,23 @@ ${this.ctx.pre}else ${alternate}`;
concretize(dataType),
eq.origin,
);
return stitch`${this.ctx.pre}${varType} ${snippet.value as string} = ${

const id = snippet.value;
if (snippet.dataType.type === 'unknown') {
const schema = Array.isArray(eq.value)
? `d.arrayOf(...)(${id})`
: `YourStructSchema(${id})`;

throw Error(
`Tried to define variable '${id}' of unknown type.
-----
- Try to wrap right-hand side with a schema \`${schema}\`.
-----`,
);
}

return stitch`${this.ctx.pre}${varType} ${snippet
.value as string} = ${
tryConvertSnippet(this.ctx, eq, dataType, false)
};`;
}
Expand Down
10 changes: 8 additions & 2 deletions packages/typegpu/tests/tgsl/typeInference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,10 @@ describe('wgsl generator js type inference', () => {
expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:myFn: Tried to define variable 'unrelated' of unknown type]
- fn:myFn: Tried to define variable 'unrelated' of unknown type.
-----
- Try to wrap right-hand side with a schema \`YourStructSchema(unrelated)\`.
-----]
`);
});

Expand All @@ -589,7 +592,10 @@ describe('wgsl generator js type inference', () => {
expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:myFn: Tried to define variable 'myArr' of unknown type]
- fn:myFn: Tried to define variable 'myArr' of unknown type.
-----
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(myArr)\`.
-----]
`);
});

Expand Down