From 72c84ea17a7e4b27cf9428ef23f713e32f5339d3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 25 Apr 2026 10:56:55 +0100 Subject: Type check struct fields --- .../TypeCheckerStructDefinitionTests.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/test/java/org/zwobble') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java index b6fddba..f13731f 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -2,10 +2,16 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructFieldDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedTypeLevelExpressionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedArb; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; +import org.zwobble.hobgoblin.compiler.types.ScalarType; import org.zwobble.hobgoblin.compiler.types.StructType; +import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; import java.util.List; @@ -33,4 +39,51 @@ public class TypeCheckerStructDefinitionTests { ) )); } + + @Test + public void fieldsAreTypeChecked() { + var int32Type = new ScalarType("Int32"); + var int64Type = new ScalarType("Int64"); + var untyped = new UntypedStructDefinitionNode( + "X", + List.of( + new UntypedStructFieldDefinitionNode("a", UntypedArb.typeLevelReference("Int32"), NullSource.INSTANCE), + new UntypedStructFieldDefinitionNode("b", UntypedArb.typeLevelReference("Int64"), NullSource.INSTANCE) + ), + NullSource.INSTANCE + ); + var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + context.declare(int32Type.name()); + context.define(int32Type.name(), new TypeLevelValueType(int32Type)); + context.declare(int64Type.name()); + context.define(int64Type.name(), new TypeLevelValueType(int64Type)); + + var typed = TypeChecker.typeCheckNamespaceStatement(untyped, context); + + assertThat(typed, instanceOf( + TypedStructDefinitionNode.class, + has( + "fields", + TypedStructDefinitionNode::fields, + isSequence( + allOf( + has("name", TypedStructFieldDefinitionNode::name, equalTo("a")), + has("type", TypedStructFieldDefinitionNode::type, has( + "value", + TypedTypeLevelExpressionNode::value, + equalTo(int32Type) + )) + ), + allOf( + has("name", TypedStructFieldDefinitionNode::name, equalTo("b")), + has("type", TypedStructFieldDefinitionNode::type, has( + "value", + TypedTypeLevelExpressionNode::value, + equalTo(int64Type) + )) + ) + ) + ) + )); + } } -- cgit v1.2.3