Follow method calls through fields, declared types and returned values; new rule unbound-method #114

Merged
sionzee merged 9 commits from receivers-by-type into main 2026-11-21 23:22:05 +01:00
Owner

Stacked on #110, which is stacked on #109. Merge those first; I'll rebase this one as they land.

The failure rules followed a method call only when the receiver's class was visible right where the call was made: this, new C(), a module-level instance, or a local declared as exactly C. Every other receiver was not followed, and not followed meant silent. So the most ordinary shape of application code was invisible:

class Service {
  constructor(private readonly repo: Repo) {}
  run(id: string): string { return this.repo.load(id); }   // Repo.load can propagate NotFound: nothing reported, anywhere
}

Receivers whose class is written down

The checker has no type checker, but in all of these the class is in the source, one hop away, and each now resolves to the same method the direct form does:

Receiver Example
A field: annotated, a constructor parameter property, = new Repo(), assigned in the constructor, inherited this.repo.load()
A property of a value whose declared type names the class deps.repo.load() with deps: Deps
The value of a call with a declared return type, also awaited makeRepo().load(), (await open()).load()
A member chain into an object literal, at module level or in a readonly field api.users.load()
Getters and setters x.value, x.name = v

All four failure rules use it: a propagating call, a dropped Result and a floating rejection are each reported through a field as they are on a direct call.

New rule: unbound-method (error, with a quick fix)

el.addEventListener("keydown", this.count);   // `this` is undefined when it runs
done.then(this.count);
const { count } = this; count();

A method that uses this, taken off its object without being called or bound. This passes tsc and crashes at run time. Not reported: arrow-function fields, static methods, methods that never use this, .bind(...), () => this.count(), forEach(fn, thisArg), and a method the constructor rebinds. The fix appends .bind(this).

Also fixed

Following more receivers exposed four false floating-rejection findings from older logic on an adopter's code: a promise also stored in an object literal and awaited on every path, and a call returned from one branch of ?:. Both are now understood. A promise kept in a list, discarded in a ?:, or only compared is still reported.

What an adopter will notice

On an adopter's project the findings are identical before and after, compared finding by finding, from its entries (673) and from its directories (1,111). The checker does see more: 14 more functions are known to propagate. But the callers reach those methods through interfaces, or already catch the failures. Dynamic dispatch is the next PR, and that is where new findings are expected.

The whole-program pass on that project's largest app goes from 725 ms to 815 ms.

How it was checked

  • The Rust suite (330 tests), test:std, typecheck, the docs examples, test:vite, the docs site build and the soundness corpus pass.
  • Eight of nine receiver cases given to the implementation are reported (one was before), and ten more it had not seen: an inherited field, Repo | undefined through ?., destructuring from a typed field, a static factory, a setter, for...of over a typed array. A first version missed (await f()).m(), a field holding an object literal, and super.reset detached; all three are fixed.
  • Legitimate code stays clean: the same receivers calling methods that can't fail, failing ones inside a try, a parameter that shadows a field's name.
  • The soundness corpus drops one class (unbound-method-this).

Still not followed

  • A value typed as an interface or a union, and an override reached through a value declared as its base class. Those need declared contracts and are the next PR.
  • Generics (Box<Repo>), elements read by index, namespace member chains.
  • unbound-method ignores statement order: a method passed before the constructor line that binds it is not reported.

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

Stacked on #110, which is stacked on #109. Merge those first; I'll rebase this one as they land. The failure rules followed a method call only when the receiver's class was visible right where the call was made: `this`, `new C()`, a module-level instance, or a local declared as exactly `C`. Every other receiver was not followed, and not followed meant silent. So the most ordinary shape of application code was invisible: ```ts class Service { constructor(private readonly repo: Repo) {} run(id: string): string { return this.repo.load(id); } // Repo.load can propagate NotFound: nothing reported, anywhere } ``` ## Receivers whose class is written down The checker has no type checker, but in all of these the class is in the source, one hop away, and each now resolves to the same method the direct form does: | Receiver | Example | |---|---| | A field: annotated, a constructor parameter property, `= new Repo()`, assigned in the constructor, inherited | `this.repo.load()` | | A property of a value whose declared type names the class | `deps.repo.load()` with `deps: Deps` | | The value of a call with a declared return type, also awaited | `makeRepo().load()`, `(await open()).load()` | | A member chain into an object literal, at module level or in a `readonly` field | `api.users.load()` | | Getters and setters | `x.value`, `x.name = v` | All four failure rules use it: a propagating call, a dropped `Result` and a floating rejection are each reported through a field as they are on a direct call. ## New rule: `unbound-method` (error, with a quick fix) ```ts el.addEventListener("keydown", this.count); // `this` is undefined when it runs done.then(this.count); const { count } = this; count(); ``` A method that uses `this`, taken off its object without being called or bound. This passes `tsc` and crashes at run time. Not reported: arrow-function fields, static methods, methods that never use `this`, `.bind(...)`, `() => this.count()`, `forEach(fn, thisArg)`, and a method the constructor rebinds. The fix appends `.bind(this)`. ## Also fixed Following more receivers exposed four false `floating-rejection` findings from older logic on an adopter's code: a promise also stored in an object literal and awaited on every path, and a call returned from one branch of `?:`. Both are now understood. A promise kept in a list, discarded in a `?:`, or only compared is still reported. ## What an adopter will notice On an adopter's project the findings are identical before and after, compared finding by finding, from its entries (673) and from its directories (1,111). The checker does see more: 14 more functions are known to propagate. But the callers reach those methods through interfaces, or already catch the failures. Dynamic dispatch is the next PR, and that is where new findings are expected. The whole-program pass on that project's largest app goes from 725 ms to 815 ms. ## How it was checked - The Rust suite (330 tests), `test:std`, `typecheck`, the docs examples, `test:vite`, the docs site build and the soundness corpus pass. - Eight of nine receiver cases given to the implementation are reported (one was before), and ten more it had not seen: an inherited field, `Repo | undefined` through `?.`, destructuring from a typed field, a static factory, a setter, `for...of` over a typed array. A first version missed `(await f()).m()`, a field holding an object literal, and `super.reset` detached; all three are fixed. - Legitimate code stays clean: the same receivers calling methods that can't fail, failing ones inside a `try`, a parameter that shadows a field's name. - The soundness corpus drops one class (`unbound-method-this`). ## Still not followed - A value typed as an interface or a union, and an override reached through a value declared as its base class. Those need declared contracts and are the next PR. - Generics (`Box<Repo>`), elements read by index, namespace member chains. - `unbound-method` ignores statement order: a method passed before the constructor line that binds it is not reported. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
The failure rules followed a method call only when the receiver's class was
visible at the call: `this`, `super`, `new C()`, a module-level instance, or a
binding declared as exactly `C`. Every other receiver was silent, so the most
ordinary shape of application code, a service holding its dependencies in
fields (`this.repo.load(id)`), propagated nothing anywhere.

