Skip to content

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

Merged
cknitt merged 0 commit into
remove-cross-module-optfrom
closure-try-binder
Nov 29, 2026
Merged

cknitt merged 0 commit into
remove-cross-module-optfrom
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.

The only output that changes is tests/tests/src/inline_export_test.mjs. There, Inline_export.catchMessage is now inlined:

let fromCatch;

try {
  fromCatch = Stdlib_JsError.throwWithMessage("boom");
} catch (raw_e) {
  let e = Primitive_exceptions.internalToException(raw_e);
  fromCatch = e.RE_EXN_ID === "JsExn" ? Stdlib_JsExn.message(e._1) : undefined;
}

The runtime's, Belt's and all other test outputs are the same, including inlining within a module.

Part of stack #9474, after #9480.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to remove-cross-module-opt November 29, 2026 08:10
@cknitt
cknitt added this pull request to stack #9474 November 29, 2026 08:10
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Inline @inline functions that contain try … catch in other modules too. They used to be called, because the variable a catch binds counted as captured from outside.

Semantic Diff

2 commits · Full page

Count the exception a try handler binds as bound

2463524 · cknitt + Claude · Nov 29, 2026, 08:08 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_export.res
@inline
let catchMessage = f =>
  try f() catch {
  | JsExn(e) => JsExn.message(e)
  | _ => None
  }

// inline_export_test.res
let fromCatch = Inline_export.catchMessage(() => JsError.throwWithMessage("boom"))

js

-let fromCatch = Inline_export.catchMessage(() => Stdlib_JsError.throwWithMessage("boom"));
+let fromCatch;
+
+try {
+  fromCatch = Stdlib_JsError.throwWithMessage("boom");
+} catch (raw_e) {
+  let e = Primitive_exceptions.internalToException(raw_e);
+  fromCatch = e.RE_EXN_ID === "JsExn" ? Stdlib_JsExn.message(e._1) : undefined;
+}

Review

  • Does inlining within a module change? Not in the repository's tests: the runtime, Belt and every test output other than this one are the same.
Add the inlined catchMessage output and the changelog entry

a717669 · cknitt + Claude · Nov 29, 2026, 08:10 UTC

The test output and the changelog fragment belonging to the previous commit, which they were left out of.

@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-29T08:12:00.911117Z 4056364 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. Another round soon, please!

Reviewed commit: 4056364fba

ℹ️ 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,238,220 → 672,252,509 (±0.00%).

Generated projects: 630,219,095 → 630,218,080 (±0.00%).

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

Instructions

In total: 9,225,935,307 → 9,225,944,970 (±0.00%).

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

Output size

In total: 27,284,100 → 27,284,222 (±0.00%).

Benchmark Base Head Change
output/tests/*.cmj 99,226 99,421 +0.20%

Pull request a717669 compared with base df0b61d in this run.

@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.43%. Comparing base (b55d092) to head (a717669).

Files with missing lines Patch % Lines
compiler/backend/lam_closure.ml 75.00% 1 Missing ⚠️
Additional details and impacted files
@@                   Coverage Diff                    @@
##           remove-cross-module-opt    #9481   +/-   ##
========================================================
  Coverage                    82.43%   82.43%           
========================================================
  Files                          465      465           
  Lines                        65953    65956    +3     
========================================================
+ Hits                         54366    54371    +5     
+ Misses                       11587    11585    -2     
Files with missing lines Coverage Δ
compiler/backend/lam_closure.ml 74.07% <75.00%> (-0.29%) ⬇️

... and 2 files with indirect coverage changes

🚀 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 4056364 to a717669 Compare November 29, 2026 08:32
@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@9481

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: a717669

@cknitt
cknitt merged commit 44b3341 into master Nov 29, 2026
@cknitt
cknitt deleted the closure-try-binder branch November 29, 2026 09:26
@cknitt
cknitt force-pushed the closure-try-binder branch from a717669 to 44b3341 Compare November 29, 2026 09:26
@cknitt
cknitt restored the closure-try-binder branch November 29, 2026 09:29
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

This was not merged into master. When the inlining stack was reordered, this commit moved below #9480, so it became part of the branch this PR was based on, and GitHub closed the PR as merged into that branch. It continues as #9485, unchanged, in stack #9486.

(Comment by Claude.)

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