[compiler] Fix incorrect closure outlining for factory-function components#36538
[compiler] Fix incorrect closure outlining for factory-function components#36538tejasupmanyu wants to merge 1 commit into
Conversation
…nents When a React component is defined inside a factory function (e.g. `makeCounter`), the compiler's `infer` compilation mode classifies factory-scope variables (e.g. `counter`) as `ModuleLocal` globals because `parentFunction.scope.parent` coincidentally matches the factory function's scope rather than the true module scope. As a result, inner callbacks that reference those variables (e.g. `getCount = () => counter.value`) end up with an empty `context` array. The `outlineFunctions` pass saw `context.length === 0` and incorrectly hoisted `getCount` to a top-level `_temp` function at module scope, where `counter` is undefined at runtime. Fix: before outlining a FunctionExpression, check whether its body contains a `LoadGlobal(ModuleLocal)` instruction for a name that does not actually exist at the true program/module scope (via `scope.getProgramParent().getBinding(name)`). If such a reference is found the function is left inline — outlined, it would capture an undefined variable. Fixes facebook#34901
|
Hi @tejasupmanyu! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Summary
When the outermost function being compiled is nested inside another function (factory/HOC,
infermode),HIRBuilderresolves identifiers againstparentFunction.scope.parent— the enclosing function's scope, not the program scope. The enclosing scope's locals are mis-tagged asModuleLocal, andoutlineFunctionsseescontext.length === 0and hoists the inner closure to module scope, where the captured name is undefined at runtime.This PR adds a guard in
outlineFunctions: a function is not outlined if its body contains aLoadGlobal(ModuleLocal)whose name does not resolve at the program scope.Fixes #34901
Alternative considered
A more principled fix is to correct the misclassification in
HIRBuilder.resolveIdentifier/isContextIdentifierby usingscope.getProgramParent()instead ofscope.parent. That change alone is semantically correct but exposes a deeper structural gap: the compiler's capture machinery (gatherCapturedContext,captureScopes) only walks scopes up toparentFunction.scope, so factory-scope variables get no value-kind initialization andInferMutationAliasingEffectsinvariant-fails. Closing that gap requires extending capture collection up to (but excluding) the program scope and gathering captures for the outermost function as well — a substantially larger change with wide effects on how nested functions are lowered. Worth doing in a follow-up; out of scope for this fix.Test plan
factory-function-component-captures-outer-scope.js. Eval output renders<div>42</div>, confirminggetCountstays inline.yarn workspace babel-plugin-react-compiler lintclean.