Skip to content

feat(ts): translate options of values that can be undefined - #134

Merged
kixelated merged 10 commits into
mainfrom
quest/m2/ts-model/nested-option
Oct 28, 2026
Merged

kixelated merged 10 commits into
mainfrom
quest/m2/ts-model/nested-option

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused any Option<T> whose T can already be undefined, such as Option<()> and Option<Option<u64>>, since None and Some(None) would both be undefined. moq's model census hit this 60 times (47 + 8 + 5 refusals), 33 of them in resume.rs.

Approach

As decided with the maintainer on 2026-10-27: only where T can be undefined is None a runtime sentinel, rt.None, as rt.Pending and rt.DONE are. Every other Option stays T | undefined.

  • erases_to tracks whether a type can already be rt.None, so an Option of an Option<()> (three values, two sentinels) is still refused with its span. A &mut that is an rt.Place counts as an object, and RangeFull as undefined.
  • Translator::none(inner) gives the None of an Option<inner>. Every place the translator makes or tests a None uses it: aggregates, constants, switches, Default, Option::take, mem::take, copies, drops, ? (ControlFlow and FromResidual), the crate's own Iterator::next, and the Option mappings (unwrap, expect, is_some, is_none, iter, into_iter).
  • Runtime functions that take an Option take rt.None as a last argument, through overloads, so existing output is unchanged: optionUnwrap, optionExpect, fmtOption, cmpOption, eqOption, hashOption, iterOption, iterToOption, iterFromOption, iterNthBack, thenSome, boolThen, sliceFirst, sliceGet.
  • A guard in library::call refuses any other std mapping whose signature or bounds hold an Option whose None is rt.None, such as Iterator::find or HashMap::get, since its runtime function would read or make that None as undefined. This replaces the scattered option_ok checks.
  • A TS quirk: a const read from a place narrowed to rt.None widens to symbol, so swap_out declares the type there.

Tests: a new lang module, nested.rs, runs Option<Option<u8>> and Option<()> through constructors, matches, ?, combinators from core's MIR, take/replace/Default, Debug, ==/Ord/Hash in map and set keys, iterators (std's and the crate's own), then/then_some/get/first, and panics, differentially against native Rust. The reject fixture now covers the remaining refusals: triple nesting, find through the guard, and a generic type's Option<T> field instantiated over ().

Merging #140 (nth_back through rt.iterNthBack) needed iterNthBack to take rt.None too; nested.rs now covers nth_back, which failed differentially without it.

moq's model census: 3203 → 3143 refusals. All 60 of this kind are gone, and none were added elsewhere.

Impact

  • Translator output: Option<T> is T | rt.None where T can already be undefined. Output for code that already translated is unchanged apart from closure-instance names that the new fixture reaches first.
  • Runtime: new rt.None (a value and its type). The functions listed above take an optional trailing none. Nothing breaks, so there is no MIGRATE.md entry.
  • README: the type table, "Not yet", and a design note on the choice.

Choices made without asking

  • Guard rather than teaching every runtime function rt.None. Only the runtime functions moq or the fixture reach take none. The others (find, last, max, pop, map get, collecting into an Option, …) are refused with a span. Each is a small overload to add once a consumer needs it.
  • A generic type's Option<T> field stays T | undefined, and an instance over a T that can be undefined is refused. A conditional rt.Option<T> type would type it exactly, but tsc can't infer a class's type arguments back through it, so every new of such a class (even Maybe<u8>) would need explicit type arguments. moq's census has no such instance.
  • Option<()>'s == stays refused, since () has no PartialEq mapping as a key. That is a separate gap.

Alternatives

  • One rt.None for every Option: considered and left out with the maintainer; it costs TS callers ?. and ??.
  • A conditional rt.Option<T> type for generic definitions: see above.

Follow-ups

  • Teach the remaining Option-returning runtime functions rt.None (Iterator::find/last/nth/max/min/peek, Vec::pop, HashMap::get/insert/remove, collecting into Option), once a consumer reaches one.
  • () as a key: PartialEq/Ord/Hash of (), so Option<()> == Option<()> translates.
  • Generic types with an Option<T> field over a T that can be undefined, by explicit type arguments at new.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 4 commits October 27, 2026 17:12
