From 31d8aad40ee10badd4829dd53fb06b625ac1ddc1 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Mon, 13 Feb 2023 16:32:50 +0000 Subject: [PATCH] [hir] Check if defined before looking up ValueKind This handles globals now without throwing --- .../src/Inference/InferReferenceEffects.ts | 4 ++- .../hir/destructure-capture-global.expect.md | 32 +++++++++++++++++++ .../hir/destructure-capture-global.js | 5 +++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.js diff --git a/compiler/forget/src/Inference/InferReferenceEffects.ts b/compiler/forget/src/Inference/InferReferenceEffects.ts index 24d45a96f3..bf64963d09 100644 --- a/compiler/forget/src/Inference/InferReferenceEffects.ts +++ b/compiler/forget/src/Inference/InferReferenceEffects.ts @@ -851,7 +851,9 @@ export function parseHookCall(place: Place): Hook | null { function hasContextRefOperand(env: Environment, instrValue: InstructionValue) { for (const place of eachInstructionValueOperand(instrValue)) { - if (env.kind(place) === ValueKind.Context) return true; + if (env.isDefined(place) && env.kind(place) === ValueKind.Context) { + return true; + } } return false; } 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 new file mode 100644 index 0000000000..9984dd1945 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.expect.md @@ -0,0 +1,32 @@ + +## Input + +```javascript +let someGlobal = {}; +function component(a) { + let x = { a, someGlobal }; + return x; +} + +``` + +## Code + +```javascript +let someGlobal = {}; +function component(a) { + const $ = React.unstable_useMemoCache(); + const c_0 = $[0] !== a; + let x; + if (c_0) { + x = { a: a, someGlobal: someGlobal }; + $[0] = a; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.js b/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.js new file mode 100644 index 0000000000..1d0e4ce350 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/destructure-capture-global.js @@ -0,0 +1,5 @@ +let someGlobal = {}; +function component(a) { + let x = { a, someGlobal }; + return x; +}