Skip to content

Conversation

@XXUZZWZ
Copy link

@XXUZZWZ XXUZZWZ commented Aug 17, 2025

fix(react-compiler): handle promoted identifiers in MethodCall codegen

Fix internal compiler error when generating code for nested method calls
like Math.floor(Math.abs(value)) and Math.ceil(Math.abs(value)).

Fixes #34072

Problem:

  • Error: "MethodCall::property must be an unpromoted + unmemoized MemberExpression. Got a Identifier"
  • Triggered by nested Math operations: Math.floor(Math.abs(value)), Math.ceil(Math.abs(value))
  • Caused compilation to fail with invariant violation in nested method calls

Root Cause:
In complex nested method calls, the inner MethodCall.property gets promoted
to an identifier despite logic in PromoteUsedTemporaries trying to prevent it.
The codegen phase expects MethodCall.property to always be a MemberExpression
but receives an Identifier instead.

Solution:
Add fallback logic in codegenInstructionValue for MethodCall case:

  • Detect when MethodCall.property has been promoted to an identifier
  • Convert the MethodCall to a regular CallExpression in these cases
  • Preserve original validation and behavior for non-promoted cases
  • Maintain semantic correctness while preventing compiler crashes

Testing:

  • ✅ Original reported cases: Math.floor(Math.abs(value)), Math.ceil(Math.abs(value))
  • ✅ 3-layer nesting: Math.round(Math.floor(Math.abs(value)))
  • ✅ 4-layer nesting: Math.max(Math.min(Math.floor(Math.abs(value)), 100), 0)
  • ✅ Plugin loads and builds correctly
  • ✅ No regressions in basic functionality

Files Changed:

  • compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts
  • compiler/packages/babel-plugin-react-compiler/src/tests/fixtures/compiler/fix-nested-math-calls.js (test case)

Fix internal compiler error when generating code for nested method calls
like Math.floor(Math.abs(value)). The issue occurred when MethodCall.property
was promoted to an identifier during compilation, but codegen expected it
to remain a MemberExpression.

**Problem:**
- Error: "MethodCall::property must be an unpromoted + unmemoized MemberExpression. Got a `Identifier`"
- Triggered by nested method calls: Math.floor(Math.abs(value))
- Caused compilation to fail with invariant violation

**Root Cause:**
In complex nested method calls, the inner MethodCall.property gets promoted
to an identifier despite logic in PromoteUsedTemporaries trying to prevent it.
The codegen phase expects MethodCall.property to always be a MemberExpression
but receives an Identifier instead.

**Solution:**
Add fallback logic in codegenInstructionValue for MethodCall case:
- Detect when MethodCall.property has been promoted to an identifier
- Convert the MethodCall to a regular CallExpression in these cases
- Preserve original validation and behavior for non-promoted cases
- Maintain semantic correctness while preventing compiler crashes

**Testing:**
- ✅ 2-layer nesting: Math.floor(Math.abs(value))
- ✅ 3-layer nesting: Math.round(Math.floor(Math.abs(value)))
- ✅ 4-layer nesting: Math.max(Math.min(Math.floor(Math.abs(value)), 100), 0)
- ✅ Plugin loads and builds correctly
- ✅ No regressions in basic functionality

**Files Changed:**
- compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts
- compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fix-nested-math-calls.js (test case)

Fixes the invariant error while maintaining backward compatibility and
semantic correctness. The fix provides a safe fallback that prevents
crashes in edge cases involving deeply nested method calls.
@meta-cla meta-cla bot added the CLA Signed label Aug 17, 2025
@josephsavona
Copy link
Member

I’m pretty sure this is AI generated. It’s also a duplicate of #34100.

@XXUZZWZ
Copy link
Author

XXUZZWZ commented Aug 17, 2025

I’m pretty sure this is AI generated. It’s also a duplicate of #34100.

yes, you are right

@XXUZZWZ
Copy link
Author

XXUZZWZ commented Aug 17, 2025 via email

@josephsavona
Copy link
Member

josephsavona commented Aug 17, 2025

This is using the same problematic approach as #34100 - it isn’t necessarily safe to covert to a call expression. @poteto has been iterating there based on feedback so let’s continue discussion there.

@XXUZZWZ
Copy link
Author

XXUZZWZ commented Aug 17, 2025 via email

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]: "Internal error: MethodCall::property must be an unpromoted..." on specific Math.floor, Math.ceil usages

2 participants