An `Option<T>` whose `T` can already be `undefined`, such as `Option<()>` or
`Option<Option<u64>>`, has `rt.None` for its `None`, so `None` stays apart from
`Some(None)`. Every other `Option` stays `T | undefined`. A std mapping whose
runtime function would read or make such an `Option` with `undefined` is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Options of values that can be undefined feat(ts): translate options of values that can be undefined Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 02:11
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T06:48:14.076254Z cd048bd New commits
🔒 Security Review ✅ Completed 2026-10-28T06:50:16.268499Z cd048bd 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: 56841dccad

ℹ️ 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 Outdated
Comment on lines +722 to +723
// The runtime ends at the first `undefined`.
if std == Std::Option && b.t.erases_to(args.type_at(0)).undefined {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check the input option's sentinel before collecting

When collecting Iterator<Item = Option<()>> into Option<Vec<()>>, this guard checks the collected payload (Vec<()>), not the input payload (()). Since the vector does not erase to undefined, translation proceeds to iterCollectOption, whose OptionShunt.next() still treats undefined as None. Consequently, an input Some(()) terminates collection as None, while an input None (rt.None) is collected as a value. The same problem affects inputs such as Option<Option<u8>>. Check the input payload value as well and reject sentinel-based input options until the runtime supports them.

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.

Fixed in 4cbfa26: collecting into an Option now refuses when either the items' payload or the collected type can be undefined, and the reject fixture's collected covers Option<()> items.

(written by Claude 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: 56841dccad168fb64601958611a34166a0e7bdb2.

One additional actionable issue: generic_none can panic on unrelated non-generic ADT fields (inline). Also see the existing collection input-sentinel finding; no duplicate inline comment added.

Direction: the selective sentinel preserves ordinary Option ergonomics, and rejecting unsupported runtime mappings is simpler than changing every Option representation. The generic-instance guard needs the ordering fix, and unsupported collection inputs must fail closed.

Verification: static review of changed lowering/runtime paths and fixtures; I did not run builds or differential tests.

(Written by OpenAI)

Comment thread ts/src/lower/mod.rs Resolved
kixelated and others added 2 commits October 27, 2026 19:21
… Option first

Collecting into an `Option` checks the items' payload too, which the runtime's shunt
ends at when `undefined`. A generic type's instance check reads an `Option`'s argument
only once it knows the field's type is an `Option`. Drop the finished quest's link.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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: 4cbfa262204fb071770ed7f80da7f485edef4cd8, comparing against 56841dccad168fb64601958611a34166a0e7bdb2 and separating the merged main-branch documentation changes.

Both prior issues are fixed in the inspected code:

  • Generic ADT panic: generic_none now checks Option identity before indexing arguments, with Tagged<Option<()>>/Ordering fixture coverage.
  • Collection input sentinel: the guard checks both input and collected payloads, and the reject fixture covers Option<()> items.

No new actionable bugs found in this delta. These focused guards are the simpler fix and preserve the intended fail-closed direction without adding runtime complexity.

Verification: static inspection of the fixes, surrounding code and updated fixtures; I did not run builds or tests.

(Written by OpenAI)

…ted-option

Resolve against #140: `nth_back` now maps to `rt.iterNthBack`, which takes the
`rt.None` of an item that can be `undefined` as its last argument, and the
`lang` harness has a case for each of `nested`, `refs`, and `skips`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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: f8715f4c0631ae099d88e3358216ba0acbdf5809, against the last reviewed 4cbfa262204fb071770ed7f80da7f485edef4cd8, accounting for the merged #140 base changes.

No new actionable bugs found in the integration delta. nth_back now passes the optional dropper and rt.None in the correct positions to iterNthBack; the runtime preserves Some(undefined) separately from exhaustion. The new nested-option fixture exercises this path, and the harness retains distinct nested/refs/skips cases. The earlier generic-ADT panic and collection-sentinel fixes remain intact.

Direction: adapting the existing iterator consumer with a trailing sentinel is a focused, simpler integration than adding a parallel iterator implementation.

Verification: static inspection of the merge delta, surrounding lowering/runtime code and generated fixture output; builds and tests were not run.

(Written by OpenAI)

@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: f8715f4c06

ℹ️ 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/mir/call.rs Outdated
(Flow::Other, false) => base.member("error"),
(Flow::Option, true) => base,
(Flow::Option, false) => Expr::undefined(),
(Flow::Option, false) => fx.b.t.none(continued(ty)),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Decode the Break payload using the residual's representation

For ControlFlow<Option<Infallible>, ()>, the enclosing Break is represented by rt.None, but its extracted payload is still None::<Infallible>, whose representation is undefined. Returning none(continued(ty)) conflates those two representations: match c { ControlFlow::Break(b) => b.is_none(), ControlFlow::Continue(()) => false } evaluates the break arm as rt.None === undefined, yielding false instead of Rust's true. Decode the payload to undefined and keep its projected type consistent, rather than using the enclosing control-flow sentinel. Cover this distinction with a differential test.

AGENTS.md reference: AGENTS.md:L15-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 on the representation: fixed in c40dd65, the Break payload reads as undefined, as main had it.

Not covered by a test: the example itself does not translate on main either. Option<Infallible> is zero-sized, so its local is elided but still read (Cannot find name '_5' from tsc), whether the ControlFlow continues with () or u8. That is a separate, pre-existing bug, left as a follow-up. Within ?, the payload only reaches from_residual, which ignores it, so no snapshot changed.

(written by Claude Opus 5.5)

The `Break` of `?` on an `Option` holds an `Option<Infallible>`, whose
`None` is `undefined`, not the `rt.None` of the `Option` it was applied to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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: c40dd65c4aa3f5b8072f4db4c052941cc72cb8ce, against f8715f4c0631ae099d88e3358216ba0acbdf5809 (base unchanged).

The one-line change addresses the existing Break residual representation finding: Flow::Option is restricted to an Option of an uninhabited type, so its extracted Break payload is always undefined, independent of the enclosing ControlFlow's sentinel. Construction and variant testing still correctly use the continuation type's sentinel. No new actionable bugs found in this delta; the direct projection fix is the simpler approach.

Verification: static inspection of classification, construction, testing and projection paths only. No builds or tests run. This fix adds no regression test; the author's reported pre-existing zero-sized-local blocker remains an unverified coverage limitation, not a newly introduced finding.

(Written by OpenAI)

kixelated and others added 2 commits October 27, 2026 23:42
…ted-option

The `lang` harness keeps a case for each of `nested`, `refs`, `counts`,
`rounding`, and `skips`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merge summary:

  • Merged main twice. fix(ts): skip through each iterator's own nth, as std's does #140 moved nth_back to rt.iterNthBack, which dropped this PR's rt.None there: nested.rs now covers nth_back (it failed differentially before the fix), and iterNthBack takes rt.None as the other Option runtime functions do. The lang harness keeps a case for nested, refs, counts, rounding, and skips.
  • Codex's ControlFlow residual finding: the Break payload reads as undefined again, as main had it. The finding's own example hits a separate, pre-existing bug (a zero-sized Option<Infallible> local is elided but still read), suggested as a follow-up.
  • Root just check and just test pass locally on the merge with feat(ts): translate the rest of format specs #136; CI passes on cd048bd, which Codex reviewed with no findings.

(written by Claude Opus 5.5)

@kixelated
kixelated merged commit ac1122a into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-model/nested-option branch October 28, 2026 07:01
kixelated added a commit that referenced this pull request Oct 28, 2026
…-more

Resolved by hand against #134: an `Option` erases with `.option()` and
drops nothing when its payload can be `undefined`, beside this branch's
`Reverse` and `NonZero`. `mem::take` passes its type to main's
`swap_out`. The lang harness adds `nested` at 50 and keeps
`wrappers` and `edits` last (`op % 57`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kixelated added a commit that referenced this pull request Oct 28, 2026
Takes #134's rt.None: Option intercepts test a payload's None as its own, and
into_iter of a &mut Option lends the payload's place only while it is Some.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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