Skip to content

Comments

[compiler] Fix module-level 'use memo' not compiling in annotation mode#35870

Open
sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor:fix-35868
Open

[compiler] Fix module-level 'use memo' not compiling in annotation mode#35870
sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor:fix-35868

Conversation

@sleitor
Copy link

@sleitor sleitor commented Feb 23, 2026

Summary

When compilationMode is set to 'annotation', module-level opt-in directives such as 'use memo' placed at the top of a file were silently ignored. Only function-level directives (inside the function body) triggered compilation, contrary to the documented behavior:

"Place directives at the top of a file to affect all functions in that module."

Root cause

Both getReactFunctionType() and processFn() only inspected the function body's own directives (fn.node.body.directives) when deciding whether to compile in annotation mode. The program-level directives (program.node.directives) were never consulted.

Fix

  • Add hasModuleScopeOptIn to ProgramContext (computed once from tryFindDirectiveEnablingMemoization(program.node.directives)).
  • In getReactFunctionType(): when compilationMode is 'annotation' and no function-level opt-in is found, fall back to hasModuleScopeOptIn before returning null. This ensures functions in the module are queued for compilation.
  • In processFn(): add !programContext.hasModuleScopeOptIn to the annotation-mode skip guard so that the compiled function is actually emitted.
  • Add a test fixture use-memo-module-level that verifies 'use memo' at module scope in annotation mode compiles the component.

Behaviour table

Directive compilationMode Before After
function-level 'use memo' annotation ✅ compiled ✅ compiled
module-level 'use memo' annotation ❌ not compiled ✅ compiled
no directive annotation ✅ skipped ✅ skipped
module-level 'use memo' infer ✅ compiled (infer compiles all) ✅ compiled

Fixes #35868

When compilationMode is set to 'annotation', module-level opt-in directives
such as 'use memo' (placed at the top of a file) were silently ignored. Only
function-level directives triggered compilation, contrary to the documented
behavior that "place directives at the top of a file to affect all functions
in that module".

Root cause: both getReactFunctionType() and processFn() only inspected the
function body's own directives (fn.node.body.directives) when deciding
whether to compile in annotation mode. The program-level directives
(program.node.directives) were never consulted.

Fix:
- Add hasModuleScopeOptIn to ProgramContext, computed once from
  tryFindDirectiveEnablingMemoization(program.node.directives).
- In getReactFunctionType(), when compilationMode is 'annotation' and no
  function-level opt-in is found, fall back to hasModuleScopeOptIn before
  returning null.  This ensures functions in the module are queued for
  compilation.
- In processFn(), add !programContext.hasModuleScopeOptIn to the annotation
  mode skip guard so that the compiled function is emitted.

Fixes facebook#35868
@meta-cla meta-cla bot added the CLA Signed label Feb 23, 2026
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]: React Compiler does not compile module-level 'use memo' but compiles function-level 'use memo'

1 participant