Skip to content

feat(ts): translate OnceCell and OnceLock - #174

Merged
kixelated merged 6 commits into
mainfrom
quest/m2/ts-shared
Oct 28, 2026
Merged

kixelated merged 6 commits into
mainfrom
quest/m2/ts-shared

Conversation

@kixelated

@kixelated kixelated commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Part of quest/m2/ts-shared.md: this lands OnceCell and OnceLock. The quest file is rewritten to what remains.

Problem

rust2-ts refused every one-time cell. Of the pieces the quest lists, that removes the most moq census refusals. At the pinned moq revision, on main at b624403:

OnceLock + OnceCell Cell/RefCell/Ref/RefMut static atomics Rc, RwLock
moq-net 41 0 2 0
model 30 0 1 0
kio 5 15 1 0

The Cells are all behind kio's thread_local!, which would also need LocalKey. Consumers: moq-net's model/cache.rs, frame.rs, group.rs, track.rs, origin.rs, demand/ranges.rs, lite/publisher.rs, and ietf/publisher.rs, plus kio's waiter.rs.

Approach

  • OnceCell<T> becomes rt.OnceCell<T> and OnceLock<T> becomes rt.OnceLock<T>. Each holds the Option<T> in v, as the translator already represents it. Where T can already be undefined, the None is rt.None, and that is the class's second type argument.
  • The mapped methods are new, Default, From, get, set, get_or_init, take, and into_inner. moq calls all of them except into_inner.
  • get_or_init drops its closure uncalled when the cell is already full, through discards like bool::then. A OnceCell that its own initializer fills drops the value the initializer made, then panics reentrant init, as std's try_init does.
  • OnceLock keeps a running flag. Initializing it from within its own initialization (get_or_init or set) panics with wasm32's Once message, "one-time initialization may not be performed recursively". As it panics, it drops what the call took: the uncalled function or the value. A panic in the initializer clears the flag and leaves the cell empty. Rust leaves the threaded case unspecified (it deadlocks on the host), and the translation targets wasm32, whose no_threads Once panics, so the panic is defined behavior there.
  • A cell drops its value as it drops: drop_value of its Option<T>, or [Symbol.dispose] when that is disposable.
  • Clone, Debug, PartialEq, get_mut, and get_or_try_init stay refused with a span. moq doesn't call them.

Proof

  • lang (differential against wasm32): new case cells::cells on OnceCell and OnceLock of u8, u64 (limbs), Option<u8> (rt.None), (), and String. The steps are get, set, get_or_init, an initializer reading get, take, a reentrant set and get_or_init, an initializer that panics, From, Default, and into_inner. caller.ts runs 9 scripts on each of the 256 kind bytes, through op("cells::cells").
  • task (differential, plus host unwinding): new step cells calls drops/once.rs. It covers:
    • a refused set's value dropping where its Result does;
    • an initializer's captures, both called and uncalled;
    • take and into_inner;
    • OnceCell reentrance dropping in order before its panic;
    • an initializer panicking while a value that drops during unwinding sees the cell empty and refills it, for both cells;
    • cells in an Arc'd struct, a Vec, and an Option;
    • cells of Option<Guard> and ().
  • tests/once.ts (runtime): a recursive OnceLock set and get_or_init drop what they took as they panic. Native Rust can't show this, because the panic traps on wasm32 and the host's Once deadlocks.
  • Mutation checks each fail:
    • Reentrant message changed: lang shows 375 differences.
    • running never cleared: lang 66, and task 617 when it is cleared only on success.
    • set skipping the recursion check: lang 190.
    • No dropValue: unwinding shows 1189 differences.
    • No dropF: task 379.
    • No [Symbol.dispose]: task 2029.
    • No drop on a recursive panic: once.ts fails.