A receiver is now written down as a value (analyze/receivers.rs) and a call on
it as `Callee::Of`; the whole-program pass (project/receivers.rs) follows the
declarations, across files, to the method `new Repo().load()` runs:

- a field declared with the class: an annotation, `= new Repo()`, a constructor
  parameter property, `this.repo = new Repo()` in the constructor; inherited;
- a property of a declared type (`deps.repo.load()` with `deps: Deps`, an
  alias, an interface, an object type, `Repo | undefined`), also destructured
  (`const { repo } = deps`, `function run({ repo }: Deps)`), and `const` copies
  (`const repo = this.repo`, `const self = this`);
- a function or method declared to return the class (`makeRepo().load()`, `T`
  of an async one's `Promise<T>` once awaited), and chains of these;
- a member chain into a module-level object literal (`api.users.load()`);
- getters and setters: `x.value` runs `get value()`, `x.value = v` runs
  `set value(v)`, keyed `C#get value` / `C#set value`.

Not followed: a receiver typed as an interface or a union of several types, an
override through a value declared as the base class (left to the declared
contracts of the next change), generic types, array and Map elements.

Two causes of false floating-rejection findings that the newly known
rejections exposed are fixed: a promise held in a binding and also stored in
an object literal (`{ promise }`) or compared (`p === q`) still settles where
every path awaits it, and `return c ? f() : undefined` (also `??`, `||`)
returns `f()`'s promise.

Following more receivers adds call sites; the failure and propagation passes
now take a function's calls from an index (`calls_of`) instead of scanning
the file's, and calls by span from another (`call_at`). Receivers are interned
per file and resolved once each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`queueMicrotask(this.tick)`, `items.forEach(this.tick)`, `return this.tick`,
`const f = ticker.tick; f()` and `const { tick } = ticker; tick()` run the
method with `this` undefined, and the first `this.x` throws TypeError. tsc,
oxlint's preset and `safe-ts check` all passed them (the soundness corpus
counted the class `unbound-method-this` as escaping).

The places the by-name machinery already visits as a function used as a value
(an argument, a return, an object or array literal, an assignment, a field's
value, a JSX attribute), and a `const` copy or a destructuring, now record the
method they name, through the same receivers the failure rules follow. The
whole-program pass reports it (an error) when that method is an instance
method of a module-level class whose body uses `this` or `super`: not an
arrow-function field, a `static` method, a method that doesn't use `this`, a
decorated one, one the constructor binds, `this.tick.bind(this)`,
`() => this.tick()`, or `items.forEach(this.tick, this)`. The quick fix binds
it (`.bind(<object>)`) where the object is `this`, `super` or a plain name;
the meaning test runs it.

`unbound-method-this` no longer escapes and comes out of escaping.txt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Receivers: an awaited call in the same expression, a readonly field holding an object literal; unbound-method through super
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m59s
Tests / test (pull_request) Successful in 8m38s
efe87d7550
Three receivers the previous two commits left silent:

- `(await makeLater()).load("")` wasn't followed, while
  `const repo = await makeLater(); repo.load("")` was. The awaited call is now
  the same value as the local holding it (`Callee::Returned` from
  `member_target`, `Value::Returned` from `value_of`).
- `private readonly api = { load(id) { ... } }` and then `this.api.load()`
  wasn't followed, while a module-level object literal and a field
  `= new Repo()` were. A `readonly` field's object literal is now keyed like a
  module-level one (`Holder#api.load`) and the field resolves to it.
- `setTimeout(super.reset, 5)` was silent when the parent's constructor sets
  `this.reset = this.reset.bind(this)`: the exemption removed the method from
  the ones that use `this`. That assignment makes an own property of the
  instance; `super.reset` reads the prototype's method, still unbound. The
  exemption now applies only to a read through the instance (the constructor
  of the method's class, or of the object's class or one between).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed receivers-by-type from efe87d7550
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m59s
Tests / test (pull_request) Successful in 8m38s
to 2f8e2d1626
All checks were successful
Tests / test (pull_request) Successful in 8m52s
Benchmarks / bench (pull_request) Successful in 3m12s
2026-11-21 06:51:27 +01:00
Compare
sionzee changed target branch from fail-closed-calls to main 2026-11-21 23:20:24 +01:00
Merge main
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m43s
Tests / test (pull_request) Successful in 7m59s
d089c59944
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch receivers-by-type 2026-11-21 23:22:05 +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!114
No description provided.