규칙 8.10
8장, 함수와 진입점 테스트
A method that writes its object (assigns to this or to a field, a component or an element of it, applies ++ or -- to one, hands one to ref(...) (Rule 8.25), or calls such a method or reads such a getter on this, on a field, a component or an element of it whatever class that field is, or through super) must take the object by reference, and may return a value like any other method; a base’s body that a class calls through super and that writes its object takes it by reference too, and so does the copy of a method that takes a function (Rule 8.18) when a function handed over writes the variable the call is on.
Its receiver must be a place a function may write: a let local, a const local whose initializer built its value (Rule 6.10), a module variable, a storage element, or this inside a constructor or another such method, or a field or an element of one of those.
A receiver that is a parameter, a const whose value something else may hold, or a value nothing holds must be refused with the remedy, as must a call of such a method that returns nothing where a value is expected.
The same rule decides what ref(...) may hand a Ref<T> parameter, which is a Ref<T> parameter’s object in all but name, and the object a call writes through counts as a reference in Rule 8.25’s check of one call.
A method whose every return is return this returns its object, and a chain of calls on what it returns that is the whole of a call statement, of a declaration’s initializer or of a return (v.setX(1.).setY(2.)) must run each call but the last as a statement of its own, in source order, on the place the chain starts from, found once before the first call (an index it is reached through is read into a let), a new at the root being held in a temporary; inside a larger expression what such a method returns is a copy, and a call on it that writes its object must be refused with the remedy.
규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.
근거
WGSL takes a place as a pointer and GLSL ES 3.00 as an inout parameter, and either leaves the return free for a value, so a generator’s next() can advance its state and return the draw as TypeScript’s own method does; the rule that such a method returns nothing belonged to the protocol that returned the struct itself, which the reference replaced. A base’s body called through super changes the object of the body that called it, which hands its own reference on. A function handed to a method may write the very object the method runs on (this.each((i) => { this.total += … })), which in TypeScript is one object, so the copy takes it by the one reference both use. A field of this is part of this, so this.body.step(dt) writes this when step writes its object, whichever class declares step; before this a method that did so was refused as one that reads its object only. return this hands back a struct, which is a value: where a chain is the whole statement it can run on the place itself, which is TypeScript’s meaning, and where it is not, the copy is all there is, which is right for a read and wrong for a write.
출처
Reference and Pointer Types; Function Calls; GLSL ES 3.00 §6.1.1 (“Evaluation of an inout parameter results in both a value and an l-value”); ECMAScript the super keyword; surface §26 (design #86 step 2).
검증 방식
테스트로 확인합니다. 테스트나 게이트 스크립트, CI 워크플로가 이 규칙을 가리키고, 아래 파일 가운데 하나라도 규칙을 더는 가리키지 않으면 추적 검사가 실패합니다.
컴파일러가 이 규칙을 적용하는 곳을 규칙이 직접 적은 내용입니다.
TS8035 CLASS_MEMBER for each refusal ("Gen.next" changes its object, and "r" is a const whose value may be one something else holds, which TypeScript would change with it and a copy here would not. Declare it with let to change a copy, or call it on the value itself.; "V.setY" changes its object, and inside this expression it would change the copy "v.setX(3.)" hands back. Make the chain a statement of its own, or call each method on the object itself.), pinned by src/compiler/ts/class-methods.test.ts and src/compiler/ts/class-syntax.test.ts, which also hold the three CPU paths to one value for a method that changes its object and returns one, a base’s body called through super that writes its object, a chain, and a method that writes its object through a field of another class, three levels deep; src/compiler/ts/higher-order.test.ts, which holds them to one value for the copy of a method whose function writes the object the call is on; src/compiler/ts/inout-params.test.ts; examples/orbit-inout.shade.ts, examples/particle-step.shade.ts, examples/rng-method.shade.ts, examples/class-builder.shade.ts and examples/class-parts.shade.ts in the compile gate.
커밋 c66579bf에서 이 규칙을 확인하는 파일입니다. 파일마다 규칙을 처음 가리키는 줄로 연결했습니다.
설명하는 절
커밋 c66579bf의 표면 문서에서 이 규칙을 설명하는 절입니다.
적용하는 오류 코드
규칙이 Enforced by 항목에서 가리키는 진단 코드와, 레지스트리 설명에서 이 규칙을 가리키는 진단 코드입니다.
함께 보기
소스
커밋 c66579bf의 규칙 원문:
docs/language-design.md:718(설계 문서)reqs/rules/RULE-0810.md(추적 항목)