Built-ins that throw: a complete, tested map of the ECMAScript built-ins, and unhandled-failure where the failing condition can arise #117

Merged
sionzee merged 31 commits from ecmascript-builtins into main 2026-11-21 23:25:56 +01:00
Owner

Based on fail-closed-integration, which is main plus the open PRs #109 to #114 merged together. Once those are in, I'll rebase this onto main and retarget it. It is independent of #115 and #116.

For Node's built-ins and the browser's APIs the project has complete maps: every callable, classified by how it fails, with a test that makes each failing one fail. For the language's own built-ins there was a short hand-written list. So these passed tsc strict, safe-ts check and oxlint, and threw at run time:

prices.reduce((a, b) => a + b)              // TypeError on an empty list
new Date(input).toISOString()               // RangeError: Invalid time value
" ".repeat(width - text.length)             // RangeError when negative
n.toFixed(digits)                           // RangeError outside 0..100
total / parts                               // bigint: RangeError when parts is 0n

What changes

A map of the ECMAScript built-ins (crates/safe_ts_core/ecmascript/ecmascript-api.json): 1,110 callables taken from TypeScript's lib files, in the same format and categories as the Node and browser maps. 290 can throw. A test makes every one of the 290 fail on the running Node and checks the error's class. The hand list is gone; the checker reads the map.

unhandled-failure reports a built-in when the failing condition can arise, with the condition and what to write in the message:

  • The receiver's state: reduce without an initial value on a list not known to have an element; toISOString and its relatives on a date made from text, also one step away (new Date(Date.parse(text)), or a project function that returns such a date).
  • An argument that comes from data: a parameter, something read off one, or parsed text. An argument the code wrote, checked just before, or computed from what the function holds is not reported: n.toFixed(2) cannot fail, n.toFixed(digits) can.
  • A bigint operator: / and % by a divisor that may be 0n, ** with an exponent that may be negative.

A literal that cannot fail is no longer reported, which fixes BigInt(1). A literal the checker can read and knows is wrong is: toFixed(101), a time zone that doesn't exist.

The rule was narrowed by what a real project showed

The first version added about 260 findings on an adopter's project. The implementer read about 125 of them and none was a real way to throw: typed-array sizes computed from dimensions, dates from the clock, DataView cursors checked in helpers. Those kinds are mapped and documented but not reported:

  • typed-array and buffer lengths, and typedArray.set;
  • a date made from a number, and a Date that arrives as a parameter or field;
  • a DataView kept in a field, or over a buffer whose length the function reads;
  • arguments the function computes itself.

After narrowing, the adopter gains 4 findings where the built-in is called (8 with their callers), compared finding by finding against the base, and nothing is removed:

  • reduce without an initial value, twice: once on a constant list the checker can't see the length of, once in a test;
  • Array(size) with size a parameter;
  • repeat(max) in a test helper.

All four are in range today for reasons the checker can't see, and all four follow the stated rule. The whole-program pass time is unchanged (860 ms before, 849 ms after).

