Repository navigation
Give each inlined copy of a function or functor body its own static exits - #9414
Conversation
Changelog🐛 Bug fix
Semantic Diff1 commit · Full page Give each inlined copy of a function or functor body its own static exitsca7fc67 · illusionalsagacity · Nov 23, 2026, 20:27 UTC
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))
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
|
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
2ee9874 to
4ef8899
Compare
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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>
4ef8899 to
ca7fc67
Compare
cknitt
left a comment
There was a problem hiding this comment.
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.
Part of #9398 (item 6).
Problem
Lam_bounded_vars.rewriterenames the bound variables of a body that is copied (an@inlinefunction 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, andLam_pass_exitssubstituted one copy's handler into the other, so the wrong arm of aswitchran. No@inlineis needed: a functor applied twice does it too.Before:
After:
Before,
computedis"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.rescovers two inlined copies, nested exits, an| exceptioncase, an or-pattern binding a variable under a guard, and a functor applied twice.inline_static_exit_cross_module_test.rescovers the same across modules (-bs-cross-module-opt), and 4 unit tests coverLam_bounded_vars.rewriteitself. 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.