From 359b9b1589bcbe7988ff782ae88dcfaa2bdba927 Mon Sep 17 00:00:00 2001 From: Mofei Zhang Date: Tue, 28 Nov 2023 17:48:22 -0500 Subject: [PATCH] [ez] Patch unsound array destructuring --- Going to hold off on landing until after codefreeze, it's not urgent as we already fixed playground in #2404. All other internal pipelines do error handling through Entrypoint, which catches and creates UnexpectedErrors as needed. --- .../src/Inference/DropManualMemoization.ts | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts b/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts index 8f9eceae1d..03be0f92a9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Inference/DropManualMemoization.ts @@ -5,7 +5,14 @@ * LICENSE file in the root directory of this source tree. */ -import { Effect, HIRFunction, IdentifierId } from "../HIR"; +import { CompilerError } from ".."; +import { + Effect, + HIRFunction, + IdentifierId, + Place, + SpreadPattern, +} from "../HIR"; import { HookKind } from "../HIR/ObjectShape"; /* @@ -53,8 +60,16 @@ export function dropManualMemoization(func: HIRFunction): void { const hookKind = hooks.get(id); if (hookKind != null) { if (hookKind === "useMemo") { - const [fn] = instr.value.args; - + const [fn] = instr.value.args as Array< + Place | SpreadPattern | undefined + >; + if (fn == null) { + CompilerError.throwInvalidReact({ + reason: "Expected useMemo call to pass a callback function", + loc: instr.loc, + suggestions: null, + }); + } /* * TODO(gsn): Consider inlining the function passed to useMemo, * rather than just calling it directly. @@ -80,7 +95,16 @@ export function dropManualMemoization(func: HIRFunction): void { }; } } else if (hookKind === "useCallback") { - const [fn] = instr.value.args; + const [fn] = instr.value.args as Array< + Place | SpreadPattern | undefined + >; + if (fn == null) { + CompilerError.throwInvalidReact({ + reason: "Expected useMemo call to pass a callback function", + loc: instr.loc, + suggestions: null, + }); + } /* * Instead of a Call, just alias the callback directly.