Skip to content

Fix a runtime ReferenceError when a local function is inlined twice - #9543

Open
cknitt wants to merge 1 commit into
masterfrom
tailcall-inline-fix
Open

cknitt wants to merge 1 commit into
masterfrom
tailcall-inline-fix

Conversation

@cknitt

@cknitt cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member

Fixes #9542.

When a local function is inlined in JavaScript into another one that is then inlined in turn, the generated code could drop a variable declaration. The program compiles, but throws ReferenceError: k is not defined at runtime.

This is a preexisting bug, in every release since January 2016. The faulty case was already in the first BuckleScript commit (78ffcbd, 2016-01-06). It became a bug with ddd8b4c (2016-01-19), which made the table that case reads a table of declarations, and added the inlining that processes a body a second time. From then on, "not in the table" no longer meant "unused".

Cause

Inlining c into b turns c's parameter k into let k = <function>. When b is then inlined into a, b's body is processed again. A case in Js_pass_tailcall_inline looked k up in the table of declarations collected before the pass, didn't find it, and kept only the declaration's value as an expression statement:

// before
(x => x + n | 0);
…
return k(n);

// after
let k = x => x + n | 0;
…
return k(n);

Fix

The case is removed. For every declaration the table knows, it did the same as the default case. The default case now processes the rest of the block before its first statement explicitly, instead of relying on OCaml's evaluation order.

Test

tailcall_inline_nested_test runs this shape and checks the result. On master it throws the ReferenceError. No other compiler test output changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix a runtime ReferenceError when a local function is inlined twice.

Semantic Diff

1 commit · Full page

Fix a runtime ReferenceError when a local function is inlined twice

eaf5e6e · cknitt + Claude · Dec 05, 2026, 16:55 UTC

Why

The JavaScript inlining of local functions could drop a variable declaration and leave its uses unbound. When c is inlined into b, its parameter k becomes let k = <function> in b's body. When b is in turn inlined into a, that body is processed again, and a case for a function declaration at the head of a block looked k up in the table of declarations collected before the pass. k is not in it, so the case took the declaration for dead and kept only its value as an expression statement. Calling k then failed with a ReferenceError. The case is removed: for every declaration the table knows, it did what the default case does.

The case goes back to the first BuckleScript commit (78ffcbd, 2016-01-06, jscomp/js_inline_and_eliminate.ml), where a variable missing from the table was one never used. ddd8b4c (2016-01-19) made the table one of declarations and added the inlining of a returned call, which processes a body again. Since then a missing entry no longer means that the variable is unused.

JS modified (a function inlined into one that is inlined in turn)

let main = z => {
  let c = (k, y) => {
    Console.log2("c", y)
    Console.log2("c", y + 1)
    Console.log2("c", y + 2)
    k(y)
  }
  let rec a = n => {
    Console.log2("a", n)
    Console.log2("a", n + 1)
    Console.log2("a", n + 2)
    n > 10 ? b(n) : 0
  }
  and b = n => {
    Console.log2("b", n)
    Console.log2("b", n + 1)
    Console.log2("b", n + 2)
    n > 100 ? a(n - 100) : c(x => x + n, n)
  }
  a(z) + a(z + 1)
}

js

 } else {
-  (x => x + n | 0);
+  let k = x => x + n | 0;
   console.log("c", n);
   …
   return k(n);
 }

The default case now processes the rest of a block before its first statement explicitly, rather than through OCaml's right-to-left evaluation of a list's head and tail: inlining a call in the rest marks the function's declaration dead, which then removes it.

Review

  • What covers it? tailcall_inline_nested_test runs this case and checks the result; on master it throws the ReferenceError. No other compiler test output changes.

@cknitt
cknitt force-pushed the tailcall-inline-fix branch from 9793781 to bc2bf9b Compare December 5, 2026 20:32
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (daa4f2d) to head (eaf5e6e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9543   +/-   ##
=======================================
  Coverage   82.68%   82.68%           
=======================================
  Files         469      469           
  Lines       66582    66576    -6     
=======================================
- Hits        55052    55050    -2     
+ Misses      11530    11526    -4     
Files with missing lines Coverage Δ
compiler/backend/js_pass_tailcall_inline.ml 93.24% <100.00%> (+3.24%) ⬆️

... and 1 file 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 Dec 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: eaf5e6e

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 656,460,725 → 656,456,571 (±0.00%).

Generated projects: 628,524,944 → 628,523,921 (±0.00%).

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

Instructions

In total: 8,976,406,245 → 8,974,759,100 (-0.02%).

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

Output size

In total: 27,594,926 → 27,594,640 (±0.00%).

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

Pull request eaf5e6e compared with base daa4f2d in this run.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the tailcall-inline-fix branch from bc2bf9b to d8022e1 Compare December 6, 2026 06:23
@cknitt cknitt changed the title Keep the declarations of a function inlined twice in JavaScript Fix a runtime ReferenceError when a local function is inlined twice Dec 6, 2026
@cknitt
cknitt marked this pull request as ready for review December 6, 2026 06:25
@cknitt
cknitt requested a review from cristianoc December 6, 2026 06:25
Why:
    The JavaScript inlining of local functions could drop a variable
    declaration and leave its uses unbound. When `c` is inlined into
    `b`, its parameter `k` becomes `let k = <function>` in `b`'s body.
    When `b` is in turn inlined into `a`, that body is processed again,
    and a case for a function declaration at the head of a block looked
    `k` up in the table of declarations collected before the pass. `k`
    is not in it, so the case took the declaration for dead and kept
    only its value as an expression statement. Calling `k` then failed
    with a `ReferenceError`. The case is removed: for every declaration
    the table knows, it did what the default case does.

    The case goes back to the first BuckleScript commit (78ffcbd,
    2016-01-06, `jscomp/js_inline_and_eliminate.ml`), where a variable
    missing from the table was one never used. ddd8b4c (2016-01-19)
    made the table one of declarations and added the inlining of a
    returned call, which processes a body again. Since then a missing
    entry no longer means that the variable is unused.

JS modified (a function inlined into one that is inlined in turn):
Example:
    let main = z => {
      let c = (k, y) => {
        Console.log2("c", y)
        Console.log2("c", y + 1)
        Console.log2("c", y + 2)
        k(y)
      }
      let rec a = n => {
        Console.log2("a", n)
        Console.log2("a", n + 1)
        Console.log2("a", n + 2)
        n > 10 ? b(n) : 0
      }
      and b = n => {
        Console.log2("b", n)
        Console.log2("b", n + 1)
        Console.log2("b", n + 2)
        n > 100 ? a(n - 100) : c(x => x + n, n)
      }
      a(z) + a(z + 1)
    }
Before (js):
    } else {
      (x => x + n | 0);
      console.log("c", n);
      …
      return k(n);
    }
After (js):
    } else {
      let k = x => x + n | 0;
      console.log("c", n);
      …
      return k(n);
    }

The default case now processes the rest of a block before its first
statement explicitly, rather than through OCaml's right-to-left
evaluation of a list's head and tail: inlining a call in the rest marks
the function's declaration dead, which then removes it.

Review: What covers it? `tailcall_inline_nested_test` runs this case and
checks the result; on master it throws the `ReferenceError`. No other
compiler test output changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the tailcall-inline-fix branch from d8022e1 to eaf5e6e Compare December 6, 2026 07:14
@cknitt

cknitt commented Dec 6, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 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-12-06T10:04:23.919047Z eaf5e6e 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. Keep it up!

Reviewed commit: eaf5e6ea9c

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

This branch has not been deployed

No deployments
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.

Generated code crashes with ReferenceError when a local function is inlined twice

2 participants