---
title: "규칙 8.19: 함수와 진입점, TypeShade 셰이더 언어 설계 규칙과 그 근거, 출처, 검증 방식"
description: "TypeShade 언어 설계 규칙 8.19(함수와 진입점)입니다. A function that writes no return type returns what its body does, as TypeScript infers it…"
url: https://typeshade.dev/ko/reference/rules/8-19/
language: ko
---

# 규칙 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](https://typeshade.dev/ko/reference/rules/8-17/)), each instance of a generic function ([Rule 8.9](https://typeshade.dev/ko/reference/rules/8-9/)) and of a function that takes a function ([Rule 8.18](https://typeshade.dev/ko/reference/rules/8-18/)), a method, a getter and a field that holds a function ([Rule 8.16](https://typeshade.dev/ko/reference/rules/8-16/)) take the type of their first `return` with a value, the `return`s after it are typed against that type as against a written one, and one with no such `return` returns nothing. Where the first `return` of a function that writes no return type (a function of the file or of a namespace, a local function or an arrow function bound to a name, a method, a getter, a static function, or an instance of a generic function; a function written as an argument keeps Rule 8.18) is an integer-written number, or a value whose type its uses decide ([Rule 5.1](https://typeshade.dev/ko/reference/rules/5-1/)), or an arrow function’s expression body is one, its callers decide the type (change 0134): each call takes the type its own use gives it. Where every call that decides asks one type, the function returns that type, unless the host calls it (a function an `export` modifier or an `export { … }` clause names, or a member of such a class), whose result stays the `f32` the host reads. A call that asks another type than the function returns takes a copy of the function for that type: a function of the module named as an instance of Rule 8.9 is (the function’s name and the type, made unique), so the function keeps the `f32` under its own name and only the other types get named copies (Amendment A5 of change 0134, “작성자 기본값 유지”). The copies of a generic function’s instance are that instance’s. Each copy infers its own locals, and a call in a copy decides its own callee’s copy. Where no call decides, a call in a conversion (`u32(g())`) included, the result is an `f32`, the compiler’s choice. A `/` over a call of a function that some call makes an integer is refused by Rule 5.1, whichever copy that call takes (Amendment A5, “거부 유지”). A result whose returns give two types where its calls fixed one must be refused with a sentence that names the calls, not a declaration. The compile lowers the module at most 16 times, and each lowering settles one more link of every chain of such functions at once, the uses of other values beside it; a chain that the lowerings do not reach to its end (a chain of 16 functions is reached, one of 17 is not, and a value its functions’ locals decide in turn takes a lowering of its own) is not followed: each such function that no call has given a type yet takes its first `return`’s type, a function its calls decided keeps its type, an `f32` asked alone included, and a mismatch at a call of one switched off says what result type to write. Rule 8.9 does not change: its copies are per type argument the author writes, and these are per result type the calls use. Literal returns of null and undefined contribute their respective absent states to an inferred result, including methods, getters, local functions and function-valued fields. A present return supplies the payload type (change 0098). A body with `return undefined;`, a bare `return;` or a path that ends the body without a `return` beside a `return` of a value of `T` returns `T | undefined`, the optional value of [Rule 7.10](https://typeshade.dev/ko/reference/rules/7-10/), as TypeScript infers it (changes 0088 and 0099); one that returns `undefined` and no value must be refused ([`TS8021`](https://typeshade.dev/ko/reference/errors/ts8021/)), since nothing says what `T` is, and one whose returns are all bare 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](https://typeshade.dev/ko/reference/rules/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](https://typeshade.dev/ko/reference/rules/8-4/)); so must `return`s of two types, a bare `return` beside one with a value where the result is written as a plain `T`, 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’s result is inferred as any function’s is (change 0136): a bare result takes the attribute its stage gives it ([Rule 8.2](https://typeshade.dev/ko/reference/rules/8-2/)), and a struct result its fields’ attributes; it writes its return type when the result carries an attribute other than its stage’s default (`builtin<"frag_depth">`, `builtin<"sample_mask">`, the position with an option, a location with an option), or when an object literal it returns matches no single struct. An object literal that an entry with no return type returns takes the one struct the program declares whose fields are its names, other than a struct a binding, a module variable or workgroup memory holds (surface §16); one that matches none or several must be refused ([`TS8022`](https://typeshade.dev/ko/reference/errors/ts8022/)). A compute entry returns nothing, so a value one returns with no return type must be refused (`TS8021`). `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. An entry’s output attribute is the compiler’s to give where it is the stage’s default, so the type the body says is all an entry needs; only a non-default attribute is the author’s to write (`OD-3`, change 0136, decision R1). 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 has none, and is refused; `undefined` beside a value of `T` is `T | undefined`, the optional value the compiler carries since change 0088 ([Rule 7.10](https://typeshade.dev/ko/reference/rules/7-10/)), and `undefined` alone names no `T`, so it is 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](https://typeshade.dev/ko/reference/rules/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](https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-annotations) (the type of the returned expressions; TS7023 for one that waits on itself); ECMAScript [`return`](https://tc39.es/ecma262/#sec-return-statement) (`return g()` evaluates the call and returns its value, `undefined` for a function that returns nothing); [Rule 8.2](https://typeshade.dev/ko/reference/rules/8-2/); [Rule 8.4](https://typeshade.dev/ko/reference/rules/8-4/); [Rule 8.9](https://typeshade.dev/ko/reference/rules/8-9/); [Rule 8.10](https://typeshade.dev/ko/reference/rules/8-10/); [Rule 8.11](https://typeshade.dev/ko/reference/rules/8-11/); [Rule 8.16](https://typeshade.dev/ko/reference/rules/8-16/); [Rule 8.17](https://typeshade.dev/ko/reference/rules/8-17/); [Rule 8.18](https://typeshade.dev/ko/reference/rules/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 where the result is a written plain `T`, for a function that returns `undefined` and no value (`"g" returns undefined and no value, so nothing says what its result is: write a result type, ": T | undefined", or return a value on another path (Rule 8.19).`, change 0088) and for a compute entry that returns a value with no return type, `TS8022 UNKNOWN_NAME` for an entry’s object literal that matches no single struct (`Object literal { a, b } matches "P" and "Q" alike, so the entry's result is not known: write the return type.`, change 0136), `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; an entry’s inferred result by `src/compiler/ts/entry-results.test.ts`, which reads both halves, the hover of the result included; `examples/gallery/inferred-returns/inferred-returns.shade.ts` in the compile gate; the inferred `T | undefined` by `src/compiler/ts/optional-values.test.ts` and `examples/gallery/optional-values/optional-values.shade.ts` (`halfOf`); a result its callers decide, its copies and the hover of each by `src/compiler/ts/use-inference-module.test.ts`, which reads both halves.

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

