Skip to content

Commit aa5757c

Browse files
authored
fix: Always resolve with strict types (#1880)
1 parent 04aa06a commit aa5757c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+972
-974
lines changed

apps/typegpu-docs/src/examples/rendering/jelly-slider/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ const renderBackground = (
618618

619619
let highlights = d.f32();
620620

621-
const highlightWidth = 1;
621+
const highlightWidth = d.f32(1);
622622
const highlightHeight = 0.2;
623623
let offsetX = d.f32();
624624
let offsetZ = d.f32(0.05);

packages/typegpu/src/core/resolve/stitch.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,12 @@ type ValueOrArray<T> = T | T[];
1111
export function stitch(
1212
strings: TemplateStringsArray,
1313
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
14-
) {
15-
return internalStitch(strings, snippets, false);
16-
}
17-
18-
/**
19-
* "The reverse of snipping"
20-
* Injects resolved snippets into a template string, ensuring
21-
* the generated code represents it's type exactly.
22-
*/
23-
export function stitchWithExactTypes(
24-
strings: TemplateStringsArray,
25-
...snippets: ValueOrArray<Snippet | string | number | undefined>[]
26-
) {
27-
return internalStitch(strings, snippets, true);
28-
}
29-
30-
function internalStitch(
31-
strings: TemplateStringsArray,
32-
snippets: ValueOrArray<Snippet | string | number | undefined>[],
33-
exact: boolean,
3414
) {
3515
const ctx = getResolutionCtx() as ResolutionCtx;
3616

3717
function resolveSnippet(maybeSnippet: Snippet | string | number) {
3818
return isSnippet(maybeSnippet)
39-
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType, exact).value
19+
? ctx.resolve(maybeSnippet.value, maybeSnippet.dataType).value
4020
: maybeSnippet;
4121
}
4222

packages/typegpu/src/data/vector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ function makeVecSchema<TValue, S extends number | boolean>(
317317
returnType: schema as AnyData,
318318
}),
319319
normalImpl: cpuConstruct,
320+
ignoreImplicitCastWarning: true,
320321
codegenImpl: (...args) => stitch`${type}(${args})`,
321322
});
322323

