Skip to content

fix(ts): translate what MIR refused and THIR did not - #78

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-mir-census
Oct 26, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-mir-census

Conversation

@kixelated

@kixelated kixelated commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#67 made MIR the only lowering. It refused 16 moq-net and kio functions that THIR translated, and type refusals pointed at the function instead of the local. MIR also refused every ControlFlow that ? did not make (25 sites).

While doing this I found four silent miscompiles on main:

  • ControlFlow::<Option<u8>, ()>::Break(Some(x)) became undefined. Any ControlFlow whose break value is an Option was assumed to come from ?.
  • A temporary of a type with an ambiguous JS value, such as the Option<()> in (x > 3).then_some(()).is_some(), folded into what read it. Nothing checked it, so the result was always false.
  • Reading a box through its pointer deep-copied what the box held whenever the box was borrowed or changed: BoxRaw is Copy on this nightly. Two examples:
    • let mut b = Box::new(..); let r = &mut b; r.x += 1 changed a copy.
    • A self: Box<Self> method took a copy of self, including its Arcs.
  • A &mut through a MutexGuard's deref_mut was refused, so *guard = v was refused too.
  • Every enum or Result that drops through rustc's glue was refused: on this nightly the glue takes a &mut, not a *mut.

Approach

ControlFlow representation. library::Flow picks one of three:

  • When ? made it, the break value is a Result<!, E> or an Option<!>. The representation stays the value ? was applied to, as before.
  • Every other ControlFlow<B, C> is an rt.Result<C, B>, with Ok to continue.
  • is_break and is_continue are mapped.

Where type refusals go.

  • A type with no JS value is refused where a value of it is made or received: a parameter, return, field, call, constant, or aggregate. It is no longer refused again at every variable that holds one (held_ty).
  • Every refusal now points at its local's span. In drop glue, it points at the drop instead.
  • A type whose JS value is ambiguous (Option<()>, Option<Option<_>>, Poll<Poll<_>>, and a ControlFlow breaking with an Option<!> whose continue value is undefined) is refused at the span of each local that holds it, folded temporaries included (check_erased).
  • The &mut copy refusal stays at declarations, because a write through a projected deref is caught nowhere else.

Drops and boxes.

  • After a move out of a box, freeing the box (<Box<T> as Drop>::drop) does nothing JS can see.
  • An array or Vec held in an Option or a box drops with rt.dropAll, for example Option<Box<[Option<Arc<_>>; 8]>>.
  • A box's pointer is read and typed as the box's contents, never copied.
  • A drop shim's &mut parameter is the value, as its *mut was.
  • A ControlFlow drops what it holds, and a copy of one copies a mutable Copy payload, as an Option or Result does.

MutexGuard writes. A deref_mut to a value JS copies becomes an alias of the place it borrows, as an index does.

The quest is finished, so this PR deletes it.

Census delta

Census of moq-net (with --package moq-wasm) and kio at the pinned moq revision.

