compare-misses-undefined and strict-truthiness follow narrowed paths (and undo them where code may change the path) #153

Merged
sionzee merged 2 commits from narrowed-paths into main 2026-11-24 11:39:12 +01:00
Owner

compare-misses-undefined and strict-truthiness reported a value as "may be undefined" when TypeScript had already narrowed it. An adopter's tree had 5 such false positives out of 8 compare-misses-undefined errors. The forms were:

  • a member of a narrowed path: b.plots.length inside if (b.plots !== undefined)
  • a let narrowed by route && route.length === 0
  • a local copied from a narrowed path: const books = b.books
  • a path assigned just before: b.books = kept.filter(f); if (b.books.length === 0)
  • a path whose longer form was tested: if (!b.plots?.some(f)) return;, then b.plots.filter(...)

What changed

  • narrowed.rs (new): the guard walk that truthiness.rs had (if / ?: / && / || / an earlier if (…) return;), plus assignment narrowing. Paths are compared by root and steps (flow::Root), not by text. A test of a longer path counts for its prefixes. The nearest narrowing decides.
  • Invalidation (new; before, a narrowing was never undone): a narrowing is void after any of these between the test and the read, counting all of a loop around the read that starts after the test:
    • a write to the path, to a part of it, or to a name in its keys
    • a method call on a part of it, or a call that gets a part of it
    • any call, await or yield, when the root is imported, global, declared outside the function, or written or handed on by a function not running here (a closure, another method)
    • an await or yield, for any member path
    • a callback that runs later: it keeps only names nothing assigns. Array callbacks that run during their call (forEach, map, filter, …) keep the narrowing, with their whole body counted as code in between.
  • Shapes::of reads it (Shapes::with_narrowing, only truthiness.rs turns it on). Every path read sees it: prefixes, declarator inits, destructured parts (in_pattern_at).
  • shape::defined_at: defined_test, and also says where the test proved it. The region starts after that point, so m.get(k) inside its own test doesn't count as a call that undoes it.
  • json_value.rs: Shape::Defined of a declared type keeps the type declared (defined_expr). Before, it converted the whole type, so Defined(world) of a large interface came out unknown and the finding was lost.
  • I went with the ancestor walk plus a region scan rather than a flow.rs graph per question. Loops are handled by counting their whole body as part of the region.

The docs list what is followed, what undoes it, and what it can't see: aliasing, getters and proxies, and a non-array .map that keeps its callback for later. CHANGELOG has an entry under Unreleased → Fixed.

Results

  • Adopter tree (pre-workaround copy, --idiomatic, fast profile):
    • compare-misses-undefined: 8 → 3. I checked the 5 removed ones by hand (invariants ×2, library, rules, social), and all were narrowing false positives. The 3 left are ?. chains, which are true reports.
    • strict-truthiness: 236 → 237.
      • Of the 6 "may be undefined" ones, 1 was a false positive: world-ops, a .filter on an Option field after a ?.some guard. It is now reported as a plain number. The other 5 are dictionary or index reads, or first?.share, and are true.
      • One new true finding: if (i) on a forEach index. It was unknown before.
    • No other rule changed.
  • Wall time: 1.490 s ± 0.053 → 1.436 s ± 0.051 (hyperfine, 8 runs).
  • Soundness corpus: 16 of 110 classes escape, before and after.
  • Tests: cargo nextest run --workspace passes, 413/413, including meaning.rs and the work ceilings (events are collected lazily, once a narrowing is found). npm run test:docs passes.

Held-out cases

In fixtures/compare_misses_undefined/narrowed.ts and fixtures/strict_truthiness/narrowed.ts, plus a message test in messages.rs. I checked that each left-alone form is reported when its narrowing is taken away.

  • Left alone:
    • a member inside an if and inside ||
    • a copied local and a destructured local
    • a call on something else
    • path.push()
    • an array-method callback
    • a ?. test: exact, and as a prefix
    • a dictionary key counts[k] and an element items[i]
    • array destructuring
    • an early return
    • a let with &&
    • a ?.some guard
    • an assignment
    • a never-assigned const inside setTimeout
  • Reported:
    • a conditional write
    • a prefix write
    • a method call (this.clear())
    • a call that gets the object, including before destructuring
    • delete
    • destructuring assignment
    • a write later in the loop
    • a deferred callback over a property
    • a key write i += 1
    • an aliasing key write items[j] = …
    • a closure writing a let
    • a let with writes inside setTimeout
    • await
    • a module-level root with any call

🤖 Generated with Claude Code

