Semantic Diff

#9414 Give each inlined copy of a function or functor body its own static exits

1 commit

6a7e3b8dbabcd2aa54643d86044d2f8ec2d6f804..pr-9414

ca7fc679a · illusionalsagacity · Nov 23, 2026, 20:27 UTC

Give each inlined copy of a function or functor body its own static exits

Why

Lam_bounded_vars.rewrite refreshes the bound variables of a function body before it is copied (an @inline function at each call site, a functor body at each application) but kept the ids of the static catches inside it. Two copies then shared an exit id, and Lam_pass_exits.subst_helper, which records inlinable handlers by exit id, substituted one copy's handler into the raises of the other, so the wrong arm of a switch with or-patterns or an | exception case ran. Each catch in the rewritten body now gets a fresh exit id (negative ids, which mark exception cases, stay negative) and the raises it scopes are redirected; raises to exits caught outside the body are left alone.

modifiedJS second inlined copy

@inline(never) let id = x => x@inline(never) let big = (s: string) => s ++ "!" ++ s @inlinelet pick = (x: int, y: int, flag: bool) =>  switch (x, y) {  | (0, _) | (_, 0) => flag ? "t" : big("a") ++ big("b")  | _ => "other"  } let constant = pick(0, 5, true)let computed = pick(id(0), id(5), id(false))
js
Before the first copy's handler "t" replaces the raise
After the raise reaches this copy's handler
let constant = "t";let constant = "t";……let exit = 0;let exit = 0; computed = x !== 0 && y !== 0 ? "other" : "t"; if (x !== 0 && y !== 0) { computed = "other";} else { exit = 1;} if (exit === 1) {if (exit === 1) { computed = flag ? "t" : big("a") + big("b"); computed = flag ? "t" : big("a") + big("b");}}

Review

  • Does a raise to an exit caught outside the copied body keep its id? Yes: only exits caught inside the body are renamed, and a catch's id is unmapped again before its handler is rewritten.
  • Is the bug only in @inline? No: a functor applied twice copies its body the same way and is covered by the tests, as are | exception cases, or-patterns nested in a handler or binding variables under a guard, and functions and functors of another module inlined with -bs-cross-module-opt.