-
Notifications
You must be signed in to change notification settings - Fork 49.3k
[compiler] Fix false positive memo validation #34276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+430
−8
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...iler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.expect.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @eslintSuppressionRules:[] | ||
|
||
// The suppression here shouldn't cause compilation to get skipped | ||
// Previously we had a bug where an empty list of suppressions would | ||
// create a regexp that matched any suppression | ||
function Component(props) { | ||
'use forget'; | ||
// eslint-disable-next-line foo/not-react-related | ||
return <div>{props.text}</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{text: 'Hello'}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @eslintSuppressionRules:[] | ||
|
||
// The suppression here shouldn't cause compilation to get skipped | ||
// Previously we had a bug where an empty list of suppressions would | ||
// create a regexp that matched any suppression | ||
function Component(props) { | ||
"use forget"; | ||
const $ = _c(2); | ||
let t0; | ||
if ($[0] !== props.text) { | ||
t0 = <div>{props.text}</div>; | ||
$[0] = props.text; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ text: "Hello" }], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>Hello</div> |
15 changes: 15 additions & 0 deletions
15
...plugin-react-compiler/src/__tests__/fixtures/compiler/empty-eslint-suppressions-config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @eslintSuppressionRules:[] | ||
|
||
// The suppression here shouldn't cause compilation to get skipped | ||
// Previously we had a bug where an empty list of suppressions would | ||
// create a regexp that matched any suppression | ||
function Component(props) { | ||
'use forget'; | ||
// eslint-disable-next-line foo/not-react-related | ||
return <div>{props.text}</div>; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{text: 'Hello'}], | ||
}; |
59 changes: 59 additions & 0 deletions
59
...zation-inner-destructured-value-mistaken-as-dependency-later-mutation.expect.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
/** | ||
* Repro from https://github.com/facebook/react/issues/34262 | ||
* | ||
* The compiler memoizes more precisely than the original code, with two reactive scopes: | ||
* - One for `transform(input)` with `input` as dep | ||
* - One for `{value}` with `value` as dep | ||
* | ||
* When we validate preserving manual memoization we incorrectly reject this, because | ||
* the original memoization had `object` depending on `input` but our scope depends on | ||
* `value`. | ||
* | ||
* This fixture adds a later potential mutation, which extends the scope and should | ||
* fail validation. This confirms that even though we allow the dependency to diverge, | ||
* we still check that the output value is memoized. | ||
*/ | ||
function useInputValue(input) { | ||
const object = React.useMemo(() => { | ||
const {value} = transform(input); | ||
return {value}; | ||
}, [input]); | ||
mutate(object); | ||
return object; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
Found 1 error: | ||
|
||
Memoization: Compilation skipped because existing memoization could not be preserved | ||
|
||
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This value was memoized in source but not in compilation output. | ||
|
||
error.repro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-later-mutation.ts:19:17 | ||
17 | */ | ||
18 | function useInputValue(input) { | ||
> 19 | const object = React.useMemo(() => { | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
> 20 | const {value} = transform(input); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 21 | return {value}; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
> 22 | }, [input]); | ||
| ^^^^^^^^^^^^^^ Could not preserve existing memoization | ||
23 | mutate(object); | ||
24 | return object; | ||
25 | } | ||
``` | ||
|
||
|
25 changes: 25 additions & 0 deletions
25
...ro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-later-mutation.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
/** | ||
* Repro from https://github.com/facebook/react/issues/34262 | ||
* | ||
* The compiler memoizes more precisely than the original code, with two reactive scopes: | ||
* - One for `transform(input)` with `input` as dep | ||
* - One for `{value}` with `value` as dep | ||
* | ||
* When we validate preserving manual memoization we incorrectly reject this, because | ||
* the original memoization had `object` depending on `input` but our scope depends on | ||
* `value`. | ||
* | ||
* This fixture adds a later potential mutation, which extends the scope and should | ||
* fail validation. This confirms that even though we allow the dependency to diverge, | ||
* we still check that the output value is memoized. | ||
*/ | ||
function useInputValue(input) { | ||
const object = React.useMemo(() => { | ||
const {value} = transform(input); | ||
return {value}; | ||
}, [input]); | ||
mutate(object); | ||
return object; | ||
} |
64 changes: 64 additions & 0 deletions
64
...moization-inner-destructured-value-mistaken-as-dependency-mutated-dep.expect.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {identity, Stringify, useHook} from 'shared-runtime'; | ||
|
||
/** | ||
* Repro from https://github.com/facebook/react/issues/34262 | ||
* | ||
* The compiler memoizes more precisely than the original code, with two reactive scopes: | ||
* - One for `transform(input)` with `input` as dep | ||
* - One for `{value}` with `value` as dep | ||
* | ||
* When we validate preserving manual memoization we incorrectly reject this, because | ||
* the original memoization had `object` depending on `input` but our scope depends on | ||
* `value`. | ||
*/ | ||
function useInputValue(input) { | ||
// Conflate the `identity(input, x)` call with something outside the useMemo, | ||
// to try and break memoization of `value`. This gets correctly flagged since | ||
// the dependency is being mutated | ||
let x = {}; | ||
useHook(); | ||
const object = React.useMemo(() => { | ||
const {value} = identity(input, x); | ||
return {value}; | ||
}, [input, x]); | ||
return object; | ||
} | ||
|
||
function Component() { | ||
return <Stringify value={useInputValue({value: 42}).value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
Found 1 error: | ||
|
||
Memoization: Compilation skipped because existing memoization could not be preserved | ||
|
||
React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. This dependency may be mutated later, which could cause the value to change unexpectedly. | ||
|
||
error.repro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-mutated-dep.ts:25:13 | ||
23 | const {value} = identity(input, x); | ||
24 | return {value}; | ||
> 25 | }, [input, x]); | ||
| ^ This dependency may be modified later | ||
26 | return object; | ||
27 | } | ||
28 | | ||
``` | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
...repro-preserve-memoization-inner-destructured-value-mistaken-as-dependency-mutated-dep.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {identity, Stringify, useHook} from 'shared-runtime'; | ||
|
||
/** | ||
* Repro from https://github.com/facebook/react/issues/34262 | ||
* | ||
* The compiler memoizes more precisely than the original code, with two reactive scopes: | ||
* - One for `transform(input)` with `input` as dep | ||
* - One for `{value}` with `value` as dep | ||
* | ||
* When we validate preserving manual memoization we incorrectly reject this, because | ||
* the original memoization had `object` depending on `input` but our scope depends on | ||
* `value`. | ||
*/ | ||
function useInputValue(input) { | ||
// Conflate the `identity(input, x)` call with something outside the useMemo, | ||
// to try and break memoization of `value`. This gets correctly flagged since | ||
// the dependency is being mutated | ||
let x = {}; | ||
useHook(); | ||
const object = React.useMemo(() => { | ||
const {value} = identity(input, x); | ||
return {value}; | ||
}, [input, x]); | ||
return object; | ||
} | ||
|
||
function Component() { | ||
return <Stringify value={useInputValue({value: 42}).value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
isUnmemoized()
check here is for sanity. I can't construct an example where it wouldn't be memoized, since the dep in this case would be constructed within the useMemo block. The only way to force such a dep to not memoize is if it somehow mixes up with a scope from outside the memo block, in which it would take on a dep that is mutated later which fails a separate validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a fixture demonstrating that case just to be safe