Rule 7.5
Chapter 7, Expressions and statements Test
A for loop means what it means in TypeScript: its initializer runs once, its condition (true when it has none) is tested before each trip, and its update runs after each trip and at each continue (change 0136). A for is counted when it has the counted shape: one let that declares 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 does not write the bound, and the step moves the variable toward the bound. A runtime bound is then compared with <, <=, > or >=, and a multiplicative step with a runtime start or bound is a whole factor of 2 or more. The compiler records the counted fact of a counted for; any other for is an open loop, as a while is: a header with no initializer, no condition or no update, two declarations or a comma update, a counter of another type, a bound the body writes, an exit that is not one comparison with a bound, and an update that is not a constant step. A header whose start and bound are constants must reach its bound within the induction type’s range, and its step must move the variable. A name the header declares with no annotation and a value written as an integer is an i32, or a u32 when the value 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 name it declares with any other value takes that value’s type, as a let does. A while loop is an open loop and may have any bool condition; an open loop 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). Whether a for has that shape is a fact the compiler can tell, so the author does not say it (OD-3, change 0136): a for of another shape is the open loop a while is, and both targets accept each such header (measured on Tint and on ANGLE for change 0136). The cost, recorded in change 0136: a for no longer promises a reader that it is counted, and a mistake in a counted header (for (let i = 0; i < n; j++)) compiles as an open loop that may not end. 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 an open loop: a while, which is a for over a counter it made, and a for that is not counted), and the proof of a kernel function’s loops takes it from there (Rule 8.22). The IR for holds one initializer and one update, as WGSL’s header does, so a header with two declarations or a comma update runs its initializer ahead of the loop and its update at the end of the body and at each continue of the loop, over the counter a while uses; each name the header declares has an IR name no other local of the function has, so nothing after the loop sees it.
Derives from
Loop Statement, For Statement, While Statement; ECMAScript for statement; surface §17; #203; change 0136 in changes/.
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:
analyzeCountedFor in src/compiler/ts/loop-bound.ts, which classifies each for header, and lowerFor in src/compiler/ts/lower/control.ts, which lowers a for that is not counted as an open loop and leaves an error for every for it does not lower, so a refused loop is never dropped from the module; TS8006 LOOP_BOUND (a constant header that leaves its type), TS8007 LOOP_INFINITE (a constant header whose step never moves the variable or never reaches the bound; a for with no condition, or one that folds to true, and 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, each sentence naming the loop as written: for (;;) has no break or return in its body, so it never ends. Leave it with a break, or write the exit into the condition.), TS8003 for a while or a for condition that is not a bool, as for an if condition, with the comparison with zero or the any/all that says a number’s or a vector of bools’ truthiness (lowerWhile); a header clause is lowered as the statement it is, so an update that is no statement is refused as the same statement in a body is; pinned by src/compiler/ts/runtime-loop-bound.test.ts, src/compiler/ts/loop-shapes.test.ts, src/compiler/ts/counter-cast.test.ts (an integer conversion that starts a counter gives it its type, in both halves) and src/compiler/ts/open-for.test.ts, which reads both halves and both CPU engines, and by examples/gallery/loops-over-data/loops-over-data.shade.ts and the open-loop leg on the compile gate. What is not checked is in Appendix B.
The files that verify it at commit af54ddad5, each at the first line that names the rule:
Explained in
The sections of the surface document that explain this rule, at commit af54ddad5:
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), a read of an optional value (
T | undefined,T | nullorT | null | undefined) that no test narrowed (Rule 7.10), or a constant the compiler folds that WGSL refuses: a shift amount past 31, a divisor of 0, clamp bounds that cross, anf32past its range or a builtin call outside its domain, such asMath.pow(-2., 2.). -
TS8006LOOP_BOUND - A loop whose condition the compiler refuses: a
forwith a constant start, bound and step whose counter leaves the range of its type before the condition fails, or a call that writes inside a loop condition. Afor...ofover an array that is not a name is refused under this code too. -
TS8007LOOP_INFINITE - A loop that certainly never ends: a
while (true)or aforwith no condition, written or folded from constants, with nobreakorreturn, or aforwith constant bounds whose step does not move the counter toward its bound.
See also
Source
The rule at commit af54ddad5:
docs/language-design.md:694(the design document)reqs/rules/RULE-0705.md(its traceability item)