From cca15a2139f733f8402fe940805179d3da65b30b Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Fri, 17 May 2024 15:25:20 -0700 Subject: [PATCH] compiler: fix accidental propagation of function effects from StartMemoize/FinishMemoize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By default, React Compiler will skip compilation if it cannot preserve existing memoization. Ie, if the code has an existing `useMemo()` or `useCallback()` and the compiler cannot determine that it is safe to keep that memoization — or do even better — then we'll leave the code alone. The actual compilation doesn't use any hints from existing memo calls, this is purely to check and avoid regressing any specific memoization that developers may have already applied. However, we were accidentally reporting some false-positive _validation_ errors due to the StartMemoize and FinishMemoize instructions that we emit to track where the memoization was in the source code. This is now fixed. Fixes #29131 Fixes #29132 ghstack-source-id: 9f6b8dbc5074ccc96e6073cf11c4920b5375faf6 Pull Request resolved: https://github.com/facebook/react/pull/29154 --- .../src/Inference/InferReferenceEffects.ts | 4 ++-- ...w-global-mutation-in-effect-indirect-usecallback.expect.md | 3 ++- .../allow-global-mutation-in-effect-indirect-usecallback.js | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) 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 24d72d1e34..3724c8d6f1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts @@ -1611,14 +1611,14 @@ function inferBlock( val, Effect.Freeze, ValueReason.Other, - functionEffects + [] ); } else { state.referenceAndRecordEffects( val, Effect.Read, ValueReason.Other, - functionEffects + [] ); } } diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md index 87778c66bc..33c6e23105 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md @@ -2,6 +2,7 @@ ## Input ```javascript +// @validatePreserveExistingMemoizationGuarantees import { useCallback, useEffect, useState } from "react"; let someGlobal = {}; @@ -33,7 +34,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -import { c as _c } from "react/compiler-runtime"; +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees import { useCallback, useEffect, useState } from "react"; let someGlobal = {}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.js index 9580b2dc1c..1c35bb9421 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.js +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.js @@ -1,3 +1,4 @@ +// @validatePreserveExistingMemoizationGuarantees import { useCallback, useEffect, useState } from "react"; let someGlobal = {};