Skip to content

Commit 77ffb35

Browse files
committed
more descriptive error
1 parent a61d124 commit 77ffb35

File tree

4 files changed

+30
-86
lines changed

4 files changed

+30
-86
lines changed

packages/typegpu/src/resolutionCtx.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,6 @@ class ItemStateStackImpl implements ItemStateStack {
246246
}
247247

248248
defineBlockVariable(id: string, snippet: Snippet): void {
249-
if (snippet.dataType.type === 'unknown') {
250-
throw Error(`Tried to define variable '${id}' of unknown type`);
251-
}
252-
253249
for (let i = this._stack.length - 1; i >= 0; --i) {
254250
const layer = this._stack[i];
255251

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ ${this.ctx.pre}}`;
218218
ptrType,
219219
'function',
220220
);
221+
222+
if (snippet.dataType.type === 'unknown') {
223+
throw Error(`Tried to define variable '${id}' of unknown type.`);
224+
}
225+
221226
this.ctx.defineVariable(id, snippet);
222227
return varName;
223228
}
@@ -257,6 +262,7 @@ ${this.ctx.pre}}`;
257262
dataType,
258263
/* origin */ varOrigin,
259264
);
265+
260266
this.ctx.defineVariable(id, snippet);
261267
return snippet;
262268
}
@@ -1018,31 +1024,27 @@ ${this.ctx.pre}else ${alternate}`;
10181024
}
10191025
}
10201026

1021-
if (eq.dataType.type === 'unknown') {
1022-
if (Array.isArray(eq.value)) {
1023-
// for arrays we can give more specific suggestion
1024-
console.warn(
1025-
`You are likely trying to define variable \`${rawId}\` with a value \`[${eq.value}]\` of an unknown type.
1026-
-----
1027-
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(...)\`.
1028-
-----`,
1029-
);
1030-
} else {
1031-
console.warn(
1032-
`You are likely trying to define variable \`${rawId}\` with a value of an unknown type.
1033-
-----
1034-
- Try to wrap right-hand side with a schema \`YourStructSchema(...)\`.
1035-
-----`,
1036-
);
1037-
}
1038-
}
1039-
10401027
const snippet = this.blockVariable(
10411028
varType,
10421029
rawId,
10431030
concretize(dataType),
10441031
eq.origin,
10451032
);
1033+
1034+
const id = snippet.value;
1035+
if (snippet.dataType.type === 'unknown') {
1036+
const schema = Array.isArray(eq.value)
1037+
? `d.arrayOf(...)(${id})`
1038+
: `YourStructSchema(${id})`;
1039+
1040+
throw Error(
1041+
`Tried to define variable '${id}' of unknown type.
1042+
-----
1043+
- Try to wrap right-hand side with a schema \`${schema}\`.
1044+
-----`,
1045+
);
1046+
}
1047+
10461048
return stitch`${this.ctx.pre}${varType} ${snippet
10471049
.value as string} = ${tryConvertSnippet(eq, dataType, false)};`;
10481050
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ describe('wgsl generator js type inference', () => {
578578
expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(`
579579
[Error: Resolution of the following tree failed:
580580
- <root>
581-
- fn:myFn: Tried to define variable 'unrelated' of unknown type]
581+
- fn:myFn: Tried to define variable 'unrelated' of unknown type.
582+
-----
583+
- Try to wrap right-hand side with a schema \`YourStructSchema(unrelated)\`.
584+
-----]
582585
`);
583586
});
584587

@@ -591,7 +594,10 @@ describe('wgsl generator js type inference', () => {
591594
expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(`
592595
[Error: Resolution of the following tree failed:
593596
- <root>
594-
- fn:myFn: Tried to define variable 'myArr' of unknown type]
597+
- fn:myFn: Tried to define variable 'myArr' of unknown type.
598+
-----
599+
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(myArr)\`.
600+
-----]
595601
`);
596602
});
597603

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

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as tinyest from 'tinyest';
2-
import { beforeEach, describe, expect, vi } from 'vitest';
2+
import { beforeEach, describe, expect } from 'vitest';
33
import { namespace } from '../../src/core/resolve/namespace.ts';
44
import * as d from '../../src/data/index.ts';
55
import { abstractFloat, abstractInt } from '../../src/data/numeric.ts';
@@ -1169,66 +1169,6 @@ describe('wgslGenerator', () => {
11691169
`);
11701170
});
11711171

1172-
it('console.warns the suggestion when snippet value is array', () => {
1173-
using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(
1174-
() => {},
1175-
);
1176-
1177-
const getArr = tgpu['~unstable'].comptime(() =>
1178-
d.arrayOf(d.f32, 2)([1, 2])
1179-
);
1180-
const f = () => {
1181-
'use gpu';
1182-
const arr = getArr();
1183-
};
1184-
1185-
expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
1186-
[Error: Resolution of the following tree failed:
1187-
- <root>
1188-
- fn*:f
1189-
- fn*:f(): Tried to define variable 'arr' of unknown type]
1190-
`);
1191-
1192-
expect(consoleWarnSpy).toHaveBeenCalledWith(
1193-
`You are likely trying to define variable \`arr\` with a value \`[1,2]\` of an unknown type.
1194-
-----
1195-
- Try to wrap right-hand side with a schema \`d.arrayOf(...)(...)\`.
1196-
-----`,
1197-
);
1198-
});
1199-
1200-
it('console.warns the suggestion when snippet value is object', () => {
1201-
using consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(
1202-
() => {},
1203-
);
1204-
1205-
const MyStruct = d.struct({
1206-
x: d.vec2f,
1207-
});
1208-
1209-
const getMyStruct = tgpu['~unstable'].comptime(() =>
1210-
MyStruct({ x: d.vec2f(1, 2) })
1211-
);
1212-
const f = () => {
1213-
'use gpu';
1214-
const s = getMyStruct();
1215-
};
1216-
1217-
expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
1218-
[Error: Resolution of the following tree failed:
1219-
- <root>
1220-
- fn*:f
1221-
- fn*:f(): Tried to define variable 's' of unknown type]
1222-
`);
1223-
1224-
expect(consoleWarnSpy).toHaveBeenCalledWith(
1225-
`You are likely trying to define variable \`s\` with a value of an unknown type.
1226-
-----
1227-
- Try to wrap right-hand side with a schema \`YourStructSchema(...)\`.
1228-
-----`,
1229-
);
1230-
});
1231-
12321172
it('generates correct indentation for nested blocks', () => {
12331173
const main = tgpu.fn([], d.i32)(() => {
12341174
let res = 0;

0 commit comments

Comments
 (0)