From 76aaf32c55c0ff410d6b6bbc02a440dcfd7bfde9 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Mon, 5 Feb 2024 21:51:09 -0800 Subject: [PATCH] HIR StoreLocal.type uses babel type --- .../src/HIR/BuildHIR.ts | 50 ++++++++----------- .../babel-plugin-react-forget/src/HIR/HIR.ts | 2 +- ...neImmediatelyInvokedFunctionExpressions.ts | 2 +- ...tractScopeDeclarationsFromDestructuring.ts | 3 +- .../ReactiveScopes/PropagateEarlyReturns.ts | 5 +- .../ReactiveScopes/PruneHoistedContexts.ts | 3 +- .../src/TypeInference/InferTypes.ts | 7 ++- 7 files changed, 33 insertions(+), 39 deletions(-) diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts index 413a41aa36..ffe464917f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts @@ -1615,7 +1615,7 @@ function lowerExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: last, - type: makeType(), + type: null, loc: exprLoc, }); } @@ -1657,7 +1657,7 @@ function lowerExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: consequent, - type: makeType(), + type: null, loc: exprLoc, }); return { @@ -1676,7 +1676,7 @@ function lowerExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: alternate, - type: makeType(), + type: null, loc: exprLoc, }); return { @@ -1727,7 +1727,7 @@ function lowerExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...leftPlace }, - type: makeType(), + type: null, loc: leftPlace.loc, }); return { @@ -1744,7 +1744,7 @@ function lowerExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...right }, - type: makeType(), + type: null, loc: right.loc, }); return { @@ -1853,7 +1853,7 @@ function lowerExpression( kind: InstructionKind.Reassign, }, value: { ...binaryPlace }, - type: makeType(), + type: null, loc: exprLoc, }); } else { @@ -2198,7 +2198,7 @@ function lowerExpression( kind: "TypeCastExpression", value: lowerExpressionToTemporary(builder, expr.get("expression")), typeAnnotation: typeAnnotation.node, - type: lowerType(builder, typeAnnotation), + type: lowerType(typeAnnotation.node), loc: exprLoc, }; } @@ -2209,7 +2209,7 @@ function lowerExpression( kind: "TypeCastExpression", value: lowerExpressionToTemporary(builder, expr.get("expression")), typeAnnotation: typeAnnotation.node, - type: lowerType(builder, typeAnnotation), + type: lowerType(typeAnnotation.node), loc: exprLoc, }; } @@ -2315,7 +2315,7 @@ function lowerOptionalMemberExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...temp }, - type: makeType(), + type: null, loc, }); return { @@ -2370,7 +2370,7 @@ function lowerOptionalMemberExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...temp }, - type: makeType(), + type: null, loc, }); return { @@ -2427,7 +2427,7 @@ function lowerOptionalCallExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...temp }, - type: makeType(), + type: null, loc, }); return { @@ -2529,7 +2529,7 @@ function lowerOptionalCallExpression( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...place } }, value: { ...temp }, - type: makeType(), + type: null, loc, }); return { @@ -3263,15 +3263,15 @@ function lowerAssignment( }); } else { const typeAnnotation = lvalue.get("typeAnnotation"); - let type: Type; + let type: t.FlowType | t.TSType | null; if (typeAnnotation.isTSTypeAnnotation()) { const typePath = typeAnnotation.get("typeAnnotation"); - type = lowerType(builder, typePath); + type = typePath.node; } else if (typeAnnotation.isTypeAnnotation()) { const typePath = typeAnnotation.get("typeAnnotation"); - type = lowerType(builder, typePath); + type = typePath.node; } else { - type = makeType(); + type = null; } temporary = lowerValueToTemporary(builder, { kind: "StoreLocal", @@ -3584,7 +3584,7 @@ function lowerAssignment( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...temp } }, value: { ...defaultValue }, - type: makeType(), + type: null, loc, }); return { @@ -3601,7 +3601,7 @@ function lowerAssignment( kind: "StoreLocal", lvalue: { kind: InstructionKind.Const, place: { ...temp } }, value: { ...value }, - type: makeType(), + type: null, loc, }); return { @@ -3857,23 +3857,17 @@ function notNull(value: T | null): value is T { return value !== null; } -function lowerType( - _builder: HIRBuilder, - path: NodePath -): Type { - const node = path.node; +export function lowerType(node: t.FlowType | t.TSType): Type { switch (node.type) { case "GenericTypeAnnotation": { - const typeAnnotation = path as NodePath; - const id = typeAnnotation.get("id"); - if (id.node.type === "Identifier" && id.node.name === "Array") { + const id = node.id; + if (id.type === "Identifier" && id.name === "Array") { return { kind: "Object", shapeId: BuiltInArrayId }; } return makeType(); } case "TSTypeReference": { - const typeReference = path as NodePath; - const typeName = typeReference.get("typeName").node; + const typeName = node.typeName; if (typeName.type === "Identifier" && typeName.name === "Array") { return { kind: "Object", shapeId: BuiltInArrayId }; } diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts index 009053754d..dfd610fccd 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/HIR.ts @@ -678,7 +678,7 @@ export type InstructionValue = kind: "StoreLocal"; lvalue: LValue; value: Place; - type: Type; + type: t.FlowType | t.TSType | null; loc: SourceLocation; } | { diff --git a/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts b/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts index c67cc14f1f..e138b68f00 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Inference/InlineImmediatelyInvokedFunctionExpressions.ts @@ -245,7 +245,7 @@ function rewriteBlock( kind: "StoreLocal", lvalue: { kind: InstructionKind.Reassign, place: { ...returnValue } }, value: terminal.value, - type: makeType(), + type: null, loc: terminal.loc, }, }); diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/ExtractScopeDeclarationsFromDestructuring.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/ExtractScopeDeclarationsFromDestructuring.ts index 57474d2b81..ff0366f32a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/ExtractScopeDeclarationsFromDestructuring.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/ExtractScopeDeclarationsFromDestructuring.ts @@ -15,7 +15,6 @@ import { ReactiveFunction, ReactiveInstruction, ReactiveScopeBlock, - makeType, } from "../HIR"; import { eachPatternOperand, mapPatternOperands } from "../HIR/visitors"; import { ReactiveFunctionTransform, visitReactiveFunction } from "./visitors"; @@ -178,7 +177,7 @@ function transformDestructuring( place: original, }, value: temporary, - type: makeType(), + type: null, loc: destructure.loc, }, loc: instr.loc, diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateEarlyReturns.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateEarlyReturns.ts index 8412709dab..6854593da9 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateEarlyReturns.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PropagateEarlyReturns.ts @@ -16,7 +16,6 @@ import { ReactiveStatement, ReactiveTerminalStatement, makeInstructionId, - makeType, } from "../HIR"; import { createTemporaryPlace } from "../HIR/HIRBuilder"; import { EARLY_RETURN_SENTINEL } from "./CodegenReactiveFunction"; @@ -221,7 +220,7 @@ class Transform extends ReactiveFunctionTransform { value: { kind: "StoreLocal", loc, - type: makeType(), + type: null, lvalue: { kind: InstructionKind.Let, place: { @@ -295,7 +294,7 @@ class Transform extends ReactiveFunctionTransform { value: { kind: "StoreLocal", loc, - type: makeType(), + type: null, lvalue: { kind: InstructionKind.Reassign, place: { diff --git a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneHoistedContexts.ts b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneHoistedContexts.ts index d263a3a39a..03516f475c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneHoistedContexts.ts +++ b/compiler/packages/babel-plugin-react-forget/src/ReactiveScopes/PruneHoistedContexts.ts @@ -11,7 +11,6 @@ import { ReactiveFunction, ReactiveInstruction, ReactiveStatement, - makeType, } from "../HIR"; import { ReactiveFunctionTransform, @@ -60,7 +59,7 @@ class Visitor extends ReactiveFunctionTransform { ...instruction.value.lvalue, kind: InstructionKind.Const, }, - type: makeType(), + type: null, kind: "StoreLocal", }, }, diff --git a/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts b/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts index 7708948427..a9a0e8d662 100644 --- a/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts +++ b/compiler/packages/babel-plugin-react-forget/src/TypeInference/InferTypes.ts @@ -8,6 +8,7 @@ import * as t from "@babel/types"; import { CompilerError } from "../CompilerError"; import { Environment } from "../HIR"; +import { lowerType } from "../HIR/BuildHIR"; import { HIRFunction, Instruction, @@ -142,8 +143,10 @@ function* generateInstructionTypes( value.lvalue.place.identifier.type, value.value.identifier.type ); - yield equation(value.type, value.lvalue.place.identifier.type); - yield equation(left, value.type); + const valueType = + value.type === null ? makeType() : lowerType(value.type); + yield equation(valueType, value.lvalue.place.identifier.type); + yield equation(left, valueType); } else { yield equation(left, value.value.identifier.type); yield equation(