Skip to content
Merged
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: 2 additions & 2 deletions packages/typegpu/src/tgsl/accessIndex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stitch } from '../core/resolve/stitch.ts';
import { isDisarray, MatrixColumnsAccess, UnknownData } from '../data/dataTypes.ts';
import { isDisarray, MatrixColumnsAccess } from '../data/dataTypes.ts';
import { derefSnippet } from '../data/ref.ts';
import { isEphemeralSnippet, type Origin, snip, type Snippet } from '../data/snippet.ts';
import { vec2f, vec3f, vec4f } from '../data/vector.ts';
Expand Down Expand Up @@ -101,7 +101,7 @@ export function accessIndex(target: Snippet, indexArg: Snippet | number): Snippe
);
}

if ((isKnownAtComptime(target) && isKnownAtComptime(index)) || target.dataType === UnknownData) {
if (isKnownAtComptime(target) && isKnownAtComptime(index)) {
// No idea what the type is, so we act on the snippet's value and try to guess
return coerceToSnippet(
// oxlint-disable-next-line typescript/no-explicit-any -- we're inspecting the value, and it could be any value
Expand Down
2 changes: 1 addition & 1 deletion packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ ${this.ctx.pre}}`;
const propertyStr = this.ctx.resolve(property.value, property.dataType).value;

throw new Error(
`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.`,
`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.`,
);
}

Expand Down
17 changes: 16 additions & 1 deletion packages/typegpu/tests/tgsl/wgslGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,22 @@ describe('wgslGenerator', () => {
expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:testFn: Value undefined (as json: undefined) is not resolvable to type u32]
- 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.]
`);
});

it('throws an error when accessing an object with a runtime known index', () => {
const Boid = d.struct({ 0: d.u32 });

const testFn = tgpu.fn([Boid])((b) => {
const i = 0;
const v = b[i];
});

expect(() => tgpu.resolve([testFn])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- 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.]
`);
});

Expand Down
Loading