mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-25 06:24:27 +08:00
`x as Sub` lowered to a call to an interned %class.assert helper that unwrapped a union box, retained the payload twice, compared the class-id range, and threw on mismatch; every use paid a call, three retains, two releases and a pending check (2,152 sites in tsc-ts). Plain operands (locals and field reads, which isSafeToRepeat already admits) now lower to the check itself: per compatible union arm a tag test plus, where the arm is not already the target or a subclass, an instanceOf range test, selecting the downcast payload, with the helper's exact TypeError as the final arm. Other operands keep the helper so they are evaluated once. The backend borrows through the result: instanceOf reads its operand as a receiver instead of retaining a temporary, and downcast/upcast are borrowable, edge-preserving projections, so `(node as Sub).field` and method receivers on a cast no longer touch reference counts. The mismatch arm unwinds directly (previous commit) and stays out of the hot path. Corpus 4432 pins the Node-observable behavior of failing casts (a TypeError before any later read, unwinding through finally blocks and callbacks) and of successful casts from bases, subclasses and unions; a harness test pins the exact messages, which Node cannot oracle.