diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index 11d6b703d6..fd45ee17ef 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -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, diff --git a/packages/typegpu/tests/tgsl/wgslGenerator.test.ts b/packages/typegpu/tests/tgsl/wgslGenerator.test.ts index 74f04f5a9b..9dadec3d74 100644 --- a/packages/typegpu/tests/tgsl/wgslGenerator.test.ts +++ b/packages/typegpu/tests/tgsl/wgslGenerator.test.ts @@ -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'; @@ -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: + - + - 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: + - + - 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(...)\`. +-----`, + ); + }); });