Rule 6.1
On this page

Rule 6.1

Chapter 6, Declarations and resources Test

A resource must be written declare const x: uniform<T>, declare const x: storage<T>, declare const x: storage<T, "read_write">, or declare const x: <texture or sampler type>. It must have no initializer, and its binding slot is the source order of declare in the file. T[], Array<T>, readonly T[] and ReadonlyArray<T> are array<T> with no size wherever that array is accepted: a storage binding’s value type, the last field of a storage struct and a kernel function’s parameter (change 0114, S1). readonly is a promise that nothing writes the array, and a write through it is refused with TS8005 in the compiler and the editor. A resource is never declared with let: declare let x: storage<T> and declare let x: uniform<T> are both refused, and each refusal names the declare const form of the line it is on. A resource may be exported, export declare const x: uniform<T>, and an exported resource is a host resource (change 0138, R10): a shader file that imports it reads the same binding (Rule 3.9), and a host file that imports it receives a value of typeshade/runtime (Rule 8.21). A uniform<T> of a class the module exports is a Uniform<T>, and a storage<array<T, N>> of such a class is a Buffer<T> of N elements; the generated module makes each one once, and the host sets its fields with set and reads them with get, never with =, since an import is a read-only binding. A storage<array<T>> of such a class, which states no size, is a Buffer<T> whose length the host sets (change 0138, Amendment A3): resize(n) gives it n elements, and set(data) the length of data and its values (Rule 8.21). The shader file does not state the size. Until the host sets the length, length is 0, and at and read throw a TypeError that names the binding and says to call resize or set. Any other exported resource (a scalar, a vector, a struct no exported class declares, an array of scalars or vectors, one storage<T>, a texture, a sampler) is never in the host view, with the reason. An entry call takes an exported Uniform<T>, or an exported storage array with no size, in its bindings object as the resource itself, and a kernel function takes an exported resource as an argument; a call that takes one does not block (Rules 8.21 and 8.24). For an exported storage<array<T, N>>, an entry call refuses the Buffer itself with a TypeError, and takes a plain array of its elements as before (change 0138, deferred question 5). The host reaches each field of a host resource as a property of its accessor, so a field named like a member of that accessor is refused (TS8079, change 0138, Amendment A5, D3, and Amendment A6). The names are set, at any depth of the class of an exported uniform<T> or of the element of an exported storage array, and needsUpdate and dispose on the class of an exported uniform<T> itself. Only an exported resource that is a host resource is read: its class is a class the module exports, is not abstract and has the layout of its address space. The refusal stands on the field’s declaration, in the class that declares it (a class above for an inherited field), once for each field, and names the binding by the name the module exports it as. The compiler and the editor report it with the same code and text. A class that no host resource holds may have such a field. A host that passes such a class to uniform(), zeros() or array() of typeshade/runtime gets a TypeError from the runtime, and its editor reports TS2345 (TS2769 for array()), whose text names TS8079 and the field (Amendment A6). An exported resource lives as long as its module, so its type in the host view is the Uniform<T> or the Buffer<T> without dispose() and [Symbol.dispose]() (Amendment A5, D7), and it is not assignable to a parameter of type Uniform<T> or Buffer<T>.

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

Rationale

declare is TypeScript’s own word for a value that exists elsewhere, which is what a host-owned resource is, and const is the only keyword that says anything true about one, since nothing rebinds the name.

Derives from

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:

  • TS8099 for a declare with a plain type (declare "x" must be uniform<T>, storage<T>, a texture, a sampler or override<T>.);
  • TS8099 for declare let x: storage<T> and for declare let x: uniform<T>, raised in src/compiler/ts/bindings.ts where the declaration’s keyword is already read, each sentence naming the declare const line to write instead;
  • TS8033 for a resource type without declare, whose sentence names the declare const form and, on a storage with no access mode of its own, the mode the KEYWORD asked for (let dst: storage<array<f32>> names declare const dst: storage<array<f32>, "read_write">, the same reading of the same keyword bindings.ts makes); a resource with no type argument names the shape storage<...>, since the type is one only the author knows;
  • examples/binding-declared.test.ts for the slot order.
  • TS8079 HOST_RESERVED_FIELD from refuseReservedFields in src/compiler/ts/bindings.ts, which reads only the bindings that hostResourceOf in src/compiler/ts/host-resource.ts calls host resources, the predicate resourceFaces reads too, and the Refuse type of src/runtime/resource.ts for the host’s editor; pinned by src/runtime/exported-binding.test.ts, which reads each refusal in compile() and in the language service with the same code, text and line, no refusal on a class that is not exported, an abstract class or a class no exported binding holds, the runtime’s TypeError, and the host calls under tsc (TS2345, TS2769 for array()).
  • resourceFaces in src/compiler/ts/host-face.ts for an exported resource in the host view and the generated module, and exportedBinding in src/runtime/resource.ts for the resource it makes; pinned by src/runtime/exported-binding.test.ts, and for a storage array with no size by src/runtime/unsized-storage.test.ts, each of which reads the source with compile() and the language service, runs the generated module and checks a host program over the view with tsc.

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:

Error codes that enforce it

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

TS8033 MODULE_VAR
A module variable declared or used where its address space forbids it: a const with an address-space wrapper, a workgroup variable with an initializer, a type that the space cannot hold (a texture, a runtime-sized array, an atomic in a per-invocation variable), an initializer that is not a constant, or a workgroup variable reached from a vertex or fragment entry. Also the retired perInvocation<T> wrapper, which a plain top-level let replaced.
TS8079 HOST_RESERVED_FIELD
A field of the class of an exported binding that is named like a member of its host accessor (Rule 6.1): set on the class of an exported uniform<T> or of the element of an exported storage array, and needsUpdate or dispose on the class of an exported uniform<T>. The host sets a field through its accessor (camera.pos.set(…)), so a field of the accessor's own name cannot be reached.
TS8099 UNSUPPORTED
The catch-all for a diagnostic whose site does not yet deserve its own code. It stays parked at TS8099, past the sequential range.

See also

Source

The rule at commit af54ddad5:

Edit this page Report a problem