Skip to content

Missing values and forgotten awaits: close holes tsc catches and inty didn't - #110

Merged
sinelaw merged 7 commits into
mainfrom
claude/vibrant-maxwell-w72c19
Oct 21, 2026
Merged

sinelaw merged 7 commits into
mainfrom
claude/vibrant-maxwell-w72c19

Conversation

@sinelaw

@sinelaw sinelaw commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes holes where inty accepted programs that crash or misbehave at runtime, found by comparing inty against tsc --checkJs --strict on the same programs. Each one was a case where tsc reported an error and inty didn't. One of them (always-truthy conditions) is a new typing rule, added to the Lean model with all its proofs. The playground also gets the cases where inty catches what strict, annotated tsc misses.

Standard library: lookups and DOM reads that may find nothing

These now return the same T | Undefined / T | Null that find, at, match and localStorage.getItem already return. No new type-system machinery.

before after
Map.get, WeakMap.get V V | Undefined
pop(), shift() T T | Undefined
RegExp.prototype.exec String[] String[] | Null
getElementById, querySelector, closest element element | Null
parentElement, parentNode, first/lastElementChild, next/previousElementSibling, an input's form, document.activeElement element element | Null
getAttribute, localStorage.key(n), URLSearchParams / url.searchParams / FormData get String String | Null
getContext (an unsupported context id) context context | Null

body, head and documentElement stay non-null: a loaded page has them.

const prices = new Map([["apple", 1.5]]);
prices.get("pear").toFixed(2);   // now: the value may be undefined (was accepted, TypeError at runtime)

Testable: an always-truthy test is a type error, in the Lean model and in inty

A function is always truthy, so testing one gives the same outcome whatever it would return: the call was forgotten.

function isReady() { return true; }
if (isReady) { … }   // Type '() => Boolean' is not an instance of Testable (a function is always truthy, …: call it)

A value tested for truthiness must be Testable: any type but a function's, and but a rigid type variable, which may stand for one. The tests are if, while, do, for, ?:, !, and the left of && / ||. Like Plus, it waits while the type is a variable, so a function that tests its parameter carries it in its type:

function pick(x) { return x ? 1 : 2; }   // <a> where Testable a => (a) => Number
pick(0);                                  // Number
pick(isReady);                            // error

This is required for the model's typing to stay closed under substitution (HasTypeS.subst), which let-polymorphism relies on. A function that may be missing says so ((() => T) | Undefined) and is tested as usual.

  • Lean model:
    • The class Cls.testable, and its instance Inst.testable, mirroring NotNew.
    • A premise on cond, while_ and ! (&& and || lower to cond).
    • Inference poses the constraint at each test.
    • Every proof is updated: soundness, substitution, rigid instantiation, label weakening, the wide system, inference soundness, completeness and principality. lake build passes with no sorry.
  • inty:
    • ClassName::Testable, posed on a type variable and decided on a known type (at the test, at generalisation, or when the pending constraint resolves).
    • Printed and parsed as Testable a in inty declarations and annotations.
    • A caught exception is tested like the model's unknown.
    • A call signature whose presence isn't decided yet (a spread of an operand not known yet) waits.
  • Beyond the model: a promise isn't Testable either (a forgotten await; the model has no promises), and &&= / ||= test their target.
  • The earlier commits' warnings and their "unless the guarded code calls it" exemption are gone. That exemption is syntactic, and the model's rule is purely type-directed.

A function declaration's type parameters are rigid in its body

/** function f<T>(x: T) => T */ didn't check the body against T. The type parameters were parsed as ordinary unification variables, which the body then filled in. A body returning 1 gave f the type (1) => 1; one computing x + x gave <a> where Plus a => (a) => a. Neither is the declared type.

