From fccc76e339651d6451bb8fc7394f21eed96e3318 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 12:07:09 +0100 Subject: Check for namespace name when defining struct --- .../org/shedlang/compiler/typechecker/TypeChecker.java | 6 ++++-- .../errors/CannotDefineStructOutsideOfNamespace.java | 10 ++++++++++ .../typechecker/TypeCheckerStructDefinitionTests.java | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/shedlang/compiler/typechecker/errors/CannotDefineStructOutsideOfNamespace.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index 54e9d9b9..ad3ed297 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -831,8 +831,10 @@ public class TypeChecker { return new TypeCheckSteps() { @Override public TypeCheckerContext declareTypes(TypeCheckerContext context) { - // TODO: better error - var type = new StructType(context.namespaceName().orElseThrow(), untyped.name()); + var namespaceName = context.namespaceName() + .orElseThrow(() -> new CannotDefineStructOutsideOfNamespace(untyped.source())); + + var type = new StructType(namespaceName, untyped.name()); context = context.addLexicalState(untyped.name(), type); diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/CannotDefineStructOutsideOfNamespace.java b/src/main/java/org/shedlang/compiler/typechecker/errors/CannotDefineStructOutsideOfNamespace.java new file mode 100644 index 00000000..6e8810eb --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/CannotDefineStructOutsideOfNamespace.java @@ -0,0 +1,10 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.InternalCompilerError; +import org.shedlang.compiler.sources.Source; + +public class CannotDefineStructOutsideOfNamespace extends InternalCompilerError { + public CannotDefineStructOutsideOfNamespace(Source source) { + super("Cannot define struct outside of namespace", source); + } +} diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java index 325f7ea7..818c026f 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -5,12 +5,14 @@ import org.shedlang.compiler.ast.typed.TypedNamespaceStatementNode; import org.shedlang.compiler.ast.untyped.Untyped; import org.shedlang.compiler.ast.untyped.UntypedNamespaceStatementNode; import org.shedlang.compiler.ast.untyped.UntypedStructDefinitionNode; +import org.shedlang.compiler.typechecker.errors.CannotDefineStructOutsideOfNamespace; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -18,6 +20,19 @@ import static org.zwobble.precisely.Matchers.*; public class TypeCheckerStructDefinitionTests { private static final NamespaceName NAMESPACE_NAME = NamespaceName.of("Example"); + @Test + public void whenOutsideOfNamespaceThenErrorIsThrown() { + var untyped = UntypedStructDefinitionNode.builder() + .withName("ExampleStruct") + .build(); + var context = TypeCheckerContext.EMPTY; + + assertThrows( + CannotDefineStructOutsideOfNamespace.class, + () -> TypeChecker.typeCheckNamespaceStatement(untyped).typeCheckAllPhases(context) + ); + } + @Test public void canTypeCheckStructDefinitionWithNoFields() { var untyped = UntypedStructDefinitionNode.builder() -- cgit v1.2.3