규칙 6.1
6장, 선언과 리소스 테스트
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>.
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
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.
출처
Variable and Value Declarations, Address Spaces, Shader Interface; surface §1, §7 and §15.
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
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.
커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
-
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
설명하는 절
커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.
적용하는 오류 코드
규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.
-
TS8033MODULE_VAR - 주소 공간이 허용하지 않는 곳에서 모듈 변수를 선언하거나 씁니다. 주소 공간 래퍼가 붙은
const, 초기값이 있는workgroup변수, 그 공간이 담을 수 없는 타입(텍스처, 런타임 크기 배열, 호출별 변수 안의 atomic), 상수가 아닌 초기값, 정점이나 프래그먼트 진입점이 닿는workgroup변수가 그렇습니다. 일반 최상위let이 대신하게 된perInvocation<T>래퍼도 여기에 속합니다. -
TS8079HOST_RESERVED_FIELD - 내보낸 바인딩의 클래스에 있는 필드 이름이 호스트 접근자의 멤버와 같습니다(규칙 6.1). 내보낸
uniform<T>의 클래스나 내보낸 스토리지 배열 원소의 클래스에서는set, 내보낸uniform<T>의 클래스에서는needsUpdate와dispose가 그렇습니다. 호스트는 접근자로 필드를 설정하므로(camera.pos.set(…)) 접근자와 같은 이름의 필드에는 닿을 수 없습니다. -
TS8099UNSUPPORTED - 자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.
함께 보기
소스
커밋 af54ddad5의 규칙 원문:
docs/language-design.md:465(설계 문서)reqs/rules/RULE-0601.md(추적 항목)