Skip to content

A finally that throws during unwinding replaces the exception; fewer-parameter functions as wider function types - #733

Merged
ASDAlexander77 merged 2 commits into
mainfrom
finally-throw
Oct 29, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
finally-throw

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Two fixes found while working through the open items from the last rounds.

A finally that throws while an exception unwinds now replaces it (Windows)

try {
    try { thrower(1); } finally { thrower(2); }
} catch (e: number) { /* e is 2, as in JavaScript */ }

On Windows this terminated the process. The finally that runs while an exception unwinds was a cleanup funclet, and a second exception thrown from a cleanup funclet counts as one thrown during unwinding.

The fix:

  • Catch-all form. A finally that may throw (it contains a call, a throw or an accessor) now uses the catch-all form that a return inside finally already used. That form rethrows at its end. An exception thrown from inside it is a nested exception, which replaces the first one, as JavaScript requires.
  • Clone fix. The copy of the finally on that path also lost its calls' unwind entries. cloneRegionBefore carried over only jumps and parentTryOp, so a call there became a plain call with no way out of the funclet. At -O0 that happened to work; under --opt, WinEHPrepare treated the call as unreachable and the program crashed. The clone now carries over the unwind entries, and the interface-field access records from EH fixes: interface-field accessors in try, a call in a catch to a typed catch, uncaught descriptions; ++/-- on accessors #732, for the same reason.

New test: 00try_finally_throws.ts. It covers a call or a throw in finally replacing the exception, a throw on the normal path, a finally that doesn't throw (the original exception is rethrown), and an outer finally that runs on the new exception's way out.

A function with fewer parameters can be passed where a wider function type is expected

apply(one), where apply takes (a: number, b: number) => number and one takes only a, was an error ("it takes 1 argument(s), 2 expected"). TypeScript allows a function to ignore the parameters it doesn't declare.

  • Wrapper. A named function, or an arrow that captures nothing, is now passed as its own private function <name>.args<N>. The wrapper takes all N parameters and calls the original with the ones it declares. Nothing is captured, so own mode is fine with it.
  • Void results. No result and a void result count as the same.
  • Not covered. A closure is still an error, because its wrapper would have to carry the captured variables.

New test: 00function_fewer_parameters.ts.

Found, not fixed

  • "k" in u for a union of interfaces needs the union to record which interface it holds. Its tag is "interface" for every member, so in and narrowing have nothing to test at run time.
  • An object literal can't be passed as such a union ('ts.Cast' op type ... can't be stored).
  • typeof an interface value is "interface", where JavaScript says "object".

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits October 29, 2026 12:39
On Windows the finally run on the way out of an exception was a cleanup
funclet, which may not throw: a call or a throw there was a second exception
during unwinding, and the process terminated. A finally that may throw is now
entered through the catch-all pad a `return` in it already used, which
rethrows at its end; thrown from it, an exception is a nested one, which
replaces the first, as JavaScript's does.

