Skip to content

Commit d7fb077

Browse files
impr: Better error on JS array runtime access (#2407)
1 parent c96d9d4 commit d7fb077

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

packages/typegpu/src/tgsl/accessIndex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { stitch } from '../core/resolve/stitch.ts';
2-
import { isDisarray, MatrixColumnsAccess, UnknownData } from '../data/dataTypes.ts';
2+
import { isDisarray, MatrixColumnsAccess } from '../data/dataTypes.ts';
33
import { derefSnippet } from '../data/ref.ts';
44
import { isEphemeralSnippet, type Origin, snip, type Snippet } from '../data/snippet.ts';
55
import { vec2f, vec3f, vec4f } from '../data/vector.ts';
@@ -101,7 +101,7 @@ export function accessIndex(target: Snippet, indexArg: Snippet | number): Snippe
101101
);
102102
}
103103

104-
if ((isKnownAtComptime(target) && isKnownAtComptime(index)) || target.dataType === UnknownData) {
104+
if (isKnownAtComptime(target) && isKnownAtComptime(index)) {
105105
// No idea what the type is, so we act on the snippet's value and try to guess
106106
return coerceToSnippet(
107107
// oxlint-disable-next-line typescript/no-explicit-any -- we're inspecting the value, and it could be any value

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ ${this.ctx.pre}}`;
556556
const propertyStr = this.ctx.resolve(property.value, property.dataType).value;
557557

558558
throw new Error(
559-
`Unable to index value ${targetStr} 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.`,
559+
`Index access '${targetStr}[${propertyStr}]' is invalid. 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.`,
560560
);
561561
}
562562

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,22 @@ describe('wgslGenerator', () => {
17571757
expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
17581758
[Error: Resolution of the following tree failed:
17591759
- <root>
1760-
- fn:testFn: Value undefined (as json: undefined) is not resolvable to type u32]
1760+
- fn:testFn: Index access 'array(9, 8, 7, 6)[i]' is invalid. 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.]
1761+
`);
1762+
});
1763+
1764+
it('throws an error when accessing an object with a runtime known index', () => {
1765+
const Boid = d.struct({ 0: d.u32 });
1766+
1767+
const testFn = tgpu.fn([Boid])((b) => {
1768+
const i = 0;
1769+
const v = b[i];
1770+
});
1771+
1772+
expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
1773+
[Error: Resolution of the following tree failed:
1774+
- <root>
1775+
- fn:testFn: Index access 'b[i]' is invalid. 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.]
17611776
`);
17621777
});
17631778

0 commit comments

Comments
 (0)