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
2 changes: 1 addition & 1 deletion packages/typegpu/src/tgsl/wgslGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ ${this.ctx.pre}}`;
convLhs.origin === 'runtime-tgpu-const-ref'
) {
throw new WgslTypeError(
`'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant.`,
`'${lhsStr} = ${rhsStr}' is invalid, because ${lhsStr} is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.`,
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/typegpu/tests/constant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('tgpu.const', () => {
[Error: Resolution of the following tree failed:
- <root>
- fn*:fn
- fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant.]
- fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.]
`);

// Since we freeze the object, we cannot mutate when running the function in JS either
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('tgpu.const', () => {
[Error: Resolution of the following tree failed:
- <root>
- fn*:fn
- fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant.]
- fn*:fn(): 'boid.pos = vec3f()' is invalid, because boid.pos is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.]
`);

// Since we freeze the object, we cannot mutate when running the function in JS either
Expand Down
13 changes: 13 additions & 0 deletions packages/typegpu/tests/tgslFn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,4 +1007,17 @@ describe('tgsl fn when using plugin', () => {
}"
`);
});

it('throws a readable error when assigning to a value defined outside of tgsl', () => {
let a = 0;
const f = tgpu.fn([])(() => {
a = 2;
});

expect(() => tgpu.resolve([f])).toThrowErrorMatchingInlineSnapshot(`
[Error: Resolution of the following tree failed:
- <root>
- fn:f: '0 = 2' is invalid, because 0 is a constant. This error may also occur when assigning to a value defined outside of a TypeGPU function's scope.]
`);
});
});