From 870aa25e96683a6686b019e40e1fe611fd36c69f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 7 Jan 2024 17:50:25 +0000 Subject: Update commonSupertype to consider sum types --- .../compiler/types/CommonSupertypeTests.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/types/CommonSupertypeTests.java b/src/test/java/org/shedlang/compiler/types/CommonSupertypeTests.java index a30ecb24..1b9873a6 100644 --- a/src/test/java/org/shedlang/compiler/types/CommonSupertypeTests.java +++ b/src/test/java/org/shedlang/compiler/types/CommonSupertypeTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.types; import org.junit.jupiter.api.Test; import org.shedlang.compiler.typechecker.TypeInfos; +import org.shedlang.compiler.util.P; import java.util.List; @@ -62,4 +63,24 @@ public class CommonSupertypeTests { assertThat(result, equalTo(Types.INT)); } + + @Test + public void commonSupertypeOfVariantsOfSameSumTypeIsThatSumType() { + var namespaceName = NamespaceName.of("Example"); + var variantType1 = Types.struct(namespaceName, "Variant1"); + var variantType2 = Types.struct(namespaceName, "Variant2"); + var sumType = Types.sum(namespaceName, "Sum"); + var typeInfos = TypeInfos.EMPTY + .addSumTypeInfo(sumType, new SumTypeInfo(P.orderedSet( + variantType1, + variantType2 + ))); + + var result = Types.commonSupertype( + List.of(variantType1, variantType2), + typeInfos + ); + + assertThat(result, equalTo(sumType)); + } } -- cgit v1.2.3