---
title: "Rule 4.8, Types: a TypeShade language design rule"
description: "Rule 4.8 of the TypeShade language design, in Types. Every matCxR with C and R in 2, 3, 4 must be a type, and a square one must also answer to matN."
url: https://typeshade.dev/reference/rules/4-8/
language: en
---

# Rule 4.8

Chapter 4, Types Test

Every `matCxR` with `C` and `R` in 2, 3, 4 must be a type, and a square one must also answer to `matN`. A matrix element must be `f32`, or `f64` on a square shape only. A two-row matrix (`mat2x2`, `mat3x2`, `mat4x2`) in a uniform block must be refused with the remedy, since WGSL gives its column a stride of 8 and std140 rounds every column to 16, so the two targets would place it and every later field at different offsets.

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

## Rationale

a shape both targets lay out alike needs no ceremony, and a shape they lay out differently is refused rather than padded, because padding would make the emitted WGSL disagree with the offsets `reflect()` reports ([Rule 6.8](https://typeshade.dev/reference/rules/6-8/)).

## Derives from

[Matrix Types](https://gpuweb.github.io/gpuweb/wgsl/#matrix-types) (`matCxR<T>`, `T` a floating-point type) and [Memory Layout](https://gpuweb.github.io/gpuweb/wgsl/#memory-layouts) (the column stride is `AlignOf(vecR<T>)`); PR [#166](https://github.com/typeshade/typeshade/issues/166), which measured the std140 stride on a WebGL2 driver and on Tint (surface §40); [#149](https://github.com/typeshade/typeshade/issues/149).

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

- `MAT_TYPE_NAMES` in `src/compiler/ts/type-map/scalar-names.ts` for the nine shapes and the three short spellings (`TS8002 UNKNOWN_TYPE` names all twelve);
- `TS8027 MAT_UNSUPPORTED` for a non-square `matCxR<f64>` (`mat2x3<f64> has no emulated-double form: the fp64 pass carries a square matrix of doubles only (mat2, mat3, mat4). Declare it mat2x3 and narrow, or use a square shape.`);
- `TS8051 LAYOUT` in `src/compiler/ts/bindings.ts` for the two-row uniform field, at the binding’s declaration and with neither target emitted (`"U.m" is in a uniform: mat2x2 in std140 is not supported`, a sentence that goes on to name `mat2x4` and two `vec2` fields as the remedies), pinned by `src/compiler/ts/uniform-layout.test.ts`; `wgslLayout` in `src/core/reflect.ts` throws the same sentence from `src/core/std140.ts` for a std140 layout built another way;
- `TS8003 TYPE_MISMATCH` for `a * b` on two matrices whose dimensions do not meet, two of one non-square shape among them (`Type mismatch: cannot * mat2x3 and mat2x3. WGSL's matrix product is matKxR * matCxK -> matCxR: …`), pinned by `src/compiler/ts/matrices.test.ts` (`refuses two matrices of ONE non-square shape, which have one type key`) ([#169](https://github.com/typeshade/typeshade/issues/169));
- `src/compiler/ts/matrices.test.ts`, `src/core/reflect.test.ts`, and `examples/gallery/normal-matrix/normal-matrix.shade.ts` on both halves of the gate, which pin them.

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/bare-uniform-glsl.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/bare-uniform-glsl.test.ts#L1)
- [`src/compiler/ts/bindings.ts:592`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/bindings.ts#L592)
- [`src/compiler/ts/matrices.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/matrices.test.ts#L1)
- [`src/compiler/ts/type-map.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/type-map.ts#L1)
- [`src/compiler/ts/type-map/scalar-names.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/type-map/scalar-names.ts#L1)
- [`src/compiler/ts/uniform-layout.test.ts:333`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/uniform-layout.test.ts#L333)
- [`src/core/reflect.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/reflect.test.ts#L1)
- [`src/core/reflect.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/reflect.ts#L1)
- [`src/core/std140.ts:7`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/std140.ts#L7)

## Explained in

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

- [§40 Matrices: every `matCxR`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#40-matrices-every-matcxr)

## Error codes that enforce it

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

**[`TS8002`](https://typeshade.dev/reference/errors/ts8002/) `UNKNOWN_TYPE`**

A type name that nothing declares, in a parameter, a type argument, an alias, an `implements` clause or a base class, or a type that this surface does not take where the mapper reads one ([Rule 2.1](https://typeshade.dev/reference/rules/2-1/)). A base that the file declares and does not collect as a struct is `STRUCT_FIELD`.

**[`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.)`.

**[`TS8027`](https://typeshade.dev/reference/errors/ts8027/) `MAT_UNSUPPORTED`**

A non-square `matCxR<f64>`: the fp64 pass carries one double-float body for each dimension, so only a square matrix of doubles lowers. Every `matCxR<f32>` is a type.

**[`TS8051`](https://typeshade.dev/reference/errors/ts8051/) `LAYOUT`**

A buffer binding's store type that breaks one of WGSL's host-shareable rules: a `bool`, alone or in a vector, in a `uniform` or `storage` binding; a runtime-sized `array<T>` that is not the last member of its struct; a runtime-sized array in a uniform; or a two-row matrix (`mat2x2`, `mat3x2`, `mat4x2`) in a uniform, which WGSL and std140 lay out at different offsets (Rule 4.8).

## See also

- [Rule 4.7](https://typeshade.dev/reference/rules/4-7/) Test
- [Rule 5.1](https://typeshade.dev/reference/rules/5-1/) Test
- [Rule 6.8](https://typeshade.dev/reference/rules/6-8/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

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