Skip to content

feat(ts): translate Arc<dyn Trait> and Arc<dyn Future> - #177

Merged
kixelated merged 4 commits into
mainfrom
quest/m3/ts-dyn
Oct 28, 2026
Merged

kixelated merged 4 commits into
mainfrom
quest/m3/ts-dyn

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Advances quest/m3/ts-dyn.md: Arc<dyn Trait> and Arc<dyn Future>.

Problem

rust2-ts refused every Arc<T> to Arc<dyn Trait> coercion. moq-net holds its group expiry policy as Arc<dyn Expiry>. An Arc unsized from an Arc<T> shares the original's allocation and count, so the trait object can't replace the rt.Arc's value, and a separate rt.Arc around a wrapper would have its own count.

Approach

  • An Arc<T> unsized to Arc<dyn Trait> is an rt.ArcDyn, a view of the rt.Arc it was made from. Its value is the trait object a Box<T> of the same value unsizes to, reusing feat(ts): translate trait objects of the crate's own traits #171's wrapper classes, and strong reads and writes the original's count. Clones, strong_count, downgrade/upgrade, and drops on either side see one count, and whichever Arc is last drops the value: rt.Arc through its own drop, rt.ArcDyn through the object's [Symbol.dispose]. Deref is .value, as for any Arc.
  • Arc<dyn Future> takes the same path. Its value is the rt.Future a Box<dyn Future> would be (feat(ts): translate Box::pin and dyn Future #170's rt.DynFuture, or the coroutine itself), dropped with the last Arc.
  • An upcast (Arc<dyn Named> to Arc<dyn Shape>) is the same view.
  • Arc::ptr_eq and Weak::ptr_eq of trait objects compare the Arcs the views were made from (rt.arcPtrEq). Two coercions of clones of one Arc are equal, as Rust's data-pointer comparison is. Other ptr_eqs are unchanged.
  • Weak<T> to Weak<dyn Trait> stays refused with its span. It has no consumer.
  • Tests:
    • lang's new dyns::shared case runs differentially. It covers two objects and their upcasts from one Arc, counts after each side's clones and drops, Weak upgrades before and after the last drop, ptr_eq of the same and of different Arcs, an atomic counter every Arc sees, and Arcs of numbers, enums, and structs in a Vec. Inputs are in caller.ts via op("dyns::shared").
    • task's dyns step now runs scenario arg % 12, where 6 to 11 are shared. They log what Arc<dyn Part>s drop: the last Arc being the original or a trait object, a Drop that panics through a trait object inside a Vec while the rest still drop, a Weak, and an Arc<dyn Future> whose async block's capture drops with the last clone. Each runs under a panic at each point, in a Drop, and with none.
  • Mutation checks:
    • A view with its own count fails lang with 776 differences and task with 133.
    • A view dropping the original's raw value instead of the object fails task with 133.
    • ptr_eq as === fails lang with 776.

Census

moq-net at the pinned revision (just ts census):

  • All 8 a coercion to _, which is not supported yet are gone.
    • The 4 Arc<dyn Expiry> translate.
    • The 4 Arc<dyn Any + Send + Sync> now report as a trait object of another crate's trait (51 to 55), which the quest's remaining Any work covers.
  • moq-net: 7821 to 7819 refusals. The Expiry wrapper classes now reach is_expired(&kio::Waiter), adding 2 kio::Waiter type refusals.
  • model: 2633 to 2644 refusals and 1926 to 1930 instances. The coercion is gone, and the 4 newly reached instances (the Expiry impls behind the objects) refuse kio calls and types: 5 calls, 6 + 1 types.
  • kio: unchanged.

Impact

  • Output: Arc<dyn Trait> and Arc<dyn Future> translate, typed rt.Arc<Trait>, with values of rt.ArcDyn. Nothing that translated before changes.
  • Runtime: new rt.ArcDyn and rt.arcPtrEq.
  • Refusal: Arc<dyn OtherCrateTrait> now says why (a trait object of another crate's trait) instead of a coercion to _.
  • No breaking change.

Alternatives

  • A count box in every rt.Arc, so a second rt.Arc could share it: one more allocation per Arc for a case only trait objects need.
  • Replacing the rt.Arc's value with the wrapper at coercion: other Arc<T> clones would see the wrapper.
  • Wrapping at each deref instead of once: an allocation per call.

Choices made without asking

  • The Arc<dyn> drop scenarios extend the existing dyns task step (arg % 12) rather than adding a step. No other scenario's arguments move.
  • Weak<T> to Weak<dyn Trait> unsizing stays refused (new weak_object reject case). It would be a few lines on the same view, but nothing needs it.
  • Arc<T>'s own Debug is not mapped for any T, so the test formats &*arc. Left alone as out of scope.
  • quest/m2/ts-model/README.md no longer lists dyn Expiry as an enum among moq's own changes.

Follow-ups

  • quest/m3/ts-dyn.md keeps what remains: other crates' traits (Fn*, Iterator, Error, Any, including Arc<dyn Any>, and bytes::Buf), and generic traits and associated types.
  • Suggested: map Arc<T>'s Debug/Display onto T's (refused today for every T).

Checks

just ts test (all fixtures, differential included), just ts check (clippy, fmt, shear, sort, tsc), quest check, taplo format --check. ffi, driver, and uniffi are untouched and were not run.

(written by Claude Opus 5.5)

🤖 Generated with Claude Code

kixelated and others added 2 commits October 28, 2026 06:45
Quest-Claim: 1bfa29973ffdf445
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kixelated kixelated changed the title Trait objects feat(ts): translate Arc<dyn Trait> and Arc<dyn Future> Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 13:58
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T14:13:52.864763Z bbcc41e New commits
🔒 Security Review ✅ Completed 2026-10-28T14:14:53.647552Z bbcc41e New commits
ℹ️ 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: 19e08a8

No actionable bugs found. The ArcDyn view is a focused extension: reusing the existing trait/future wrappers while forwarding the original Arc's count avoids changing all Arc allocations or replacing the concrete value. I checked clone/drop and Weak upgrade paths, final-owner drop glue, upcasts, pointer equality, and the added differential fixtures (including panic cleanup and both kinds of final owner). The explicit Weak-unsizing refusal keeps the unsupported boundary clear.

Verification: static GitHub source/diff review only; I did not run the compiler, TypeScript checks, or differential tests.

# Conflicts:
#	ts/fixtures/lang/caller.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/task/expected/drops.ts
#	ts/tests/task.ts
# Conflicts:
#	ts/fixtures/reject/census.md
#	ts/fixtures/reject/expected.txt
@kixelated

Copy link
Copy Markdown
Collaborator Author

Landing at bbcc41e2c089c57a5aace74794950fab4cb2a37f (squash), as the maintainer authorized.

  • Codex code and security reviews of this head: no findings. The OpenAI review of 19e08a8 had no actionable findings. The changes since are two merges of main (feat(ts): translate OnceCell and OnceLock #174, feat(ts): lend std's &mut to values JS copies as rt.Places #149), whose conflicts were in test registrations and regenerated reject snapshots.
  • CI Check passed. quest check --base origin/main passed.
  • No open decisions. The choices made without asking are listed in the body.
  • Follow-ups: Arc<dyn Any> (in the remaining quest/m3/ts-dyn.md), and Arc<T>'s Debug/Display.

(written by Claude Opus 5.5)

@kixelated
kixelated merged commit b885dd1 into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m3/ts-dyn branch October 28, 2026 14:23
kixelated added a commit that referenced this pull request Oct 28, 2026
Regenerates lang's dyns.ts snapshot, which #176 and #177 left stale on main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant