aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-28 12:07:09 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-28 12:07:09 +0100
commitfccc76e339651d6451bb8fc7394f21eed96e3318 (patch)
tree64353f2cfa78b8a7bc858a63b43a915f42e758f4 /src
parent2f8e32636912e23552f5b43a2c84109b77fe7a92 (diff)
Check for namespace name when defining struct
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java6
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/errors/CannotDefineStructOutsideOfNamespace.java10
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java15
3 files changed, 29 insertions, 2 deletions
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<TypedStructDefinitionNode>() {
@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.*;
@@ -19,6 +21,19 @@ 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()
.withName("ExampleStruct")