Skip to content

[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107

Merged
mofeiZ merged 1 commit intofacebook:mainfrom
sleitor:fix-36101
Apr 8, 2026
Merged

[compiler] Fix set-state-in-effect false negative with NewExpression default param#36107
mofeiZ merged 1 commit intofacebook:mainfrom
sleitor:fix-36101

Conversation

@sleitor
Copy link
Copy Markdown
Contributor

@sleitor sleitor commented Mar 19, 2026

Summary

Fixes #36101

When a component function has a destructured prop with a NewExpression default value (e.g. { value = new Number() }), the React Compiler bails out during HIR construction when trying to lower the default value via lowerReorderableExpression. This causes validateNoSetStateInEffects to never run, silently suppressing the set-state-in-effect diagnostic.

Root cause: isReorderableExpression did not have a case for NewExpression, so it fell through to the default: return false branch. lowerReorderableExpression then recorded a Todo error and aborted compilation of the function before any validation passes ran.

Fix: Add a NewExpression case to isReorderableExpression that mirrors the existing CallExpression case — 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-param that reproduces the bug from the issue. The fixture verifies that the EffectSetState diagnostic is correctly emitted for a component with a NewExpression default prop value.

All 1720 compiler snapshot tests pass.

…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
@meta-cla meta-cla bot added the CLA Signed label Mar 19, 2026
@sleitor
Copy link
Copy Markdown
Contributor Author

sleitor commented Apr 7, 2026

👋 Gentle ping — just checking if this is still on the radar for review. Happy to address any feedback or rebase if needed!

@mofeiZ
Copy link
Copy Markdown
Contributor

mofeiZ commented Apr 8, 2026

Thanks for the contribution. We're not planning to land this PR since new expressions call out to function constructors which may be side-effectful.

Looking at this again, you're right in that we already allow for function calls (which also may be side-effectful). My main worry here is reordering across function / constructor calls, but it looks like our lowering should preserve that

@mofeiZ mofeiZ closed this Apr 8, 2026
@mofeiZ mofeiZ reopened this Apr 8, 2026
@mofeiZ mofeiZ merged commit 808e7ed into facebook:main Apr 8, 2026
41 checks passed
@sleitor sleitor deleted the fix-36101 branch April 8, 2026 19:22
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.

Bug: react-hooks/set-state-in-effect fails to flag violation if prop has a NewExpression default value

2 participants