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
19 changes: 19 additions & 0 deletions packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,25 @@ ${this.ctx.pre}else ${alternate}`;
}
}

if (eq.dataType.type === 'unknown') {
if (Array.isArray(eq.value)) {
// for arrays we can give more specific suggestion
console.warn(
`You are likely trying to define variable \`${rawId}\` with a value \`[${eq.value}]\` of an unknown type.
-----
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(...)\`.
-----`,
);
} else {
console.warn(
`You are likely trying to define variable \`${rawId}\` with a value of an unknown type.
-----
- Try to wrap right-hand side with a schema \`YourStructSchema(...)\`.
-----`,
);
}
}

const snippet = this.blockVariable(
varType,
rawId,
Expand Down
62 changes: 61 additions & 1 deletion packages/typegpu/tests/tgsl/wgslGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as tinyest from 'tinyest';
import { beforeEach, describe, expect } from 'vitest';
import { beforeEach, describe, expect, vi } from 'vitest';
import { namespace } from '../../src/core/resolve/namespace.ts';
import * as d from '../../src/data/index.ts';
import { abstractFloat, abstractInt } from '../../src/data/numeric.ts';
Expand Down Expand Up @@ -1168,4 +1168,64 @@ describe('wgslGenerator', () => {
- fn:testFn: Constants cannot be defined within TypeGPU function scope. To address this, move the constant definition outside the function scope.]
`);
});

it('console.warns the suggestion when snippet value is array', () => {
using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(
() => {},
);

const getArr = tgpu['~unstable'].comptime(() =>
d.arrayOf(d.f32, 2)([1, 2])
);
const f = () => {
'use gpu';
const arr = getArr();
};

expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn*:f
- fn*:f(): Tried to define variable 'arr' of unknown type]
`);

expect(consoleWarnSpy).toHaveBeenCalledWith(
`You are likely trying to define variable \`arr\` with a value \`[1,2]\` of an unknown type.
-----
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(...)\`.
-----`,
);
});

it('console.warns the suggestion when snippet value is object', () => {
using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(
() => {},
);

const MyStruct = d.struct({
x: d.vec2f,
});

const getMyStruct = tgpu['~unstable'].comptime(() =>
MyStruct({ x: d.vec2f(1, 2) })
);
const f = () => {
'use gpu';
const s = getMyStruct();
};

expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn*:f
- fn*:f(): Tried to define variable 's' of unknown type]
`);

expect(consoleWarnSpy).toHaveBeenCalledWith(
`You are likely trying to define variable \`s\` with a value of an unknown type.
-----
- Try to wrap right-hand side with a schema \`YourStructSchema(...)\`.
-----`,
);
});
});