Calls nobody wrote: implicitly called methods must not fail; an override must not ask for more #125

Merged
sionzee merged 90 commits from implicit-calls into main 2026-11-21 23:37:25 +01:00
Owner

Based on borrow-escapes (#122), the top of the chain (#119, #120, #121, #123, #116, #122); the diff shown is this piece alone. It does not depend on the Mut change and can be moved below #116 if a non-breaking release should carry it.

Two holes of one family: code runs that no call in the source names, so the rules that follow calls never see it. Both passed tsc strict, safe-ts check --deny-warnings and oxlint.

class Money {
  toString(): string {
    if (this.cents < 0) throw new Unreadable("negative");   // a tracked failure
    return String(this.cents);
  }
}
setTimeout(() => console.log(`${money}`), 1);     // the template calls toString: nothing was reported anywhere

class Shelter { admit(animal: Animal): string { return animal.kind; } }
class Kennel extends Shelter {
  override admit(dog: Dog): string { return dog.bark(); }   // tsc accepts a narrower parameter on a method
}
function use(shelter: Shelter) { return shelter.admit({ kind: "cat" }); }   // TypeError when it is a Kennel

implicit-propagation (error)

A method the language calls for you must not be able to fail. It is reported where the method is written, because the places that call it (String(x), a template, JSON.stringify, for...of, await) are everywhere and mostly untyped, so following them could never be complete, while the definition is one place.

Protocol Who calls it Before Now
toString, valueOf, [Symbol.toPrimitive], toLocaleString templates, String(x), +x, x < y, join unseen refused at the definition
toJSON JSON.stringify, at any depth unseen refused
[Symbol.iterator], [Symbol.asyncIterator], an iterator's next for...of, spread, destructuring, Array.from, for await unseen refused
[Symbol.dispose], [Symbol.asyncDispose] using at block end unseen refused
then on a non-promise await, Promise.resolve unseen refused
[Symbol.hasInstance] and the other well-known symbols, Proxy traps, an object literal's getters and setters operators and built-ins unseen refused
a class getter read by destructuring, a tagged template, a decorator the syntax unseen followed like a call
a class getter or setter, static blocks, field initialisers followed unchanged

What to write instead: handle the failure inside and return a value that says so, or offer a named method. For iteration that is a named generator (for (const row of table.rows())), which is followed where it is stepped, also through spread and Array.from.

override-parameter (error)

TypeScript compares a class method's parameters both ways against the method it overrides or implements, so an override may ask for more than its base promises. The rule reports exactly the cells tsc leaves unchecked (the table is on the rule's page): a narrower parameter, a required parameter where the base's is optional or a rest, a writable list where the base takes a readonly one; on base methods, abstract methods, statics, and an arrow property over a method. An earlier change (no-method-signature) closed the interface half; this is the class half.

With no type checker, "narrower" is not decidable in general, so each pair of written types gets one of three answers and only a proven "no" is reported. Proven fine: the same type after opening aliases, unknown, a union containing the base's type, an object type asking for a subset, readonly over the writable form, and a wider class. "Wider" is known for the program's classes and interfaces, and for the platform's from data: 406 class-to-parent pairs generated from the DOM library and checked in Chromium, Firefox and WebKit, the language's Error family, and Node's maps. So override on(event: Event) over a base taking MouseEvent is silent. A package's class nobody describes, under a different name, is unknown and silent.

Evidence

  • Held-out cases. Every implicit protocol is refused where written, including an object literal's toString that fails through a call and an inherited toJSON; a method that catches inside or handles a Result is silent. For overrides: generics bound through extends Box<Dog>, two levels of inheritance, statics, the arity shapes, and the wider forms in both directions.
  • An adopter's project: no findings from either rule, and nothing else changes. It has five implicitly called methods (an object literal's getter and four Proxy traps in tests); all were read and none can fail. None of its classes overrides a method of another of its own classes.
  • Soundness corpus: implicit-toPrimitive-throw is closed, and four classes were added and closed (iteration, toJSON, valueOf, the override). 2 of 58 escape: division by zero and one documented Mut hole.
  • Whole-program pass on that project's largest entry: no measurable change.

What turns red on upgrade

A destructured getter, a tagged template's tag and a decorator are now followed, so a failure that reaches a caller only through one of them is reported as uncaught-propagation or unhandled-failure where it was silent.

