Rule 9.5
On this page

Rule 9.5

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

A function the file declares or imports (Rule 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) 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); 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); the host calls it by the name written (D7). A name a function’s body declares, a local function (Rule 8.17) or a parameter that takes a function (Rule 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’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). 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 (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 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 (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’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:

Explained in

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

See also

Source

The rule at commit af54ddad5:

Edit this page Report a problem