From 87fe94df5edfd95ecc4f7c429229bd3829a12429 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 29 Nov 2026 09:03:15 +0000 Subject: [PATCH] Count the exception a try handler binds as bound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: Lam_closure did not add the variable a `try … catch` handler binds to the bound variables, so inside the handler it counted as free, and every function containing a handler looked as if it captured a variable. Such functions were never closed, so an `@inline` function with a `try … catch` was never stored for other modules and always called. Inlined copies get fresh names for that variable, since Lam_bounded_vars renames it like any other binder. JS modified (a module using an @inline function with try … catch): Example: // inline_assign_cross_module_lib.res @inline let orDefault = (f, default) => try f() catch { | _ => default } // inline_assign_cross_module_test.res let caught = () => ( Inline_assign_cross_module_lib.orDefault(() => 1, 0), Inline_assign_cross_module_lib.orDefault(() => JsError.throwWithMessage("boom"), 2), ) Before (js): return [ Inline_assign_cross_module_lib.orDefault(() => 1, 0), Inline_assign_cross_module_lib.orDefault(() => Stdlib_JsError.throwWithMessage("boom"), 2) ]; After (js): the second copy's binder is renamed function caught() { let tmp; try { tmp = 1; } catch (exn) { tmp = 0; } let tmp$1; try { throw new Error("boom"); } catch (exn$1) { tmp$1 = 2; } return [ tmp, tmp$1 ]; } Review: Does inlining within a module change? Not in the repository's tests: the runtime, Belt and every other test output are the same. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D Signed-off-by: Christoph Knittel --- changes/inline-try-catch.polish.md | 1 + compiler/backend/lam_closure.ml | 7 +++- .../src/inline_assign_cross_module_lib.mjs | 9 +++++ .../src/inline_assign_cross_module_lib.res | 8 ++++ .../src/inline_assign_cross_module_test.mjs | 38 +++++++++++++++---- .../src/inline_assign_cross_module_test.res | 9 +++++ 6 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 changes/inline-try-catch.polish.md diff --git a/changes/inline-try-catch.polish.md b/changes/inline-try-catch.polish.md new file mode 100644 index 0000000000..16d6ff78c5 --- /dev/null +++ b/changes/inline-try-catch.polish.md @@ -0,0 +1 @@ +- When inlining functions from other modules, include `@inline` functions that contain `try … catch`. They used to be called, because the variable a `catch` binds counted as captured from outside. diff --git a/compiler/backend/lam_closure.ml b/compiler/backend/lam_closure.ml index 5e51d66285..843ca143f2 100644 --- a/compiler/backend/lam_closure.ml +++ b/compiler/backend/lam_closure.ml @@ -117,7 +117,12 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t) iter sink_pos e1; local_add_list vars; iter sink_pos e2 - | Ltrywith (e1, _, e2) | Ltryfinally (e1, e2) -> + | Ltrywith (e1, exn, e2) -> + iter top e1; + (* The handler binds the exception. *) + local_add exn; + iter sink_pos e2 + | Ltryfinally (e1, e2) -> iter top e1; iter sink_pos e2 | Lifthenelse (e1, e2, e3) -> diff --git a/tests/tests/src/inline_assign_cross_module_lib.mjs b/tests/tests/src/inline_assign_cross_module_lib.mjs index e3da4caba6..bb9487176c 100644 --- a/tests/tests/src/inline_assign_cross_module_lib.mjs +++ b/tests/tests/src/inline_assign_cross_module_lib.mjs @@ -45,11 +45,20 @@ function matchAfterAssign(x) { } } +function orDefault(f, $$default) { + try { + return f(); + } catch (exn) { + return $$default; + } +} + export { bumpTwice, readThenAssign, copyThenAssign, negated, matchAfterAssign, + orDefault, } /* No side effect */ diff --git a/tests/tests/src/inline_assign_cross_module_lib.res b/tests/tests/src/inline_assign_cross_module_lib.res index 14d7b076d0..1264c2671c 100644 --- a/tests/tests/src/inline_assign_cross_module_lib.res +++ b/tests/tests/src/inline_assign_cross_module_lib.res @@ -49,3 +49,11 @@ let matchAfterAssign = (x: string) => { | _ => 2 } } + +// Inlined by inline_assign_cross_module_test.res: the handler binds the +// exception, which each inlined copy renames. +@inline +let orDefault = (f, default) => + try f() catch { + | _ => default + } diff --git a/tests/tests/src/inline_assign_cross_module_test.mjs b/tests/tests/src/inline_assign_cross_module_test.mjs index 50709fc967..a2ef08121d 100644 --- a/tests/tests/src/inline_assign_cross_module_test.mjs +++ b/tests/tests/src/inline_assign_cross_module_test.mjs @@ -76,9 +76,28 @@ function matchAfterAssignA() { } } +function caught() { + let tmp; + try { + tmp = 1; + } catch (exn) { + tmp = 0; + } + let tmp$1; + try { + throw new Error("boom"); + } catch (exn$1) { + tmp$1 = 2; + } + return [ + tmp, + tmp$1 + ]; +} + Mocha.describe("Inline_assign_cross_module_test", () => { - Mocha.test("a variable the inlined function assigns, twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 34, characters 7-14", 14, addBumped(id(3), id(4)))); - Mocha.test("a variable the inlined function assigns, with a constant argument", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 38, characters 7-14", [ + Mocha.test("a variable the inlined function assigns, twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 39, characters 7-14", 14, addBumped(id(3), id(4)))); + Mocha.test("a variable the inlined function assigns, with a constant argument", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 43, characters 7-14", [ 12, 4 ], bumpConstants())); @@ -86,27 +105,31 @@ Mocha.describe("Inline_assign_cross_module_test", () => { let r = 5; let p = r; r = 10; - Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 42, characters 7-14", 15, p + r | 0); + Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 47, characters 7-14", 15, p + r | 0); }); Mocha.test("a copy of an assigned variable, made before the assignment", () => { let r = 1; let s = r; r = s + 1 | 0; - Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 46, characters 7-14", 3, s + r | 0); + Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 51, characters 7-14", 3, s + r | 0); }); - Mocha.test("a read of an assigned variable, passed to a bound closure before the assignment", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 50, characters 7-14", 15, readBeforeAssignBound())); + Mocha.test("a read of an assigned variable, passed to a bound closure before the assignment", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 55, characters 7-14", 15, readBeforeAssignBound())); Mocha.test("a condition on an assigned variable bound to a constant", () => { let r = true; r = !r; - Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 54, characters 7-14", 2, r ? 1 : 2); + Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 59, characters 7-14", 2, r ? 1 : 2); }); Mocha.test("a switch on an assigned variable bound to a constant", () => { let r = "a"; r = "b"; let match = r; let tmp = match === "a" ? 1 : 2; - Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 58, characters 7-14", 2, tmp); + Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 63, characters 7-14", 2, tmp); }); + Mocha.test("a function with a try handler, inlined twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 67, characters 7-14", [ + 1, + 2 + ], caught())); }); export { @@ -118,5 +141,6 @@ export { readBeforeAssignBound, negatedTrue, matchAfterAssignA, + caught, } /* Not a pure module */ diff --git a/tests/tests/src/inline_assign_cross_module_test.res b/tests/tests/src/inline_assign_cross_module_test.res index e3346cfaf0..02be8aa57f 100644 --- a/tests/tests/src/inline_assign_cross_module_test.res +++ b/tests/tests/src/inline_assign_cross_module_test.res @@ -29,6 +29,11 @@ let negatedTrue = () => Inline_assign_cross_module_lib.negated(true) let matchAfterAssignA = () => Inline_assign_cross_module_lib.matchAfterAssign("a") +let caught = () => ( + Inline_assign_cross_module_lib.orDefault(() => 1, 0), + Inline_assign_cross_module_lib.orDefault(() => JsError.throwWithMessage("boom"), 2), +) + describe(__MODULE__, () => { test("a variable the inlined function assigns, twice in a function", () => { eq(__LOC__, 14, addBumped(id(3), id(4))) @@ -57,4 +62,8 @@ describe(__MODULE__, () => { test("a switch on an assigned variable bound to a constant", () => { eq(__LOC__, 2, matchAfterAssignA()) }) + + test("a function with a try handler, inlined twice in a function", () => { + eq(__LOC__, (1, 2), caught()) + }) })