just check and just test pass locally after merging main (#173).

Impact

  • Public API: new translator output rt.OnceCell and rt.OnceLock, with rt.onceSet, rt.onceTake, and rt.onceGetOrInit in rt/task.ts. Nothing breaks, so there is no MIGRATE.md entry.
  • just ts census at the pinned moq revision:
    • moq-net: 7745 to 7705 refusals.
    • model: 2647 to 2618.
    • kio: 181 to 176.
    • No OnceLock or OnceCell refusal is left. One newly exposed refusal is moq_clock::Instant inside cache.rs's OnceLock, which belongs to crates.
  • Not measured on the TS bench: no benchmarked path uses a cell.

Choices made without asking

  • Both OnceCell and OnceLock are in this PR. They share the representation, and moq uses both.
  • The runtime has one set of functions, which tell the two classes apart by instanceof OnceLock, rather than a pair of functions per method.
  • The constructor parameter is NoInfer, so the cell's type comes from its place. Otherwise an empty cell's undefined would narrow T.
  • The runtime functions take f: () => T | void, because the translator types a () closure as returning void.
  • The task cases run the cells step for all 256 argument bytes, instead of hardcoding the scenario count in TS.

Alternatives

  • One class with a flag for OnceLock, instead of a subclass. The subclass keeps OnceCell free of the field.
  • Lowering std's own bodies from MIR. They go through UnsafeCell and Once, which are refused.

Follow-ups

  • PoisonError is now the largest shared-type refusal: 81 in moq-net and 57 in model. It is already listed in this quest.
  • Arc::default and Mutex::default (21 and 8 in moq-net) and Mutex::get_mut (6) are listed in this quest's Plan too.
  • OnceCell/OnceLock Clone, Debug, and PartialEq can be added when a consumer derives them.

🤖 Generated with Claude Code

(Written by Claude Opus 5.5)

kixelated and others added 4 commits October 28, 2026 05:58
Quest-Claim: 9d12ea6fc7996515
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
@kixelated kixelated changed the title Shared ownership in the TypeScript translator feat(ts): translate OnceCell and OnceLock Oct 28, 2026
@kixelated
kixelated marked this pull request as ready for review October 28, 2026 13:28
@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-28T13:44:46.356047Z 9344bab New commits
🔒 Security Review ✅ Completed 2026-10-28T13:46:15.140777Z 9344bab 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.

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

ℹ️ 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/runtime/rt/task.ts Resolved

@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: a5635173ab142a0982b73de8365334a4adc37147.

The shared runtime representation and explicit refusal of unsupported methods are a focused direction; reusing the existing Option/drop machinery avoids unnecessary duplication.

One actionable issue remains: the P2 recursive-OnceLock argument cleanup finding at task.ts:264. I agree with the existing finding rather than adding a duplicate inline comment: the recursion guard throws before consuming/dropping the supplied initializer or set value. Add ownership/drop-order coverage for both recursive calls alongside the cleanup fix. No additional actionable bugs found in the reviewed changes.

Verification: static review of runtime, lowering, drop glue, and added source/generated fixtures; I did not execute tests or independently verify the reported test results.

(Written by OpenAI)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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: c1ed513be6fb4b39b6f0651e4996e59108623b45, compared with a5635173ab142a0982b73de8365334a4adc37147 (unchanged base).

The previous recursive-OnceLock cleanup finding is addressed: the panic path now drops the consumed value or uncalled initializer, and lowering supplies the value dropper without shifting the optional None sentinel incorrectly. The direct runtime regression test covers both paths and checks that a normal rejected set still returns its value undropped.

No new actionable bugs found. The focused shared cleanup helper is a reasonable direction without additional abstraction. Verification was static review of the update, generated call sites, and test wiring; tests were not executed.

(Written by OpenAI)

# Conflicts:
#	ts/fixtures/lang/caller.ts
#	ts/fixtures/lang/expected/harness.ts
#	ts/fixtures/lang/src/harness.rs
#	ts/fixtures/lang/src/lib.rs
#	ts/fixtures/task/expected/drops.ts
#	ts/fixtures/task/src/drops.rs
#	ts/fixtures/task/src/harness.rs
#	ts/fixtures/task/ts.toml
#	ts/tests/task.ts
@kixelated

Copy link
Copy Markdown
Collaborator Author

Merging with maintainer authorization at reviewed head 9344bab. That head passes CI, and Codex gave it a 👍 after its one finding was fixed: a recursive OnceLock now drops what it took. The only change on main since is #175, which merges cleanly. Choices made without asking are in the PR body.

(Written by Claude Opus 5.5)

@kixelated
kixelated merged commit 3ca71ab into main Oct 28, 2026
1 check passed
@kixelated
kixelated deleted the quest/m2/ts-shared branch October 28, 2026 13:59
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