Skip to content

feat(ts): translate std::panic where it matches panic=abort - #538

Merged
kixelated merged 7 commits into
mainfrom
ts-std-panic
Nov 3, 2026
Merged

kixelated merged 7 commits into
mainfrom
ts-std-panic

Conversation

@kixelated

@kixelated kixelated commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

rust2-ts refused AssertUnwindSafe outright (found by #528): it couldn't be made, so it couldn't wrap a future or a closure either. The rest of std::panic was refused as unsupported calls: catch_unwind, resume_unwind, panic_any, Location, set_hook, and take_hook, and Debug of a dyn Any, which catch_unwind(..).unwrap() needs. No moq consumer calls these; the maintainer plans all of std.

Approach

Panics abort, on wasm32's panic=abort and in the translation alike, so most of std::panic has an exact translation:

  • AssertUnwindSafe<T> is what it holds, as Reverse<T> is, and JS erases it as it does a Box or Pin where a write in place checks what JS keeps: made, its field read and written, Deref/DerefMut, Default, From, Debug (AssertUnwindSafe(..), as std's derived-style tuple), drops, a call through its FnOnce, and a poll through its Future, each forwarding to what it holds.
  • catch_unwind(f) is Ok(f()): a panic inside throws on, as native aborts after the hook.
  • resume_unwind throws rt.Abort, as native aborts without running the hook.
  • panic_any(p) panics with p if it is a &str or String, and with Box<dyn Any> otherwise, as std's hook writes it. Edition-2015 begin_panic had the same bug (any payload was its JS string) and shares the fix.
  • Debug of a dyn Any writes Any { .. }.

What can't match yet stays refused with a span and a reason: Location (it needs each #[track_caller] function's caller), and set_hook/take_hook (a hook reads each panic's Location, which panics don't carry).

Tests: a new lang/panics differential fixture (673 cases, 97 panics, 31 aborts). It covers AssertUnwindSafe of numbers, a Vec, a struct, nested wrappers, written through *, .0, and &mut *, and an Arc shared with a clone and a Vec<u8> replaced whole through a generic &mut; Debug plain, alternate, padded, and nested; Default and From; drop order moved, replaced, and at scope end; calls of closures, fn items, closures that write captures or own values that drop; catch_unwind of closures, fn items, borrowed closures, wrapped closures, a Box<dyn FnOnce>, and one that panics; futures polled by hand and awaited, nested, and dropped unpolled; panic_any of each payload kind and resume_unwind. Mutating resumeUnwind to a plain panic and the Box<dyn Any> text gives 72 differences. The reject fixture gains Location::caller and the hooks.

Impact

  • Public API: the translator's output. New runtime export: rt.resumeUnwind. No breaking change.

Alternatives

  • Lowering AssertUnwindSafe's impls from core's MIR: its Future uses Pin::map_unchecked_mut, and a class per wrapper would allocate for a marker. Erasing it, as Reverse is, needs no runtime.
  • Translating Location::caller here: it needs a hidden location parameter on every #[track_caller] function and refusals for indirect calls, a change of its own (quest below).

Follow-ups

  • A panic's Location (new quest): Location::caller and #[track_caller].
  • A panic hook (new quest): set_hook, take_hook, PanicHookInfo; requires the first.

Review

Codex (quest review, gpt-6.1-sol, medium):

🤖 Generated with Claude Code

@kixelated
kixelated marked this pull request as ready for review November 3, 2026 19:08
@pullfrog

pullfrog Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

This run was cancelled before it finished.

Pullfrog  | View workflow run | via Pullfrog

@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 36 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7841dc59-8173-4e61-9291-b2cb402a9941
Commits

Reviewing files that changed from the base of the PR and between f0b2a31 and 10b2168.

Files ignored due to path filters: 1
  • ts/Cargo.lock is excluded by !**/*.lock
Files selected for processing: 37
  • quest/m3/README.md
  • quest/m3/ts-panic-hook.md
  • quest/m3/ts-panic-location.md
  • ts/README.md
  • ts/fixtures/lang/panics/Cargo.toml
  • ts/fixtures/lang/panics/expected/core/any.Any.ts
  • ts/fixtures/lang/panics/expected/core/option.ts
  • ts/fixtures/lang/panics/expected/core/result.ts
  • ts/fixtures/lang/panics/expected/core/task/poll.ts
  • ts/fixtures/lang/panics/expected/harness.ts
  • ts/fixtures/lang/panics/expected/lib.ts
  • ts/fixtures/lang/panics/expected/panics.Countdown.ts
  • ts/fixtures/lang/panics/expected/panics.Logged.ts
  • ts/fixtures/lang/panics/expected/panics.Point.ts
  • ts/fixtures/lang/panics/expected/panics.ts
  • ts/fixtures/lang/panics/src/lib.rs
  • ts/fixtures/lang/panics/src/panics.rs
  • ts/fixtures/lang/panics/ts.toml
  • ts/fixtures/reject/census.txt
  • ts/fixtures/reject/expected.txt
  • ts/fixtures/reject/src/lib.rs
  • ts/fixtures/reject/src/panics.rs
  • ts/fixtures/reject/ts.toml
  • ts/runtime/rt.ts
  • ts/src/census.rs
  • ts/src/lower/body.rs
  • ts/src/lower/copy.rs
  • ts/src/lower/drop.rs
  • ts/src/lower/dynamic.rs
  • ts/src/lower/fmt.rs
  • ts/src/lower/library.rs
  • ts/src/lower/mir/call.rs
  • ts/src/lower/mir/place.rs
  • ts/src/lower/mir/rvalue.rs
  • ts/src/lower/mod.rs
  • ts/src/lower/task.rs
  • ts/tests/fixtures.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@kixelated
kixelated enabled auto-merge November 3, 2026 19:13
@kixelated
kixelated added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit 7e7e420 Nov 3, 2026
7 checks passed
@kixelated
kixelated deleted the ts-std-panic branch November 3, 2026 19:19
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