The copy of the finally on that path also lost its calls' unwind entries
(cloneRegionBefore carried over jumps and parentTryOp only): a call there was
a plain call, which in the funclet has no way out, and -O3 crashed on it. Its
unwind entries, and the interface-field accesses of #732, are carried over now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`apply(one)` where `apply` takes `(a: number, b: number) => number` and `one`
takes `a` only was an error ("it takes 1 argument(s), 2 expected"), though
TypeScript lets a function ignore the parameters it does not declare. A named
function or an arrow that captures nothing is now given as a private function
of its own (`<name>.args<N>`) that takes them all and calls it with the first
ones. No result and a `void` one count as the same. A closure is still an
error: its wrapper would have to carry the captures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit ded8116 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the finally-throw branch October 29, 2026 13:16
ASDAlexander77 added a commit that referenced this pull request Oct 29, 2026
…pe (#735)

`apply((a: number) => a + k)` for `apply(f: (a: number, b: number) =>
number)` was "it takes 2 argument(s), 2 expected": the arrow's captures
counted as a parameter, and #733's thunk covered only named functions
and arrows that capture nothing.

fewerParametersThunk also takes a closure (ts.CreateBoundFunction with
__owns_capture over a symbol): the thunk takes the captures first, then
every parameter of the type, and calls the arrow with the captures and
the first ones. The closure is rebound to the thunk in place, so what
owns its captures (rc's retain and release, own's box) is unchanged.

Test: 00closure_fewer_parameters (compile and JIT; gc, rc, none, own).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 29, 2026
…s; design notes (#738)

* `in` and `instanceof` on a union of interfaces

`"swim" in a` for `a: Fish | Bird` was "not supported", and
`a instanceof Fish` was always false (the types compared, `Fish | Bird`
against `Fish`). Every interface's union tag reads "interface", so
nothing at run time said which interface the union held.

Which members have the key, or are the interface, is decided when
compiling: every one or none folds to a constant. Otherwise the union's
tag is asked: an interface's descriptor now has an id of its name in
TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's.
`"k" in a` narrows `a` to the members with the key (and the `else` to
the others), as `instanceof` already narrowed.

Test: 00in_interface_union (compile and JIT; gc, rc, none, own).

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

* An object literal or class object given to a union of interfaces

`move({ swim: () => "s" })` for `move(a: Fish | Bird)` failed with
"'ts.Cast' op type [...] can't be stored in [...]", and so did a class
object. castToTaggedUnionType now casts such a value to the first
interface member whose every required field and method it has
(interfaceMemberFitting, by name, as `"k" in v` asks), and the union's
tag says which (`"swim" in a`, #737).

Test: 00interface_union_literal (compile and JIT; gc, rc, none, own).

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

* Fewer-parameter callbacks: generic inference and a thunk's owned result

Two bugs in the fewer-parameters thunk (#733, #735), found by giving the
default library's array callbacks an index parameter:

- A closure given for a generic callback `(v: T, i: number) => void` had
  its captures paired with `T` (`string | Reference<{...}>`): its type is
  its function bound over its captures. Inference now drops the bound
  first parameter, of the closure's type and of the arrow type rebuilt.
- Under -mm=own a heap result the thunk hands on (a string a `map`
  callback builds) was "a second reference": the thunk's call is now
  marked owned and consumed by its return, as `return f(x)` is.

Test: 00closure_fewer_parameters (a generic callback, a string result).

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

* Design notes: generic interface methods, classes as values

- docs/generic-interface-methods-design.md: generic methods in interfaces
  (rejected today) and in class hierarchies (not virtual: an override is
  never reached through a base reference, silently). Options: erased
  `any` slots, a closed-world table, dictionary passing; recommends an
  error for overridden generic class methods now, erased slots later.
- docs/classes-as-values-design.md: `typeof A` values (ts.ClassRef has no
  value lowering) and local classes that capture (members lifted with an
  extra parameter nobody passes). Recommends the vtable as the class
  value with a `.constructor` slot, and the captures in a hidden
  `.captured` field of the instance.
- docs/tuple-object-shape-ambiguity-design.md: re-checked, still open.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 30, 2026
…ge_check is private to each module (#747)

Two failures only a debug build sees, found building the debug default library as the release
workflow does (`--di --opt_level=0`, TSLANG_DEBUG_CRT=/MT) and running its tests:

- `a.every((v) => v < 40)` - since array callbacks take the element's index too, any one-parameter
  callback - was rejected by LLVM in every model: "inlinable function call in a function with a
  DISubprogram location must have a debug location". fewerParametersThunk (#733) made the thunk with
  no location; with --di the debug-info pass gave it a subprogram and its call had none. It now gets a
  subprogram at the place that first needs it and its body is scoped under it, as the catch copy
  thunk (mlirGenCatchCopyThunk) does.
- A program linked against the debug default library failed with "LNK2005: ___range_check already
  defined": the RangeError check (#731) is generated from source as a public function in every module
  that checks, the library's included. It is private to each module now (internal linkage, as the
  thunks). An optimized link dropped one copy, which is why release builds linked.

test-compile-debug-info-glue (debug-info-glue.cmake): the fewer-parameter tests emitted with
`--di --opt_level=0` under every model - test-runner in a release build never compiles with --di.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77 added a commit that referenced this pull request Oct 30, 2026
…er terminates (#749)

A disposal that threw on the way out of an exception - `try { using d = ...; throw "x" } catch`
with a throwing dispose - terminated the process in every model, in both tiers, on both back ends.

- Windows: the cleanup region ran in a cleanup funclet, and an exception out of one is thrown
  during unwinding to the MSVC personality (0x80000003). A cleanup that may throw is now entered
  through a catch-all pad, as #733 did for `finally` (windowsCleanupCatches).
- Linux: the cleanup shared its landing pad with the try's catch, ran before it with nothing
  caught, and its dispose call had no unwind edge, so the exception left the function. With a
  catch or finally of its own, such a cleanup is a catch-all of its own that rethrows to them
  (linuxCleanupCatches).
- The cleanup's calls unwind to a copy of the cleanup (cleanupRetries), which re-runs it - each
  disposal's flag is cleared before its call - so the resources still to dispose are disposed and
  the releases after the throwing call run. Without it the ownership verifier reported every
  `using` as leaking on that path. The copy's pad is nested in the cleanup's, so the cleanup's
  terminator moves after it, where Win32ExceptionPass reads the nesting from.

The disposal's exception replaces the one it interrupted, as a throw in a `finally` does; there
is no SuppressedError. A second disposal that throws is a nested exception from the copy: this
try's catch takes it on Windows, the caller on Linux.

Test: 00using_dispose_throws.ts (compile, JIT, and the rc/none corpus). Also corrects the
00nested_catch.ts header, which still said reading a catch value was broken (item 5am, fixed).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant