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
Variable and Value Declarations, Address Spaces, Shader Interface; surface §1, §7 and §15.
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:
TS8099for adeclarewith a plain type (declare "x" must be uniform<T>, storage<T>, a texture, a sampler or override<T>.);TS8099fordeclare let x: storage<T>and fordeclare let x: uniform<T>, raised insrc/compiler/ts/bindings.tswhere the declaration’s keyword is already read, each sentence naming thedeclare constline to write instead;TS8033for a resource type withoutdeclare, whose sentence names thedeclare constform and, on astoragewith no access mode of its own, the mode the KEYWORD asked for (let dst: storage<array<f32>>namesdeclare const dst: storage<array<f32>, "read_write">, the same reading of the same keywordbindings.tsmakes); a resource with no type argument names the shapestorage<...>, since the type is one only the author knows;examples/binding-declared.test.tsfor the slot order.TS8079 HOST_RESERVED_FIELDfromrefuseReservedFieldsinsrc/compiler/ts/bindings.ts, which reads only the bindings thathostResourceOfinsrc/compiler/ts/host-resource.tscalls host resources, the predicateresourceFacesreads too, and theRefusetype ofsrc/runtime/resource.tsfor the host’s editor; pinned bysrc/runtime/exported-binding.test.ts, which reads each refusal incompile()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’sTypeError, and the host calls undertsc(TS2345,TS2769forarray()).resourceFacesinsrc/compiler/ts/host-face.tsfor an exported resource in the host view and the generated module, andexportedBindinginsrc/runtime/resource.tsfor the resource it makes; pinned bysrc/runtime/exported-binding.test.ts, and for a storage array with no size bysrc/runtime/unsized-storage.test.ts, each of which reads the source withcompile()and the language service, runs the generated module and checks a host program over the view withtsc.
The files that verify it at commit af54ddad5, each at the first line that names the rule:
-
examples/binding-declared.test.ts:29 -
src/compiler/ts/bindings.ts:1 -
src/compiler/ts/host-face.ts:5 -
src/compiler/ts/host-resource.ts:1 -
src/compiler/ts/readonly-ts-arrays.test.ts:1 -
src/compiler/ts/unsized-ts-arrays.test.ts:1 -
src/runtime/exported-binding.test.ts:79 -
src/runtime/resource-call.test.ts:10 -
src/runtime/resource.ts:610 -
src/runtime/unsized-storage.test.ts:1
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:
-
TS8033MODULE_VAR - A module variable declared or used where its address space forbids it: a
constwith an address-space wrapper, aworkgroupvariable 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 aworkgroupvariable reached from a vertex or fragment entry. Also the retiredperInvocation<T>wrapper, which a plain top-levelletreplaced. -
TS8079HOST_RESERVED_FIELD - A field of the class of an exported binding that is named like a member of its host accessor (Rule 6.1):
seton the class of an exporteduniform<T>or of the element of an exported storage array, andneedsUpdateordisposeon the class of an exporteduniform<T>. The host sets a field through its accessor (camera.pos.set(…)), so a field of the accessor's own name cannot be reached. -
TS8099UNSUPPORTED - 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:
docs/language-design.md:465(the design document)reqs/rules/RULE-0601.md(its traceability item)