Skip to content

Commit 731d6f5

Browse files
impr: Error messages for invalid array and tgpu.const usage (#1856)
1 parent f36db5e commit 731d6f5

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
numericLiteralToSnippet,
3636
} from './generationHelpers.ts';
3737
import type { ShaderGenerator } from './shaderGenerator.ts';
38+
import { constant } from '../core/constant/tgpuConstant.ts';
3839

3940
const { NodeTypeCatalog: NODE } = tinyest;
4041

@@ -370,7 +371,7 @@ ${this.ctx.pre}}`;
370371
}
371372

372373
throw new Error(
373-
`Cannot index value ${targetStr} of unknown type with index ${propertyStr}`,
374+
`Unable to index a value of unknown type with index ${propertyStr}. If the value is an array, to address this, consider one of the following approaches: (1) declare the array using 'tgpu.const', (2) store the array in a buffer, or (3) define the array within the GPU function scope.`,
374375
);
375376
}
376377

@@ -441,6 +442,12 @@ ${this.ctx.pre}}`;
441442
);
442443
}
443444

445+
if (callee.value === constant) {
446+
throw new Error(
447+
'Constants cannot be defined within TypeGPU function scope. To address this, move the constant definition outside the function scope.',
448+
);
449+
}
450+
444451
if (callee.value instanceof InfixDispatch) {
445452
// Infix operator dispatch.
446453
if (!argNodes[0]) {

packages/typegpu/tests/tgsl/wgslGenerator.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,4 +1147,31 @@ describe('wgslGenerator', () => {
11471147
}"
11481148
`);
11491149
});
1150+
1151+
it('throws a descriptive error when accessing an external array with a runtime known index', () => {
1152+
const myArray = [9, 8, 7, 6];
1153+
1154+
const testFn = tgpu.fn([d.u32], d.u32)((i) => {
1155+
return myArray[i] as number;
1156+
});
1157+
1158+
expect(() => asWgsl(testFn)).toThrowErrorMatchingInlineSnapshot(`
1159+
[Error: Resolution of the following tree failed:
1160+
- <root>
1161+
- fn:testFn: Unable to index a value of unknown type with index i. If the value is an array, to address this, consider one of the following approaches: (1) declare the array using 'tgpu.const', (2) store the array in a buffer, or (3) define the array within the GPU function scope.]
1162+
`);
1163+
});
1164+
1165+
it('throws a descriptive error when declaring a const inside TGSL', () => {
1166+
const testFn = tgpu.fn([d.u32], d.u32)((i) => {
1167+
const myArray = tgpu.const(d.arrayOf(d.u32, 4), [9, 8, 7, 6]);
1168+
return myArray.$[i] as number;
1169+
});
1170+
1171+
expect(() => asWgsl(testFn)).toThrowErrorMatchingInlineSnapshot(`
1172+
[Error: Resolution of the following tree failed:
1173+
- <root>
1174+
- fn:testFn: Constants cannot be defined within TypeGPU function scope. To address this, move the constant definition outside the function scope.]
1175+
`);
1176+
});
11501177
});

0 commit comments

Comments
 (0)