Skip to content

Commit

Permalink
compiler: fix accidental propagation of function effects from StartMe…
Browse files Browse the repository at this point in the history
…moize/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: #29154
  • Loading branch information
josephsavona committed May 20, 2024
1 parent 8592369 commit cca15a2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1611,14 +1611,14 @@ function inferBlock(
val,
Effect.Freeze,
ValueReason.Other,
functionEffects
[]
);
} else {
state.referenceAndRecordEffects(
val,
Effect.Read,
ValueReason.Other,
functionEffects
[]
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Input

```javascript
// @validatePreserveExistingMemoizationGuarantees
import { useCallback, useEffect, useState } from "react";

let someGlobal = {};
Expand Down Expand Up @@ -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 = {};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @validatePreserveExistingMemoizationGuarantees
import { useCallback, useEffect, useState } from "react";

let someGlobal = {};
Expand Down

0 comments on commit cca15a2

Please sign in to comment.