diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts index fbd4ce8fa05b8..6a4da94d706e2 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/AnalyseFunctions.ts @@ -85,13 +85,13 @@ function inferAliasesForCapturing( for (const block of fn.body.blocks.values()) { for (const instr of block.instructions) { const {lvalue, value} = instr; - const hasStore = + const hasCapture = lvalue.effect === Effect.Store || Iterable_some( eachInstructionValueOperand(value), - operand => operand.effect === Effect.Store, + operand => operand.effect === Effect.Capture, ); - if (!hasStore) { + if (!hasCapture) { continue; } const operands: Array = []; @@ -101,6 +101,7 @@ function inferAliasesForCapturing( for (const operand of eachInstructionValueOperand(instr.value)) { if ( operand.effect === Effect.Store || + operand.effect === Effect.Mutate || operand.effect === Effect.Capture ) { operands.push(operand.identifier); diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts index 01d78663d0a2b..2de141bbd3c11 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts @@ -32,6 +32,7 @@ import { isMapType, isMutableEffect, isSetType, + isObjectType, } from '../HIR/HIR'; import {FunctionSignature} from '../HIR/ObjectShape'; import { @@ -1313,7 +1314,7 @@ function inferBlock( state.referenceAndRecordEffects( freezeActions, instrValue.object, - typeof instrValue.property === 'string' + isObjectType(instrValue.object.identifier) ? Effect.Store : Effect.Mutate, ValueReason.Other, @@ -1342,25 +1343,21 @@ function inferBlock( state.referenceAndRecordEffects( freezeActions, instrValue.object, - Effect.Read, + Effect.Capture, ValueReason.Other, ); const lvalue = instr.lvalue; - lvalue.effect = Effect.ConditionallyMutate; + lvalue.effect = Effect.Store; state.initialize(instrValue, state.kind(instrValue.object)); state.define(lvalue, instrValue); continuation = {kind: 'funeffects'}; break; } case 'ComputedStore': { - const effect = - state.kind(instrValue.object).kind === ValueKind.Context - ? Effect.ConditionallyMutate - : Effect.Capture; state.referenceAndRecordEffects( freezeActions, instrValue.value, - effect, + Effect.Capture, ValueReason.Other, ); state.referenceAndRecordEffects( @@ -1372,7 +1369,9 @@ function inferBlock( state.referenceAndRecordEffects( freezeActions, instrValue.object, - Effect.Store, + isObjectType(instrValue.object.identifier) + ? Effect.Store + : Effect.Mutate, ValueReason.Other, ); @@ -1409,7 +1408,7 @@ function inferBlock( state.referenceAndRecordEffects( freezeActions, instrValue.object, - Effect.Read, + Effect.Capture, ValueReason.Other, ); state.referenceAndRecordEffects( @@ -1419,7 +1418,7 @@ function inferBlock( ValueReason.Other, ); const lvalue = instr.lvalue; - lvalue.effect = Effect.ConditionallyMutate; + lvalue.effect = Effect.Store; state.initialize(instrValue, state.kind(instrValue.object)); state.define(lvalue, instrValue); continuation = {kind: 'funeffects'}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md index 4294deb974bba..8b4dbc8f863d6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-access-assignment.expect.md @@ -33,7 +33,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { c as _c } from "react/compiler-runtime"; function Component(t0) { - const $ = _c(10); + const $ = _c(6); const { a, b, c } = t0; let t1; if ($[0] !== a || $[1] !== b || $[2] !== c) { @@ -47,26 +47,7 @@ function Component(t0) { t2 = $[5]; } const y = t2; - let t3; - let t4; - if ($[6] === Symbol.for("react.memo_cache_sentinel")) { - t3 = {}; - t4 = {}; - $[6] = t3; - $[7] = t4; - } else { - t3 = $[6]; - t4 = $[7]; - } - let t5; - if ($[8] !== c) { - t5 = { zero: c }; - $[8] = c; - $[9] = t5; - } else { - t5 = $[9]; - } - const z = { zero: t3, one: t4, two: t5 }; + const z = { zero: {}, one: {}, two: { zero: c } }; x.zero = y.one; z.zero.zero = x.zero; t1 = { zero: x, one: z };