-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Remove transitive memo check in validatePreserveMemo
[ghstack-poisoned]
- Loading branch information
Showing
7 changed files
with
198 additions
and
119 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
57 changes: 0 additions & 57 deletions
57
...eserve-memo-validation/error.false-positive-extended-contextvar-scope.expect.md
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
...rve-memo-validation/error.false-positive-useCallback-dep-scope-pruned.expect.md
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
...xtures/compiler/preserve-memo-validation/useCallback-dep-scope-pruned.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,65 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees | ||
import {useCallback} from 'react'; | ||
import {identity, useIdentity} from 'shared-runtime'; | ||
|
||
/** | ||
* Repro showing a manual memo whose declaration (useCallback's 1st argument) | ||
* is memoized, but not its dependency (x). In this case, `x`'s scope is pruned | ||
* due to hook-call flattening. | ||
*/ | ||
function useFoo(a) { | ||
const x = identity(a); | ||
useIdentity(2); | ||
mutate(x); | ||
|
||
return useCallback(() => [x, []], [x]); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [3], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees | ||
import { useCallback } from "react"; | ||
import { identity, useIdentity } from "shared-runtime"; | ||
|
||
/** | ||
* Repro showing a manual memo whose declaration (useCallback's 1st argument) | ||
* is memoized, but not its dependency (x). In this case, `x`'s scope is pruned | ||
* due to hook-call flattening. | ||
*/ | ||
function useFoo(a) { | ||
const $ = _c(2); | ||
const x = identity(a); | ||
useIdentity(2); | ||
mutate(x); | ||
let t0; | ||
if ($[0] !== x) { | ||
t0 = () => [x, []]; | ||
$[0] = x; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
return t0; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useFoo, | ||
params: [3], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) mutate is not defined |
File renamed without changes.
105 changes: 105 additions & 0 deletions
105
...mpiler/preserve-memo-validation/useCallback-extended-contextvar-scope.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,105 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import {useCallback} from 'react'; | ||
import {Stringify, useIdentity} from 'shared-runtime'; | ||
|
||
/** | ||
* Here, the *inferred* dependencies of cb are `a` and `t1 = LoadContext capture x_@1`. | ||
* - t1 does not have a scope as it captures `x` after x's mutable range | ||
* - `x` is a context variable, which means its mutable range extends to all | ||
* references / aliases. | ||
* - `a`, `b`, and `x` get the same mutable range due to potential aliasing. | ||
* | ||
* We currently bail out because `a` has a scope and is not transitively memoized | ||
* (as its scope is pruned due to a hook call) | ||
*/ | ||
function useBar({a, b}, cond) { | ||
let x = useIdentity({val: 3}); | ||
if (cond) { | ||
x = b; | ||
} | ||
|
||
const cb = useCallback(() => { | ||
return [a, x]; | ||
}, [a, x]); | ||
|
||
return <Stringify cb={cb} shouldInvoke={true} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useBar, | ||
params: [{a: 1, b: 2}, true], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees:true | ||
|
||
import { useCallback } from "react"; | ||
import { Stringify, useIdentity } from "shared-runtime"; | ||
|
||
/** | ||
* Here, the *inferred* dependencies of cb are `a` and `t1 = LoadContext capture x_@1`. | ||
* - t1 does not have a scope as it captures `x` after x's mutable range | ||
* - `x` is a context variable, which means its mutable range extends to all | ||
* references / aliases. | ||
* - `a`, `b`, and `x` get the same mutable range due to potential aliasing. | ||
* | ||
* We currently bail out because `a` has a scope and is not transitively memoized | ||
* (as its scope is pruned due to a hook call) | ||
*/ | ||
function useBar(t0, cond) { | ||
const $ = _c(6); | ||
const { a, b } = t0; | ||
let t1; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t1 = { val: 3 }; | ||
$[0] = t1; | ||
} else { | ||
t1 = $[0]; | ||
} | ||
let x; | ||
x = useIdentity(t1); | ||
if (cond) { | ||
x = b; | ||
} | ||
|
||
const t2 = x; | ||
let t3; | ||
if ($[1] !== a || $[2] !== t2) { | ||
t3 = () => [a, x]; | ||
$[1] = a; | ||
$[2] = t2; | ||
$[3] = t3; | ||
} else { | ||
t3 = $[3]; | ||
} | ||
x; | ||
const cb = t3; | ||
let t4; | ||
if ($[4] !== cb) { | ||
t4 = <Stringify cb={cb} shouldInvoke={true} />; | ||
$[4] = cb; | ||
$[5] = t4; | ||
} else { | ||
t4 = $[5]; | ||
} | ||
return t4; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: useBar, | ||
params: [{ a: 1, b: 2 }, true], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>{"cb":"[[ function params=0 ]]","shouldInvoke":true}</div> |
File renamed without changes.