@samitouri / QOS-React-1 / commits / cca15a2139

compiler: fix accidental propagation of function effects from StartMemoize/FinishMemoize

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

Joe Savona committed May 17, 2024 at 15:25 UTC cca15a2139f733f8402fe940805179d3da65b30b
3 files changed +5 -3
compiler/packages/babel-plugin-react-compiler/src/Inference/InferReferenceEffects.ts
+2 -2
@@ -1611,14 +1611,14 @@ function inferBlock(
1611 val,
1612 Effect.Freeze,
1613 ValueReason.Other,
1614 - functionEffects
1614 + []
1615 );
1616 } else {
1617 state.referenceAndRecordEffects(
1618 val,
1619 Effect.Read,
1620 ValueReason.Other,
1621 - functionEffects
1621 + []
1622 );
1623 }
1624 }
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.expect.md
+2 -1
@@ -2,6 +2,7 @@
2 ## Input
3
4 ```javascript
5 +// @validatePreserveExistingMemoizationGuarantees
6 import { useCallback, useEffect, useState } from "react";
7
8 let someGlobal = {};
@@ -33,7 +34,7 @@ export const FIXTURE_ENTRYPOINT = {
34 ## Code
35
36 ```javascript
36 -import { c as _c } from "react/compiler-runtime";
37 +import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees
38 import { useCallback, useEffect, useState } from "react";
39
40 let someGlobal = {};
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/allow-global-mutation-in-effect-indirect-usecallback.js
+1
@@ -1,3 +1,4 @@
1 +// @validatePreserveExistingMemoizationGuarantees
2 import { useCallback, useEffect, useState } from "react";
3
4 let someGlobal = {};