-
Notifications
You must be signed in to change notification settings - Fork 47.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Patch ValidatePreserveMemo to bailout correctly for refs
ghstack-source-id: 73fc47f23488b392644ffa27f124c309dc82b69c Pull Request resolved: #30603
- Loading branch information
Showing
14 changed files
with
373 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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
45 changes: 0 additions & 45 deletions
45
.../__tests__/fixtures/compiler/bug-maybe-mutable-ref-memo-not-preserved.expect.md
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
...dation/error.false-positive-useMemo-dropped-infer-always-invalidating.expect.md
This file contains 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,42 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {useMemo} from 'react'; | ||
import {useHook} from 'shared-runtime'; | ||
|
||
// useMemo values may not be memoized in Forget output if we | ||
// infer that their deps always invalidate. | ||
// This is technically a false positive as the useMemo in source | ||
// was effectively a no-op | ||
function useFoo(props) { | ||
const x = []; | ||
useHook(); | ||
x.push(props); | ||
|
||
return useMemo(() => [x], [x]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
13 | x.push(props); | ||
14 | | ||
> 15 | return useMemo(() => [x], [x]); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ CannotPreserveMemoization: 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. (15:15) | ||
16 | } | ||
17 | | ||
18 | export const FIXTURE_ENTRYPOINT = { | ||
``` | ||
This file contains 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
35 changes: 35 additions & 0 deletions
35
...mpiler/preserve-memo-validation/error.maybe-mutable-ref-not-preserved.expect.md
This file contains 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,35 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import {useRef, useMemo} from 'react'; | ||
import {makeArray} from 'shared-runtime'; | ||
|
||
function useFoo() { | ||
const r = useRef(); | ||
return useMemo(() => makeArray(r), []); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
6 | function useFoo() { | ||
7 | const r = useRef(); | ||
> 8 | return useMemo(() => makeArray(r), []); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ CannotPreserveMemoization: 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. (8:8) | ||
9 | } | ||
10 | | ||
11 | export const FIXTURE_ENTRYPOINT = { | ||
``` | ||
File renamed without changes.
52 changes: 52 additions & 0 deletions
52
...erve-memo-validation/prune-nonescaping-useMemo-mult-returns-primitive.expect.md
This file contains 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,52 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {useMemo} from 'react'; | ||
import {identity} from 'shared-runtime'; | ||
|
||
function useFoo(cond) { | ||
useMemo(() => { | ||
if (cond) { | ||
return 2; | ||
} else { | ||
return identity(5); | ||
} | ||
}, [cond]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import { useMemo } from "react"; | ||
import { identity } from "shared-runtime"; | ||
|
||
function useFoo(cond) { | ||
let t0; | ||
if (cond) { | ||
t0 = 2; | ||
} else { | ||
t0 = identity(5); | ||
} | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) |
19 changes: 19 additions & 0 deletions
19
...res/compiler/preserve-memo-validation/prune-nonescaping-useMemo-mult-returns-primitive.ts
This file contains 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,19 @@ | ||
// @validatePreserveExistingMemoizationGuarantees | ||
|
||
import {useMemo} from 'react'; | ||
import {identity} from 'shared-runtime'; | ||
|
||
function useFoo(cond) { | ||
useMemo(() => { | ||
if (cond) { | ||
return 2; | ||
} else { | ||
return identity(5); | ||
} | ||
}, [cond]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [true], | ||
}; |
Oops, something went wrong.