From bc2bf9b163ff52b795c26ee091a9d2bbb8af87cc Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Dec 2026 16:55:53 +0000 Subject: [PATCH] Keep the declarations of a function inlined twice in JavaScript MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 = ` 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 (78ffcbdd4f, 2016-01-06, `jscomp/js_inline_and_eliminate.ml`), where a variable missing from the table was one never used. ddd8b4cdb9 (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 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- .../tailcall-inline-nested-declaration.fix.md | 1 + compiler/backend/js_pass_tailcall_inline.ml | 37 ++++------------ .../src/tailcall_inline_nested_test.mjs | 43 +++++++++++++++++++ .../src/tailcall_inline_nested_test.res | 42 ++++++++++++++++++ 4 files changed, 95 insertions(+), 28 deletions(-) create mode 100644 changes/tailcall-inline-nested-declaration.fix.md create mode 100644 tests/compiler_tests/src/tailcall_inline_nested_test.mjs create mode 100644 tests/compiler_tests/src/tailcall_inline_nested_test.res diff --git a/changes/tailcall-inline-nested-declaration.fix.md b/changes/tailcall-inline-nested-declaration.fix.md new file mode 100644 index 0000000000..7cb4552cee --- /dev/null +++ b/changes/tailcall-inline-nested-declaration.fix.md @@ -0,0 +1 @@ +- Fix a `ReferenceError` when a local function that is inlined into another one in turn passes a function to a third one it inlines: the parameter's declaration was dropped. diff --git a/compiler/backend/js_pass_tailcall_inline.ml b/compiler/backend/js_pass_tailcall_inline.ml index 704b6ac419..be91d4c85c 100644 --- a/compiler/backend/js_pass_tailcall_inline.ml +++ b/compiler/backend/js_pass_tailcall_inline.ml @@ -178,7 +178,7 @@ let inline_call (assignable : Set_ident.t) (immutable_list : bool list) params *) let super = Js_record_map.super -let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats = +let subst (assignable : Set_ident.t) stats = { super with statement = @@ -203,23 +203,6 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats = block = (fun self bs -> match bs with - | ({ - statement_desc = - Variable - ({value = Some ({expression_desc = Fun _; _} as v)} as vd); - comment = _; - } as st) - :: rest -> ( - let is_export = Set_ident.mem vd.ident export_set in - if is_export then self.statement self st :: self.block self rest - else - match Hash_ident.find_opt stats vd.ident with - (* TODO: could be improved as [mem] *) - | None -> - if Js_analyzer.no_side_effect_expression v then - S.exp v :: self.block self rest - else self.block self rest - | Some _ -> self.statement self st :: self.block self rest) | [ ({ statement_desc = @@ -262,14 +245,9 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats = self.block self body (* see #278 before changes*) in + (* The function is marked dead above, so its declaration, which + comes earlier in the block, is removed: see [x :: xs] below *) inline_call assignable no_tailcall params args processed_blocks - (* Ext_list.fold_right2 - params args processed_blocks - (fun param arg acc -> - S.define_variable ~kind:Variable param arg :: acc) *) - (* Mark a function as dead means it will never be scanned, - here we inline the function - *) | None | Some _ -> [self.statement self st]) | [ { @@ -302,12 +280,15 @@ let subst (export_set : Set_ident.t) (assignable : Set_ident.t) stats = (* see #278 before changes*) in inline_call assignable no_tailcall params args processed_blocks - | x :: xs -> self.statement self x :: self.block self xs + | x :: xs -> + (* The rest first: inlining a call in it marks the declaration of + the function dead, which removes it here *) + let xs = self.block self xs in + self.statement self x :: xs | [] -> []); } let tailcall_inline ~assignable (program : J.program) = let stats = Js_pass_get_used.get_stats program in - let export_set = program.export_set in - let obj = subst export_set assignable stats in + let obj = subst assignable stats in obj.program obj program diff --git a/tests/compiler_tests/src/tailcall_inline_nested_test.mjs b/tests/compiler_tests/src/tailcall_inline_nested_test.mjs new file mode 100644 index 0000000000..4ce7b7dd55 --- /dev/null +++ b/tests/compiler_tests/src/tailcall_inline_nested_test.mjs @@ -0,0 +1,43 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; + +let steps = { + contents: 0 +}; + +function main(z) { + let a = n => { + steps.contents = steps.contents + n | 0; + steps.contents = (steps.contents + n | 0) + 1 | 0; + steps.contents = (steps.contents + n | 0) + 2 | 0; + if (n > 10) { + steps.contents = steps.contents + n | 0; + steps.contents = (steps.contents + n | 0) + 1 | 0; + steps.contents = (steps.contents + n | 0) + 2 | 0; + if (n > 100) { + return a(n - 100 | 0); + } else { + let k = x => x + n | 0; + steps.contents = steps.contents + n | 0; + steps.contents = (steps.contents + n | 0) + 1 | 0; + steps.contents = (steps.contents + n | 0) + 2 | 0; + return k(n); + } + } else { + return 0; + } + }; + return a(z) + a(z + 1 | 0) | 0; +} + +Mocha.describe("Tailcall_inline_nested_test", () => { + Mocha.test("a function inlined into one that is inlined in turn", () => Test_utils.eq("File \"tailcall_inline_nested_test.res\", line 41, characters 71-78", main(20), 82)); +}); + +export { + steps, + main, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/tailcall_inline_nested_test.res b/tests/compiler_tests/src/tailcall_inline_nested_test.res new file mode 100644 index 0000000000..4c1ed6339b --- /dev/null +++ b/tests/compiler_tests/src/tailcall_inline_nested_test.res @@ -0,0 +1,42 @@ +// `b` is inlined into `a` in JavaScript, and `c` into `b`. Inlining `c` binds +// its parameter `k` to a function, so the body of `b` that `a` gets declares +// `k`. That declaration must stay a declaration: it was once turned into an +// expression statement, and calling `k` failed with a ReferenceError. +open Mocha +open Test_utils + +let steps = ref(0) + +let main = z => { + let c = (k, y) => { + steps := steps.contents + y + steps := steps.contents + y + 1 + steps := steps.contents + y + 2 + k(y) + } + let rec a = n => { + steps := steps.contents + n + steps := steps.contents + n + 1 + steps := steps.contents + n + 2 + if n > 10 { + b(n) + } else { + 0 + } + } + and b = n => { + steps := steps.contents + n + steps := steps.contents + n + 1 + steps := steps.contents + n + 2 + if n > 100 { + a(n - 100) + } else { + c(x => x + n, n) + } + } + a(z) + a(z + 1) +} + +describe(__MODULE__, () => { + test("a function inlined into one that is inlined in turn", () => eq(__LOC__, main(20), 82)) +})