- [`examples/shade-examples.test.ts:27`](https://github.com/typeshade/typeshade/blob/af54ddad5/examples/shade-examples.test.ts#L27)
- [`src/compiler/ts/entry-results.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/entry-results.test.ts#L1)
- [`src/compiler/ts/lower/caller-decided.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/lower/caller-decided.ts#L1)
- [`src/compiler/ts/optional-values.test.ts:1`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/optional-values.test.ts#L1)
- [`src/compiler/ts/return-inference.test.ts:61`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/return-inference.test.ts#L61)
- [`src/compiler/ts/use-inference-module.test.ts:3`](https://github.com/typeshade/typeshade/blob/af54ddad5/src/compiler/ts/use-inference-module.test.ts#L3)

## 설명하는 절

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

- [§3 Entries — stage attributes and explicit builtins](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#3-entries--stage-attributes-and-explicit-builtins)
- [§14 TypeScript shapes the parser already had](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#14-typescript-shapes-the-parser-already-had)
- [§16 Object literals take the declared struct](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#16-object-literals-take-the-declared-struct)
- [§26 Classes with methods, a constructor and static functions](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#26-classes-with-methods-a-constructor-and-static-functions)
- [§63 An array's methods](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#63-an-arrays-methods)
- [§76 Optional values: `T | undefined`, `T | null` and three states](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/use-typeshade-surface.md#76-optional-values-t--undefined-t--null-and-three-states)

## 적용하는 오류 코드

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

**[`TS8002`](https://typeshade.dev/ko/reference/errors/ts8002/) `UNKNOWN_TYPE`**

아무것도 선언하지 않은 타입 이름입니다. 매개변수, 타입 인자, 별칭, `implements` 절, 기반 클래스 어디에 써도 걸리고, 이 표면이 매퍼가 읽는 자리에서 받지 않는 타입도 여기에 속합니다([규칙 2.1](https://typeshade.dev/ko/reference/rules/2-1/)). 파일이 선언했지만 구조체로 모으지 않은 기반은 `STRUCT_FIELD`입니다.

**[`TS8003`](https://typeshade.dev/ko/reference/errors/ts8003/) `TYPE_MISMATCH`**

연산자나 조건, 선언, 반환, 인자처럼 두 값이 만나는 자리에서 타입이 서로 맞지 않거나, 검사로 좁히지 않은 선택적 값(`T | undefined`, `T | null`, `T | null | undefined`)을 읽거나([규칙 7.10](https://typeshade.dev/ko/reference/rules/7-10/)), 컴파일러가 접는 상수를 WGSL이 거부합니다. 31을 넘는 시프트 양, 0인 제수, 서로 엇갈린 clamp 경계, 범위를 넘는 `f32` 값, `Math.pow(-2., 2.)`처럼 정의역 밖에서 부르는 내장 함수가 그렇습니다.

**[`TS8020`](https://typeshade.dev/ko/reference/errors/ts8020/) `FUNCTION_SHAPE`**

TypeShade가 지원하지 않는 함수 선언이나 매개변수 모양입니다. 이름이나 본문이 없는 경우, 나머지 매개변수, 구조 분해 매개변수가 그렇습니다.

**[`TS8021`](https://typeshade.dev/ko/reference/errors/ts8021/) `RETURN_SHAPE`**

`return`의 모양이 잘못되었습니다. 값이 필요한 곳의 값 없는 `return`, `undefined`를 반환하면서 값은 없는 함수, 반환 타입 표기가 없는데 값을 반환하는 compute 진입점이 그렇습니다.

**[`TS8022`](https://typeshade.dev/ko/reference/errors/ts8022/) `UNKNOWN_NAME`**

TypeShade가 풀지 못하는 이름을 가리킵니다. 함수 호출(`UNKNOWN_FN`)이나 타입 이름(`UNKNOWN_TYPE`)은 아닙니다. 라이브러리가 TypeScript 자체를 위해 선언한 이름(`Object`, `Math`, `Symbol`)이나 파일이 enum, 네임스페이스, 클래스, 타입으로 선언한 이름을 값으로 읽으면, 메시지가 그 이름이 무엇인지 알려 줍니다.

**[`TS8031`](https://typeshade.dev/ko/reference/errors/ts8031/) `RECURSION`**

호출 순환입니다. 함수가 직접, 또는 다른 함수를 거쳐 자기 자신에 닿습니다. WGSL에는 호출 스택이 없으므로 컴파일러가 타깃이 보기 전에 순환을 거부합니다.

## 함께 보기

- [규칙 8.18](https://typeshade.dev/ko/reference/rules/8-18/) 테스트
- [규칙 8.20](https://typeshade.dev/ko/reference/rules/8-20/) 테스트
- [규칙 5.1](https://typeshade.dev/ko/reference/rules/5-1/)
- [규칙 7.2](https://typeshade.dev/ko/reference/rules/7-2/)
- [규칙 7.10](https://typeshade.dev/ko/reference/rules/7-10/)
- [규칙 8.2](https://typeshade.dev/ko/reference/rules/8-2/)
- [규칙 8.4](https://typeshade.dev/ko/reference/rules/8-4/)
- [규칙 8.9](https://typeshade.dev/ko/reference/rules/8-9/)
- [규칙 8.10](https://typeshade.dev/ko/reference/rules/8-10/)
- [규칙 8.11](https://typeshade.dev/ko/reference/rules/8-11/)
- [규칙 8.16](https://typeshade.dev/ko/reference/rules/8-16/)
- [규칙 8.17](https://typeshade.dev/ko/reference/rules/8-17/)
- [설계 규칙](https://typeshade.dev/ko/reference/rules/)
- [오류 코드](https://typeshade.dev/ko/reference/errors/)

## 소스

커밋 af54ddad5의 규칙 원문:

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