Skip to content

[compiler] Support logical assignment operators in BuildHIR - #38190

Open
magic-akari wants to merge 5 commits into
react:mainfrom
magic-akari:fix/compiler/logical-assignment-operators
Open

magic-akari wants to merge 5 commits into
react:mainfrom
magic-akari:fix/compiler/logical-assignment-operators

Conversation

@magic-akari

Copy link
Copy Markdown
Contributor

React Compiler currently skips functions that use ??=, &&=, or ||=.

Add support in both the TypeScript and Rust compilers, preserving short-circuit behavior, evaluation order, and member references. Capture target objects and computed keys once, while keeping temporary declarations outside expression-only blocks.

Lower ??=, &&=, and ||= to short-circuiting logical expressions in the
TypeScript compiler and Rust port. Evaluate member targets once, and only
evaluate the right-hand side and store when the assignment is taken.

Closes react#33315

Co-authored-by: Raashish Aggarwal <94279692+raashish1601@users.noreply.github.com>
Co-authored-by: lauren <lauren@anysphere.co>
@meta-cla meta-cla Bot added the CLA Signed label Nov 10, 2026
Value blocks cannot contain local declarations. Hoist only capture
declarations and keep their assignments in the logical test so callee,
argument, and operand evaluation stays in source order.
Fix the member base before evaluating the computed key, and snapshot
identifier operands before reading the property. Getters and key
expressions can rebind those variables, but the conditional store must
retain the original target reference.
@magic-akari
magic-akari force-pushed the fix/compiler/logical-assignment-operators branch from 3e51e1b to 712d62f Compare November 10, 2026 17:50
A logical assignment may short-circuit without writing to its target.
Treating it as an ordinary reassignment reports a false syntax error,
while rewriting the binding to let would lose the runtime const check.

Keep const targets out of reassignment capture analysis and record a
Todo during lowering so both compilers preserve the original function.
Treat a nullish check followed by a store to the same ref.current as the
existing null-guarded initialization pattern. Index direct current loads
and stores at logical joins so initializers may contain value blocks.

Keep render-time ref reads and additional writes subject to the existing
validation in both compilers.
@magic-akari
magic-akari force-pushed the fix/compiler/logical-assignment-operators branch from 712d62f to 221e18d Compare November 10, 2026 18:10
@magic-akari
magic-akari marked this pull request as ready for review November 10, 2026 18:21

This branch has not been deployed

No deployments
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]: Todo: (BuildHIR::lowerExpression) Handle ??= operators in AssignmentExpression (164:164)

2 participants