[compiler] Don't outline functions that reference unbound module-local names#36539
[compiler] Don't outline functions that reference unbound module-local names#36539tejasupmanyu wants to merge 1 commit into
Conversation
…l names
When the outermost function being compiled is itself nested inside
another function (e.g. a factory or HOC, as in `infer` compilation
mode), `HIRBuilder` resolves identifiers relative to
`parentFunction.scope.parent`, which is the enclosing function's scope
rather than the program scope. References to the enclosing function's
locals are therefore tagged as `ModuleLocal` and surface in the
outlining pass as `LoadGlobal` instructions with `context.length === 0`,
making the inner function look outlineable.
Hoisting such a function to module scope would leave the captured name
undefined at runtime. Repro from the linked issue:
function makeCounter(initialValue) {
const counter = {value: initialValue};
const Counter = () => {
const getCount = () => counter.value; // hoisted; `counter` undef
return <div>{getCount()}</div>;
};
return Counter;
}
This change skips outlining for any `FunctionExpression` whose body
contains a `LoadGlobal(ModuleLocal)` for a name that does not actually
resolve at the program scope.
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! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. 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.Supersedes #36538 (closed; force-push diverged history, GitHub wouldn't reopen).