From b532465ce293b01b0654fc509b4f3a322d2eab52 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Tue, 28 Feb 2023 16:36:00 -0500 Subject: [PATCH] [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. --- .../forget/src/Inference/InferReferenceEffects.ts | 3 ++- .../hir/destructure-capture-global.expect.md | 13 +++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/compiler/forget/src/Inference/InferReferenceEffects.ts b/compiler/forget/src/Inference/InferReferenceEffects.ts index fb379f1d2b..1f851f5530 100644 --- a/compiler/forget/src/Inference/InferReferenceEffects.ts +++ b/compiler/forget/src/Inference/InferReferenceEffects.ts @@ -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 { diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md b/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md index 499ebec08f..9f0d022a43 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md @@ -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; }