What stays unseen

  • A method assigned later, put on a prototype or made with defineProperty; one inherited from a package's class; a Proxy handler not written at the new Proxy call.
  • Failures that are not tracked errors inside such a method (the built-ins that throw are still reported inside it by unhandled-failure).
  • An override of a package's base class, and parameter types the opener stops at (conditional and mapped types).
  • An object literal assigned to a class type, and an unrelated class used where a class type is asked.

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.

Based on `borrow-escapes` (#122), the top of the chain (#119, #120, #121, #123, #116, #122); the diff shown is this piece alone. It does not depend on the `Mut` change and can be moved below #116 if a non-breaking release should carry it. Two holes of one family: code runs that no call in the source names, so the rules that follow calls never see it. Both passed `tsc` strict, `safe-ts check --deny-warnings` and oxlint. ```ts class Money { toString(): string { if (this.cents < 0) throw new Unreadable("negative"); // a tracked failure return String(this.cents); } } setTimeout(() => console.log(`${money}`), 1); // the template calls toString: nothing was reported anywhere class Shelter { admit(animal: Animal): string { return animal.kind; } } class Kennel extends Shelter { override admit(dog: Dog): string { return dog.bark(); } // tsc accepts a narrower parameter on a method } function use(shelter: Shelter) { return shelter.admit({ kind: "cat" }); } // TypeError when it is a Kennel ``` ## `implicit-propagation` (error) A method the language calls for you must not be able to fail. It is reported where the method is written, because the places that call it (`String(x)`, a template, `JSON.stringify`, `for...of`, `await`) are everywhere and mostly untyped, so following them could never be complete, while the definition is one place. | Protocol | Who calls it | Before | Now | |---|---|---|---| | `toString`, `valueOf`, `[Symbol.toPrimitive]`, `toLocaleString` | templates, `String(x)`, `+x`, `x < y`, `join` | unseen | refused at the definition | | `toJSON` | `JSON.stringify`, at any depth | unseen | refused | | `[Symbol.iterator]`, `[Symbol.asyncIterator]`, an iterator's `next` | `for...of`, spread, destructuring, `Array.from`, `for await` | unseen | refused | | `[Symbol.dispose]`, `[Symbol.asyncDispose]` | `using` at block end | unseen | refused | | `then` on a non-promise | `await`, `Promise.resolve` | unseen | refused | | `[Symbol.hasInstance]` and the other well-known symbols, `Proxy` traps, an object literal's getters and setters | operators and built-ins | unseen | refused | | a class getter read by destructuring, a tagged template, a decorator | the syntax | unseen | followed like a call | | a class getter or setter, static blocks, field initialisers | | followed | unchanged | What to write instead: handle the failure inside and return a value that says so, or offer a named method. For iteration that is a named generator (`for (const row of table.rows())`), which is followed where it is stepped, also through spread and `Array.from`. ## `override-parameter` (error) TypeScript compares a class method's parameters both ways against the method it overrides or implements, so an override may ask for more than its base promises. The rule reports exactly the cells `tsc` leaves unchecked (the table is on the rule's page): a narrower parameter, a required parameter where the base's is optional or a rest, a writable list where the base takes a `readonly` one; on base methods, abstract methods, statics, and an arrow property over a method. An earlier change (`no-method-signature`) closed the interface half; this is the class half. With no type checker, "narrower" is not decidable in general, so each pair of written types gets one of three answers and only a proven "no" is reported. Proven fine: the same type after opening aliases, `unknown`, a union containing the base's type, an object type asking for a subset, `readonly` over the writable form, and a wider class. "Wider" is known for the program's classes and interfaces, and for the platform's from data: 406 class-to-parent pairs generated from the DOM library and checked in Chromium, Firefox and WebKit, the language's `Error` family, and Node's maps. So `override on(event: Event)` over a base taking `MouseEvent` is silent. A package's class nobody describes, under a different name, is unknown and silent. ## Evidence - **Held-out cases.** Every implicit protocol is refused where written, including an object literal's `toString` that fails through a call and an inherited `toJSON`; a method that catches inside or handles a `Result` is silent. For overrides: generics bound through `extends Box<Dog>`, two levels of inheritance, statics, the arity shapes, and the wider forms in both directions. - **An adopter's project:** no findings from either rule, and nothing else changes. It has five implicitly called methods (an object literal's getter and four `Proxy` traps in tests); all were read and none can fail. None of its classes overrides a method of another of its own classes. - **Soundness corpus:** `implicit-toPrimitive-throw` is closed, and four classes were added and closed (iteration, `toJSON`, `valueOf`, the override). 2 of 58 escape: division by zero and one documented `Mut` hole. - **Whole-program pass** on that project's largest entry: no measurable change. ## What turns red on upgrade A destructured getter, a tagged template's tag and a decorator are now followed, so a failure that reaches a caller only through one of them is reported as `uncaught-propagation` or `unhandled-failure` where it was silent. ## What stays unseen - A method assigned later, put on a prototype or made with `defineProperty`; one inherited from a package's class; a `Proxy` handler not written at the `new Proxy` call. - Failures that are not tracked errors inside such a method (the built-ins that throw are still reported inside it by `unhandled-failure`). - An override of a package's base class, and parameter types the opener stops at (conditional and mapped types). - An object literal assigned to a class type, and an unrelated class used where a class type is asked. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open: code that no call in the source names ran unseen by the rules that follow calls. A
`toString`, `valueOf`, `[Symbol.toPrimitive]`, `toJSON`, `[Symbol.iterator]`, an iterator's
`next`, `[Symbol.dispose]`, a thenable's `then`, `[Symbol.hasInstance]`, a `Proxy` trap or an
object literal's getter could propagate a `PropagateError` that reached no caller that was told
(`implicit-toPrimitive-throw` in the soundness corpus). And a class method overriding a base
class's method is compared in both directions by TypeScript, so `override admit(dog: Dog)` over
`admit(animal: Animal)` compiled and crashed when called through the base.

Closes:
- `implicit-propagation` (error): such a method that can propagate is refused where it is
  defined, in a class or an object literal at any depth, also when the member names a function.
  Each one is made a followed function (`analyze/implicit.rs`) and the whole-program pass asks
  what it propagates (`project/implicit.rs`). Following the call sites was rejected: they are
  every template, `String(x)`, comparison and library, mostly untyped.
- What the source does name without parentheses is now followed like a call: a tagged
  template's tag, a decorator, a getter destructured off a value of a written-down class.
- `override-parameter` (error): an override (method or arrow property, instance or static, over
  a method or abstract method of a class of the program, found across files) whose parameter is
  written with another type after opening aliases, unless provably fine (same, `unknown`, a
  union holding the base's type, an object asking for a subset, a class the base's extends);
  required where optional; `T[]` where `readonly T[]`.
- Soundness corpus: `implicit-toPrimitive-throw` comes off escaping.txt; new classes
  `implicit-iterator-throw`, `implicit-tojson-throw`, `implicit-valueof-throw` and
  `class-override-param-bivariance`, all rejected now (all escaped before).

Rule pages, fixtures, README, CHANGELOG, guide, skill and oxlint preset follow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
override-parameter: a wider platform class is fine; a required parameter where the base's rest may be empty is not
All checks were successful
Benchmarks / bench (pull_request) Successful in 4m5s
Tests / test (pull_request) Successful in 9m29s
2024f9ef23
Open (review of the first commit): parameter classes that weren't the program's were compared by
name, so `override on(event: Event)` over `on(event: MouseEvent)` was reported though it is
wider; and a rest parameter wasn't compared at all, so `rest(first: string, ...parts)` over
`rest(...parts)` passed while `base.rest()` gives it nothing.

Closes:
- Each pair of types is answered yes / no / unknown, and only a no is reported. Classes are
  compared through what extends what: the program's, lib.dom's from a new `globals.parents` in
  the browser pack (generated by test/pack.ts for every class with a class as its base, checked
  on Node and in Chromium, Firefox and WebKit by test/browsers/parents.test.ts; three CSS
  classes whose base the engines disagree on are left out), the language's from a new `extends`
  in ecmascript-api.json (checked on Node, with no class of the map left out), Node's modules'
  from their API map. A package's class nobody describes, compared with another name, is unknown.
- What an interface extends and a part of an intersection are wider (an object type with a method
  member is read as an object that has at least that member); `Readonly<T>`, `readonly T[]`,
  `ReadonlyArray`, `ReadonlyMap` and `ReadonlySet` are their writable types, so a read-only view
  over the writable form is wider (the reverse stays reported).
- Parameters are compared by position, a rest parameter (a tuple's elements one by one, then its
  own rest) over the positions it covers: a required parameter where the base's rest may be
  given nothing is reported, also through a tuple-typed rest; a default removed was already.
- Fixtures for each (classes.ts, types.ts, arity.ts); the rule page's arity table; docs of the
  pack format; CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee changed target branch from borrow-escapes to main 2026-11-21 23:36:01 +01:00
Merge main
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m46s
Tests / test (pull_request) Successful in 8m23s
e623e7710e
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch implicit-calls 2026-11-21 23:37:25 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!125
No description provided.