From 2d3d2c5f27a38c25e6d697c8635c38655f1a9728 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 7 Jan 2024 18:05:55 +0000 Subject: Update commonSupertype to handle supertype of sum and its variants --- .../java/org/shedlang/compiler/typechecker/TypeInfos.java | 14 ++++++++++---- src/main/java/org/shedlang/compiler/types/Types.java | 7 +++---- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java b/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java index ff2e0aad..3910b472 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java @@ -131,12 +131,18 @@ public class TypeInfos { }; } - public Set supertypes(Type type) { + /** + * Get the supertypes of a type, from most specific to least specific. + */ + public Set supertypes(Type type) { + var supertypes = P.set(type); if (type instanceof StructType structType) { - return this.structSupertypes.getOrDefault(structType, P.orderedSet()); - } else { - return P.set(); + supertypes = supertypes.plusAll( + this.structSupertypes.getOrDefault(structType, P.orderedSet()) + ); } + supertypes.plus(Types.TOP); + return supertypes; } private Optional simpleSumTypeInfo(SimpleSumType type) { diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java index 2e5f2562..59d1f96e 100644 --- a/src/main/java/org/shedlang/compiler/types/Types.java +++ b/src/main/java/org/shedlang/compiler/types/Types.java @@ -83,15 +83,14 @@ public class Types { public static Type commonSupertype(List subtypes, TypeInfos typeInfos) { var uniqueSubtypes = subtypes.stream() .filter(subtype -> subtype != Types.BOTTOM) - .collect(Collectors.toSet()); + .distinct() + .toList(); if (uniqueSubtypes.isEmpty()) { return BOTTOM; - } else if (uniqueSubtypes.size() == 1) { - return uniqueSubtypes.iterator().next(); } - for (var supertype : typeInfos.supertypes(uniqueSubtypes.iterator().next())) { + for (var supertype : typeInfos.supertypes(uniqueSubtypes.getFirst())) { if (uniqueSubtypes.stream().allMatch(subtype -> isSubType(subtype, supertype, typeInfos))) { return supertype; } -- cgit v1.2.3