Skip to content

[compiler] Don't outline functions that reference unbound module-local names#36539

Open
tejasupmanyu wants to merge 1 commit into
facebook:mainfrom
tejasupmanyu:fix/outline-functions-factory-arrow
Open

[compiler] Don't outline functions that reference unbound module-local names#36539
tejasupmanyu wants to merge 1 commit into
facebook:mainfrom
tejasupmanyu:fix/outline-functions-factory-arrow

Conversation

@tejasupmanyu
Copy link
Copy Markdown

Summary

When the outermost function being compiled is nested inside another function (factory/HOC, infer mode), HIRBuilder resolves identifiers against parentFunction.scope.parent — the enclosing function's scope, not the program scope. The enclosing scope's locals are mis-tagged as ModuleLocal, and outlineFunctions sees context.length === 0 and 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 a LoadGlobal(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 / isContextIdentifier by using scope.getProgramParent() instead of scope.parent. That change alone is semantically correct but exposes a deeper structural gap: the compiler's capture machinery (gatherCapturedContext, captureScopes) only walks scopes up to parentFunction.scope, so factory-scope variables get no value-kind initialization and InferMutationAliasingEffects invariant-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

  • Added regression fixture factory-function-component-captures-outer-scope.js. Eval output renders <div>42</div>, confirming getCount stays inline.
  • Full snap suite: 1720/1720 pass.
  • yarn workspace babel-plugin-react-compiler lint clean.

Supersedes #36538 (closed; force-push diverged history, GitHub wouldn't reopen).

…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
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 26, 2026

Hi @tejasupmanyu!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label May 26, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla Bot commented May 26, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compiler Bug]: Incorrect closure hoisting causes runtime error

1 participant