---
title: "규칙 6.1: 선언과 리소스, TypeShade 셰이더 언어 설계 규칙과 그 근거, 출처, 검증 방식"
description: "TypeShade 언어 설계 규칙 6.1(선언과 리소스)입니다. A resource must be written declare const x: uniform<T>, declare const x: storage<T>, declare const x…"
url: https://typeshade.dev/ko/reference/rules/6-1/
language: ko
---

# 규칙 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`](https://typeshade.dev/ko/reference/errors/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](https://typeshade.dev/ko/reference/rules/3-9/)), and a host file that imports it receives a value of `typeshade/runtime` ([Rule 8.21](https://typeshade.dev/ko/reference/rules/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](https://typeshade.dev/ko/reference/rules/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`](https://typeshade.dev/ko/reference/errors/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](https://gpuweb.github.io/gpuweb/wgsl/#var-and-value), [Address Spaces](https://gpuweb.github.io/gpuweb/wgsl/#address-space), [Shader Interface](https://gpuweb.github.io/gpuweb/wgsl/#shader-interface); surface §1, §7 and §15.

## 검증 방식

테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.

컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.

- [`TS8099`](https://typeshade.dev/ko/reference/errors/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`](https://typeshade.dev/ko/reference/errors/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`.

커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.

- [`examples/binding-declared.test.ts:29`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/binding-declared.test.ts#L29)
- [`src/compiler/ts/bindings.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/bindings.ts#L1)
- [`src/compiler/ts/host-face.ts:5`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/host-face.ts#L5)
- [`src/compiler/ts/host-resource.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/host-resource.ts#L1)
- [`src/compiler/ts/readonly-ts-arrays.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/readonly-ts-arrays.test.ts#L1)
- [`src/compiler/ts/unsized-ts-arrays.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/unsized-ts-arrays.test.ts#L1)
- [`src/runtime/exported-binding.test.ts:79`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/runtime/exported-binding.test.ts#L79)
- [`src/runtime/resource-call.test.ts:10`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/runtime/resource-call.test.ts#L10)
- [`src/runtime/resource.ts:610`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/runtime/resource.ts#L610)
- [`src/runtime/unsized-storage.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/runtime/unsized-storage.test.ts#L1)

## 설명하는 절

커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.

- [§1 Resources — `declare`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#1-resources--declare)
- [§64 Calling a module from host code](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#64-calling-a-module-from-host-code)

## 적용하는 오류 코드

규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.

**[`TS8033`](https://typeshade.dev/ko/reference/errors/ts8033/) `MODULE_VAR`**

주소 공간이 허용하지 않는 곳에서 모듈 변수를 선언하거나 씁니다. 주소 공간 래퍼가 붙은 `const`, 초기값이 있는 `workgroup` 변수, 그 공간이 담을 수 없는 타입(텍스처, 런타임 크기 배열, 호출별 변수 안의 atomic), 상수가 아닌 초기값, 정점이나 프래그먼트 진입점이 닿는 `workgroup` 변수가 그렇습니다. 일반 최상위 `let`이 대신하게 된 `perInvocation<T>` 래퍼도 여기에 속합니다.

**[`TS8079`](https://typeshade.dev/ko/reference/errors/ts8079/) `HOST_RESERVED_FIELD`**

내보낸 바인딩의 클래스에 있는 필드 이름이 호스트 접근자의 멤버와 같습니다(규칙 6.1). 내보낸 `uniform<T>`의 클래스나 내보낸 스토리지 배열 원소의 클래스에서는 `set`, 내보낸 `uniform<T>`의 클래스에서는 `needsUpdate`와 `dispose`가 그렇습니다. 호스트는 접근자로 필드를 설정하므로(`camera.pos.set(…)`) 접근자와 같은 이름의 필드에는 닿을 수 없습니다.

**[`TS8099`](https://typeshade.dev/ko/reference/errors/ts8099/) `UNSUPPORTED`**

자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.

## 함께 보기

- [규칙 5.5](https://typeshade.dev/ko/reference/rules/5-5/) 테스트
- [규칙 6.2](https://typeshade.dev/ko/reference/rules/6-2/) 테스트
- [규칙 3.9](https://typeshade.dev/ko/reference/rules/3-9/)
- [규칙 8.21](https://typeshade.dev/ko/reference/rules/8-21/)
- [규칙 8.24](https://typeshade.dev/ko/reference/rules/8-24/)
- [설계 규칙](https://typeshade.dev/ko/reference/rules/)
- [오류 코드](https://typeshade.dev/ko/reference/errors/)

## 소스

커밋 af54ddad5의 규칙 원문:

- [`docs/language-design.md:465`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/language-design.md#L465) (설계 문서)
- [`reqs/rules/RULE-0601.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/reqs/rules/RULE-0601.md) (추적 항목)
