규칙 8.19
이 페이지에서

규칙 8.19

8장, 함수와 진입점 테스트

A function that writes no return type returns what its body does, as TypeScript infers it: a function of the file or of a namespace, a local function (Rule 8.17), each instance of a generic function (Rule 8.9) and of a function that takes a function (Rule 8.18), a method, a getter and a field that holds a function (Rule 8.16) take the type of their first return with a value, the returns after it are typed against that type as against a written one, and one with no such return returns nothing. An arrow function whose body is an expression returns its value, unless the expression is an assignment, ++, -- or a call of a function that returns nothing, which runs as a statement, and the function returns nothing; a method whose every return is return this returns its object, as one written this does (Rule 8.10). A call that needs the type before the body’s turn lowers that body first, so a function may be called above its declaration and from any body. A setter’s value that writes no type takes the type its getter returns, written or said by the getter’s body, as TypeScript types it; an assignment that needs the type before the getter’s body is lowered lowers it first. A call back into a body still being lowered is a call cycle, and must be refused as one (Rule 8.4); so must returns of two types, a bare return beside one with a value, a default parameter value that calls such a function, since every default is lowered before any body, and a setter’s value with no type and no getter, or beside a getter that returns nothing. An entry point writes its return type, which is its output (Rule 8.2). return g() where g returns nothing calls g and returns nothing, in any function.

규칙 본문과 그 아래 항목은 설계 문서에 적힌 컴파일러 원문이라 한국어 페이지에서도 영어로 둡니다.

근거

TypeScript code leaves return types off as often as it writes them, most of all on a helper and on a local arrow function (const f = (x: f32) => x * k), and the body saying the type is the ordinary TypeScript experience. Before this rule a function with none returned void beside a TS8021 warning, so one that returned a value was a type mismatch at every call, and an arrow function whose body is an expression, a getter and a field that holds a function with none were refused. The front end has no checker to ask, but it lowers every body, and the type of the first return with a value is the one TypeScript infers whenever the program has a shader meaning: a union of two types, or undefined beside a value, has none, and those are refused. Lowering a body when a call first needs it, rather than in declaration order, is what lets a function be called above its declaration, as TypeScript hoists it; a type that waits on itself only arises in a call cycle, which is refused anyway. An assignment’s value is what an arrow function written to run it is never for, so it runs as a statement, the one divergence here, which Rule 7.2 records. No call says a setter’s value type, and the getter does: TypeScript types the property from the getter’s return, inferred or written, and a setter with no getter is its implicit any (TS7006), which a shader has no word for.

출처

TypeScript’s return type inference (the type of the returned expressions; TS7023 for one that waits on itself); ECMAScript return (return g() evaluates the call and returns its value, undefined for a function that returns nothing); Rule 8.2; Rule 8.4; Rule 8.9; Rule 8.10; Rule 8.11; Rule 8.16; Rule 8.17; Rule 8.18; surface §14 and §26.

검증 방식

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

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

TS8031 RECURSION for a cycle (Recursive call: "a" -> "b" -> "a". WGSL has no call stack, so a function must not take part in a call cycle.), TS8003 TYPE_MISMATCH for two types (Function "f" returns f32 at its first "return" and vec2 at another; a function returns one type (Rule 8.19): make them agree, or write the return type.), TS8021 RETURN_SHAPE for a bare return beside a value and for an entry that returns a value with no return type, TS8020 FUNCTION_SHAPE for a default ("h" says what it returns in its body, and a default is lowered before any body: write the return type on "h" (Rule 8.19).) and TS8002 UNKNOWN_TYPE for a setter’s value with nothing to take a type from (The setter "A.x" needs a type for "v": write "set x(v: T)"., The getter "A.x" returns nothing, so the setter has no type for "v": write "set x(v: T)".); pinned by src/compiler/ts/return-inference.test.ts, which holds WGSL, GLSL ES 3.00, the CPU oracle, the codegen and the debugger to one value for each form, compares each with the program that writes its return types, and holds the multi-file path and the editor’s silence; examples/inferred-returns.shade.ts in the compile gate.

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

설명하는 절

커밋 c66579bf의 표면 문서에서 이 규칙을 설명하는 절입니다.

적용하는 오류 코드

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

TS8002 UNKNOWN_TYPE
A type name nothing declares, said once for the file wherever it is written (a parameter, a claim, a type argument, B<vec3<Foo>> included, an alias, an implements clause, the base a class or an interface extends, a body no call lowers), a type the library declares for TypeScript alone (Number) where the file declares none of its name, or a type this surface does not take in a position the mapper reads (Rule 2.1).
TS8003 TYPE_MISMATCH
연산자나 조건, 선언, 반환, 인자처럼 두 값이 만나는 자리에서 타입이 서로 맞지 않거나, 컴파일러가 접는 상수를 WGSL이 거부합니다. 31을 넘는 시프트 양, 0인 제수, 서로 엇갈린 clamp 경계, 범위를 넘는 f32 값이 그렇습니다.
TS8020 FUNCTION_SHAPE
A function declaration or parameter shape TypeShade does not support (missing name or body, optional/rest/destructured parameter).
TS8021 RETURN_SHAPE
A return shape problem: bare return where a value is required, or an entry function with no return type annotation that returns a value.
TS8031 RECURSION
A call cycle: a function that reaches itself, directly or through other functions.

함께 보기

소스

커밋 c66579bf의 규칙 원문:

이 페이지 편집 문제 보고