Semantic Diff

#9543 Keep the declarations of a function inlined twice in JavaScript

1 commit

daa4f2dc76a583422359cbe950b05c399e2f65db..pr-9543

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

Keep the declarations of a function inlined twice in JavaScript

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 (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.

modifiedJS 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
Before
After
} else {} else { (x => x + n | 0); let k = x => x + n | 0; console.log("c", n); console.log("c", n); … … return k(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.