---
title: "Rule 9.5: Built-in functions and the TypeShade extensions"
description: "Rule 9.5 of the TypeShade language design, in Built-in functions and the TypeShade extensions."
url: https://typeshade.dev/reference/rules/9-5/
language: en
---

# Rule 9.5

Chapter 9, Built-in functions and the TypeShade extensions Test

A function the file declares or imports ([Rule 3.9](https://typeshade.dev/reference/rules/3-9/)) must win over every builtin function of its name, whether a WGSL built-in function, the free spelling of a `Math` member ([Rule 9.4](https://typeshade.dev/reference/rules/9-4/)) or a TypeShade extension that is a function (§9.3), as a module-scope declaration hides a predeclared function in WGSL and as TypeScript’s lookup finds the file’s own name before a global: a call of the name reaches the declaration in the constant folder, on the CPU, on WebGPU and on WebGL2, and a function handed by its name to a fold, to an array method or to a function that takes one follows the call. A value constructor (a scalar cast, a vector, a matrix or `array`) must keep its precedence over a declaration of its name, as a type name does over an alias ([Rule 4.2](https://typeshade.dev/reference/rules/4-2/)); `bool` and `f64` are the exception and stay the declaration’s, and a declaration of a constructor’s name is refused where it is handed by that name, since no callback is a constructor. Each writer must emit a declared function whose name its target predeclares under a name the target does not have, and every call through the declaration with it, so that a builtin call, the author’s and the compiler’s own alike, keeps the builtin: `fract_`, then `fract_1` where that is taken, on both targets. An entry point whose name its target predeclares receives a fresh emitted name, recorded beside the authored name ([Rule 3.2](https://typeshade.dev/reference/rules/3-2/)); the host calls it by the name written (D7). A name a function’s body declares, a local function ([Rule 8.17](https://typeshade.dev/reference/rules/8-17/)) or a parameter that takes a function ([Rule 8.18](https://typeshade.dev/reference/rules/8-18/)), must win over every builtin, as TypeScript’s lookup finds it before any global. A name a `const` binds to a `Math` member is a declaration of the file and wins over a builtin of its name in its scope, as TypeScript’s lookup finds it; a call the member’s signature refuses is refused in that scope, with a sentence that names the builtin and how to reach it.

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

## Rationale

the rule once kept the builtins that predate it in front of a function of the module, so that a program that meant the builtin kept meaning it, and that held in the constant folder and on the CPU only. No program declaring such a name ever meant the builtin on WebGPU, where WGSL calls the declaration, and GLSL ES 3.00 does not let a program redeclare one of its built-in functions at all, so [#403](https://github.com/typeshade/typeshade/issues/403)’s `fract` had three answers: the declaration on WebGPU, the builtin on the CPU and none on WebGL2, where ANGLE refuses the module. The editor resolves the call to the declaration as TypeScript does, so keeping the builtin promised in the editor what the compiler did not do ([Rule 12.7](https://typeshade.dev/reference/rules/12-7/)). The rename is what lets one module hold both. WGSL hides a predeclared name for the whole module beside a module-scope declaration, a type included (a declared `f32` hides the type in every `x: f32`), so without it every `fract(…)` reached the declaration, and `random(p)`, which expands to a call of `fract`, called the author’s function. No program calling a name its own body declares meant a builtin: `step(i)` on a parameter `step`, and a local `const mix = …` called as `mix(…)`, reached WGSL’s builtin before, a wrong arity or a silently different value, where TypeScript calls the author’s function.

## Derives from

[Declaration and Scope](https://gpuweb.github.io/gpuweb/wgsl/#declaration-and-scope) (the example “Shadowing predeclared objects”) for the first clause and for the names WGSL predeclares; section 8 of the GLSL ES 3.00 specification for the names GLSL does, measured on ANGLE through the compile gate; [Rule 4.2](https://typeshade.dev/reference/rules/4-2/) for the constructors, and surface §10 for the two casts that stay the declaration’s, `bool` and `f64`, which WGSL does not have; ECMAScript [ResolveBinding](https://tc39.es/ecma262/#sec-resolvebinding) (the innermost environment first) for a name a body declares.

## 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/builtins.test.ts` and `src/compiler/ts/declared-builtin-names.test.ts`, which declare and call every builtin function a file can call and hold the folder, the three CPU walks, both writers and the editor to one answer;
- `renamePredeclaredFunctions` in `src/core/passes/rename-predeclared.ts`, pinned by `src/core/passes/rename-predeclared.test.ts`, and the two lists it renames by, `WGSL_PREDECLARED` and `GLSL_ES300_BUILTIN_FUNCTIONS` in `src/core/reserved-words.ts`, held to the specification’s names by `src/core/spec-conformance/predeclared-names.test.ts`;
- the compile gate (`scripts/compile-gate.ts`, its declared-names leg), which gives Tint and ANGLE a function declared under every name a writer renames, and runs [#403](https://github.com/typeshade/typeshade/issues/403)’s program on WebGPU against the CPU oracle;
- `src/compiler/ts/closures.test.ts` and `src/compiler/ts/higher-order.test.ts` for a local function and a parameter.

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

- [`scripts/compile-gate.ts:55`](https://github.com/typeshade/typeshade/blob/af54ddad5/scripts/compile-gate.ts#L55)
- [`src/compiler/ts/builtins.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/builtins.test.ts#L1)
- [`src/compiler/ts/closures.test.ts:638`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/closures.test.ts#L638)
- [`src/compiler/ts/declared-builtin-names.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/declared-builtin-names.test.ts#L1)
- [`src/compiler/ts/higher-order.test.ts:1015`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/higher-order.test.ts#L1015)
- [`src/compiler/ts/link.test.ts:5`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/link.test.ts#L5)
- [`src/compiler/ts/lower/constructors.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/constructors.ts#L1)
- [`src/compiler/ts/math-binding.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/math-binding.test.ts#L1)
- [`src/compiler/ts/math-binding.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/math-binding.ts#L1)
- [`src/core/backends/glsl-spelling.test.ts:6`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/backends/glsl-spelling.test.ts#L6)
- [`src/core/backends/wgsl-spelling.test.ts:5`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/backends/wgsl-spelling.test.ts#L5)
- [`src/core/passes/predeclared-names.ts:3`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/predeclared-names.ts#L3)
- [`src/core/passes/rename-predeclared.test.ts:2`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/rename-predeclared.test.ts#L2)
- [`src/core/passes/rename-predeclared.ts:3`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/rename-predeclared.ts#L3)
- [`src/core/passes/spell-names.ts:2`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/passes/spell-names.ts#L2)
- [`src/core/reserved-words.ts:94`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/reserved-words.ts#L94)
- [`src/core/spec-conformance/predeclared-names.test.ts:11`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/spec-conformance/predeclared-names.test.ts#L11)

## Explained in

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

- [§10 Builtins, casts and `discard`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#10-builtins-casts-and-discard)
- [§77 The subgroup built-in functions: `subgroupAdd` and the twenty beside it](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#77-the-subgroup-built-in-functions-subgroupadd-and-the-twenty-beside-it)

## See also

- [Rule 9.4](https://typeshade.dev/reference/rules/9-4/) Test
- [Rule 9.6](https://typeshade.dev/reference/rules/9-6/) Test
- [Rule 3.2](https://typeshade.dev/reference/rules/3-2/)
- [Rule 3.9](https://typeshade.dev/reference/rules/3-9/)
- [Rule 4.2](https://typeshade.dev/reference/rules/4-2/)
- [Rule 8.17](https://typeshade.dev/reference/rules/8-17/)
- [Rule 8.18](https://typeshade.dev/reference/rules/8-18/)
- [Rule 12.7](https://typeshade.dev/reference/rules/12-7/)
- [Design rules](https://typeshade.dev/reference/rules/)
- [Error codes](https://typeshade.dev/reference/errors/)

## Source

The rule at commit af54ddad5:

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