Skip to content

feat(ts): translate bool::then, iter::successors, and From<bool> - #113

Merged
kixelated merged 5 commits into
mainfrom
quest/m2/ts-std-gaps
Oct 27, 2026
Merged

kixelated merged 5 commits into
mainfrom
quest/m2/ts-std-gaps

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

moq-net calls bool::then 54 times, From<bool> for integers 20 times, and iter::successors once (discovery.rs's covering), and all of them were refused. Separately, None.ok_or(e) translated to rt.optionOkOr(undefined, e), where TypeScript infers T = undefined and tsc rejects the Result<undefined, E> (found in #87).

Approach

  • bool::then lowers to a new rt.boolThen(cond, f, drop?), which calls f only when cond holds. When it doesn't, f drops, so a closure that owns a value that drops is refused, the same as everywhere else. It is not named then, because an export named then makes rt's module namespace a thenable.
  • iter::successors lowers to rt.iterSuccessors(first, succ). Like std, it calls succ on each item as it yields that item, and takes the next item before succ runs, so a panic leaves it done.
  • successors and iter::from_fn refuse a closure that owns a value that drops, since an rt.Iter never drops it. Before, from_fn never checked its closure. The reject fixture's owning_lazy covers then, successors, and from_fn.
  • From<bool> for every integer type lowers through the same num::cast as bool as T.
  • None narrowing, fixed where #100 widens: find_tested also marks every local a std call takes, so widen gives a None (or any union-typed value) assigned to it its whole type. A constant passed straight to std (NONE.ok_or(a)) gets the same as in operands. widen's tail is now the shared whole helper.
  • bool::then_some was already translated, so nothing changes for it.

Differential tests, extending existing ops so the harness modulus stays at 45:

  • shapes::library covers then with a closure that counts its calls and with a function item (Vec::new), From<bool> into u8, i8, usize, u64, u128, and i64, and ok_or on a None local, a None literal, and a None constant.
  • iters::options covers successors with a closure that logs each call, under take(n) for n = 0 to 3 and under count.
  • strs::slices now runs discovery's actual covering walk (successors + then + map_or + str slice) instead of the while let loop that stood in for it.

Three runtime mutants each fail the 343,909-case run: then calling f eagerly (1065 differences), successors computing one item ahead (1028), and successors deferring succ until the following next (646).

Census (moq-net)

Before After
Refusals 11416 11344
unsupported call 7041 6966
dropping a closure that owns a value that drops 8 11
discovery.rs spans 56 53

discovery.rs lost its usize::from(want) (174), successors (309), and then (310) refusals. One refusal is left in covering: .map(|path| Path::new(path).to_owned()).collect() at 313, an iterator that owns a Path, which drops. That one belongs to ts-drop. The 3 new closure refusals are then calls whose closure owns a value that drops (ietf/subscriber.rs:3886, model/origin.rs:5093, model/resume.rs:1906), which were "unsupported call" before. kio is unchanged. moq-net's translated codec (just ts moq) is byte-identical to main's, apart from the two new runtime functions.

Impact

  • The translator's output: bool::then, iter::successors, and integer From<bool> translate instead of refusing. A union-typed value made into a local or constant that std takes now carries an as of its whole type. In the task fixture, that turns five const x: Held.Held = {...} into const x = {...} as Held.Held.
  • Runtime: new rt.boolThen and rt.iterSuccessors.

Choices made without asking

  • Covered From<bool> for every integer target, not only usize: moq-net also uses u8, u64, and i64.
  • Widened every local std takes, rather than only the ones a runtime function's type parameter is inferred from alone. Knowing which ones those are would mean modeling each mapping's signature. An as keeps the emitted JS the same, but tsc checks it less strictly than an annotation.
  • Named the runtime function boolThen rather than then, for the thenable reason above.

Alternatives

  • Explicit type arguments on runtime calls (rt.optionOkOr<rt.u8, rt.u8>(...)). This works for every narrowing, but it needs each mapping to know how its runtime function's type parameters line up with Rust's generic arguments.
  • NoInfer on optionOkOr's option. T then comes only from the contextual return type, and that type is absent wherever the result is matched directly.

Follow-ups

  • Unverified: Option::map, Result::map, map_err, and and_then list no discarded operands in discards, so a closure that owns a value that drops, passed to one of them on None or Err, may never run its drop. then lists its function for this reason.

Checks

just check, just test (with UPDATE_EXPECT=1 just ts test for the snapshots), just ts moq, just ts census, quest check.

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 3 commits October 26, 2026 23:23
Quest-Claim: 356195bafdf2dea4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title Small std gaps feat(ts): translate bool::then, iter::successors, and From<bool> Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 06:53
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T18:05:15.353082Z ecb8f2b New commits
🔒 Security Review ✅ Completed 2026-10-27T18:06:48.103285Z ecb8f2b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3eb03f3215

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ts/src/lower/iter.rs
Comment on lines +222 to +223
no_drops(b, targ(0)?, span);
return Some(Expr::rt("iterSuccessors", args));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject successor closures that own dropping captures

When succ is a move closure owning a value with an observable destructor, this mapping checks only the item type T, not the closure type F. Dropping Rust's Successors<T, F> drops that capture even if next() was never called, but rt.iterSuccessors has no corresponding cleanup. The generic dropping-operand check in library::call is bypassed by its direct return into the iterator mappings, and Translator::drops treats the unrecognized std iterator type as non-dropping, so this silently loses destructor effects instead of refusing translation. Check the second type argument with no_drops as well.

AGENTS.md reference: AGENTS.md:L14-L15

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, fixed in 911485b. successors now refuses a closure that owns a value that drops. iter::from_fn had the same gap: it never checked its closure, which is its second type argument. It now refuses one too. The reject fixture's owning_lazy covers then, successors, and from_fn. moq-net's census is unchanged.

(written by Opus 5.5)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 3eb03f3215878c09cb42869bdda11593c0c0dbd6.

Confirmed the existing successor-closure drop finding at ts/src/lower/iter.rs:222-223; no duplicate inline comment. The new mapping checks the item but not the owned closure, so a successor capturing an Arc or custom Drop value can lose observable cleanup even when the iterator is never advanced. Refuse dropping closure types until iterator cleanup is supported, with coverage for early drop and consumption.

Direction is sound: small runtime helpers preserve then laziness and successors' call-before-yield/panic state; sharing the existing widening helper is simpler than per-mapping generic-signature machinery. No additional actionable bugs found in the changed areas. The earlier plan's successor-timing concern is addressed here.

Verification: static review of diff, generated fixtures, iterator/drop lowering, and widening paths only; I did not run tests or independently reproduce the reported differential/mutant results.

(Written by OpenAI)

@kixelated kixelated left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review by review (OpenAI)

Reviewed commit: 911485b13b27950d58473b2b0836627a1c279fe6, compared with my prior review of 3eb03f3215878c09cb42869bdda11593c0c0dbd6.

The successor-closure drop finding is fixed in code: ts/src/lower/iter.rs:221-228 now checks the owned closure type for both successors and from_fn. The owning_lazy reject fixture covers both paths and then; the existing thread remains unresolved in GitHub, but the underlying issue is addressed.

No new actionable bugs found in this update. Reusing no_drops is the simple, consistent choice until iterator cleanup is implemented; extending the same guard to from_fn closes the equivalent gap without runtime complexity.

Verification: static inspection of the one-commit delta, surrounding drop checks, fixture registration, and expected diagnostics only. I did not run tests.

(Written by OpenAI)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary for ecb8f2b:

  • The one review finding (a successors closure that owns a dropping value) is fixed in 911485b, which also covers from_fn. The follow-up review confirmed the fix, and Codex found nothing on the current head.
  • CI passes, the branch contains current main, and quest check --base origin/main passes.
  • No design decisions were left open. Follow-up from the body: check whether Option::map, Result::map, map_err, and and_then need discards entries for closures that own dropping values.

(written by Opus 5.5)

@kixelated
kixelated merged commit 165071a into main Oct 27, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-std-gaps branch October 27, 2026 20:32
kixelated added a commit that referenced this pull request Oct 27, 2026
Resolves #113 against this PR: `bool::then` now drops an owning closure on
`false` through `rt.boolThen`'s dropper (checked in `drops/closures.rs`), and
`from_fn` and `successors` refuse an owning closure through `called_closures`.
#101 removed `no_drops`, so `successors` of items that drop refuses inline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated added a commit that referenced this pull request Oct 27, 2026
Keeps #113's refusal of what `from_fn` and `successors` would never drop: a
closure that owns a value that drops, and `successors` of items that drop,
whose pending item `rt.iterSuccessors` leaves undropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated added a commit that referenced this pull request Oct 27, 2026
#101 squash-merged. Its own merge of #113 refused owning closures in
from_fn and successors through closure_no_drops; this branch's
called_closures refusal replaces it, keeping successors' refusal of
items that drop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant