From c869c3850656d37966b49c5732340064c09a0b65 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Tue, 21 May 2024 07:08:03 +0100 Subject: [PATCH] compiler: getGlobalDeclaration() takes a NonLocalBinding No-op refactor to make Environment#getGlobalDeclaration() take a NonLocalBinding instead of just a name. The idea is that in subsequent PRs we can use information about the binding to resolve a type more accurately. For example, we can resolve `Array` differently if its an import or local and not the global Array. Similar for resolving local `useState` differently than the one from React. [ghstack-poisoned] --- .../src/HIR/Environment.ts | 5 ++++- .../src/HIR/HIRBuilder.ts | 20 ------------------- .../src/Inference/DropManualMemoization.ts | 2 +- .../src/TypeInference/InferTypes.ts | 2 +- 4 files changed, 6 insertions(+), 23 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts index f3adbfa997980..c75b0a15b037b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts @@ -25,6 +25,7 @@ import { Effect, FunctionType, IdentifierId, + NonLocalBinding, PolyType, ScopeId, Type, @@ -511,7 +512,8 @@ export class Environment { return this.#hoistedIdentifiers.has(node); } - getGlobalDeclaration(name: string): Global | null { + getGlobalDeclaration(binding: NonLocalBinding): Global | null { + const name = binding.name; let resolvedName = name; if (this.config.hookPattern != null) { @@ -534,6 +536,7 @@ export class Environment { log(() => `Undefined global \`${name}\``); } } + return resolvedGlobal; } diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts index be6cdf84d110a..bcf532ca701bb 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts @@ -9,7 +9,6 @@ import { Binding, NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import { CompilerError } from "../CompilerError"; import { Environment } from "./Environment"; -import { Global } from "./Globals"; import { BasicBlock, BlockId, @@ -186,25 +185,6 @@ export default class HIRBuilder { }; } - resolveGlobal( - path: NodePath - ): (Global & { name: string }) | null { - const name = path.node.name; - const resolvedGlobal = this.#env.getGlobalDeclaration(name); - if (resolvedGlobal) { - return { - ...resolvedGlobal, - name, - }; - } else { - // if env records no global with the given name, load it as an unknown type - return { - kind: "Poly", - name, - }; - } - } - #resolveBabelBinding( path: NodePath ): Binding | null { diff --git a/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts b/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts index d681deb8cc78e..5aaf7989fe1b3 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts @@ -127,7 +127,7 @@ function collectTemporaries( break; } case "LoadGlobal": { - const global = env.getGlobalDeclaration(value.binding.name); + const global = env.getGlobalDeclaration(value.binding); const hookKind = global !== null ? getHookKindForType(env, global) : null; const lvalId = instr.lvalue.identifier.id; if (hookKind === "useMemo" || hookKind === "useCallback") { diff --git a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts index 6c37e830a8baa..6db6fb22c5fd1 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts @@ -197,7 +197,7 @@ function* generateInstructionTypes( } case "LoadGlobal": { - const globalType = env.getGlobalDeclaration(value.binding.name); + const globalType = env.getGlobalDeclaration(value.binding); if (globalType) { yield equation(left, globalType); }