Repro for false positive in validatePreserveMemoization on non-escaping value

This demonstrates a false positive in validatePreserveExistingManualMemoization. 
We prune memoization of non-escaping values, but the validation pass just sees 
that the value "should" have a scope and that scope doesn't exist, and thinks we 
failed to preserve memoization.
This commit is contained in:
Joe Savona
2024-01-11 16:52:57 -08:00
parent c3a947643f
commit 8e4d2fb69d
2 changed files with 41 additions and 0 deletions
@@ -0,0 +1,28 @@
## Input
```javascript
// @validatePreserveExistingMemoizationGuarantees @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
function Component({ entity, children }) {
// showMessage doesn't escape so we don't memoize it.
// However, validatePreserveExistingMemoizationGuarantees only sees that the scope
// doesn't exist, and thinks the memoization was missed instead of being intentionally dropped.
const showMessage = useCallback(() => entity != null);
if (!showMessage) {
return children;
}
return <Message>{children}</Message>;
}
```
## Error
```
[ReactForget] InvalidReact: This value was manually memoized, but cannot be memoized under Forget because it may be mutated after it is memoized (6:6)
```
@@ -0,0 +1,13 @@
// @validatePreserveExistingMemoizationGuarantees @enableAssumeHooksFollowRulesOfReact @enableTransitivelyFreezeFunctionExpressions
function Component({ entity, children }) {
// showMessage doesn't escape so we don't memoize it.
// However, validatePreserveExistingMemoizationGuarantees only sees that the scope
// doesn't exist, and thinks the memoization was missed instead of being intentionally dropped.
const showMessage = useCallback(() => entity != null);
if (!showMessage) {
return children;
}
return <Message>{children}</Message>;
}