summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-02 12:00:52 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-02 12:00:52 +0100
commitbbee238f6f5989dbcfd2790d7310aad5664336ec (patch)
tree17f5071efbdd5c2a846d59744b5b001960e577bd
parent3b74ee086e4b59e4265fa632c6db93fd0a9d07ea (diff)
Store source for sum types
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java7
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java5
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java9
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java8
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java10
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java3
6 files changed, 29 insertions, 13 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
index d87b7ff..a40bd92 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -293,7 +293,12 @@ public class TypeChecker {
var typeCheckedFieldDefinitions = typeCheckFieldDefinitions(untyped.fields(), context);
- context.defineSumType(sumType, variants, typeCheckedFieldDefinitions.fields);
+ context.defineSumType(
+ sumType,
+ variants,
+ typeCheckedFieldDefinitions.fields,
+ untyped.source()
+ );
for (var variant : variants) {
if (context.isDefined(variant.valueType())) {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java
index 804fd68..56bc5ed 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContext.java
@@ -76,9 +76,10 @@ public class TypeCheckerNamespaceContext {
public void defineSumType(
SumType sumType,
List<SumVariant> variantTypes,
- List<Field> fields
+ List<Field> fields,
+ Source source
) {
- this.typesInfo.defineSumType(sumType, variantTypes, fields);
+ this.typesInfo.defineSumType(sumType, variantTypes, fields, source);
}
public List<SumType> variantOf(StructType variantType) {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java
index 64c285b..07c3673 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypesInfoInMemory.java
@@ -1,5 +1,6 @@
package org.zwobble.hobgoblin.compiler.typechecker;
+import org.zwobble.hobgoblin.compiler.sources.Source;
import org.zwobble.hobgoblin.compiler.types.*;
import java.util.*;
@@ -7,7 +8,8 @@ import java.util.*;
public class TypesInfoInMemory implements TypesInfo {
private record SumTypeInfo(
List<SumVariant> variants,
- List<Field> fields
+ List<Field> fields,
+ Source source
) {}
public static TypesInfoInMemory empty() {
@@ -69,9 +71,10 @@ public class TypesInfoInMemory implements TypesInfo {
public void defineSumType(
SumType sumType,
List<SumVariant> variants,
- List<Field> fields
+ List<Field> fields,
+ Source source
) {
- var sumTypeInfo = new SumTypeInfo(variants, fields);
+ var sumTypeInfo = new SumTypeInfo(variants, fields, source);
this.sumTypeInfos.put(sumType, sumTypeInfo);
for (var variant : variants) {
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 30dbc7b..526f6bb 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/analysis/ArbitraryValueAnalysisTests.java
@@ -21,7 +21,7 @@ public class ArbitraryValueAnalysisTests {
public void whenTypeHasNoVariantsThenErrorIsThrown() {
var sumType = new SumType(NamespaceName.of(), "X");
var typesInfo = TypesInfoInMemory.empty();
- typesInfo.defineSumType(sumType, List.of(), List.of());
+ typesInfo.defineSumType(sumType, List.of(), List.of(), NullSource.INSTANCE);
var error = assertThrows(
SumTypeHasNoVariantsError.class,
@@ -41,7 +41,8 @@ public class ArbitraryValueAnalysisTests {
List.of(
new SumVariant(0, variantType, variantType)
),
- List.of()
+ List.of(),
+ NullSource.INSTANCE
);
typesInfo.defineStructType(variantType, Optional.of(List.of(
new Field("x", sumType, NullSource.INSTANCE)
@@ -67,7 +68,8 @@ public class ArbitraryValueAnalysisTests {
new SumVariant(0, recursiveVariantType, recursiveVariantType),
new SumVariant(1, nonRecursiveVariantType, nonRecursiveVariantType)
),
- List.of()
+ List.of(),
+ NullSource.INSTANCE
);
typesInfo.defineStructType(recursiveVariantType, Optional.of(List.of(
new Field("x", sumType, NullSource.INSTANCE)
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 7080b81..f22bc3e 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSubtypingTests.java
@@ -1,6 +1,7 @@
package org.zwobble.hobgoblin.compiler.typechecker;
import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.sources.NullSource;
import org.zwobble.hobgoblin.compiler.types.*;
import java.util.List;
@@ -42,7 +43,8 @@ public class TypeCheckerSubtypingTests {
new SumVariant(0, variantType1, variantType1),
new SumVariant(1, variantType2, variantType2)
),
- List.of()
+ List.of(),
+ NullSource.INSTANCE
);
var isSubtype = isSubtype(variantType1, sumType, typesInfo);
@@ -63,7 +65,8 @@ public class TypeCheckerSubtypingTests {
new SumVariant(0, variantType1, variantType1),
new SumVariant(1, variantType2, variantType2)
),
- List.of()
+ List.of(),
+ NullSource.INSTANCE
);
var otherSumType = new SumType(NamespaceName.of(), "Shape3D");
@@ -71,7 +74,8 @@ public class TypeCheckerSubtypingTests {
typesInfo.defineSumType(
otherSumType,
List.of(new SumVariant(0, otherVariantType, otherVariantType)),
- List.of()
+ List.of(),
+ NullSource.INSTANCE
);
var isSubtype = isSubtype(variantType1, otherSumType, typesInfo);
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
index 96f2e5d..ef67cb5 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java
@@ -322,7 +322,8 @@ public class TypeCheckerSumDefinitionTests {
namespaceContext.defineSumType(
sumType,
List.of(new SumVariant(0, variantType, variantType)),
- List.of(new Field("area", int32Type, NullSource.INSTANCE))
+ List.of(new Field("area", int32Type, NullSource.INSTANCE)),
+ NullSource.INSTANCE
);
assertThrows(