Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export/README
- [Byte slices without objects](/quest/m2/ts-byte-views/README.md) - reading through a translated cursor allocates no object per read
- [Async in the TypeScript translator](/quest/m2/ts-async.md) - moq-net's and kio's async code translates with Rust's lazy futures
- [Shared ownership](/quest/m2/ts-shared.md) - `Arc`/`Rc`/`Weak` keep exact counts, and locks and atomics become plain values
- [Option::cloned through Clone](/quest/m2/ts-option-cloned.md) - `cloned` of an `Option<&T>` runs `T`'s `Clone`, so an `Arc`'s count matches native
- [Translate moq-net's model to TypeScript](/quest/m2/ts-model/README.md) - the model translates with kio, moq-clock, and moq-pattern, for moq to compare against WASM
- [Translate against the prebuilt std](/quest/m2/ts-std-build.md) - measure whether `-Zbuild-std` is still needed now that panics abort, and drop it if not
- [Waiting: Swift 6.4 in nixpkgs-unstable](/quest/m2/uniffi-swift-arm.md) - the nightly UniFFI fixtures move back to arm
Expand Down
39 changes: 39 additions & 0 deletions quest/m2/ts-option-cloned.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [S] Option::cloned through Clone

## Goal

[rust2-ts](/ts/README.md) translates `Option::cloned` through the payload's `Clone`, as
std does. An `Option<&Arc<T>>` adds the strong count its clone later drops, and a `Clone`
written by hand runs. Differential tests against native wasm32 prove it.

## Plan

Found on 2026-10-29 while writing [#238](https://github.com/kixelated/rust2/pull/238)'s
fixture. `library.rs` maps `("Option", "copied" | "cloned")` to `Translator::copied`, which
copies JS values but never calls `Clone`. So `v.first().cloned()` of a `Vec<Arc<T>>` took
no count, and its drop released one the `Arc`'s owners still held. The value then dropped
early, which the lang differential caught as a different drop order. This is a silent
miscompile: the output type-checks.

- `cloned` maps the payload as `Iterator::cloned` does in `iter.rs`. A type whose `Clone`
only copies (`clone_copies`) goes through the copy shim, and any other through
`Clone::clone`. `None` passes through. `copied` stays as it is.
- `clone_copies` assumes a `Copy` type. For a std type it checks only the type arguments,
so `Arc<u8>` and `Vec<Arc<T>>` pass as copying. `Iterator::cloned` still reaches `Clone`
for an `Arc`, but takes the copy shim for a `Vec<Arc<T>>` or a derived struct holding an
`Arc`. Check whether that shim clones each `Arc`, and fix every `cloned` that shares
the gap here.
- Tests extend `ts/fixtures/lang/src/counts.rs`. They cover `cloned` of an `Option<&Arc>`,
an `Option<&Rc>`, an `Option<&mut Arc>`, a struct holding an `Arc` with a derived
`Clone`, and a hand-written `Clone`, both through `Option` and through `Iterator`. They
check the counts and the drop log.
- Milestone m2: moq-net and kio, the first consumers, call `Option::cloned` on map lookups
and handles, for example `kio/src/log.rs` and moq-net's `ietf/subscriber.rs`. So
[the model's translation](/quest/m2/ts-model/README.md) would miscount silently. Not m0,
since no consumer runs translated output yet. Not m3, which holds std that no consumer
calls.

## Related

- [Shared ownership](/quest/m2/ts-shared.md) - the exact `Arc` counts this breaks
- [Translate moq-net's model to TypeScript](/quest/m2/ts-model/README.md) - calls `Option::cloned`
Loading