Repository navigation
feat(ts): translate options of values that can be undefined - #134
Conversation
Quest-Claim: 8f9f13b363f8069b
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| // The runtime ends at the first `undefined`. | ||
| if std == Std::Option && b.t.erases_to(args.type_at(0)).undefined { |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)
… 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
left a comment
There was a problem hiding this comment.
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_nonenow checks Option identity before indexing arguments, withTagged<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
left a comment
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
💡 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".
| (Flow::Other, false) => base.member("error"), | ||
| (Flow::Option, true) => base, | ||
| (Flow::Option, false) => Expr::undefined(), | ||
| (Flow::Option, false) => fx.b.t.none(continued(ty)), |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)
…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>
|
Merge summary:
(written by Claude Opus 5.5) |
…-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>
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>
Problem
rust2-ts refused any
Option<T>whoseTcan already beundefined, such asOption<()>andOption<Option<u64>>, sinceNoneandSome(None)would both beundefined. moq's model census hit this 60 times (47 + 8 + 5 refusals), 33 of them inresume.rs.Approach
As decided with the maintainer on 2026-10-27: only where
Tcan beundefinedisNonea runtime sentinel,rt.None, asrt.Pendingandrt.DONEare. Every otherOptionstaysT | undefined.erases_totracks whether a type can already bert.None, so anOptionof anOption<()>(three values, two sentinels) is still refused with its span. A&mutthat is anrt.Placecounts as an object, andRangeFullasundefined.Translator::none(inner)gives theNoneof anOption<inner>. Every place the translator makes or tests aNoneuses it: aggregates, constants, switches,Default,Option::take,mem::take, copies, drops,?(ControlFlowandFromResidual), the crate's ownIterator::next, and theOptionmappings (unwrap,expect,is_some,is_none,iter,into_iter).Optiontakert.Noneas a last argument, through overloads, so existing output is unchanged:optionUnwrap,optionExpect,fmtOption,cmpOption,eqOption,hashOption,iterOption,iterToOption,iterFromOption,iterNthBack,thenSome,boolThen,sliceFirst,sliceGet.library::callrefuses any other std mapping whose signature or bounds hold anOptionwhoseNoneisrt.None, such asIterator::findorHashMap::get, since its runtime function would read or make thatNoneasundefined. This replaces the scatteredoption_okchecks.constread from a place narrowed tort.Nonewidens tosymbol, soswap_outdeclares the type there.Tests: a new
langmodule,nested.rs, runsOption<Option<u8>>andOption<()>through constructors, matches,?, combinators from core's MIR,take/replace/Default,Debug,==/Ord/Hashin 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,findthrough the guard, and a generic type'sOption<T>field instantiated over().Merging #140 (
nth_backthroughrt.iterNthBack) needediterNthBackto takert.Nonetoo;nested.rsnow coversnth_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
Option<T>isT | rt.NonewhereTcan already beundefined. Output for code that already translated is unchanged apart from closure-instance names that the new fixture reaches first.rt.None(a value and its type). The functions listed above take an optional trailingnone. Nothing breaks, so there is no MIGRATE.md entry.Choices made without asking
rt.None. Only the runtime functions moq or the fixture reach takenone. The others (find,last,max,pop, mapget, collecting into anOption, …) are refused with a span. Each is a small overload to add once a consumer needs it.Option<T>field staysT | undefined, and an instance over aTthat can beundefinedis refused. A conditionalrt.Option<T>type would type it exactly, but tsc can't infer a class's type arguments back through it, so everynewof such a class (evenMaybe<u8>) would need explicit type arguments. moq's census has no such instance.Option<()>'s==stays refused, since()has noPartialEqmapping as a key. That is a separate gap.Alternatives
rt.Nonefor everyOption: considered and left out with the maintainer; it costs TS callers?.and??.rt.Option<T>type for generic definitions: see above.Follow-ups
Option-returning runtime functionsrt.None(Iterator::find/last/nth/max/min/peek,Vec::pop,HashMap::get/insert/remove, collecting intoOption), once a consumer reaches one.()as a key:PartialEq/Ord/Hashof(), soOption<()> == Option<()>translates.Option<T>field over aTthat can beundefined, by explicit type arguments atnew.(written by Claude Opus 5.5)
🤖 Generated with Claude Code