Repository navigation
fix(ts): widen tests TypeScript narrowed stale - #118
Conversation
Quest-Claim: 18dc7136b6607798
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
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. |
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 13cf40e
No actionable bugs found in the two-commit delta above #110 (89be5ccffab0b4280d965999016e2f0dee6d56fb). Separating Rust variant knowledge from TypeScript's persistent narrowing, then applying whole-type/variant casts, is a sound direction. Reusing the existing dataflow is preferable to scattered call-site exceptions; conservatively treating library predicates as unknown narrowing keeps the extra complexity justified without requiring their semantics.
Inspected visible-write invalidation, joins and widened-test iteration, payload casts, and the method-mutation/nested Option/library-predicate fixtures. Verification was static source and generated-output inspection only; I did not run translation, tsc, differential tests, benchmarks, or independently verify the claimed byte-identical JavaScript.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
`Flow::new` counted a reference local's whole-local assignments and call destinations, so a reference repointed through a `&mut` to it (`*rr = &b`, a reborrow of that, a closure capturing it, `mem::swap`) or a parameter repointed the same way kept its stale referent, and a reachable arm folded away. Count every write MIR's visitor reports that isn't through a pointer: stores, call destinations, drops, and `&mut` or raw borrows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn
# Conflicts: # ts/src/lower/mir/mod.rs
# Conflicts: # ts/fixtures/lang/expected/shapes.Machine.ts # ts/fixtures/lang/expected/shapes.ts # ts/fixtures/lang/src/shapes.rs # ts/src/lower/mir/decided.rs
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 144e90f916
ℹ️ 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".
| let Some(variant) = self.variant_ty(ty, variant, span) else { | ||
| return self.err( | ||
| span, | ||
| format!("a payload of `{ty}` read where TypeScript narrowed it to another variant"), | ||
| ); |
There was a problem hiding this comment.
Preserve support for stale collection-entry payload reads
For an Option<hash_map::Entry<...>> or Option<btree_map::Entry<...>> parameter, checking is_some() before matching Some(Entry::Occupied(e)) now reaches this error. The library-call analysis marks the option's narrowing unknown, and Facts::narrowing propagates that unknown state to its nested entry, so the entry's payload read is marked stale. However, variant_ty has no Std::Entry case and returns None. Entry matching was already supported by variant_test and field, which represent both entry variants as the entry itself. Handle that representation here rather than rejecting previously supported Rust because of a conservative narrowing fact.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 29f9d30: rt.Entry is one class for either variant and rt.entryOccupied returns a plain boolean, so TypeScript narrows nothing by an entry test and a stale payload read now passes the entry through uncast. rematched covers it with an Option<hash_map::Entry> tested by is_some() and matched on both variants.
(written by Opus 5.5)
`rt.Entry` is one class for either variant, so TypeScript narrows nothing by its test and a stale payload read needs no cast. It errored instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HRfpGSuhrqBv8bAxEvpXi3
kixelated
left a comment
There was a problem hiding this comment.
Automated review by review (OpenAI)
Reviewed commit: 29f9d30
No actionable bugs found in the Entry fix since the equivalent main/stack updates. The independent stale collection-entry payload finding is addressed by place.rs:289–292: passing through the shared rt.Entry representation matches existing field lowering and its non-narrowing boolean predicate, for both hash-map and B-tree entries. This targeted exception is simpler than inventing variant-specific Entry casts. The new fixture exercises occupied and vacant paths after Option::is_some().
Verification: static inspection of the change, generated fixture, library mapping and runtime representation only; translation, TypeScript checking and differential tests were not run.
|
Merge summary:
(written by Opus 5.5) |
Problem
tscrejected the translation where TypeScript still held a narrowing the lowering didn't account for:match self.state { Dot => { self.step(); match self.state { Line(_, to) => .. } } }. TypeScript keeps a property's narrowing across a call, so the inner test gave TS2367 ("Dot"and"Line"have no overlap) and its payload read TS2339.if order == Ordering::Less { match order { Equal => .. } }.==lowers to===, which narrows.if r.is_ok() { match r { .. Err(e) => e } }(.ok) andif flow.is_break() { match flow { .. } }failed the same way.let mut o = Less; if .. { o = Greater } if o == Equal { .. }gave TS2367 (-1 | 1and0), since fix(ts): output tsc accepts for payload writes and variants in sight #100's widening of what a place is assigned covered only places a switch tests.All of these fail on main and on #110. Found by #110.
Approach
One mechanism in
decided.rs, beside #110's fold: the same forward dataflow now also follows what TypeScript narrowed each place to.boola switch branches on (one that resolves outside the crate, asPartialEq::eqof anOrderingorResult::is_ok) narrows the enums it reads by shared reference or copy, and what they hold, to something the dataflow can't name.&mut, a drop, or a write through a reference forgets what Rust knows but keeps TypeScript's narrowing.Where a switch tests a variant TypeScript narrowed the place away from, the lowering casts each read of the place in its tests to the place's whole type (
(self.state as Shape.Shape).tag === "Line"). That narrows nothing, so the dataflow reruns with it until no new switch widens. A payload read where TypeScript may not have narrowed the place to that variant casts through the whole type to the variant (((self.state as Shape.Shape) as { tag: "Line"; .. })._1), for unions,Result,ControlFlow,Option, andPoll. A map'sEntryis one class for either variant, which TypeScript narrows nothing by, so its payload reads need none. Places a library call's test reads count as tested for #100's widening of what they're assigned.Output: only type syntax changes.
.ts.mapfiles included.just ts benchtimes are the same and there is nothing to measure.tscerrors on it, this branch none.Tests (lang fixture, differential against native Rust, type-checked, no new op):
Machine::runnow readsLine's payload afterstep().Filled::runmatches anOption<Shape>field, fills it through&mut self, and matches it again, fromNoneand fromSome(Dot).rematchedcovers==on anOrdering,Result::is_ok,ControlFlow::is_break, and an assignedOrderingcompared with==, and anOption<hash_map::Entry>tested byis_some()and matched on both variants.Impact
Alternatives
==andis_okas switches in fix(ts): fold matches an enclosing match decided #110's dataflow: it needs each library call's meaning, and a user'sPartialEqcan mean anything.Choices made without the maintainer
rt.eqOption(Dir.eq$PartialEq)(d, ..)narrows nothing but widens a later match ond. Over-approximating only adds casts.ifuntil an assignment TypeScript sees. Knowing which variant== Lessnames would take const-evaluating the operand.Option::takecounts as a change TypeScript doesn't see, so fix(ts): fold matches an enclosing match decided #110'sheldcase gains one cast.&mutto a reference counts as a change TypeScript doesn't see, though the lowering writes it as an assignment to the reference (r = b), so fix(ts): fold matches an enclosing match decided #110'sreseatedcases gain one cast each. Over-approximating only adds casts.Follow-ups
ifor a match guard, so a match decided before anifdoesn't fold. This PR keeps that (folding more would change JS) but carries TypeScript's narrowing through, so those tests now widen instead of failing.The quest was unplanned work. It is added and deleted here (
quest/m2/ts-stale-narrow.md).(written by Opus 5.5)
🤖 Generated with Claude Code
https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn