@samitouri / QOS-React-2 / commits / 788ed90b18

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-source-id: c8063e6fb8acdd347a56477d6b06238dd54979b1 Pull Request resolved: https://github.com/facebook/react/pull/29189

Joe Savona committed May 24, 2024 at 06:59 UTC 788ed90b186f1e61497e0ab4a57dab3714caae0f
4 files changed +6 -23
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts
+4 -1
@@ -25,6 +25,7 @@ import {
25 Effect,
26 FunctionType,
27 IdentifierId,
28 + NonLocalBinding,
29 PolyType,
30 ScopeId,
31 Type,
@@ -511,7 +512,8 @@ export class Environment {
512 return this.#hoistedIdentifiers.has(node);
513 }
514
514 - getGlobalDeclaration(name: string): Global | null {
515 + getGlobalDeclaration(binding: NonLocalBinding): Global | null {
516 + const name = binding.name;
517 let resolvedName = name;
518
519 if (this.config.hookPattern != null) {
@@ -534,6 +536,7 @@ export class Environment {
536 log(() => `Undefined global \`${name}\``);
537 }
538 }
539 +
540 return resolvedGlobal;
541 }
542
compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts
-20
@@ -9,7 +9,6 @@ import { Binding, NodePath } from "@babel/traverse";
9 import * as t from "@babel/types";
10 import { CompilerError } from "../CompilerError";
11 import { Environment } from "./Environment";
12 -import { Global } from "./Globals";
12 import {
13 BasicBlock,
14 BlockId,
@@ -186,25 +185,6 @@ export default class HIRBuilder {
185 };
186 }
187
189 - resolveGlobal(
190 - path: NodePath<t.Identifier | t.JSXIdentifier>
191 - ): (Global & { name: string }) | null {
192 - const name = path.node.name;
193 - const resolvedGlobal = this.#env.getGlobalDeclaration(name);
194 - if (resolvedGlobal) {
195 - return {
196 - ...resolvedGlobal,
197 - name,
198 - };
199 - } else {
200 - // if env records no global with the given name, load it as an unknown type
201 - return {
202 - kind: "Poly",
203 - name,
204 - };
205 - }
206 - }
207 -
188 #resolveBabelBinding(
189 path: NodePath<t.Identifier | t.JSXIdentifier>
190 ): Binding | null {
compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts
+1 -1
@@ -127,7 +127,7 @@ function collectTemporaries(
127 break;
128 }
129 case "LoadGlobal": {
130 - const global = env.getGlobalDeclaration(value.binding.name);
130 + const global = env.getGlobalDeclaration(value.binding);
131 const hookKind = global !== null ? getHookKindForType(env, global) : null;
132 const lvalId = instr.lvalue.identifier.id;
133 if (hookKind === "useMemo" || hookKind === "useCallback") {
compiler/packages/babel-plugin-react-compiler/src/TypeInference/InferTypes.ts
+1 -1
@@ -199,7 +199,7 @@ function* generateInstructionTypes(
199 }
200
201 case "LoadGlobal": {
202 - const globalType = env.getGlobalDeclaration(value.binding.name);
202 + const globalType = env.getGlobalDeclaration(value.binding);
203 if (globalType) {
204 yield equation(left, globalType);
205 }