diff --git a/compiler/forget/src/HIR/Environment.ts b/compiler/forget/src/HIR/Environment.ts index 487900d013..d41e044713 100644 --- a/compiler/forget/src/HIR/Environment.ts +++ b/compiler/forget/src/HIR/Environment.ts @@ -7,7 +7,7 @@ import { FunctionType, IdentifierId, makeIdentifierId, - Type, + ObjectType, ValueKind, } from "./HIR"; import { BUILTIN_HOOKS, Hook } from "./Hooks"; @@ -75,19 +75,23 @@ export class Environment { }; } - getPropertyType(receiver: Type, property: string): BuiltInType | null { - if (receiver.kind === "Object" || receiver.kind === "Function") { - const { shapeId } = receiver; - if (shapeId !== null) { - const shape = BUILTIN_SHAPES.get(shapeId); - invariant( - shape !== undefined, - `[HIR] Forget internal error: cannot resolve shape ${shapeId}` - ); - return shape.properties.get(property) ?? null; - } + getPropertyType( + receiver: ObjectType | FunctionType, + property: string + ): BuiltInType | null { + const { shapeId } = receiver; + if (shapeId !== null) { + // If an object or function has a shapeId, it must have been assigned + // by Forget (and be present in a builtin or user-defined registry) + const shape = BUILTIN_SHAPES.get(shapeId); + invariant( + shape !== undefined, + `[HIR] Forget internal error: cannot resolve shape ${shapeId}` + ); + return shape.properties.get(property) ?? null; + } else { + return null; } - return null; } getFunctionSignature(type: FunctionType): FunctionSignature | null { diff --git a/compiler/forget/src/TypeInference/InferTypes.ts b/compiler/forget/src/TypeInference/InferTypes.ts index 8508658a38..e4ac142231 100644 --- a/compiler/forget/src/TypeInference/InferTypes.ts +++ b/compiler/forget/src/TypeInference/InferTypes.ts @@ -231,13 +231,17 @@ class Unifier { unify(tA: Type, tB: Type | PolyType): void { if (tB.kind === "Property") { const objectType = this.get(tB.object); - const propertyType = this.env.getPropertyType( - objectType, - tB.propertyName - ); - if (propertyType !== null) { - this.unify(tA, propertyType); + if (objectType.kind === "Object" || objectType.kind === "Function") { + const propertyType = this.env.getPropertyType( + objectType, + tB.propertyName + ); + if (propertyType !== null) { + this.unify(tA, propertyType); + } } + // We do not error if tB is not a known object or function (even if it + // is a primitive), since JS implicit conversion to objects return; } else if (tB.kind === "FunctionCall") { this.unifyFunctionCall(tA, tB);