Skip to content

Bind an untyped/any catch's value as a real any; catch (e: number) takes an int - #347

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-untyped-catch-value
Sep 23, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-untyped-catch-value

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

catch (e) { print(e) } crashed the first time e was read, unless the code had thrown throw <any>x. That includes throw "message". catch (e: any) had the same problem. The catch variable is an any, but it received the raw thrown value. On Linux such a catch did not even catch a thrown number.

Windows

  • An int, double, string or class throw carries a .PEAX CatchableType, which is what an untyped catch filters on. That entry is no longer a plain byte copy. It now names a copy function: a small thunk that MLIRGen synthesizes (MLIRGenImpl::mlirGenCatchCopyThunk). The thunk boxes the value into an any, and the CRT calls it to build the catch variable.
  • An int throw also lists .N with a widening thunk, so catch (e: number) now catches throw 1. An integer literal is an int in tslang.
  • On x86 the CRT calls copy functions as __thiscall, so CxxThrowCallingConvPass sets that convention on every .eh.copy.* function.
  • The changed records, arrays and ThrowInfos have new names, because they are linkonce_odr. With the old names, an object built by an older compiler (for example a prebuilt default lib) could be merged with them and the linker would keep either copy.

Linux

  • An untyped/any catch used a _ZTIPv clause. In the Itanium ABI that clause matches only pointer exceptions, so a thrown number was never caught. It is now a real catch-all.
  • Binding the variable compares __cxa_current_exception_type() against the int, double, string and any typeinfos, and against the typeinfo of each class this module throws. MLIRGen records those classes in the module attribute ts.thrown_classes. The value is then boxed with its real type descriptor, which unboxing to a class needs.
  • A class thrown only from another module can't be identified at the catch site. It is boxed as a plain object, so reading it works, but casting it back to its class does not.
  • A number catch also lists the int typeinfo as a clause and widens the value.

Tests

  • New 00catch_untyped_value.ts: untyped/any catches read a number, int, string, class and derived class. It also checks catch (e: number) against throw 1 and that typed catches are unchanged.
  • 00throw_number_untyped_catch.ts was Windows-only and now runs on Linux too.
  • check-x86-eh.sh: the int ThrowInfo's array is now _CTA3H.

Verified locally

  • Windows ctest: 2806/2806.
  • Linux (WSL, ninja release) ctest: 2800/2800.
  • check-x86-eh.sh: 53/53 ok.
  • check-x86-run.sh: 21 ok, none failed.
  • An i686 AOT program exercising every case gives the same output as x64.

🤖 Generated with Claude Code

…ber) takes an int

`catch (e) { print(e) }` crashed unless the throw was `throw <any>x`: the catch
binds an `any`, but received the raw thrown value. Same for `catch (e: any)`.

Windows: the `.PEAX` CatchableType an int/double/string/class throw carries is
no longer a plain copy. It names a copy function (a thunk MLIRGen synthesizes)
that boxes the value, and the CRT calls it to build the catch variable
(`__thiscall` on x86 - CxxThrowCallingConvPass sets it). An int throw also
lists `.N` with a widening thunk, so `catch (e: number)` catches `throw 1`.
The changed records, arrays and ThrowInfos get new names so objects built
before them cannot be merged with them.

Linux: an untyped/any catch used a `_ZTIPv` clause, which only matches pointer
exceptions (a thrown number was never caught). It is a real catch-all now, and
binding it compares __cxa_current_exception_type() with each type tslang
throws - including the classes this module throws, recorded by MLIRGen - and
boxes accordingly. A `number` catch also lists the int type_info and widens.

00throw_number_untyped_catch.ts now runs on Linux too; new test
00catch_untyped_value.ts reads the caught values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 24141c3 into main Sep 23, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-untyped-catch-value branch September 23, 2026 12:07
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