Skip to content

fix(ts): output tsc accepts for payload writes and variants in sight - #100

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-tsc-shapes
Oct 27, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-tsc-shapes

Conversation

@kixelated

@kixelated kixelated commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Two shapes #83's tests hit translate to TypeScript that tsc rejects:

  • if let Err(e) = &mut res { *e += 1 } wrote res.error = ..., but rt.Result's fields are readonly. So did mem::replace, mem::take, mem::swap, Option::take, and String::push on a payload, and the same goes for Ok's value and for ControlFlow, which is an rt.Result too.
  • TypeScript narrows a variable to the union member it was assigned. let ready = Poll::Ready(n); match ready { .. } became const ready: rt.Poll<rt.u8> = n; if (ready !== rt.Pending), which is error TS2367 (the comparison has no overlap). Every enum TS sees as a union hits the same thing when it is built and then matched: None and Pending (TS2339, a property read on never in the dead arm), a user enum literal, whose as the declaration stripped, a tag enum, and Ordering::Equal.

On main the new tests give 23 tsc errors.

Approach

  • Payload writes (place.rs rebuilt): a store to the payload of an Option, Poll, Result, or ControlFlow, either directly or through an alias of a borrowed place, builds the enum again at the place that holds it: res = rt.Err(rt.u8WrappingAdd(res.error, 1)). Option and Poll already stored this way, since their JS value is the payload. rt.Result stays readonly. The copy rules rely on that: a Copy Result is shared instead of copied (mutable_aggregate), so writing its fields in place would change every copy (let before = res; in the test). mem::replace/take/swap, Option::take, and String::push now store through store as well.
  • Variants in sight (mod.rs find_tested, widen): a local is marked if a switch reads its discriminant, directly, through an alias, or from a closure or async block that captures it. An aggregate or constant assigned to such a local, if its type is a TS union (narrows), gets as its whole type, and the declaration keeps that as in place of the annotation: const ready = n as rt.Poll<rt.u8>;. Two cases are left alone: Some(v) of a payload that doesn't narrow, which only a None test refutes and that test reads nothing of it, and rt.Ok/rt.Err, whose type is already the whole union.

Differential tests against native Rust (lang fixture, op 17, payloads and known): writes through &mut to Ok, Err, Break, Some, and Ready payloads, through a struct field, nested in Poll<Option<Result>>, from a closure, and by mem::replace, Option::take, and String::push, with a copy taken before the write. Matches on Poll::Ready/Pending, None, Some, user enum literals, a tag enum, Ordering::Equal, and captures read in closures.

Bench

No run. The emitted JavaScript for both bench fixtures and moq-net is byte-identical to main's. The only changes are two as casts in the codec fixture's rescale (scaled = undefined as rt.u64 | undefined) and one source-map entry in moq-net's Decoder::rest, where mem::take's write now carries its span. Rebuilding a Result allocates where an in-place write would not, but nothing in moq-net or the bench writes a Result payload.

Impact

  • Translator output: a payload write through &mut rebuilds its Result or ControlFlow. A value built and then matched gets an as of its whole type.
  • Runtime: unchanged. rt.Result stays readonly.

Alternatives

  • Make rt.Result's value and error writable, as the quest suggested. This needs no allocation per write, but every Copy Result and ControlFlow holding a payload would then have to be copied where Rust copies it, and that costs an allocation on every copy (copies are more common than payload writes). readonly would also stop enforcing that sharing is sound.
  • Widen every enum construction, not just ones assigned to a tested local. This also covers values that flow elsewhere, but it adds as to every Some(x) argument and return in moq-net.
  • Change rt.Pending's type so it overlaps everything. That breaks the narrowing that types Ready's payload, and it fixes nothing for other enums.

Follow-ups

  • TS narrowing by a test, not by construction, still fails tsc (loud, not a miscompile). One example is re-matching a scrutinee inside its own arm: match x { Dot => match x { Line(..) => .. } } gives TS2367. Another is a copy of a scrutinee made inside its arm and matched again. Fixing these needs the lowering to fold a test that an enclosing test already decided.
  • Refused shapes seen while writing the tests: &mut to a non-object local assigned on two paths (let mut r = if c { Ok(..) } else { Err(..) }), ref mut in or-patterns, Result::clone, Result::unwrap_or_else, Poll constants, and u8::wrapping_mul.

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

@kixelated kixelated changed the title Output tsc accepts for writes through Result and Poll compares fix(ts): output tsc accepts for payload writes and variants in sight Oct 27, 2026
@kixelated
kixelated marked this pull request as ready for review October 27, 2026 02:15
@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-27T04:05:15.896135Z dd8a5b8 Manual request
🔒 Security Review ✅ Completed 2026-10-27T04:06:17.636089Z dd8a5b8 Manual request
ℹ️ 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.

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

No actionable bugs found in the changed lowering and generated fixtures. Rebuilding immutable Result/ControlFlow payloads through the shared store path preserves Copy isolation without making every enum copy allocate. Routing replace/take/swap through that path is the right consolidation; the nested payload, closure, and pre-write-copy fixtures exercise important boundaries.

The construction-time widening is scoped coherently. Widening all enum constructions would be simpler, but the selective analysis is reasonable to avoid unnecessary generated casts; the documented test-induced narrowing cases remain outside this fix, not newly resolved.

Verification: source and generated-snapshot inspection only. I did not run the translator, tsc, native/wasm differential tests, or benchmarks.

# Conflicts:
#	ts/fixtures/lang/expected/shapes.ts
#	ts/src/lower/mir/mod.rs
# Conflicts:
#	ts/fixtures/lang/expected/shapes.ts

@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: 4c64604

Follow-up to a585274, excluding inherited main changes. No actionable bugs found in the substantive integration: String::push now uses the same rebuilding store path as replace/take/swap, with a Result<String, u8> payload regression fixture whose output reconstructs rt.Ok rather than writing its readonly value field. The small Borrowed/set_borrowed consolidation is preferable to another special-case write. Earlier Copy-isolation and narrowing behavior remain intact in the reviewed changes; the documented test-induced narrowing limitation remains outside scope.

Verification: source and generated-snapshot inspection only; translator, tsc, differential tests, and benchmarks were not run.

@kixelated

Copy link
Copy Markdown
Collaborator Author

@codex review

(written by Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: dd8a5b8ded

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: dd8a5b8ded

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@kixelated
kixelated merged commit b09f8aa into main Oct 27, 2026
1 check passed
kixelated added a commit that referenced this pull request Oct 27, 2026
From #100's report.


Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

Co-authored-by: Luke Curley <432854+kixelated@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated
kixelated deleted the quest/m2/ts-tsc-shapes branch October 27, 2026 20:32
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.

1 participant