From 717719b8ea6dfb248b5cffa0224641f18de11d61 Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Thu, 19 Jan 2023 10:10:56 -0500 Subject: [PATCH] Fix infinite loop in BabelPlugin After some painful debugging I isolated the infinite loop when attempting to use the BabelPlugin in hir-test rather than manually parsing and traversing it. The issue is that in the BabelPlugin we were replacing the original FunctionDeclaration with a new one, which would add it to Babel's traversal queue. This would effectively create an infinite loop where we would try to optimize a function that was already compiled by Forget (aside: _should_ running the compiler multiple times on code work?). To get around this we can just call the handy `skip` method on the new FunctionDeclaration to tell Babel to stop traversing it. I'm also moving the scope check here because I'll remove it from hir-test in a later commit. --- compiler/forget/src/Babel/BabelPlugin.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/compiler/forget/src/Babel/BabelPlugin.ts b/compiler/forget/src/Babel/BabelPlugin.ts index a5d80132a2..fe68898c3a 100644 --- a/compiler/forget/src/Babel/BabelPlugin.ts +++ b/compiler/forget/src/Babel/BabelPlugin.ts @@ -24,8 +24,15 @@ export default function (babel: typeof BabelCore): PluginObj { visitor: { FunctionDeclaration: { enter(fn, pass) { + if (fn.scope.getProgramParent() !== fn.scope.parent) { + return; + } const ast = compile(fn); + + // We are generating a new FunctionDeclaration node, so we must skip over it or this + // traversal will loop infinitely. fn.replaceWith(ast); + fn.skip(); }, }, },