Repository navigation
fix(ts): translate what MIR refused and THIR did not - #78
Conversation
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
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: 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".
| 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]), |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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)
| 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)]), |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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-49omitsControlFlow, 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-91omitsControlFlow, 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
|
@codex review (written by Opus 5.5) |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
left a comment
There was a problem hiding this comment.
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)
| Some(library::Std::Option | library::Std::Result | library::Std::ControlFlow) => { | ||
| args.types().any(|t| self.mutable_aggregate(t)) | ||
| } |
There was a problem hiding this comment.
[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)
There was a problem hiding this comment.
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)
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
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
|
@codex review (written by Opus 5.5) |
There was a problem hiding this comment.
💡 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".
| true => next_item(self.tcx, ty), | ||
| false => ty, | ||
| }; | ||
| self.b.t.check_erased(held, self.local_span(local)); |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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)
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
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
|
@codex review (written by Opus 5.5) |
kixelated
left a comment
There was a problem hiding this comment.
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,404now 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)
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
🛡️ Codex Security Review · Automatically triggeredSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
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
ControlFlowthat?did not make (25 sites).While doing this I found four silent miscompiles on
main:ControlFlow::<Option<u8>, ()>::Break(Some(x))becameundefined. AnyControlFlowwhose break value is anOptionwas assumed to come from?.Option<()>in(x > 3).then_some(()).is_some(), folded into what read it. Nothing checked it, so the result was alwaysfalse.BoxRawisCopyon this nightly. Two examples:let mut b = Box::new(..); let r = &mut b; r.x += 1changed a copy.self: Box<Self>method took a copy ofself, including itsArcs.&mutthrough aMutexGuard'sderef_mutwas refused, so*guard = vwas refused too.Resultthat drops through rustc's glue was refused: on this nightly the glue takes a&mut, not a*mut.Approach
ControlFlowrepresentation.library::Flowpicks one of three:?made it, the break value is aResult<!, E>or anOption<!>. The representation stays the value?was applied to, as before.ControlFlow<B, C>is anrt.Result<C, B>, withOkto continue.is_breakandis_continueare mapped.Where type refusals go.
held_ty).Option<()>,Option<Option<_>>,Poll<Poll<_>>, and aControlFlowbreaking with anOption<!>whose continue value isundefined) is refused at the span of each local that holds it, folded temporaries included (check_erased).&mutcopy refusal stays at declarations, because a write through a projected deref is caught nowhere else.Drops and boxes.
<Box<T> as Drop>::drop) does nothing JS can see.Vecheld in anOptionor a box drops withrt.dropAll, for exampleOption<Box<[Option<Arc<_>>; 8]>>.&mutparameter is the value, as its*mutwas.ControlFlowdrops what it holds, and a copy of one copies a mutableCopypayload, as anOptionorResultdoes.MutexGuardwrites. Aderef_mutto 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.mainCompared with THIR before collections, 3 functions are still refused that THIR translated:
ietf::subscriber::resolve_fetch_groupandRequest::request_startbuild anOption<Option<u64>>(.map(Some)). THIR translated them only because nothing checked temporaries.Writer<Ranked<..>>::abortdrops 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):
new,default, andclonefunctions: moq-net'sAnnounceRun::new,Enforce::default, twoWeakCache::default, andstats::Snapshot'sdefaultandclone, and kio'sQueue::with_capacity. This is ts-drop scope.lite::compress::Live::base, for the sameOption<Option<_>>reason as above.Compared with MIR on
main, 35 functions are newly translated and 3 are newly refused: theOption<Option<_>>temporaries above.Choices made without asking
kio::Unused,Bound,MaybeUninit,ManuallyDrop, andflate2::Status. THIR refused the same sites as "a pattern on" and "a field of".charswitches refused (6 sites). They need acharrepresentation, and THIR refused them too.Option<Option<_>>. A refusal is exact. A second representation is a feature of its own.Impact
ControlFlowother than?'s, which is now anrt.Result.Alternatives
ControlFlowone way, including?'s. Rejected:?would build a second object around theResultorOptionit already has.Tests
shapes::flowscase covers:ControlFlow<Option<u8>, u16>built by hand, matched, and tested withis_breakandis_continue;Poll<Result<ControlFlow<()>, u8>>, as moq returns;?shapes (Result<Infallible, _>andOption<Infallible>) built by hand;Optionreplaced through aMutexGuard.shapes::copiesadds writes through a box and through a borrow of it.turnslogs a guard moved out of its box by aself: Box<Self>method, an optional boxed array of guards dropping in order, and aResultand aControlFlowholding a guard.ControlFlow<(), [u8; 2]>changed throughref mutleaves the original alone.Option<Option<_>>temporaries are refused. Theslotcase (an enum dropped by its glue) translates now, so it is gone.just checkandjust testpass.Follow-ups
Recommendations for
/quest-plan:charrepresentation: unblocks the 6charswitches.vec![x]on this nightly (Box::new_uninit,write_box_via_move,box_assume_init_into_vec_unsafe): 17 sites, refused by both lowerings.Rank).Option<Option<_>>representation: only if moq needs the 3 functions; otherwise rewrite them in moq.rt.copy: it deep-copies borrows inside aCopyaggregate, 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