Rule 8.8
On this page

Rule 8.8

Chapter 8, Functions and entry points Test

An object has the identity a JavaScript object has (change 0142). An object is a struct, an instance of a class, or an array, with a size or with none. A function of the file or of a namespace changes the caller’s object when it writes a field or an element of its object parameter, calls push or fill on it, calls on it a method that writes its object (Rule 8.10), or hands it to a parameter that its function writes. The author writes no qualifier: the compiler reads the source and passes the caller’s place (Rule 8.25). A number, a bool, a vector and a matrix are values, as a JavaScript primitive is. A function gives a value back by returning it, and a list assignment, [a, b] = f(a, b), takes two results apart. An author writes no pointer value: no ptr, no & and no qualifier. Whole-value assignment, compound assignment and integer updates of a value parameter must rebind a mutable local initialized from the input, leaving the caller’s value unchanged. A whole assignment to an object parameter (p = new P()) makes p name a local, and the caller’s object does not change. Where the body writes the caller’s object through p before that assignment, the assignment is TS8099, not supported yet (change 0142, stage 2): the compiler does not yet keep the caller’s object and the local apart under one name. A loop name, for (let p of ps), follows the same rule. Only a parameter whose declaration a whole write resolves to gets this local; a shadowing declaration does not count. Within that body’s scope the local may be written through its fields or elements. A value parameter with no whole write retains its existing read-only place semantics. Resource handles and the reference-backed arrays of Rule 8.23 are not local-copy values. A second name of an object is that object, as in JavaScript, and the compiler never compiles a copy that JavaScript would not make (OD-9; change 0142, Amendments A3 and A4). Six rules keep it. Rules 1 to 5 read one function, and rule 6 reads the module. A refusal is TS8099, “One object under two names is not supported yet (change 0142, stage N)”, unless a rule below names another code. Rule 1. A second name is the same place. const q = p, let q = p, const q = ps[i], const q = h.c, for (const q of ps) and let b = f(a), where every return of f names one parameter, make q a name of that place. The compiler makes no copy, and it reads an index in the place once, where the name is declared. A second name that is assigned whole later, or that a function inside the body uses, is TS8099; so is an index that the statement cannot hold once. Rule 2. A store is a move. A statement that puts an object that has a name into another holder (xs[i] = p, h.c = p, [p, …], { c: p }, new B(p), xs.push(p), q = p for a q that exists) is accepted only where p is a local of the function that the function does not use after the statement. After the statement the holder is the one name of the object. Every other such statement is TS8099. xs.fill(o) of a new object is TS8099 too, because every element is then one object. Rule 3. A new object is one the source shows to be new: new, an object or an array literal, structuredClone(v) of a struct that is no class, xs.slice() and xs.map(f) of values, and a call of a function of the file whose every return gives an object that the function built and stored nowhere else. A store of a new object is no refusal. Rule 4. The result of any other call that gives an object may be an object that has a name. A const may take it where the function, after that declaration, writes no object and only reads the const. Any other declaration of it, a let included, is TS8099. Rule 5. A place that a second name names stays that place while the name is read. A statement that assigns the place, or a holder of it, with a read of the name after it, is TS8099. A call of a function declared inside the function runs that function’s body where the call is, and a use of a getter or a setter that may write runs its body on its object; rules 4 and 5 and the loop of rule 1 read both there. Rule 6. An object of a type that the program never writes is a copy, because no write shows through a second name and === between objects is refused (TS8003). So a store of such an object (rule 2), a name for such an object that a call or a conditional gives (rule 4) and a let second name that is assigned whole later (rule 1) are accepted, and the compiler makes a copy. A struct or an array type is written where a store, an atomic or a texture store reaches a field or an element through a value of that type, and a type is never written where it and every struct and array type inside it are not. A constructor’s own stores to its object build it and do not count. The compiler reads the lowered program whole, by type and not by name, and holds a refusal that rule 6 can lift until every function is lowered. Rule 6 lifts no other refusal: rule 5, a parameter or a loop name assigned whole after a write, TS8074, xs.slice() of objects and structuredClone of a class instance keep theirs. A copy is written with a name JavaScript has, and the language adds no copy construct (change 0142, Table 3): xs.slice() of numbers or vectors, and structuredClone(v) of a struct that is no class or of an array with a size. [...xs] alone is TS8013, and its sentence names xs.slice(); structuredClone of an instance of a class and slice() of an array of objects are TS8099, not supported yet (stage 3). f(a, a) where f writes one of the two, a module variable passed to a function that also uses its name (TS8074), and === or !== between objects (TS8003) are refused as not supported yet (change 0142, stage 2). A parameter of a method, a constructor, a setter, a local function, a function handed as an argument and a generic function takes a value in this version, and a write to an object one is TS8018 until stage 2 of change 0142. An entry’s parameter is the pipeline’s value, which no caller in the program holds. A write to a field or an element of it is TS8018 in this version, not supported yet: copy the value into a let and write that. The object of a method that changes its object, and a variable of the function around it that a local function writes, are not parameters an author writes: Rules 8.10 and 8.17 govern them, and each is passed by reference. A kernel function’s parameter of an array with no size is the caller’s storage (Rule 8.23).

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

Rationale

a struct, an instance of a class and an array are objects in JavaScript, and a function that writes its parameter changes the caller’s object there. The compiler reads which parameters a function writes, so the author writes no mark for it (OD-3, OD-5). A number is a value in JavaScript, so a function that changes one returns it. The contract is about identity and sharing only: which object a name refers to, and who sees a write to it, is never other than what JavaScript says. Where the compiler does not implement a form yet, it refuses the form with a text that says so (OD-9). The two references of Rules 8.10 and 8.17 are how the emitted function keeps TypeScript’s own meaning (a method writes the object it was called on, a closure writes the variable it closes over), and no author writes either one.

Derives from

change 0040 in changes/, which took the roadmap’s “pointers and reference parameters”; change 0142 in changes/; docs/owner-decisions.md, OD-5 and OD-9; Function Calls.

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:

TS8020 FUNCTION_SHAPE for a parameter shape the surface does not take, which since change 0093 includes an optional parameter x?: T on an entry point or on a kernel function and x?: T = d (the sentences are in Rule 7.10’s “Enforced by”, from src/compiler/ts/lower/function.ts), and no longer an optional parameter on any other function, which Rule 7.10 takes (change 0093). The reading of which object parameters a function writes is isWrittenObjectParameter in src/compiler/ts/object-reference/written.ts, and the reading of the six rules is src/compiler/ts/object-share.ts (rules 2, 4 and 5), src/compiler/ts/new-object.ts (rule 3), secondNameOf and copyOfObject in src/compiler/ts/lower/second-name.ts (rules 1 and 4), src/compiler/ts/written-types.ts with src/compiler/ts/lower/pending-shares.ts (rule 6) and reportSharedObjects in src/compiler/ts/lower/function.ts, pinned by src/compiler/ts/second-name.test.ts, which holds each rule and the three examples of Amendment A3 and the programs of Amendment A4 in both halves and on the CPU oracle, and by src/compiler/ts/object-reference.test.ts. Value rebinding, caller isolation, captures, class bodies, generic/higher-order copies and entry inputs are pinned by src/compiler/ts/mutable-parameters.test.ts; the immutable GPU input and distinct local by src/compiler/ts/tint-invalid.test.ts; the packed-package path by journeys/mutable-parameters/.

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:

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.

See also

Source

The rule at commit af54ddad5:

Edit this page Report a problem