How it was checked

  • The Rust suite (338 tests), test:std (386, including the map's own test), typecheck, the docs examples, test:vite, the docs site build and the soundness corpus pass.
  • Twelve lines given to the implementation are reported, and fifteen more it had not seen (a locale and a time zone read off a config parameter, BigInt(text), repeat of parsed text, with(at, ...), a date through a helper function in three forms). Eighteen functions that cannot fail stay clean.
  • The soundness corpus drops seven classes on this branch: 14 of 31 escape.

Not done

  • Date.parse(text) used as a number is the wrong-value half and belongs to the sentinel-values change.
  • safe-ts coverage does not list this map yet.
  • The Node and browser maps' "fails only on wrong arguments" entries are not yet reported under the same argument rule.

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

Based on `fail-closed-integration`, which is `main` plus the open PRs #109 to #114 merged together. Once those are in, I'll rebase this onto `main` and retarget it. It is independent of #115 and #116. For Node's built-ins and the browser's APIs the project has complete maps: every callable, classified by how it fails, with a test that makes each failing one fail. For the language's own built-ins there was a short hand-written list. So these passed `tsc` strict, `safe-ts check` and oxlint, and threw at run time: ```ts prices.reduce((a, b) => a + b) // TypeError on an empty list new Date(input).toISOString() // RangeError: Invalid time value " ".repeat(width - text.length) // RangeError when negative n.toFixed(digits) // RangeError outside 0..100 total / parts // bigint: RangeError when parts is 0n ``` ## What changes **A map of the ECMAScript built-ins** (`crates/safe_ts_core/ecmascript/ecmascript-api.json`): 1,110 callables taken from TypeScript's lib files, in the same format and categories as the Node and browser maps. 290 can throw. A test makes every one of the 290 fail on the running Node and checks the error's class. The hand list is gone; the checker reads the map. **`unhandled-failure` reports a built-in when the failing condition can arise**, with the condition and what to write in the message: - *The receiver's state*: `reduce` without an initial value on a list not known to have an element; `toISOString` and its relatives on a date made from text, also one step away (`new Date(Date.parse(text))`, or a project function that returns such a date). - *An argument that comes from data*: a parameter, something read off one, or parsed text. An argument the code wrote, checked just before, or computed from what the function holds is not reported: `n.toFixed(2)` cannot fail, `n.toFixed(digits)` can. - *A bigint operator*: `/` and `%` by a divisor that may be `0n`, `**` with an exponent that may be negative. A literal that cannot fail is no longer reported, which fixes `BigInt(1)`. A literal the checker can read and knows is wrong is: `toFixed(101)`, a time zone that doesn't exist. ## The rule was narrowed by what a real project showed The first version added about 260 findings on an adopter's project. The implementer read about 125 of them and none was a real way to throw: typed-array sizes computed from dimensions, dates from the clock, `DataView` cursors checked in helpers. Those kinds are mapped and documented but not reported: - typed-array and buffer lengths, and `typedArray.set`; - a date made from a number, and a `Date` that arrives as a parameter or field; - a `DataView` kept in a field, or over a buffer whose length the function reads; - arguments the function computes itself. After narrowing, the adopter gains 4 findings where the built-in is called (8 with their callers), compared finding by finding against the base, and nothing is removed: - `reduce` without an initial value, twice: once on a constant list the checker can't see the length of, once in a test; - `Array(size)` with `size` a parameter; - `repeat(max)` in a test helper. All four are in range today for reasons the checker can't see, and all four follow the stated rule. The whole-program pass time is unchanged (860 ms before, 849 ms after). ## How it was checked - The Rust suite (338 tests), `test:std` (386, including the map's own test), `typecheck`, the docs examples, `test:vite`, the docs site build and the soundness corpus pass. - Twelve lines given to the implementation are reported, and fifteen more it had not seen (a locale and a time zone read off a config parameter, `BigInt(text)`, `repeat` of parsed text, `with(at, ...)`, a date through a helper function in three forms). Eighteen functions that cannot fail stay clean. - The soundness corpus drops seven classes on this branch: 14 of 31 escape. ## Not done - `Date.parse(text)` used as a number is the wrong-value half and belongs to the sentinel-values change. - `safe-ts coverage` does not list this map yet. - The Node and browser maps' "fails only on wrong arguments" entries are not yet reported under the same argument rule. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Open: for Node's built-ins and the browser's APIs the project had complete maps, each failing
member made to fail by a test; for the language's own built-ins there was only a short list
inside the checker, so `reduce` on an empty list, `toISOString` of an invalid date, `toFixed`,
`repeat`, the `Intl` constructors, `DataView` reads and bigint `/` were known to nothing.

Closes: crates/safe_ts_core/ecmascript/ecmascript-api.json maps all 1,110 callables of the libs
lib.esnext.d.ts references (Intl, the typed arrays, DataView, Atomics, Temporal included) in
the Node and browser maps' categories: 290 throws, 224 propagates, 12 returns, 45 args, 539
never (`limits` lists the members whose only failure is a result past the engine's limits).
Each failing member has its error in the packs' notation and its condition; `checks` says what
unhandled-failure looks at before it reports one; `operators` lists bigint `/`, `%`, `**`;
`platform` the web globals both runtimes have (atob, btoa, structuredClone, new URL); `values`
the time zones and units an Intl option takes. inventory.ts enumerates the lib, triggers.ts makes
every failing member fail, and errors.test.ts (now part of `npm run test:std`) checks the map
against the installed TypeScript and the running Node: 290 of 290 throwing members and the four
web globals fail with the class their text names, none is written from the spec alone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Open: `prices.reduce((a, b) => a + b)`, `new Date(input).toISOString()`, `" ".repeat(width -
text.length)`, `n.toFixed(digits)`, `n.toString(radix)`, `toLocaleString` with a time zone from
data, `new Array(count)`, `String.fromCodePoint(code)`, `text.normalize(form)`, a DataView read
past a buffer it was given, and bigint `/` by `0n` passed every gate and threw at run time; and
`BigInt(1)` was reported though a literal integer can't fail.

Closes: the hand list in analyze.rs is gone; a call of a member the map's `checks` list is
reported unless it is shown unable to fail (crate builtins.rs, analyze/builtins.rs):
- the receiver's class comes from its shape (a declared type here or in another file, through
  `Want::NotBuiltin`, a literal, `new X()`); a receiver of no known class isn't reported;
- the receiver's state: `reduce` with an initial value, or on a list bounds.rs proves non-empty
  (also a tuple type, a non-empty literal and what `.map` makes of one); `toISOString` of a date
  parsed from text (also through a field declared `string` elsewhere) unless checked with
  `Number.isNaN(date.getTime())`; a DataView made over a buffer the function was given whose
  length nothing reads;
- an argument: fine when written in the code (literal, const, literal objects), checked against
  the range just before (bounds.rs now answers what an argument is known to be, also for a value
  that isn't an integer, and whether `NaN` is excluded), or computed by the function itself; one
  from data (a parameter, something read off one or off `this`, Number(text), JSON.parse) is
  reported, as is a literal known to be wrong (`toFixed(101)`, a malformed tag, a time zone that
  doesn't exist);
- bigint `/`, `%` (also `/=`, `%=`) by a divisor not known to be non-zero, `**` by an exponent
  not known to be non-negative.
Messages name the condition and what to write (an initial value, a check of the date, padStart
for padding, a check of the argument or divisor, the formatter made once in unsafe(...)).
Narrowed after reading an adopter's findings, where each was in range for a reason the checker
can't see: typed-array and buffer lengths, `ta.set(src, offset)`, dates made from numbers or out
of sight, DataViews in fields or over checked buffers, arguments the function computes itself.
The soundness corpus drops seven classes. Fixtures: builtins/ (new); loops/main.ts:15 and
unbounded_spread/main.ts:20 now also expect unhandled-failure (`reduce` without an initial value
on a parameter, `String.fromCodePoint(...points)` of data). Docs: guide/builtins.md (new), the
rule page, libraries.md, README, CHANGELOG, the migration skill.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Open: the checks that ask for an argument written in the code (Temporal, fromBase64,
DisplayNames#of) refused the arguments left out after it, so a literal call was reported.
Closes: those arguments may be left out; the builtins fixture covers a Temporal call from text
and one written out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A date one step from text: a number parsed from it, and what a project function returns
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m57s
Tests / test (pull_request) Successful in 8m28s
fc1c46ac18
Open: `new Date(text).toISOString()` was reported, but `new Date(Date.parse(text))` (also
`Number(text)`, `parseInt`, `+text`, a const holding one) was not, and neither was
`parseStamp(text).toISOString()` with `parseStamp` returning `new Date(text)`, in the same file
or another: the first rewrite an agent would make, throwing the same RangeError.

Closes: a date made from a number parsed from text is reported as one parsed from text, unless
that number was checked with `Number.isNaN` / `Number.isFinite` first. Each function's summary
now records whether it returns such a date unchecked (`FnInfo::invalid_date`), or another
function's date (`dates_from`); a caller's `toISOString()` / `toTemporalInstant()` /
`formatter.format(date)` of what it returns is reported locally and kept by the whole-program
pass only if the callee (followed across files and through functions returning another's date)
returns one, the message naming where the date is made. A check in the function before it
returns, or in the caller before the use, clears it. A Date parameter or field stays unreported.
No change on the adopter or the examples. Fixture: builtins/dates.ts and the new cases in
builtins/main.ts. Docs: guide/builtins.md, the rule page, CHANGELOG.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee changed target branch from fail-closed-integration to failure-contracts 2026-11-21 12:22:35 +01:00
Merge failure-contracts: build on it
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m22s
Tests / test (pull_request) Successful in 7m19s
fdac858d40
# Conflicts:
#	CHANGELOG.md
#	crates/safe_ts_core/src/analyze.rs
Merge failure-contracts: one finding for a declared handler slot
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m53s
Tests / test (pull_request) Successful in 7m59s
0b85edd506
sionzee changed target branch from failure-contracts to main 2026-11-21 23:24:46 +01:00
Merge main
Some checks failed
Tests / test (pull_request) Failing after 6s
Benchmarks / bench (pull_request) Successful in 3m14s
a8ba6bcfaa
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch ecmascript-builtins 2026-11-21 23:25:56 +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!117
No description provided.