@samitouri / QOS-React / commits / b532465ce2

[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.

Mofei Zhang committed Feb 28, 2023 at 16:36 UTC b532465ce293b01b0654fc509b4f3a322d2eab52
2 files changed +7 -9
compiler/forget/src/Inference/InferReferenceEffects.ts
+2 -1
@@ -308,7 +308,8 @@ class InferenceState {
308 case Effect.Capture: {
309 if (
310 valueKind === ValueKind.Frozen ||
311 - valueKind === ValueKind.MaybeFrozen
311 + valueKind === ValueKind.MaybeFrozen ||
312 + valueKind === ValueKind.Immutable
313 ) {
314 effect = Effect.Read;
315 } else {
compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md
+5 -8
@@ -15,18 +15,15 @@ function component(a) {
15 ```javascript
16 let someGlobal = {};
17 function component(a) {
18 - const $ = React.unstable_useMemoCache(3);
19 - const t0 = someGlobal;
18 + const $ = React.unstable_useMemoCache(2);
19 const c_0 = $[0] !== a;
21 - const c_1 = $[1] !== t0;
20 let x;
23 - if (c_0 || c_1) {
24 - x = { a: a, someGlobal: t0 };
21 + if (c_0) {
22 + x = { a: a, someGlobal: someGlobal };
23 $[0] = a;
26 - $[1] = t0;
27 - $[2] = x;
24 + $[1] = x;
25 } else {
29 - x = $[2];
26 + x = $[1];
27 }
28 return x;
29 }