The const form (/** const f: <T>(T) => T */, the model's letPoly) already checked its initialiser with the parameters rigid. A function declaration now does the same:

  • Hoisting binds the name to the annotation's scheme, so peers and recursive calls see it at any instance.
  • The body is checked with the type parameters rigid (check_function_annotation), with the same escape check as the const form (now shared, reject_escaped_skolems).
  • Generalisation leaves the scheme as it is.
  • Hovers and inlay hints inside the body show the annotation's type parameters, not the internal rigid variables.
  • A reassigned function, or an annotation with a where clause, keeps the old flexible reading.
/** function f<T>(x: T) => T */ function f(x) { return 1; }    // now an error
/** function first<T>(xs: T[]) => T? */                          // usable at every T

Rest parameters are rejected

...xs was parsed as one ordinary parameter. So function f(...xs) { return xs; } f(1).toFixed() was accepted with xs: Number, while JavaScript binds [1]. The existing test also accepted f(1, [2, 3]) with rest typed Number[], but at runtime rest is [[2, 3]].

Typing the binding as T[] isn't sound either. A function with fewer parameters stands in for one with more (the extra arguments are ignored), and a rest parameter collects exactly those extras: [1, 2].forEach((...xs) => …) gets [elem, index, array]. inty has no variadic calls, so a rest parameter is now an error, as setters are, pointing to an array parameter instead.

Playground: what strict, annotated tsc misses

The playground's "TS wouldn't catch this" section gets eight cases, each a bug Node shows at runtime that tsc --checkJs --strict accepts with full JSDoc types:

  • array covariance
  • method parameter bivariance
  • a narrowing a call undoes
  • a dictionary read at a missing key
  • spreading an optional undefined over a default
  • a misspelled field passed through a variable
  • a switch that forgets a union case (a warning)
  • a function tested instead of called, through a generic function (Testable)

Fallout

  • examples/spa/app.js reads its elements through one byId helper that throws on a missing id.
  • examples/go-backend/dijkstra*.js and crates/inty-go/tests/programs/objects.js read the last element by index before pop(), since the Go backend has no ??.
  • Rosaclef regression tests:
    • m.get("a") + m.size now uses ?? 0.
    • The canvas tests check getContext("2d") before use, and the detached-method err cases assert they fail because the method is detached.
    • Two tests that wrote <T> for a parameter whose shape the body relies on (raw.c) now spell out the shape. The generic decoder in docs/patterns.md (function decode<T>(text: String) => T) still checks.
  • a_record_field_holds_a_callback now calls its @type placeholder field instead of testing it.
  • tests/model_corpus/programs.txt: the removed rest-parameter unit test's record is dropped. That's the only change; a full regeneration would also pull in unrelated property-test programs.
  • lean/Inty/Wire.lean's guard on (not (func …)) now expects a reject.
  • Docs: lean/README.md, lean/COVERAGE.md, docs/type-system.md, examples/spa/gaps.md.

Test plan

  • cd lean && lake build: every proof checks, no sorry; the built model rejects testing a function and accepts a generic pick(5) but not pick(function () {…})
  • cargo test --workspace --release --no-fail-fast with INTY_REQUIRE_MODEL=1: all pass, including differential (inty vs. the Lean model vs. Node), model_corpus, lean_coverage, web_examples, the LSP inlay-hint tests, and the Go backend's Go-vs-Node output comparison
  • bash web/build.sh builds the playground with the new examples
  • cargo fmt --all -- --check; clippy shows only the pre-existing subst.rs warning
  • Regression tests lookups_that_may_find_nothing, dom_reads_that_may_find_nothing, a_promise_is_not_a_condition, a_function_is_not_a_condition, a_function_annotations_type_parameters_are_rigid and rest_parameter_is_rejected: every err case was checked to fail for the intended reason
  • Every tracked .js file checked before and after the whole PR: only examples/spa/app.js (fixed) and crates/inty/tests/microbench.js (a QuickJS benchmark that already had 54 errors; +1 real one) changed. Neither Testable nor rigid annotations add a diagnostic to any of them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f

Close holes where the standard library promised a value that JavaScript
may not give, using the same `T | Undefined` / `T | Null` that `find`,
`at`, `match` and `localStorage.getItem` already return:

- `Map` and `WeakMap` `get`: `V | Undefined`
- `pop()` / `shift()`: the element type `| Undefined` (an empty array)
- `RegExp.prototype.exec`: `String[] | Null`, as `String.prototype.match`
- `document.getElementById`, `querySelector`, `closest`: `| Null`
- `URLSearchParams`, `url.searchParams` and `FormData` `get`: `String | Null`

A rest parameter (`...xs`) was parsed as one ordinary parameter, so
`f(1)` typed `xs` as `1` while JavaScript binds `[1]`, and
`f(1, [2, 3])` was accepted with `xs` at run time `[[2, 3]]`. Typing it
as an array is not sound either: a function with fewer parameters stands
for one with more, the extra arguments ignored, and a rest parameter
collects exactly those. inty has no variadic calls, so a rest parameter
is now rejected, as setters are, with a pointer to taking an array.

Fallout: the SPA example checks its element lookups through one `byId`
helper; the Go backend's programs read the last element by index before
`pop()` (the backend has no `??`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
The DOM's links between elements and its attribute reads return `null`
when there is nothing there, and now say so (`T | Null`):
`parentElement`, `parentNode`, `first`/`lastElementChild`,
`next`/`previousElementSibling`, `getAttribute`, an input's `form`,
`getContext` (an unsupported context id), `localStorage.key(n)` and
`document.activeElement`. `body`, `head` and `documentElement` stay
non-null: a loaded page has them.

A promise is always truthy, so a promise tested as a condition (`if`,
`while`, `do`, `for`, `?:`), negated with `!`, or on the left of `&&` /
`||` is a forgotten `await`: the outcome doesn't depend on what it
resolves to. That is now an error saying to `await` it. A promise whose
type isn't known yet, or a union with nothing (`Promise<T> | Undefined`),
is still accepted.

The canvas regression tests check `getContext("2d")` before use; the
detached-method cases now also assert they fail for that reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
@sinelaw sinelaw changed the title Lookups that may find nothing say so; rest parameters are rejected Missing values and forgotten awaits: close holes tsc catches and inty didn't Oct 20, 2026
A function is always truthy, so testing one (`if (isReady)`,
`isReady ? … : …`, `isReady && …`, a loop's test) gives the same
outcome whatever it would return: the call was forgotten. That is now
an error, unless the code the test guards calls it, as TypeScript
allows: `if (hooks.mark) hooks.mark()` and `cb && cb()` test a function
that may be missing at run time (a JSDoc `@type` placeholder initialised
with `null`, a host feature). `!f` and `f || …` aren't checked: they are
the missing branch of that same guard. A value that may also be nothing
(`(() => T) | Undefined`) is tested as usual.

The guarded code's calls are collected by the free-identifier walker as
dotted paths (`f`, `o.m`, `o.m?.()`), inside nested functions too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
The Lean workflow's differential test failed with 98 disagreements:
generated programs that test a function (`f && …`, `f ? … : …`) are
well-typed in the model, which inty's typing must agree with, and inty
rejected them. A promise or function tested as a condition is
suspicious, not ill-typed (the program doesn't get stuck), so it is now
a warning, as a non-exhaustive `switch` is, rather than a type error.
The same programs are reported; typing agrees with the model again.

The model corpus still held the rest-parameter unit test's program,
which no longer parses; that one record is dropped. (Regenerating the
whole corpus would also pull in unrelated property-test programs.)

Verified locally with the model built (`lake build`) and
INTY_REQUIRE_MODEL=1: the differential test reproduced the 98
disagreements before this change and passes after; the corpus test
passes; the whole workspace passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
…d in inty

A value tested for truthiness (`if`, `while`, `?:`, `!`, the left of
`&&` / `||`) must be `Testable`: any type but a function's, which is
always truthy, so a test of one always comes out the same (a forgotten
call), and but a rigid type variable, which may stand for one.

The Lean model gets the class `Cls.testable` with its instance
(`Inst.testable`, as `NotNew`'s), a premise on `cond`, `while_` and `!`
(`&&` and `||` reach the model as `cond`), and inference posing the
constraint at each test. It waits while its type is a variable, so a
function that tests its parameter carries it in its scheme (as `Plus`),
which keeps typing closed under substitution and inference principal.
Every proof is updated: soundness, substitution, rigid instantiation,
label weakening, the wide system, inference soundness, completeness and
principality; `lake build` passes with no `sorry`.

inty gets the same class, `ClassName::Testable`: posed on a variable,
decided on a known type (at the test, at generalisation, or when the
pending constraint resolves), printed and parsed as `Testable a`.
`function pick(x) { return x ? 1 : 2; }` is now
`<a> where Testable a => (a) => Number`, and `pick(isReady)` with a
function is an error. The previous commit's warnings are now these errors,
and the guarded-call exemption (`if (f) f()`) is dropped: a function that
may be missing says so in its type (`(() => T) | Undefined`). Beyond the
model, a promise is not `Testable` either (a forgotten `await`), and
`&&=` / `||=` test their target. A caught exception may be anything and
is tested as the model's `unknown`; a call signature whose presence isn't
decided yet (a spread of an operand not known yet) waits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
`/** function f<T>(x: T) => T */` didn't check the body against `T`: the
annotation's type parameters were parsed as flexible variables, which
the body then instantiated. A body returning `1` gave `f` the type
`(1) => 1`, and one computing `x + x` the type `<a> where Plus a =>
(a) => a`, neither of which is what the annotation declares. The `const`
form (`/** const f: <T>(T) => T */`) already checked its initialiser with
the parameters rigid (`check_polymorphic_annotation`, the model's
`letPoly`).

A function declaration's annotation with type parameters and no `where`
clause is now its scheme, as a `const`'s is: hoisting binds the name to
it, so peers and the body itself see it at any instance; the body is
checked against it with the type parameters rigid
(`check_function_annotation`), and no skolem may escape into the context
or a constraint left (the check now shared with the `const` form,
`reject_escaped_skolems`); generalisation leaves it as it is. Hovers and
inlay hints inside the body show the annotation's type parameters, not
the skolems. A function the program reassigns, or an annotation with a
`where` clause, keeps the flexible reading.

Two Rosaclef regression tests wrote `<T>` for a parameter whose shape
the body relies on; they now say the shape. A generic decoder,
`function decode<T>(text: String) => T { return JSON.parse(text); }`
(docs/patterns.md), still checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
The "TS wouldn't catch this" section gets the cases from comparing inty
with `tsc --checkJs --strict` on fully annotated programs, each a bug
Node shows at runtime: array covariance, method bivariance, a narrowing
a call undoes, a dictionary read at a missing key, a spread of an
optional `undefined` over a default, a misspelled field passed through
a variable, a switch that forgets a union case (a warning), and a
function tested instead of called after passing through a generic
function (`Testable`). Each file says what tsc does with JSDoc types and
what happens at runtime; `web_examples` checks each against its
manifest expectation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VPxGAuMVv84djuDWNLW92f
@sinelaw
sinelaw merged commit 7a738b3 into main Oct 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants