규칙 6.2
6장, 선언과 리소스 테스트
A storage binding’s access mode is inferred from what the program does: a binding that some entry writes, by assignment, a mutating method, an atomic builtin, or a write through a function or a reference parameter, is var<storage, read_write>, and every other storage binding is var<storage, read>.
A write inside a function that no entry reaches does not make the binding read_write and draws no TS8005, and a function that no entry reaches and that writes such a binding is not emitted (change 0114, Amendment A17). A write to a binding that readonly or "read" promises is refused wherever it stands, because the editor reads that promise from the type.
readonly in the value type (storage<readonly T[]>) is the author’s promise that nothing writes the binding, and a write to it is refused.
The compiler still accepts an explicit second type argument: "read_write" keeps var<storage, read_write> and "read" keeps var<storage, read>, and the only two words it takes are "read" and "read_write", spelled as string literal types. Change 0114, decision D3, removes the "read_write" argument in S4; D3 does not name "read".
A uniform binding is read-only and has no access mode, so uniform<T> takes one type argument.
A write to a read-only resource must be refused, by the compiler and by the editor.
A binding that the module writes and a vertex entry reads is refused, and so is a binding declared "read_write" that a vertex entry reads, written or not, and a storage binding that a vertex entry writes, directly or through a function it calls, because WebGPU allows no writable storage buffer in the vertex stage.
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
the compiler reads the access mode from the program, as it reads the address space of a module variable from a top-level let (OD-3), and readonly is TypeScript’s own word for “not written”; the host’s bind group layout follows the inferred mode, so an edit that adds or removes a write changes the layout; the declaration keyword cannot carry the mode, because a TypeScript const array forbids rebinding the name and permits arr[0] = 1.
출처
var Declarations and Memory Access Mode; surface §7. The two words are WGSL’s own enumerants and already appear on this surface in texture_storage_2d<"r32float", "read_write">, so neither takes a §9.3 row.
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
TS8002for an access word outside the two, and for a second type argument on auniform<T>, raised insrc/compiler/ts/bindings.ts, which recovers the binding asread_writeso that the author’s own write is not a second sentence on the same program; the inference issrc/compiler/ts/storage-access.ts, which also refuses a written binding that a vertex entry reads, a binding declared"read_write"that a vertex entry reads, and a storage binding that a vertex entry writes, each withTS8099, and which drops a function that no entry reaches and that writes a binding the program leavesread, so that the write draws noTS8005;TS8099for the retired{ access }option of the call form, raised in the same file, whose sentence names the type-argument spelling to write;TS8005 CONST_ASSIGNfor a write to a read-only resource, whose sentence names thestorage<T>remedy for a write to a binding whose value type isreadonly(src/compiler/ts/lower/readonly-promise.ts) and thestorage<T, "read_write">remedy for every other storage binding the write is refused on (writableRemedyinsrc/compiler/ts/context.ts; the"read_write"spelling in that remedy is one that change 0114, decision D3, removes in S4), and for an atomic builtin on a read binding, whose sentence names the mode rather than the keyword (src/compiler/ts/lower/atomics.ts). It names no line in the two cases where no line would close the program: when the binding’s declared value type was itself refused and recovered, since the line would be built from a type the compiler could not read (storage<mat2x3<f64>>was answered withstorage<mat2x3, "read_write">, the<f64>dropped), and when the target is no place on either mode, which is why the root’s access is checked after the target is known to be one, for an element (md[0]on anf64matrix) and for a member after every one of that branch’s refusals (src.length, an emulated-double lanedv[0].x, a multi-component swizzlev.xy);- TypeScript’s own codes in the editor, because the ambient library resolves
uniform<T>andstorage<T, "read">toReadView<T>andstorage<T>andstorage<T, "read_write">toTitself, so areadonlyvalue type draws the error from its own type:TS2542on an indexed write (src[0] = 1.),TS2540on a field write (u.scale = 1.),TS2344on an access word outside the two andTS2314on a second type argument to auniform(src/language-service/ambient-storage-access.test.ts); src/compiler/ts/declare-bind.test.tsfor the access mode each spelling collects, andsrc/compiler/ts/remedy-lines.test.tsfor the lines these sentences name: every refusal that quotes a declaration has that declaration written back into its own program, which must then compile clean and be clean in the editor;- not by the editor in two places, which Appendix B records: an atomic builtin on a read binding and a mutating method on a class-typed read binding are refused by the compiler alone.
커밋 af54ddad5에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
-
src/compiler/ts/bindings.ts:1 -
src/compiler/ts/context.ts:1 -
src/compiler/ts/declare-bind.test.ts:1 -
src/compiler/ts/lower/atomics.ts:1 -
src/compiler/ts/lower/readonly-promise.ts:1 -
src/compiler/ts/member-assign-emit.test.ts:1 -
src/compiler/ts/member-assign-places.test.ts:1 -
src/compiler/ts/member-assign.test.ts:1 -
src/compiler/ts/remedy-lines.test.ts:1 -
src/compiler/ts/storage-access-paths.test.ts:1 -
src/compiler/ts/storage-access.test.ts:1 -
src/compiler/ts/storage-access.ts:1 -
src/language-service/ambient-matrix-index.test.ts:1 -
src/language-service/ambient-storage-access.test.ts:1 -
src/language-service/ambient-texture-lowering.test.ts:1 -
src/language-service/ambient-texture-refusals.test.ts:1 -
src/language-service/ambient-vector-math.test.ts:1 -
src/language-service/ambient.test.ts:1
설명하는 절
커밋 af54ddad5의 언어 문서에서 이 규칙을 설명하는 절입니다.
적용하는 오류 코드
규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.
-
TS8002UNKNOWN_TYPE - 아무것도 선언하지 않은 타입 이름입니다. 매개변수, 타입 인자, 별칭,
implements절, 기반 클래스 어디에 써도 걸리고, 이 표면이 매퍼가 읽는 자리에서 받지 않는 타입도 여기에 속합니다(규칙 2.1). 파일이 선언했지만 구조체로 모으지 않은 기반은STRUCT_FIELD입니다. -
TS8005CONST_ASSIGN - 바뀔 수 없는 이름이나 자리에 값을 대입합니다.
const에 담은 수나 벡터,const벡터나 행렬의 성분,readonly필드나 static, 그리고readonly나"read"로 쓰지 않겠다고 약속한 스토리지 바인딩에는 값을 쓸 수 없습니다.const객체의 필드나 원소에 쓰는 것은 여기에 해당하지 않습니다.const는 객체를 담고 있을 뿐이고, 객체는 JavaScript에서처럼 계속 쓸 수 있기 때문입니다(규칙 6.10). -
TS8099UNSUPPORTED - 자기 코드를 받을 만하지 않은 진단을 위한 포괄 코드입니다. 순차 범위 뒤인 TS8099에 따로 둡니다.
함께 보기
소스
커밋 af54ddad5의 규칙 원문:
docs/language-design.md:482(설계 문서)reqs/rules/RULE-0602.md(추적 항목)