Skip to content

Count the exception a try handler binds as bound - #9485

Merged
cknitt merged 1 commit into
inline-fallback-unloadable-modulefrom
closure-try-binder
Nov 30, 2026
Merged

cknitt merged 1 commit into
inline-fallback-unloadable-modulefrom
closure-try-binder

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Lam_closure didn't count the variable a try … catch handler binds as bound, so inside the handler it looked captured from outside. Every function containing a handler then counted as not closed. So an @inline function with a try … catch was never stored for other modules, and was always called.

Inlined copies get fresh names for that variable, since Lam_bounded_vars renames it like any other binder.

New test: orDefault in tests/tests/src/inline_assign_cross_module_lib.res, an @inline function with a handler, inlined twice in one function of inline_assign_cross_module_test.res. Both copies are inlined, and the second binder is renamed (exn$1). Other outputs, including the runtime's and Belt's, are the same.

This replaces #9481, which GitHub closed as merged when the stack was reordered: its commit became part of the branch it was based on. Part of stack #9486, after #9478.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to inline-fallback-unloadable-module November 29, 2026 09:29
@cknitt
cknitt added this pull request to stack #9486 November 29, 2026 09:29
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • 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.

Semantic Diff

1 commit · Full page

Count the exception a try handler binds as bound

87fe94d · cknitt + Claude · Nov 29, 2026, 09:03 UTC

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)

// 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),
)

js · After: the second copy's binder is renamed

-return [
-  Inline_assign_cross_module_lib.orDefault(() => 1, 0),
-  Inline_assign_cross_module_lib.orDefault(() => Stdlib_JsError.throwWithMessage("boom"), 2)
-];
+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.

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T09:32:45.960241Z 44b3341 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 44b3341e79

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 672,761,691 → 672,762,274 (±0.00%).

Generated projects: 630,418,670 → 630,418,804 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,225,746,058 → 9,225,758,584 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,303,751 → 27,303,679 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 87fe94d compared with base ed1ff33 in this run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9485

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9485

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9485

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9485

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9485

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9485

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9485

commit: 87fe94d

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.45%. Comparing base (3a025b4) to head (87fe94d).

Files with missing lines Patch % Lines
compiler/backend/lam_closure.ml 75.00% 1 Missing ⚠️
Additional details and impacted files
@@                          Coverage Diff                          @@
##           inline-fallback-unloadable-module    #9485      +/-   ##
=====================================================================
- Coverage                              82.45%   82.45%   -0.01%     
=====================================================================
  Files                                    465      465              
  Lines                                  65984    65987       +3     
=====================================================================
+ Hits                                   54406    54408       +2     
- Misses                                 11578    11579       +1     
Files with missing lines Coverage Δ
compiler/backend/lam_closure.ml 85.18% <75.00%> (-0.72%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cknitt
cknitt force-pushed the closure-try-binder branch from 17aa2b0 to 87fe94d Compare November 29, 2026 10:07
@cknitt
cknitt marked this pull request as ready for review November 29, 2026 12:08
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
@cknitt
cknitt merged commit 939c576 into master Nov 30, 2026
42 of 45 checks passed
@cknitt
cknitt deleted the closure-try-binder branch November 30, 2026 07:53
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.

2 participants