TS8006
On this page

TS8006

Front-end code LOOP_BOUND

A counted for loop whose exit the compiler cannot prove: the counter is not compared to a bound, or the body writes the bound.

The registry has no documentation comment for this constant, so the line above is written by this site from the messages the compiler raises it with.

The registry text and every compiler message on these pages are the compiler's own English.

Example

example.shade.ts
"use typeshade"
@fragment
export function main(@location(0) uv: vec2): vec4 {
let g = 0.
let n: i32 = 8
for (let i = 0; i < n; i++) {
n -= 1
g += 0.1
}
return vec4(g, g, g, 1.)
}

Compiled at build time against the pinned compiler, this program gets:

TS8006 error, line 7: for bound reads "n", which the loop body writes, so it does not bound the loop. Read it into a const before the loop, or write the loop as a while.

How to fix it

The same program, corrected. It compiles with no diagnostic.

example.shade.ts
"use typeshade"
@fragment
export function main(@location(0) uv: vec2): vec4 {
let g = 0.
let n: i32 = 8
const count = n
for (let i = 0; i < count; i++) {
n -= 1
g += 0.1
}
return vec4(g, g, g, 1.)
}

The rules this enforces

Rule 7.5 Test
A for loop must be counted: an i32 or u32 induction variable, a constant step, and an exit that compares the induction variable to a bound; the start and the bound may be runtime values, the loop body must not write the bound, and the step must move the variable toward the bound.
Rule 7.9 Test
An expression must be evaluated left to right, as TypeScript and WGSL both evaluate it, and a call that writes (its object, a module variable, a storage binding, an atomic location) inside a larger expression must take effect in that order on every target.

See also

Source

Where the compiler raises this code at commit 26de7be8, one line per file:

Edit this page Report a problem