diff --git a/packages/typegpu/src/tgsl/wgslGenerator.ts b/packages/typegpu/src/tgsl/wgslGenerator.ts index 900a51edc..2c95b96d9 100644 --- a/packages/typegpu/src/tgsl/wgslGenerator.ts +++ b/packages/typegpu/src/tgsl/wgslGenerator.ts @@ -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.`, ); } diff --git a/packages/typegpu/tests/constant.test.ts b/packages/typegpu/tests/constant.test.ts index 3fbfd9eaa..f5dffb063 100644 --- a/packages/typegpu/tests/constant.test.ts +++ b/packages/typegpu/tests/constant.test.ts @@ -83,7 +83,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - 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 @@ -158,7 +158,7 @@ describe('tgpu.const', () => { [Error: Resolution of the following tree failed: - - 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 diff --git a/packages/typegpu/tests/tgslFn.test.ts b/packages/typegpu/tests/tgslFn.test.ts index 4787b0686..bfca257cc 100644 --- a/packages/typegpu/tests/tgslFn.test.ts +++ b/packages/typegpu/tests/tgslFn.test.ts @@ -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: + - + - 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.] + `); + }); });