summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-02 12:39:49 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-02 12:39:49 +0100
commitdf914fb9c7eeee62b050164d95f99bb81b395bc9 (patch)
treebc1bf21f86c9c3d2116784173d3961b736d1118d /src
parentdec0f4467d4089c251b17d2cc6b4a0a8fed18f82 (diff)
Handle Box and Shared when determining constructibility
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysis.java23
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java12
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java110
3 files changed, 141 insertions, 4 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysis.java b/src/main/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysis.java
index d7b3c60..6bc4852 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysis.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysis.java
@@ -2,6 +2,7 @@ package org.zwobble.hobgoblin.compiler.analysis;
import org.zwobble.hobgoblin.compiler.analysis.errors.SumTypeHasNoVariantsError;
import org.zwobble.hobgoblin.compiler.analysis.errors.TypeIsInfiniteError;
+import org.zwobble.hobgoblin.compiler.builtins.NativeTypes;
import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo;
import org.zwobble.hobgoblin.compiler.types.*;
@@ -31,9 +32,25 @@ public class ArbitraryValueAnalysis {
}
return switch (type) {
- case ConstructedNativeType _ ->
- // TODO: Handle Box and Shared
- true;
+ case ConstructedNativeType constructedNativeType -> {
+ if (
+ constructedNativeType.constructor().equals(NativeTypes.BOX) ||
+ constructedNativeType.constructor().equals(NativeTypes.SHARED)
+ ) {
+ yield isNonRecursivelyConstructable(
+ constructedNativeType.args().getFirst(),
+ seenTypes,
+ typesInfo
+ );
+ } else if (
+ constructedNativeType.constructor().equals(NativeTypes.LIST) ||
+ constructedNativeType.constructor().equals(NativeTypes.OPTION)
+ ) {
+ yield true;
+ } else {
+ throw new UnsupportedOperationException("TODO");
+ }
+ }
case EnumType _ ->
true;
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java b/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java
index 9d81063..485df8c 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/builtins/NativeTypes.java
@@ -29,6 +29,10 @@ public class NativeTypes {
LIST_INNER
);
+ public static Type list(Type elementType) {
+ return new ConstructedNativeType(LIST, List.of(elementType));
+ }
+
public static final SimpleNativeType OPTION_INNER = SimpleNativeType.builtin("Option");
public static TypeConstructor<SimpleNativeType> OPTION = new TypeConstructor<>(
@@ -36,10 +40,18 @@ public class NativeTypes {
OPTION_INNER
);
+ public static Type option(Type elementType) {
+ return new ConstructedNativeType(OPTION, List.of(elementType));
+ }
+
public static final SimpleNativeType SHARED_INNER = SimpleNativeType.builtin("Shared");
public static TypeConstructor<SimpleNativeType> SHARED = new TypeConstructor<>(
List.of(new TypeParam("T")),
SHARED_INNER
);
+
+ public static Type shared(Type elementType) {
+ return new ConstructedNativeType(SHARED, List.of(elementType));
+ }
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
index 526f6bb..b802386 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
@@ -57,7 +57,7 @@ public class ArbitraryValueAnalysisTests {
}
@Test
- public void firstNonRecursiveVariantIsChosen() {
+ public void firstNonRecursivelyConstructibleVariantIsChosen() {
var sumType = new SumType(NamespaceName.of(), "X");
var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
@@ -82,4 +82,112 @@ public class ArbitraryValueAnalysisTests {
assertThat(variant, equalTo(nonRecursiveVariantType));
}
+
+ @Test
+ public void boxIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.box(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.box(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(nonRecursiveVariantType));
+ }
+
+ @Test
+ public void listIsAlwaysNonRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.list(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.list(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(recursiveVariantType));
+ }
+
+ @Test
+ public void optionIsAlwaysNonRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.option(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.option(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(recursiveVariantType));
+ }
+
+ @Test
+ public void sharedIsRecursivelyConstructibleIffInnerTypeIsRecursivelyConstructible() {
+ var sumType = new SumType(NamespaceName.of(), "X");
+ var recursiveVariantType = new StructType(NamespaceName.of(), "Y");
+ var nonRecursiveVariantType = new StructType(NamespaceName.of(), "Z");
+ var typesInfo = TypesInfoInMemory.empty();
+ typesInfo.defineSumType(
+ sumType,
+ List.of(
+ new SumVariant(0, recursiveVariantType, recursiveVariantType),
+ new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
+ ),
+ List.of(),
+ NullSource.INSTANCE
+ );
+ typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.shared(sumType), NullSource.INSTANCE)
+ )));
+ typesInfo.defineStructType(nonRecursiveVariantType, Optional.of(List.of(
+ new Field("x", NativeTypes.shared(NativeTypes.INT_64), NullSource.INSTANCE)
+ )));
+
+ var variant = selectArbitraryVariant(sumType, typesInfo);
+
+ assertThat(variant, equalTo(nonRecursiveVariantType));
+ }
}