---
title: "Rule 7.4: Expressions and statements in TypeShade"
description: "Rule 7.4 of the TypeShade language design, in Expressions and statements."
url: https://typeshade.dev/reference/rules/7-4/
language: en
---

# 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 amount takes WGSL’s answer on every target ([Rule 11.12](https://typeshade.dev/reference/rules/11-12/)). A run-time divisor takes it too where an operand has a declared integer type: `ii / (ii + 3)` with `ii: i32` is `ii` where `ii + 3` is 0. Where no operand has a declared type and a position that states an integer type reads the quotient ([Rule 5.1](https://typeshade.dev/reference/rules/5-1/)), a divisor that is zero at run time gives TypeScript’s value, 0. It does not give WGSL’s `x / 0 = x` (the owner’s decision of 2026-11-17, Amendment A8 of change 0134).

## Derives from

[Bit Expressions](https://gpuweb.github.io/gpuweb/wgsl/#bit-expr) (the concrete shift rows); [Arithmetic Expressions](https://gpuweb.github.io/gpuweb/wgsl/#arithmetic-expr); surface §22 ([#71](https://github.com/typeshade/typeshade/issues/71), [#68](https://github.com/typeshade/typeshade/issues/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 af54ddad5, each at the first line that names the rule:

- [`src/compiler/ts/loop-bound.ts:4`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/loop-bound.ts#L4)
- [`src/compiler/ts/lower/expression.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/expression.ts#L1)
- [`src/compiler/ts/lower/statement.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/statement.ts#L1)
- [`src/compiler/ts/module-const.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/module-const.ts#L1)
- [`src/compiler/ts/shift-amount.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/shift-amount.test.ts#L1)
- [`src/compiler/ts/zero-divisor.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/zero-divisor.test.ts#L1)

## Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

- [§22 Constant checks on a shift amount and a divisor](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#22-constant-checks-on-a-shift-amount-and-a-divisor)

## Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

**[`TS8003`](https://typeshade.dev/reference/errors/ts8003/) `TYPE_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 | null` or `T | null | undefined`) that no test narrowed ([Rule 7.10](https://typeshade.dev/reference/rules/7-10/)), or a constant the compiler folds that WGSL refuses: a shift amount past 31, a divisor of 0, clamp bounds that cross, an `f32` past its range or a builtin call outside its domain, such as `Math.pow(-2., 2.)`.

## See also

- [Rule 7.3](https://typeshade.dev/reference/rules/7-3/) Test
- [Rule 7.5](https://typeshade.dev/reference/rules/7-5/) Test
- [Rule 5.1](https://typeshade.dev/reference/rules/5-1/)
- [Rule 11.12](https://typeshade.dev/reference/rules/11-12/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

- [`docs/language-design.md:688`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L688) (the design document)
- [`reqs/rules/RULE-0704.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0704.md) (its traceability item)
