From f727123a92878a96bfb6a357207c1a6c4ec902d3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 29 Nov 2026 23:26:27 +0000 Subject: Declare variables up-front --- .../TypeCheckerBinaryOperationTests.java | 14 +++-- .../TypeCheckerConstructedTypeTests.java | 24 ++++---- .../typechecker/TypeCheckerContextArb.java | 12 ++++ .../TypeCheckerDatabaseSchemaDefinitionTests.java | 13 +++-- .../TypeCheckerEnumDefinitionTests.java | 9 ++- .../TypeCheckerFunctionExpressionTests.java | 13 +++-- .../typechecker/TypeCheckerMemberAccessTests.java | 10 ++-- .../typechecker/TypeCheckerMetaTypeTests.java | 15 +++-- .../TypeCheckerNamespaceContextTests.java | 6 +- .../TypeCheckerNativeTypeDefinitionTests.java | 5 +- .../typechecker/TypeCheckerNumberLiteralTests.java | 3 +- .../TypeCheckerQueryDefinitionTests.java | 5 +- .../typechecker/TypeCheckerReferenceTests.java | 8 ++- .../TypeCheckerStructDefinitionTests.java | 34 +++++++---- .../typechecker/TypeCheckerSumDefinitionTests.java | 66 ++++++++++++++-------- .../TypeCheckerTableDefinitionTests.java | 15 +++-- .../TypeCheckerTypeLevelReferenceTests.java | 10 ++-- .../compiler/typechecker/VariableMatchers.java | 21 +++++++ 18 files changed, 193 insertions(+), 90 deletions(-) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java (limited to 'src/test') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java index 64e2be3..d99037b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java @@ -28,9 +28,10 @@ public class TypeCheckerBinaryOperationTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); - namespaceContext.declare("b", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .define("b", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -51,9 +52,10 @@ public class TypeCheckerBinaryOperationTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); - namespaceContext.declare("b", NativeTypes.STRING, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .define("b", NativeTypes.STRING, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnsupportedBinaryOperationError.class, diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java index 49aa4b6..84e930a 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java @@ -21,11 +21,12 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of(UntypedArb.typeLevelReference("Int32")) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); - context.declare("List", new TypeLevelValueType(listType), NullSource.INSTANCE); var int32Type = SimpleNativeType.builtin("Int32"); - context.declare("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listType), NullSource.INSTANCE) + .define("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -42,12 +43,13 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of(UntypedArb.typeLevelReference("Int32")) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); var int32Type = SimpleNativeType.builtin("Int32"); - context.declare("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContext().enter() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .define("Int32", new TypeLevelValueType(int32Type), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); @@ -62,10 +64,11 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("List"), List.of() ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( WrongNumberOfTypeLevelArgsError.class, @@ -85,10 +88,11 @@ public class TypeCheckerConstructedTypeTests { UntypedArb.typeLevelReference("Int64") ) ); - var context = TypeCheckerContextArb.namespaceContext(); var listType = SimpleNativeType.builtin("List"); var listTypeConstructor = Types.constructor(List.of(new TypeParam(listType, "T")), listType); - context.declare("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(listTypeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( WrongNumberOfTypeLevelArgsError.class, diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java index 4959aa0..e8d44d1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerContextArb.java @@ -15,6 +15,18 @@ public class TypeCheckerContextArb { } public static TypeCheckerNamespaceContext namespaceContext(NamespaceName namespaceName) { + return TypeCheckerGlobalContext.initial() + .enterNamespace(namespaceName) + .endDeclarations(); + } + + public static TypeCheckerNamespaceContext.Declaring namespaceContextDeclaring() { + return namespaceContextDeclaring(NamespaceName.of("example")); + } + + public static TypeCheckerNamespaceContext.Declaring namespaceContextDeclaring( + NamespaceName namespaceName + ) { return TypeCheckerGlobalContext.initial() .enterNamespace(namespaceName); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java index 63ed810..37434f3 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerDatabaseSchemaDefinitionTests.java @@ -15,6 +15,7 @@ import java.util.Optional; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedDatabaseSchemaDefinitionNodeMatcher.isTypedDatabaseSchemaDefinitionNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedTableDefinitionNodeMatcher.isTypedTableDefinitionNode; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement; +import static org.zwobble.hobgoblin.compiler.typechecker.VariableMatchers.isDefinedVariable; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -33,9 +34,11 @@ public class TypeCheckerDatabaseSchemaDefinitionTests { ) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .declare("blogApp", NullSource.INSTANCE) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("id", NativeTypes.INT_32, NullSource.INSTANCE) ))); @@ -51,8 +54,10 @@ public class TypeCheckerDatabaseSchemaDefinitionTests { )) ); assertThat( - namespaceContext.lookupType("blogApp"), - isOptionalOf(equalTo(Types.databaseSchema(new DatabaseSchema(NamespaceName.of("App"), "blogApp")))) + namespaceContext.lookupVariable("blogApp"), + isDefinedVariable( + Types.databaseSchema(new DatabaseSchema(NamespaceName.of("App"), "blogApp")) + ) ); var typesInfo = namespaceContext.toTypesInfo(); var schemaInfo = typesInfo.databaseSchemaInfo(new DatabaseSchema(NamespaceName.of("App"), "blogApp")); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java index ac85b7d..f47eb1c 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEnumDefinitionTests.java @@ -3,6 +3,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.*; import org.zwobble.hobgoblin.compiler.ast.untyped.*; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.*; import java.util.List; @@ -17,7 +18,9 @@ public class TypeCheckerEnumDefinitionTests { var untyped = UntypedEnumDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -42,7 +45,9 @@ public class TypeCheckerEnumDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java index a03beae..af8005f 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -30,8 +30,9 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("a", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -54,7 +55,8 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typeHint = new FunctionType( List.of(NativeTypes.INT_64, NativeTypes.STRING), @@ -82,7 +84,8 @@ public class TypeCheckerFunctionExpressionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typeHint = new FunctionType( List.of(NativeTypes.INT_32), @@ -100,6 +103,6 @@ public class TypeCheckerFunctionExpressionTests { NativeTypes.INT_32 ))) ); - assertThat(namespaceContext.lookupType("a"), isOptionalEmpty()); + assertThat(namespaceContext.lookupVariable("a"), equalTo(new Variable.Undeclared())); } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java index 8c5e053..6bd99d2 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMemberAccessTests.java @@ -26,9 +26,10 @@ public class TypeCheckerMemberAccessTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); - namespaceContext.declare("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE) + .endDeclarations(); var table = new Table(databaseSchema, "authors"); var rowType = new SimpleStructType(namespaceName, "Author"); var idField = new Field("id", NativeTypes.INT_32, NullSource.INSTANCE); @@ -54,10 +55,11 @@ public class TypeCheckerMemberAccessTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); var table = new Table(databaseSchema, "authors"); - namespaceContext.declare("authors", Types.table(table), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("authors", Types.table(table), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java index 713cd13..76bbb59 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerMetaTypeTests.java @@ -18,9 +18,10 @@ public class TypeCheckerMetaTypeTests { @Test public void whenVariableIsNotTypeLevelValueThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", nativeType, NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", nativeType, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -34,10 +35,11 @@ public class TypeCheckerMetaTypeTests { @Test public void whenVariableIsTypeLevelValueButNotMetaTypeThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("List"); - var context = TypeCheckerContextArb.namespaceContext(); var innerType = SimpleNativeType.builtin("List"); var typeConstructor = Types.constructor(List.of(new TypeParam(innerType, "T")), innerType); - context.declare("List", new TypeLevelValueType(typeConstructor), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("List", new TypeLevelValueType(typeConstructor), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -51,9 +53,10 @@ public class TypeCheckerMetaTypeTests { @Test public void whenNameIsTypeLevelValueThenValueIsType() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckMetaType(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java index 4f22537..8c93776 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNamespaceContextTests.java @@ -12,14 +12,14 @@ import static org.zwobble.precisely.Matchers.equalTo; public class TypeCheckerNamespaceContextTests { @Test - public void declaringVariablesWithSameThrowsError() { + public void declaringVariablesWithSameNameThrowsError() { var context = TypeCheckerGlobalContext.initial() .enterNamespace(NamespaceName.of()); - context.declare("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE); + context.define("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE); var error = assertThrows( DuplicateVariableNameError.class, - () -> context.declare("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE) + () -> context.define("Shape", new SumType(NamespaceName.of(), "Shape"), NullSource.INSTANCE) ); assertThat(error.name(), equalTo("Shape")); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java index 1777506..4409261 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNativeTypeDefinitionTests.java @@ -3,6 +3,7 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.TypedNativeTypeDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; import org.zwobble.hobgoblin.compiler.types.SimpleNativeType; import org.zwobble.hobgoblin.compiler.types.TypeOrConstructor; @@ -17,7 +18,9 @@ public class TypeCheckerNativeTypeDefinitionTests { var untyped = UntypedNativeTypeDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java index 40df722..47c6c0b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerNumberLiteralTests.java @@ -21,7 +21,8 @@ public class TypeCheckerNumberLiteralTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java index 43ffb04..e28e2d5 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerQueryDefinitionTests.java @@ -27,10 +27,11 @@ public class TypeCheckerQueryDefinitionTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); var databaseSchema = new DatabaseSchema(namespaceName, "blogApp"); var table = new Table(databaseSchema, "authors"); - namespaceContext.declare("authors", Types.table(table), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("authors", Types.table(table), NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java index 4671a22..2fb179b 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerReferenceTests.java @@ -22,8 +22,9 @@ public class TypeCheckerReferenceTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); - namespaceContext.declare("count", NativeTypes.INT_32, NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .define("count", NativeTypes.INT_32, NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckExpression(untyped, Optional.empty(), namespaceContext); @@ -40,7 +41,8 @@ public class TypeCheckerReferenceTests { .build(); var globalContext = TypeCheckerContextArb.globalContext(); var namespaceName = NamespaceName.of("App"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .endDeclarations(); var error = assertThrows( UndeclaredVariableError.class, 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 f25a600..982e449 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -5,6 +5,7 @@ import org.zwobble.hobgoblin.compiler.ast.typed.TypedNamespaceNode; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.*; +import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.typechecker.errors.SingletonStructCannotHaveTypeParamsError; import org.zwobble.hobgoblin.compiler.types.*; @@ -13,6 +14,7 @@ import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.zwobble.hobgoblin.compiler.typechecker.TypeCheckerTesting.typeCheckNamespaceStatement; +import static org.zwobble.hobgoblin.compiler.typechecker.VariableMatchers.isDefinedVariable; import static org.zwobble.hobgoblin.compiler.types.TypeMatchers.isField; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -23,7 +25,9 @@ public class TypeCheckerStructDefinitionTests { var untyped = UntypedStructDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -36,8 +40,8 @@ public class TypeCheckerStructDefinitionTests { equalTo(TypeOrConstructor.type(expectedType)) ) )); - assertThat(context.lookupType("X"), isOptionalOf( - equalTo(Types.metaType(expectedType)) + assertThat(context.lookupVariable("X"), isDefinedVariable( + Types.metaType(expectedType) )); } @@ -51,7 +55,9 @@ public class TypeCheckerStructDefinitionTests { ))) .withFields(List.of()) .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -71,8 +77,8 @@ public class TypeCheckerStructDefinitionTests { equalTo(TypeOrConstructor.constructor(expectedTypeConstructor)) ) )); - assertThat(context.lookupType("X"), isOptionalOf( - equalTo(Types.metaType(expectedTypeConstructor)) + assertThat(context.lookupVariable("X"), isDefinedVariable( + Types.metaType(expectedTypeConstructor) )); } @@ -91,7 +97,9 @@ public class TypeCheckerStructDefinitionTests { globalContext.addNativeType(int32Type); globalContext.addNativeType(int64Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -130,7 +138,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -158,7 +168,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); assertThrows( SingletonStructCannotHaveTypeParamsError.class, @@ -187,7 +199,9 @@ public class TypeCheckerStructDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext.enterNamespace(namespaceName); + var namespaceContext = globalContext.enterNamespace(namespaceName) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); 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 a1cb5a9..1474e39 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerSumDefinitionTests.java @@ -27,7 +27,9 @@ public class TypeCheckerSumDefinitionTests { var untyped = UntypedSumDefinitionNode.arbitrary() .withName("X") .build(); - var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + var context = TypeCheckerContextArb.namespaceContextDeclaring(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, context); @@ -54,9 +56,11 @@ public class TypeCheckerSumDefinitionTests { .build(); var globalContext = TypeCheckerGlobalContext.initial(); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE); - namespaceContext.declare(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE) + .define(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -103,9 +107,11 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE); - namespaceContext.declare(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define(rectangleType.name(), new TypeLevelValueType(rectangleType), NullSource.INSTANCE) + .define(triangleType.name(), new TypeLevelValueType(triangleType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -147,7 +153,9 @@ public class TypeCheckerSumDefinitionTests { globalContext.addNativeType(NativeTypes.INT_32); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( SumVariantMustBeStructError.class, @@ -171,8 +179,10 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeTypeConstructor(NativeTypes.BOX); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); - namespaceContext.declare("Variant", new TypeLevelValueType(variantType), NullSource.INSTANCE); + .enterNamespace(NamespaceName.of("a", "b")) + .define("Variant", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( SumVariantMustBeInSameNamespaceError.class, @@ -203,7 +213,9 @@ public class TypeCheckerSumDefinitionTests { globalContext.addNativeType(int32Type); globalContext.addNativeType(int64Type); var namespaceContext = globalContext - .enterNamespace(NamespaceName.of("a", "b")); + .enterNamespace(NamespaceName.of("a", "b")) + .declare("X", NullSource.INSTANCE) + .endDeclarations(); var typed = typeCheckNamespaceStatement(untyped, namespaceContext); @@ -246,10 +258,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of())); var error = assertThrows( @@ -283,10 +297,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of(new Field("area", int64Type, NullSource.INSTANCE)))); // TODO: more specific error @@ -319,10 +335,12 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Square", new TypeLevelValueType(variantType), NullSource.INSTANCE) + .declare("Shape", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(variantType, Optional.of(List.of())); assertThrows( @@ -340,11 +358,13 @@ public class TypeCheckerSumDefinitionTests { var globalContext = TypeCheckerGlobalContext.initial(); globalContext.addNativeType(int32Type); var namespaceName = NamespaceName.of("a", "b"); - var namespaceContext = globalContext - .enterNamespace(namespaceName); var sumType = new SumType(namespaceName, "Shape"); var variantType = new SimpleStructType(namespaceName, "Square"); - namespaceContext.declare("Sum", new TypeLevelValueType(sumType), NullSource.INSTANCE); + var namespaceContext = globalContext + .enterNamespace(namespaceName) + .define("Sum", new TypeLevelValueType(sumType), NullSource.INSTANCE) + .declare("Square", NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineSumType( sumType, List.of(new SumVariant(0, variantType, variantType)), diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java index 9dbee97..ef89ee0 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTableDefinitionTests.java @@ -30,9 +30,10 @@ public class TypeCheckerTableDefinitionTests { .withPrimaryKey(UntypedTypeLevelReferenceNode.arbitrary().withName("id")) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("id", NativeTypes.INT_32, NullSource.INSTANCE) ))); @@ -61,9 +62,10 @@ public class TypeCheckerTableDefinitionTests { ) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleNativeType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( TableRowTypeMustBeStructError.class, @@ -88,9 +90,10 @@ public class TypeCheckerTableDefinitionTests { .withPrimaryKey(UntypedTypeLevelReferenceNode.arbitrary().withName("id")) .build(); var globalContext = TypeCheckerContextArb.globalContext(); - var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")); var authorType = new SimpleStructType(NamespaceName.of("App"), "Author"); - namespaceContext.declare("Author", Types.metaType(authorType), NullSource.INSTANCE); + var namespaceContext = globalContext.enterNamespace(NamespaceName.of("App")) + .define("Author", Types.metaType(authorType), NullSource.INSTANCE) + .endDeclarations(); namespaceContext.defineStructType(authorType, Optional.of(List.of( new Field("author_id", NativeTypes.INT_32, NullSource.INSTANCE) ))); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java index 98f4f22..003641e 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerTypeLevelReferenceTests.java @@ -31,9 +31,10 @@ public class TypeCheckerTypeLevelReferenceTests { @Test public void whenVariableIsNotTypeLevelValueThenErrorIsThrown() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", nativeType, NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", nativeType, NullSource.INSTANCE) + .endDeclarations(); var error = assertThrows( UnexpectedTypeError.class, @@ -47,9 +48,10 @@ public class TypeCheckerTypeLevelReferenceTests { @Test public void whenNameIsTypeLevelValueThenValueIsType() { var untyped = UntypedArb.typeLevelReference("X"); - var context = TypeCheckerContextArb.namespaceContext(); var nativeType = SimpleNativeType.builtin("Int"); - context.declare("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE); + var context = TypeCheckerContextArb.namespaceContextDeclaring() + .define("X", new TypeLevelValueType(nativeType), NullSource.INSTANCE) + .endDeclarations(); var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java new file mode 100644 index 0000000..9e17a99 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/VariableMatchers.java @@ -0,0 +1,21 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.zwobble.hobgoblin.compiler.types.Type; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.*; + +public class VariableMatchers { + public VariableMatchers() {} + + static Matcher isDefinedVariable(Type type) { + return instanceOf( + Variable.Defined.class, + has( + "type", + Variable.Defined::type, + equalTo(type) + ) + ); + } +} -- cgit v1.2.3