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; +}