diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-02 13:54:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-02 13:54:53 +0100 |
| commit | 4c2db9bb403c8ffc04eadb6ab262d5137f216000 (patch) | |
| tree | f990ed4fbf61b53757ee79e92b22b67e3d8d5f4c /src/main/java/org/zwobble | |
| parent | 75ccf6f44905cbc94b866821f34f340370430292 (diff) | |
Check that sum variant is struct
Diffstat (limited to 'src/main/java/org/zwobble')
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java | 8 | ||||
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SumVariantMustBeStructError.java | 18 |
2 files changed, 24 insertions, 2 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 a40bd92..c2eae29 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -286,8 +286,12 @@ public class TypeChecker { } else { variantValueType = variantType.value(); } - // TODO: handle not struct type - var variant = new SumVariant(variants.size(), variantType.value(), (StructType) variantValueType); + + if (!(variantValueType instanceof StructType variantValueStructType)) { + throw new SumVariantMustBeStructError(variantValueType, untypedVariant.source()); + } + + var variant = new SumVariant(variants.size(), variantType.value(), variantValueStructType); variants.add(variant); } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SumVariantMustBeStructError.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SumVariantMustBeStructError.java new file mode 100644 index 0000000..78235ae --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SumVariantMustBeStructError.java @@ -0,0 +1,18 @@ +package org.zwobble.hobgoblin.compiler.typechecker.errors; + +import org.zwobble.hobgoblin.compiler.errors.SourceError; +import org.zwobble.hobgoblin.compiler.sources.Source; +import org.zwobble.hobgoblin.compiler.types.Type; + +public class SumVariantMustBeStructError extends SourceError { + private final Type variantType; + + public SumVariantMustBeStructError(Type variantType, Source source) { + super("sum variants must be structs, but was " + variantType.describe(), source); + this.variantType = variantType; + } + + public Type variantType() { + return variantType; + } +} |
