[inference] Capturing an immutable value should be a read

--- 

Following #1216: 

If a value is known to be immutable, then it doesn't need to be considered 
'captured' since no mutation should occur. 

Couldn't figure out a unit test in which this specific fix matters, but we need 
this to fix test output of #1273 

cc. @gsathya, would love some feedback / eyes on this. This makes sense for 
Primitives in particular (which are always read / copied in rval position), but 
I'm not as familiar with edge cases for other immutable values especially around 
lambdas.
This commit is contained in:
Mofei Zhang
2023-02-28 16:36:00 -05:00
parent 474c38c573
commit b532465ce2
2 changed files with 7 additions and 9 deletions
@@ -308,7 +308,8 @@ class InferenceState {
case Effect.Capture: {
if (
valueKind === ValueKind.Frozen ||
valueKind === ValueKind.MaybeFrozen
valueKind === ValueKind.MaybeFrozen ||
valueKind === ValueKind.Immutable
) {
effect = Effect.Read;
} else {
@@ -15,18 +15,15 @@ function component(a) {
```javascript
let someGlobal = {};
function component(a) {
const $ = React.unstable_useMemoCache(3);
const t0 = someGlobal;
const $ = React.unstable_useMemoCache(2);
const c_0 = $[0] !== a;
const c_1 = $[1] !== t0;
let x;
if (c_0 || c_1) {
x = { a: a, someGlobal: t0 };
if (c_0) {
x = { a: a, someGlobal: someGlobal };
$[0] = a;
$[1] = t0;
$[2] = x;
$[1] = x;
} else {
x = $[2];
x = $[1];
}
return x;
}