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
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_testruns this case and checks the result; on master it throws theReferenceError. No other compiler test output changes.