`compare-misses-undefined` and `strict-truthiness` reported a value as "may be `undefined`" when TypeScript had already narrowed it. An adopter's tree had 5 such false positives out of 8 `compare-misses-undefined` errors. The forms were: - a member of a narrowed path: `b.plots.length` inside `if (b.plots !== undefined)` - a `let` narrowed by `route && route.length === 0` - a local copied from a narrowed path: `const books = b.books` - a path assigned just before: `b.books = kept.filter(f); if (b.books.length === 0)` - a path whose longer form was tested: `if (!b.plots?.some(f)) return;`, then `b.plots.filter(...)` ## What changed - **`narrowed.rs` (new):** the guard walk that `truthiness.rs` had (`if` / `?:` / `&&` / `||` / an earlier `if (…) return;`), plus assignment narrowing. Paths are compared by root and steps (`flow::Root`), not by text. A test of a longer path counts for its prefixes. The nearest narrowing decides. - **Invalidation (new; before, a narrowing was never undone):** a narrowing is void after any of these between the test and the read, counting all of a loop around the read that starts after the test: - a write to the path, to a part of it, or to a name in its keys - a method call on a part of it, or a call that gets a part of it - any call, `await` or `yield`, when the root is imported, global, declared outside the function, or written or handed on by a function not running here (a closure, another method) - an `await` or `yield`, for any member path - a callback that runs later: it keeps only names nothing assigns. Array callbacks that run during their call (`forEach`, `map`, `filter`, …) keep the narrowing, with their whole body counted as code in between. - **`Shapes::of` reads it** (`Shapes::with_narrowing`, only `truthiness.rs` turns it on). Every path read sees it: prefixes, declarator inits, destructured parts (`in_pattern_at`). - **`shape::defined_at`:** `defined_test`, and also says where the test proved it. The region starts after that point, so `m.get(k)` inside its own test doesn't count as a call that undoes it. - **`json_value.rs`:** `Shape::Defined` of a declared type keeps the type declared (`defined_expr`). Before, it converted the whole type, so `Defined(world)` of a large interface came out unknown and the finding was lost. - I went with the ancestor walk plus a region scan rather than a `flow.rs` graph per question. Loops are handled by counting their whole body as part of the region. The docs list what is followed, what undoes it, and what it can't see: aliasing, getters and proxies, and a non-array `.map` that keeps its callback for later. CHANGELOG has an entry under Unreleased → Fixed. ## Results - **Adopter tree** (pre-workaround copy, `--idiomatic`, fast profile): - `compare-misses-undefined`: 8 → 3. I checked the 5 removed ones by hand (invariants ×2, library, rules, social), and all were narrowing false positives. The 3 left are `?.` chains, which are true reports. - `strict-truthiness`: 236 → 237. - Of the 6 "may be `undefined`" ones, 1 was a false positive: world-ops, a `.filter` on an `Option` field after a `?.some` guard. It is now reported as a plain `number`. The other 5 are dictionary or index reads, or `first?.share`, and are true. - One new true finding: `if (i)` on a `forEach` index. It was unknown before. - No other rule changed. - **Wall time:** 1.490 s ± 0.053 → 1.436 s ± 0.051 (hyperfine, 8 runs). - **Soundness corpus:** 16 of 110 classes escape, before and after. - **Tests:** `cargo nextest run --workspace` passes, 413/413, including `meaning.rs` and the work ceilings (events are collected lazily, once a narrowing is found). `npm run test:docs` passes. ## Held-out cases In `fixtures/compare_misses_undefined/narrowed.ts` and `fixtures/strict_truthiness/narrowed.ts`, plus a message test in `messages.rs`. I checked that each left-alone form is reported when its narrowing is taken away. - **Left alone:** - a member inside an `if` and inside `||` - a copied local and a destructured local - a call on something else - `path.push()` - an array-method callback - a `?.` test: exact, and as a prefix - a dictionary key `counts[k]` and an element `items[i]` - array destructuring - an early return - a `let` with `&&` - a `?.some` guard - an assignment - a never-assigned `const` inside `setTimeout` - **Reported:** - a conditional write - a prefix write - a method call (`this.clear()`) - a call that gets the object, including before destructuring - `delete` - destructuring assignment - a write later in the loop - a deferred callback over a property - a key write `i += 1` - an aliasing key write `items[j] = …` - a closure writing a `let` - a `let` with writes inside `setTimeout` - `await` - a module-level root with any call 🤖 Generated with [Claude Code](https://claude.com/claude-code)
compare-misses-undefined and strict-truthiness follow narrowed paths, and undo them where code may change the path
Some checks failed
Benchmarks / bench (pull_request) Successful in 2m44s
Tests / test (pull_request) Failing after 25m1s
1913d71694
A member of a path TypeScript has narrowed (`b.plots.length` inside `if (b.plots !== undefined)`),
a `let` narrowed by `route && ...`, a local copied or destructured from a narrowed path, a path
assigned just before (`b.books = kept.filter(f);`) and a path whose longer form was tested
(`if (!b.plots?.some(f)) return;`) were reported as "may be `undefined`".

The guard walk moves from truthiness.rs into narrowed.rs and is read through `Shapes::of`
(`with_narrowing`), so every path read, including prefixes, declarator inits and destructured
parts, sees it. Paths are compared by root and steps (flow::Root), not text. A narrowing is now
also undone by a write to the path or a part of it or a name in its keys, a method call on a part
of it or a call that gets a part of it, and any call/await where the root is imported, global,
declared outside the function, or written by a closure; a callback that runs later keeps only
names nothing assigns, while array callbacks that run during the call keep it.

`Shape::Defined` of a declared type in the whole-program pass keeps the type declared instead of
converting it whole, so `Defined(world)` of a large interface isn't lost as unknown.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge main
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
84c6ff8ecf
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch narrowed-paths 2026-11-24 11:39:12 +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!153
No description provided.