You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inlining a function twice in JavaScript drops a variable declaration #9542
The declaration let k = x => x + n | 0 has become a bare expression, so return k(n) reads an unbound variable. It should print 82.
Cause
The bug is in Js_pass_tailcall_inline, which copies the body of a local function into its single call site when that call is a return f(…). Inlining c into b turns c's parameter k into let k = <function> in b's body. Then b is inlined into a in the same way, and that body is processed a second time. A case for a function declaration at the head of a block looks the variable up in a table of declarations collected before the pass. k didn't exist then, so the case treats the declaration as unused and keeps only its value.
Since when
This is not a regression. It has been in every release since January 2016:
The case was already in the first BuckleScript commit (78ffcbd, 2016-01-06, jscomp/js_inline_and_eliminate.ml). There, the table counted uses, so a variable missing from it really was unused.
ddd8b4c (2016-01-19) made the table one of declarations, and added the inlining of a returned call, which processes a body a second time. Since then, a missing entry no longer means that the variable is unused.
The shape is rare: the functions must be too big to be inlined earlier, and each must be called once, in a return.
Found by Claude while reviewing optimizer cleanups.
Activity