From 9c2421d317068021c64a42be9f785e5ec73afd43 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 26 Jun 2026 16:25:34 +0100 Subject: Treat variant as subtype of sum --- .../typechecker/TypeCheckerSubtypingTests.java | 40 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'src/test') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java index 35779fe..e2b43d0 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java @@ -1,7 +1,12 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.types.NamespaceName; import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; +import org.zwobble.hobgoblin.compiler.types.StructType; +import org.zwobble.hobgoblin.compiler.types.SumType; + +import java.util.List; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerSubtyping.isSubtype; import static org.zwobble.precisely.AssertThat.assertThat; @@ -13,7 +18,7 @@ public class TypeCheckerSubtypingTests { var nativeType1 = SimpleNativeType.builtin("Int32"); var nativeType2 = SimpleNativeType.builtin("Int32"); - var isSubtype = isSubtype(nativeType1, nativeType2); + var isSubtype = isSubtype(nativeType1, nativeType2, TypesInfo.EMPTY); assertThat(isSubtype, equalTo(true)); } @@ -23,7 +28,38 @@ public class TypeCheckerSubtypingTests { var nativeType1 = SimpleNativeType.builtin("Int32"); var nativeType2 = SimpleNativeType.builtin("Int64"); - var isSubtype = isSubtype(nativeType1, nativeType2); + var isSubtype = isSubtype(nativeType1, nativeType2, TypesInfo.EMPTY); + + assertThat(isSubtype, equalTo(false)); + } + + @Test + public void variantIsSubtypeOfSumType() { + var variantType1 = new StructType(NamespaceName.of(), "Rectangle"); + var variantType2 = new StructType(NamespaceName.of(), "Circle"); + var sumType = new SumType(NamespaceName.of(), "Shape"); + var typesInfo = TypesInfoInMemory.empty(); + typesInfo.defineSumType(sumType, List.of(variantType1, variantType2), List.of()); + + var isSubtype = isSubtype(variantType1, sumType, typesInfo); + + assertThat(isSubtype, equalTo(true)); + } + + @Test + public void variantIsNotSubtypeOfUnrelatedSumType() { + var typesInfo = TypesInfoInMemory.empty(); + + var variantType1 = new StructType(NamespaceName.of(), "Rectangle"); + var variantType2 = new StructType(NamespaceName.of(), "Circle"); + var sumType = new SumType(NamespaceName.of(), "Shape"); + typesInfo.defineSumType(sumType, List.of(variantType1, variantType2), List.of()); + + var otherSumType = new SumType(NamespaceName.of(), "Shape3D"); + var otherVariantType = new StructType(NamespaceName.of(), "Cube"); + typesInfo.defineSumType(sumType, List.of(otherVariantType), List.of()); + + var isSubtype = isSubtype(variantType1, otherSumType, typesInfo); assertThat(isSubtype, equalTo(false)); } -- cgit v1.2.3