[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107
Open
sleitor wants to merge 1 commit intofacebook:mainfrom
Open
[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107sleitor wants to merge 1 commit intofacebook:mainfrom
sleitor wants to merge 1 commit intofacebook:mainfrom
Conversation
…default param
When a component function has a destructured prop with a NewExpression
default value (e.g. `{ value = new Number() }`), the compiler would
bail out during BuildHIR when trying to lower the default value via
`lowerReorderableExpression`. This caused `validateNoSetStateInEffects`
to never run, silently suppressing the set-state-in-effect diagnostic.
The fix adds a 'NewExpression' case to `isReorderableExpression`, treating
it the same as 'CallExpression' — safe to reorder when the callee and all
arguments are themselves reorderable (e.g., global identifiers and literals).
Fixes facebook#36101
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #36101
When a component function has a destructured prop with a
NewExpressiondefault value (e.g.{ value = new Number() }), the React Compiler bails out during HIR construction when trying to lower the default value vialowerReorderableExpression. This causesvalidateNoSetStateInEffectsto never run, silently suppressing theset-state-in-effectdiagnostic.Root cause:
isReorderableExpressiondid not have a case forNewExpression, so it fell through to thedefault: return falsebranch.lowerReorderableExpressionthen recorded aTodoerror and aborted compilation of the function before any validation passes ran.Fix: Add a
NewExpressioncase toisReorderableExpressionthat mirrors the existingCallExpressioncase — the expression is safe to reorder when the callee and all arguments are themselves reorderable (e.g. global identifiers and literals).How did you test this change?
Added a new compiler fixture
invalid-setState-in-useEffect-new-expression-default-paramthat reproduces the bug from the issue. The fixture verifies that theEffectSetStatediagnostic is correctly emitted for a component with aNewExpressiondefault prop value.All 1720 compiler snapshot tests pass.