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

# Rule 7.9

Chapter 7, Expressions and statements 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. The compiler binds each such call to a temporary ahead of its statement, in source order, and binds ahead of the call an operand evaluated before it that reads what it writes; a call TypeScript evaluates conditionally, in an arm of `?:` or the right operand of `&&` or `||`, keeps its condition as an `if`. A call `select(f, t, c)` is a call, not a conditional: its three arguments are evaluated in the order they are written, `f`, `t`, then `c`, and a call that writes in any of them takes effect in that order on every target, whichever value `c` picks. An operand is evaluated conditionally only where TypeScript evaluates it conditionally, as in `?:`, `&&`, `||` and `??`. A call that takes a reference ([Rule 8.25](https://typeshade.dev/reference/rules/8-25/)) is a call that writes each place it is handed, and the argument of a parameter that its function writes is a place and not a read: a call that writes a variable, in an argument after that variable is handed to such a parameter, runs ahead of the call that takes the reference, which then reads the variable as the call that wrote it left it. A loop condition runs on every iteration, so it may hold such a call only as one side of its comparison.

The rule text and the parts under it are the compiler's own English, as the design document writes them.

## Rationale

GLSL ES 3.00 leaves the order of an operator’s operands open, and a scalar `?:` lowers to WGSL’s `select`, which evaluates both arms, so the one order every target keeps is the order the compiler writes out; the passes that fold and repeat expressions then see no call that writes inside one.

## Derives from

- [Program Order Within an Invocation](https://gpuweb.github.io/gpuweb/wgsl/#program-order) (“The order of evaluation for operands of an expression is left-to-right in WGSL. For example, foo() + bar() must evaluate foo() before bar().”);
- [Function Calls](https://gpuweb.github.io/gpuweb/wgsl/#function-calls) (“Function call argument values are evaluated. The relative order of evaluation is left-to-right.”);
- GLSL ES 3.00 §5.11, which relaxes C++‘s rules and fixes no order for an operator’s operands, §6.1.1 (“All arguments are evaluated at call time, exactly once, in order, from left to right”), and §5.8 (“Expressions on the left of an assignment are evaluated before expressions on the right of the assignment”);
- surface §19 and §26.

## 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:

`src/compiler/ts/sequence.ts` (`sequenceEffects`), pinned by `src/compiler/ts/sequence.test.ts`, which holds the CPU oracle, the codegen and the debugger to the source order; `TS8006 LOOP_BOUND` for a loop condition that holds a call that writes anywhere but as one side of its comparison (`A while condition runs "rng.next()" on every iteration, and a call that writes can stand there only as one side of the comparison. …`); `examples/gallery/rng-method/rng-method.shade.ts` in the compile gate.

The files that verify it at commit af54ddad5, each at the first line that names the rule:

- [`examples/shade-examples.test.ts:26`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/shade-examples.test.ts#L26)
- [`src/compiler/ts/reference-parameters.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/reference-parameters.test.ts#L1)
- [`src/compiler/ts/sequence-select.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/sequence-select.test.ts#L1)
- [`src/compiler/ts/sequence.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/sequence.test.ts#L1)
- [`src/compiler/ts/sequence.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/sequence.ts#L1)

## Explained in

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

- [§7 Diagnostics (required)](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#7-diagnostics-required)
- [§10 Builtins, casts and `discard`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#10-builtins-casts-and-discard)
- [§19 A call as a statement](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#19-a-call-as-a-statement)
- [§26 Classes with methods, a constructor and static functions](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#26-classes-with-methods-a-constructor-and-static-functions)
- [§27 Boolean vectors](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#27-boolean-vectors)
- [§70 Objects, passed as JavaScript passes them](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#70-objects-passed-as-javascript-passes-them)
- [§76 Optional values: `T | undefined`, `T | null` and three states](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#76-optional-values-t--undefined-t--null-and-three-states)

## Error codes that enforce it

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

**[`TS8006`](https://typeshade.dev/reference/errors/ts8006/) `LOOP_BOUND`**

A loop whose condition the compiler refuses: a `for` with 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. A `for...of` over an array that is not a name is refused under this code too.

## See also

- [Rule 7.8](https://typeshade.dev/reference/rules/7-8/) Test
- [Rule 7.10](https://typeshade.dev/reference/rules/7-10/) Test
- [Rule 8.25](https://typeshade.dev/reference/rules/8-25/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

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