From 4c2db9bb403c8ffc04eadb6ab262d5137f216000 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 2 Aug 2026 13:54:53 +0100 Subject: Check that sum variant is struct --- .../hobgoblin/compiler/typechecker/TypeChecker.java | 8 ++++++-- .../errors/SumVariantMustBeStructError.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SumVariantMustBeStructError.java (limited to 'src/main/java/org') 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; + } +} -- cgit v1.2.3