Rule 7.5
Chapter 7, Expressions and statements 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. A runtime bound must be compared with <, <=, > or >=, and a multiplicative step with a runtime start or bound must be a whole factor of 2 or more. A header whose start and bound are constants must reach its bound within the induction type’s range. An induction variable with no annotation is an i32, or a u32 when its initializer is a non-negative integer literal and the bound it is compared with is a u32; that is the integer type the position declares for Rule 5.1. A while loop is an open loop and may have any bool condition; a while whose condition is constantly true must have a break or a return in its body. No limit is placed on a trip count. A for…of over an array (array<T, N>, or a runtime-sized storage array) is a counted loop over the array’s indices, from 0 to its length; its variable is a const or a let holding a copy of the element, and the array must be a name, or a member or index path to one. A for…in must be refused.
The rule text and the parts under it are the compiler's own English, as the design document writes them.
Rationale
both targets accept a runtime bound and an open loop (#203 measured each on Tint and on ANGLE), and a program over data loops over a count it learns at run time. The analyses of roadmap items 15, 18 and 21 need a counted shape, a step whose direction is known and a bound the body does not move; none of them needs the bound to be a literal (#203). What a counted for promises a reader is exactly that shape, so a loop that ends some other way is written as a while, which says so. for (let i = 0; i < data.length; i++) is the loop a TypeScript author writes first, and data.length is a u32: a counter that stayed i32 would make it a comparison of two integer types the author never wrote. for (const x of xs) is the other loop a TypeScript author writes over data: its trip count is the array’s length, which the body cannot change (WGSL has no array that grows), so it is a counted loop with nothing left to check. for…in enumerates an object’s keys, which a shader value does not have. Roadmap item 15 is the first to read it: the IR for carries the counted fact the front end proved (counted, absent on a while, which is a for over a counter it made), and the proof of a kernel function’s loops takes it from there (Rule 8.22).
Derives from
Loop Statement, For Statement, While Statement; surface §17; #203.
How it is verified
Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.
Where the rule says the compiler enforces it:
TS8006 LOOP_BOUND (an exit that is not a comparison with a bound; an && exit, named by the clause that is not its bound; == or != against a runtime bound; a multiplicative step with a runtime start or bound that is not a whole factor of 2 or more; a bound the body writes; a constant header that leaves its type), TS8007 LOOP_INFINITE (a step away from the bound, or one that never moves the variable; a constant header that never exits; a for with no condition; while (true), or a while whose condition folds to true from constants through !, &&, || and comparisons, a float compared only over literals and module constants that f32 holds exactly, with nothing that leaves it), TS8008 LOOP_INDUCTION (an update that is none of the counted forms, one sentence whatever its operator, including a compound assignment to a member or an element; i = i + c is i += c spelled out, its sum typed as in a body), TS8003 for a while condition that is not a bool, as for an if or a for condition, with the comparison with zero or the any/all that says a number’s or a vector of bools’ truthiness (lowerWhile); lowerFor in src/compiler/ts/lower/control.ts, which leaves an error for every for it does not lower, so a refused loop is never dropped from the module; pinned by src/compiler/ts/runtime-loop-bound.test.ts and src/compiler/ts/loop-shapes.test.ts, and by examples/loops-over-data.shade.ts on the compile gate. What is not checked is in Appendix B.
The files that verify it at commit 26de7be8, each at the first line that names the rule:
Explained in
The sections of the surface document that explain this rule, at commit 26de7be8:
Error codes that enforce it
The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:
-
TS8003TYPE_MISMATCH - Two types that do not fit where they meet (an operator, a condition, a declaration, a return or an argument), or a constant the compiler folds that WGSL refuses: a shift amount past 31, a divisor of 0, clamp bounds that cross or an f32 past its range.
-
TS8006LOOP_BOUND - A counted
forloop whose exit the compiler cannot prove: the counter is not compared to a bound, or the body writes the bound. -
TS8007LOOP_INFINITE - A loop that certainly never ends: a
whilewhose condition istrue, written or folded from constants, with nobreakorreturn, or a step that moves the counter away from its bound. -
TS8008LOOP_INDUCTION - A
forloop whose counter is not oneletof typei32oru32, or whose update is not a constant step.
See also
- Rule 7.4 Test
- Rule 7.6 Not enforced
- Rule 5.1
- Rule 8.22
- Design rules
- Error codes
Source
The rule at commit 26de7be8:
docs/language-design.md:562(the design document)reqs/rules/RULE-0705.md(its traceability item)