Rule 7.4
Chapter 7, Expressions and statements Test
A shift amount the compiler can fold must be in 0 to 31; a divisor the compiler can prove to be zero must be refused where the division is lowered.
The rule text and the parts under it are the compiler's own English, as the design document writes them.
Rationale
WGSL takes a shift amount modulo the bit width and makes a const-expression amount at or past it a shader-creation error, and makes a const-expression divisor of zero a shader-creation error, giving x only where the divisor is not known before shader execution; a program Tint refuses should not compile, and a divisor that is certainly zero or an amount certainly past the width is most likely a mistake. A run-time divisor and amount take WGSL’s answer on every target (Rule 11.12).
Derives from
Bit Expressions (the concrete shift rows); Arithmetic Expressions; surface §22 (#71, #68).
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:
TS8003 TYPE_MISMATCH from src/compiler/ts/lower/expression.ts, where lowerBinary folds a shift amount through foldConstNumber (src/compiler/ts/loop-bound.ts) and a divisor through divisorIsZero; src/compiler/ts/lower/statement.ts (lowerBitwiseAssignOp) for a compound shift assignment; and foldsToZero in src/compiler/ts/module-const.ts, which refuses a module const whose initializer divides by a proven zero; pinned by src/compiler/ts/shift-amount.test.ts and src/compiler/ts/zero-divisor.test.ts.
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.
See also
Source
The rule at commit 26de7be8:
docs/language-design.md:556(the design document)reqs/rules/RULE-0704.md(its traceability item)