Skip to content

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

Merged
cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-inline-static-exit
Nov 28, 2026
Merged

cknitt merged 1 commit into
rescript-lang:masterfrom
illusionalsagacity:fix-inline-static-exit

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Part of #9398 (item 6).

Problem

Lam_bounded_vars.rewrite renames the bound variables of a body that is copied (an @inline function at each call site, a functor at each application), but it kept the ids of the static catches inside. Two copies then shared an exit id, and Lam_pass_exits substituted one copy's handler into the other, so the wrong arm of a switch ran. No @inline is needed: a functor applied twice does it too.

@inline(never) let id = x => x
@inline(never) let big = (s: string) => s ++ "!" ++ s

@inline
let 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))

Before:

let constant = "t";
…
let exit = 0;

computed = x !== 0 && y !== 0 ? "other" : "t";

if (exit === 1) {
  computed = flag ? "t" : big("a") + big("b");
}

After:

let constant = "t";
…
let exit = 0;

if (x !== 0 && y !== 0) {
  computed = "other";
} else {
  exit = 1;
}

if (exit === 1) {
  computed = flag ? "t" : big("a") + big("b");
}

Before, computed is "t" when it should be "a!ab!b".

Fix

Each catch in a copied body gets a fresh exit id and the raises it scopes are redirected to it. Raises to exits caught outside the body keep theirs. Negative ids, which mark exception cases, stay negative.

Tests

inline_static_exit_test.res covers two inlined copies, nested exits, an | exception case, an or-pattern binding a variable under a guard, and a functor applied twice. inline_static_exit_cross_module_test.res covers the same across modules (-bs-cross-module-opt), and 4 unit tests cover Lam_bounded_vars.rewrite itself. With the fix reverted, 7 of the mocha tests and all 4 unit tests fail. No other checked-in output changes.

12.x

The mocha tests all pass on 12.x without the change, so I have no backport for this one. The unit tests were not run there.

The fix was written by Claude. A second Claude agent reviewed the first version; this version addresses that review and has not been reviewed again.

@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a function body or functor body copied more than once (an @inline function inlined at several call sites, or a functor applied several times) running another copy's branch of a switch with or-patterns or an | exception case.

Semantic Diff

1 commit · Full page

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

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

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.

JS modified (second inlined copy)

@inline(never) let id = x => x
@inline(never) let big = (s: string) => s ++ "!" ++ s

@inline
let 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 exit = 0;
 
-computed = x !== 0 && y !== 0 ? "other" : "t";
+if (x !== 0 && y !== 0) {
+  computed = "other";
+} else {
+  exit = 1;
+}
 
 if (exit === 1) {
   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.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.28%. Comparing base (6a7e3b8) to head (ca7fc67).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 92.72% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9414      +/-   ##
==========================================
+ Coverage   82.25%   82.28%   +0.03%     
==========================================
  Files         462      462              
  Lines       65650    65733      +83     
==========================================
+ Hits        53999    54089      +90     
+ Misses      11651    11644       -7     
Files with missing lines Coverage Δ
compiler/backend/lam_bounded_vars.ml 78.16% <100.00%> (+6.55%) ⬆️
tests/ounit_tests/ounit_lam_bounded_vars_tests.ml 92.47% <92.72%> (+0.36%) ⬆️

... and 8 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.

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: ca7fc67

@cknitt

cknitt commented Nov 26, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 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-26T20:23:45.026477Z 4ef8899 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: 4ef8899756

ℹ️ 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".

@illusionalsagacity
illusionalsagacity marked this pull request as ready for review November 27, 2026 20:00
…xits

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.

JS modified (second inlined copy):
Example:
    @inline(never) let id = x => x
    @inline(never) let big = (s: string) => s ++ "!" ++ s

    @inline
    let 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))
Before (js): the first copy's handler "t" replaces the raise
    let constant = "t";
    …
    let exit = 0;

    computed = x !== 0 && y !== 0 ? "other" : "t";

    if (exit === 1) {
      computed = flag ? "t" : big("a") + big("b");
    }
After (js): the raise reaches this copy's handler
    let constant = "t";
    …
    let exit = 0;

    if (x !== 0 && y !== 0) {
      computed = "other";
    } else {
      exit = 1;
    }

    if (exit === 1) {
      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.
Review: 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.
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

@cknitt cknitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I built it and checked every Lambda constructor that binds or raises static exits; the cases from the description and nested inlining, closures and functors all give the right result. The rebase only keeps both sides of the test-file conflict with #9424.

This review was done by Claude.

@cknitt
cknitt merged commit 64cdd3c into rescript-lang:master Nov 28, 2026
23 checks passed
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