packages/typegpu/src/resolutionCtx.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,6 @@ export class ResolutionCtxImpl implements ResolutionCtx {
702702
resolve(
703703
item: unknown,
704704
schema?: AnyData | UnknownData | undefined,
705-
exact = false,
706705
): ResolvedSnippet {
707706
if (isTgpuFn(item) || hasTinyestMetadata(item)) {
708707
if (
@@ -719,7 +718,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
719718
if (isProviding(item)) {
720719
return this.withSlots(
721720
item[$providing].pairs,
722-
() => this.resolve(item[$providing].inner, schema, exact),
721+
() => this.resolve(item[$providing].inner, schema),
723722
);
724723
}
725724

@@ -743,41 +742,34 @@ export class ResolutionCtxImpl implements ResolutionCtx {
743742

744743
// This is a value that comes from the outside, maybe we can coerce it
745744
if (typeof item === 'number') {
746-
const reinterpretedType = exact
747-
? schema
748-
: numericLiteralToSnippet(item).dataType;
749745
const realSchema = schema ?? numericLiteralToSnippet(item).dataType;
750-
invariant(
751-
reinterpretedType,
752-
'Schema has to be defined for resolving numbers',
753-
);
754746
invariant(
755747
realSchema.type !== 'unknown',
756748
'Schema has to be known for resolving numbers',
757749
);
758750

759-
if (reinterpretedType.type === 'abstractInt') {
751+
if (realSchema.type === 'abstractInt') {
760752
return snip(`${item}`, realSchema);
761753
}
762-
if (reinterpretedType.type === 'u32') {
754+
if (realSchema.type === 'u32') {
763755
return snip(`${item}u`, realSchema);
764756
}
765-
if (reinterpretedType.type === 'i32') {
757+
if (realSchema.type === 'i32') {
766758
return snip(`${item}i`, realSchema);
767759
}
768760

769761
const exp = item.toExponential();
770762
const decimal =
771-
reinterpretedType.type === 'abstractFloat' && Number.isInteger(item)
763+
realSchema.type === 'abstractFloat' && Number.isInteger(item)
772764
? `${item}.`
773765
: `${item}`;
774766

775767
// Just picking the shorter one
776768
const base = exp.length < decimal.length ? exp : decimal;
777-
if (reinterpretedType.type === 'f32') {
769+
if (realSchema.type === 'f32') {
778770
return snip(`${base}f`, realSchema);
779771
}
780-
if (reinterpretedType.type === 'f16') {
772+
if (realSchema.type === 'f16') {
781773
return snip(`${base}h`, realSchema);
782774
}
783775
return snip(base, realSchema);

packages/typegpu/src/std/boolean.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type v4b,
2020
} from '../data/wgslTypes.ts';
2121
import { $internal } from '../shared/symbols.ts';
22+
import { unify } from '../tgsl/conversion.ts';
2223
import { sub } from './operators.ts';
2324

2425
function correspondingBooleanVectorSchema(dataType: AnyData) {
@@ -333,7 +334,10 @@ function cpuSelect<T extends number | boolean | AnyVecInstance>(
333334
*/
334335
export const select = dualImpl({
335336
name: 'select',
336-
signature: (...argTypes) => ({ argTypes, returnType: argTypes[0] }),
337+
signature: (f, t, cond) => {
338+
const [uf, ut] = unify([f, t]) ?? [f, t] as const;
339+
return ({ argTypes: [uf, ut, cond], returnType: uf });
340+
},
337341
normalImpl: cpuSelect,
338342
codegenImpl: (f, t, cond) => stitch`select(${f}, ${t}, ${cond})`,
339343
});

packages/typegpu/src/std/operators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dualImpl } from '../core/function/dualImpl.ts';
2-
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
33
import { abstractFloat, f16, f32 } from '../data/numeric.ts';
44
import { vecTypeToConstructor } from '../data/vector.ts';
55
import { VectorOps } from '../data/vectorOps.ts';
@@ -192,7 +192,7 @@ export const div = dualImpl({
192192
});
193193
},
194194
normalImpl: cpuDiv,
195-
codegenImpl: (lhs, rhs) => stitchWithExactTypes`(${lhs} / ${rhs})`,
195+
codegenImpl: (lhs, rhs) => stitch`(${lhs} / ${rhs})`,
196196
ignoreImplicitCastWarning: true,
197197
});
198198

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as tinyest from 'tinyest';
2-
import { stitch, stitchWithExactTypes } from '../core/resolve/stitch.ts';
2+
import { stitch } from '../core/resolve/stitch.ts';
33
import { arrayOf } from '../data/array.ts';
44
import {
55
type AnyData,
@@ -254,7 +254,7 @@ ${this.ctx.pre}}`;
254254
// Post-Update Expression
255255
const [_, op, arg] = expression;
256256
const argExpr = this.expression(arg);
257-
const argStr = this.ctx.resolve(argExpr.value).value;
257+
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;
258258

259259
return snip(`${argStr}${op}`, argExpr.dataType);
260260
}
@@ -263,7 +263,7 @@ ${this.ctx.pre}}`;
263263
// Unary Expression
264264
const [_, op, arg] = expression;
265265
const argExpr = this.expression(arg);
266-
const argStr = this.ctx.resolve(argExpr.value).value;
266+
const argStr = this.ctx.resolve(argExpr.value, argExpr.dataType).value;
267267

268268
const type = operatorToType(argExpr.dataType, op);
269269
return snip(`${op}${argStr}`, type);
@@ -735,7 +735,7 @@ ${this.ctx.pre}else ${alternate}`;
735735
rawId,
736736
concretize(eq.dataType as wgsl.AnyWgslData),
737737
);
738-
return stitchWithExactTypes`${this.ctx.pre}var ${snippet
738+
return stitch`${this.ctx.pre}var ${snippet
739739
.value as string} = ${eq};`;
740740
}
741741

packages/typegpu/tests/accessor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('tgpu.accessor', () => {
5656
.with(multiplierAccess, 2);
5757

5858
expect(asWgsl(getColor)).toMatchInlineSnapshot(
59-
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0) * 2; }"`,
59+
`"fn getColor() -> vec3f{ return vec3f(1, 0, 0) * 2f; }"`,
6060
);
6161
});
6262

packages/typegpu/tests/array.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe('array', () => {
197197

198198
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
199199
"fn testFn() {
200-
var myArray = array<u32, 1>(10);
200+
var myArray = array<u32, 1>(10u);
201201
var myClone = myArray;
202202
return;
203203
}"
@@ -215,7 +215,7 @@ describe('array', () => {
215215

216216
expect(asWgsl(testFn)).toMatchInlineSnapshot(`
217217
"fn testFn() {
218-
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10));
218+
var myArrays = array<array<i32, 1>, 1>(array<i32, 1>(10i));
219219
var myClone = myArrays[0];
220220
return;
221221
}"
@@ -278,7 +278,7 @@ describe('array', () => {
278278

279279
expect(asWgsl(foo)).toMatchInlineSnapshot(`
280280
"fn foo() {
281-
var result = array<f32, 4>(1, 2, 3, 4);
281+
var result = array<f32, 4>(1f, 2f, 3f, 4f);
282282
}"
283283
`);
284284
});
@@ -290,7 +290,7 @@ describe('array', () => {
290290

291291
expect(asWgsl(foo)).toMatchInlineSnapshot(`
292292
"fn foo() {
293-
var result = array<f32, 4>(4, 3, 2, 1);
293+
var result = array<f32, 4>(4f, 3f, 2f, 1f);
294294
}"
295295
`);
296296
});
@@ -304,7 +304,7 @@ describe('array', () => {
304304

305305
expect(asWgsl(foo)).toMatchInlineSnapshot(`
306306
"fn foo() {
307-
var result = array<f32, 4>(4, 3, 2, 1);
307+
var result = array<f32, 4>(4f, 3f, 2f, 1f);
308308
}"
309309
`);
310310
});
@@ -326,7 +326,7 @@ describe('array', () => {
326326

327327
expect(asWgsl(foo)).toMatchInlineSnapshot(`
328328
"fn foo() {
329-
var result = array<f32, 8>(0, 1, 2, 3, 4, 5, 6, 7);
329+
var result = array<f32, 8>(0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f);
330330
}"
331331
`);
332332
});
@@ -356,7 +356,7 @@ describe('array.length', () => {
356356
var acc = 1f;
357357
for (var i = 0u; (i < arrayLength(&values)); i++) {
358358
values[i] = acc;
359-
acc *= 2;
359+
acc *= 2f;
360360
}
361361
}"
362362
`);
@@ -385,7 +385,7 @@ describe('array.length', () => {
385385
var acc = 1f;
386386
for (var i = 0; (i < 128); i++) {
387387
values[i] = acc;
388-
acc *= 2;
388+
acc *= 2f;
389389
}
390390
}"
391391
`);

packages/typegpu/tests/computePipeline.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ describe('TgpuComputePipeline', () => {
551551
a[0] = f16(_arg_0.gid.x);
552552
}
553553
{
554-
a[1] = 1;
554+
a[1] = 1h;
555555
}
556556
557557
}"

0 commit comments

Comments
 (0)