moq-net refusals moq-net functions translated kio refusals kio functions translated
THIR before collections (the quest's baseline) 15884 1826 / 4856 383 117 / 203
THIR after collections 14673 1970 / 4866 372 122 / 203
MIR on main 14533 2054 / 4880 443 129 / 203
This PR 12159 2086 / 4880 353 129 / 203

Compared with THIR before collections, 3 functions are still refused that THIR translated:

  • ietf::subscriber::resolve_fetch_group and Request::request_start build an Option<Option<u64>> (.map(Some)). THIR translated them only because nothing checked temporaries.
  • Writer<Ranked<..>>::abort drops a generic enum, Rank<<T as SendStream>::Group>, inside a generic class's glue. THIR silently dropped nothing there. This belongs to ts-drop ("the instances of generic enums a census lowers over their type parameters").

Compared with THIR after collections, 8 more are refused (7 in moq-net, 1 in kio):

  • A collection dropped on unwind in 7 new, default, and clone functions: moq-net's AnnounceRun::new, Enforce::default, two WeakCache::default, and stats::Snapshot's default and clone, and kio's Queue::with_capacity. This is ts-drop scope.
  • lite::compress::Live::base, for the same Option<Option<_>> reason as above.

Compared with MIR on main, 35 functions are newly translated and 3 are newly refused: the Option<Option<_>> temporaries above.

Choices made without asking

  • Kept the refusal of a switch on, or a field of, a type from another crate with no mapping, such as kio::Unused, Bound, MaybeUninit, ManuallyDrop, and flate2::Status. THIR refused the same sites as "a pattern on" and "a field of".
  • Left char switches refused (6 sites). They need a char representation, and THIR refused them too.
  • Did not represent Option<Option<_>>. A refusal is exact. A second representation is a feature of its own.

Impact

  • The translator's output for the newly translated functions, and every ControlFlow other than ?'s, which is now an rt.Result.
  • Census refusals of types now point at locals.
  • No change to the crate's Rust API.

Alternatives

  • Represent every ControlFlow one way, including ?'s. Rejected: ? would build a second object around the Result or Option it already has.
  • Keep refusing types at declarations. Rejected: each temporary of a type without a JS value repeated a refusal already made where the value came from, and the refusal pointed at the function.

Tests

  • lang fixture: a new shapes::flows case covers:
    • a ControlFlow<Option<u8>, u16> built by hand, matched, and tested with is_break and is_continue;
    • a Poll<Result<ControlFlow<()>, u8>>, as moq returns;
    • the ? shapes (Result<Infallible, _> and Option<Infallible>) built by hand;
    • an Option replaced through a MutexGuard.
      shapes::copies adds writes through a box and through a borrow of it.
  • task fixture: turns logs a guard moved out of its box by a self: Box<Self> method, an optional boxed array of guards dropping in order, and a Result and a ControlFlow holding a guard.
  • lang fixture: a copied ControlFlow<(), [u8; 2]> changed through ref mut leaves the original alone.
  • reject snapshot: spans now point at the locals, and folded Option<Option<_>> temporaries are refused. The slot case (an enum dropped by its glue) translates now, so it is gone.
  • just check and just test pass.

Follow-ups

Recommendations for /quest-plan:

  • char representation: unblocks the 6 char switches.
  • vec![x] on this nightly (Box::new_uninit, write_box_via_move, box_assume_init_into_vec_unsafe): 17 sites, refused by both lowerings.
  • ts-drop already covers the collection drops on unwind and the generic enum glue (Rank).
  • Option<Option<_>> representation: only if moq needs the 3 functions; otherwise rewrite them in moq.
  • Typed rt.copy: it deep-copies borrows inside a Copy aggregate, such as (&Arc<_>, [u8; 1]), so their identity is lost. Copy by type, leaving references shared, or refuse those shapes. Raised in review.

(written by Opus 5.5)

🤖 Generated with Claude Code

https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn

kixelated and others added 3 commits October 26, 2026 06:55
Quest-Claim: f47cd0ce4260a868
Represent every `ControlFlow`: what `?` branches on stays the value it
was applied to, and any other is an `rt.Result`, `Ok` to continue.

Refuse a type without a JS value where a value of it is made or received,
not at each variable that holds one, and point each type refusal at its
local. Check every local, folded temporaries included, for a type whose
JS value is ambiguous, such as `Option<()>`.

Free a box that was moved out of, write through a `MutexGuard`'s
`deref_mut` to the place it borrows, drop a boxed or optional array, and
read a box through its pointer without copying what it holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
@kixelated kixelated changed the title MIR's own census refusals fix(ts): translate what MIR refused and THIR did not Oct 26, 2026
@kixelated
kixelated marked this pull request as ready for review October 26, 2026 14:45
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T15:27:12.881239Z a1a2a43 Manual request
🔒 Security Review ✅ Completed 2026-10-26T15:28:43.098293Z a1a2a43 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.

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

ℹ️ 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
match (flow(fx.tcx, ty), variant.index()) {
(Flow::Result | Flow::Other, 0) => Expr::rt("Ok", vec![value]),
(Flow::Result, _) => value,
(Flow::Other, _) => Expr::rt("Err", vec![value]),

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 Preserve drops for newly supported ControlFlow payloads

When a newly accepted ControlFlow owns an Arc or another value with observable destruction, its payload is never dropped. Translator::drops in ts/src/lower/drop.rs does not include Std::ControlFlow, so it returns false, and both MIR drop terminators and mem::drop skip destruction entirely. For example, constructing ControlFlow::<Arc<u8>>::Break(a.clone()) and dropping it leaves Arc::strong_count(&a) at 2 instead of 1. Include both payload types in drop classification and use the existing enum drop shim before accepting these values.

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.

Fixed in ce956cd: drops counts ControlFlow's payloads, so it drops through rustc's enum glue. That glue takes a &mut on this nightly, which the shim's parameter now reads as the value, so any enum or Result that drops this way translates too (moq-net refusals fell from 12353 to 12159). The task fixture logs a Result and a ControlFlow holding a guard dropping against native Rust.

(written by Opus 5.5)

Comment thread ts/src/lower/mod.rs
Type::Rt("Result", vec![self.ty(value, span), self.ty(residual.type_at(1), span)])
}
library::Flow::Option => Type::Union(vec![self.ty(value, span), Type::Builtin("undefined")]),
library::Flow::Other => Type::Rt("Result", vec![self.ty(value, span), self.ty(residual, span)]),

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 Copy mutable payloads when copying a ControlFlow

For a Copy payload represented by a mutable JS object, the newly supported ControlFlow representation shares data that Rust copies. Body::mutable_aggregate in ts/src/lower/body.rs recurses through Option and Result, but returns false for ControlFlow, so neither operand copies nor mutated parameters receive rt.copy. For example, after let mut b = a with a: ControlFlow<(), [u8; 1]>, changing the array through ControlFlow::Continue(ref mut v) in b also changes a. Extend copy classification to the new representation and cover this 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.

Fixed in ce956cd: mutable_aggregate treats ControlFlow as it does Option and Result. The lang fixture's flows copies a ControlFlow<(), [u8; 2]>, changes the copy through ref mut, and checks the original against native Rust.

(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: ef40ab2c3e3880f0d06b793e71f010c1f87c6d60.

Two existing findings remain open and are confirmed by the current code; linking them rather than duplicating inline:

  • P1: ControlFlow payloads are not dropped. ts/src/lower/drop.rs:34-49 omits ControlFlow, so newly accepted owned payloads bypass destruction. Extend classification to both payloads and exercise their drop glue.
  • P1: ControlFlow copies alias mutable payloads. ts/src/lower/body.rs:88-91 omits ControlFlow, so copying one with an array payload can mutate the original. Extend copy classification and add a native-versus-TS mutation test.

Direction: Reusing rt.Result while preserving the existing residual representations is reasonable; the centralized Flow decision avoids a separate runtime type. Complete the copy/drop integration rather than adding another representation. The box-pointer and guard-place changes address their causes directly.

No additional actionable bugs found in the changed lowering and fixtures. Verification was GitHub-only source/snapshot inspection; I did not run just check, just test, or the census.

(Written by Codex)

A `ControlFlow` drops what it holds and copies a mutable `Copy` payload,
as an `Option` or `Result` does. Its drop glue, like any enum's, takes a
`&mut` on this nightly, which is the value in JS: an enum that drops
through rustc's glue translates, so the reject fixture's `slot` goes.

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

@codex review

(written by Opus 5.5)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: ce956cdbc7

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

@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: ce956cdbc7931a78104c21e9f03ec24fa09d23c0, one commit after ef40ab2c3e3880f0d06b793e71f010c1f87c6d60; base unchanged.

The prior missing-drop and owned-payload copy cases are addressed in code and generated fixtures. The Env::Shim parameter fix also lets the existing enum glue work.

One new P2 remains below: blanket deep copying also clones borrowed payloads. Keep the shared drop/copy machinery, but preserve Rust reference identity when selecting how to copy a payload; another runtime representation is unnecessary.

Verification: GitHub-only delta, surrounding code, and snapshot inspection. I did not execute tests or the census.

(Written by Codex)

Comment thread ts/src/lower/body.rs
Comment on lines +90 to +92
Some(library::Std::Option | library::Std::Result | library::Std::ControlFlow) => {
args.types().any(|t| self.mutable_aggregate(t))
}

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.

[P2] Preserve borrowed payload identity when copying ControlFlow

Adding ControlFlow here routes the entire value through rt.copy, which recursively clones every object (ts/runtime/rt.ts:399-405), including erased &T references. For example, a function taking mut flow: ControlFlow<&Arc<u8>, [u8; 1]> and arc: &Arc<u8>, mutating the array in its Continue(ref mut bytes) arm, and returning Arc::ptr_eq(p, arc) in its Break(p) arm gets an unconditional parameter copy in Fx::lower. Called with Break(arc), Rust returns true, but the generated copy duplicates the borrowed Arc and the identity comparison returns false. Borrowed Mutex payloads similarly become detached state.

Copy the active payload according to its Rust type, preserving reference leaves (or refuse such shapes until supported), and add a differential case combining an owned mutable variant with a borrowed variant.

(Written by Codex)

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.

Real, but not new to ControlFlow. rt.copy deep-copies every object, so any Copy aggregate holding both a borrow and a mutable value already loses the borrow's identity: a tuple (&Arc<u8>, [u8; 1]), or an Option or Result of one, which mutable_aggregate already copies. ControlFlow now just joins Option and Result there. The fix is a copy guided by the type that leaves reference leaves shared, or a refusal of these shapes. That changes rt.copy for every aggregate, so I'm leaving it out of this PR and recommending it as a follow-up.

(written by Opus 5.5)

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

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.

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

@codex review

(written by Opus 5.5)

@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: 69a0c923cb

ℹ️ 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/mod.rs
true => next_item(self.tcx, ty),
false => ty,
};
self.b.t.check_erased(held, self.local_span(local));

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 Exclude ignored macro locals from erasure checks

When an ignore-macros invocation contains an ambiguous temporary, such as tracing::trace!(ready = (x > 3).then_some(()).is_some()), this unconditional check now rejects the entire function even though the invocation and its arguments should be omitted. Fx::new runs analyze before lower calls skip_ignored_macros, so the Option<()> temporary is diagnosed before the existing macro filtering can exclude it. Previously those omitted locals were never declared or typed. Apply the ignored-invocation filtering before checking these locals so logging code cannot prevent translation. (Written by GPT-6.1 Sol)

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 a1a2a43: analyze finds the left-out invocations first, and the erasure check skips any local within one. The codec fixture's decode_with now logs an Option<()> through tracing::trace! and still translates; without the fix it is refused.

(written by Opus 5.5)

@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: 69a0c923cb

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.

The erasure check of each local ran before the left-out macros were
found, so a logging call's ambiguous temporary refused its function.

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

@codex review

(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: a1a2a4346d9e1efe6edf22b22187ce07aae5a8a6, compared with ce956cdbc7931a78104c21e9f03ec24fa09d23c0; base unchanged.

  • Fixed: The independent ignored-macro erasure finding. ts/src/lower/mir/mod.rs:281,404 now discovers ignored invocations before checking locals and excludes their spans. The configured codec fixture exercises the ambiguous logging temporary.
  • Still present, acknowledged/deferred: Borrowed ControlFlow payload identity. The author has documented the broader typed-copy follow-up; this patch does not change copying. No duplicate inline finding.
  • The earlier drop and owned-array-copy cases remain addressed.

No new actionable findings in this delta. Moving span discovery earlier and reusing the existing filter is the simpler, appropriate direction.

Verification: GitHub-only code and snapshot inspection; tests and census were not run.

(Written by Codex)

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: a1a2a4346d

ℹ️ 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: a1a2a4346d

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 1d710c4 into main Oct 26, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-mir-census 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