Rule 8.19
On this page

Rule 8.19

Chapter 8, Functions and entry points Test

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. 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), 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, as TypeScript infers it (changes 0088 and 0099); one that returns undefined and no value must be refused (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). 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 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), 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). 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.

The rule text and the parts under it are the compiler's own English, as the design document writes them.

Rationale

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), 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 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.

Derives from

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.

How it is verified

Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.

Where the rule says the compiler enforces it:

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.

The files that verify it at commit af54ddad5, each at the first line that names the rule:

Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

TS8002 UNKNOWN_TYPE
A type name that nothing declares, in a parameter, a type argument, an alias, an implements clause or a base class, or a type that this surface does not take where the mapper reads one (Rule 2.1). A base that the file declares and does not collect as a struct is STRUCT_FIELD.
TS8003 TYPE_MISMATCH
Two types that do not fit where they meet (an operator, a condition, a declaration, a return or an argument), a read of an optional value (T | undefined, T | null or T | null | undefined) that no test narrowed (Rule 7.10), or a constant the compiler folds that WGSL refuses: a shift amount past 31, a divisor of 0, clamp bounds that cross, an f32 past its range or a builtin call outside its domain, such as Math.pow(-2., 2.).
TS8020 FUNCTION_SHAPE
A function declaration or parameter shape that TypeShade does not support, such as a missing name or body, a rest parameter or a destructured parameter.
TS8021 RETURN_SHAPE
A return of the wrong shape: a bare return where a value is required, a function that returns undefined and no value, or a compute entry with no return type annotation that returns a value.
TS8022 UNKNOWN_NAME
A reference to a name that TypeShade cannot resolve and that is not a function call (UNKNOWN_FN) or a type name (UNKNOWN_TYPE). A name that the library declares for TypeScript's own use (Object, Math, Symbol), or that the file declares as an enum, a namespace, a class or a type, and that is read as a value, says what it is.
TS8031 RECURSION
A call cycle: a function that reaches itself, directly or through other functions. WGSL has no call stack, so the compiler refuses the cycle before a target sees it.

See also

Source

The rule at commit af54ddad5:

Edit this page Report a problem