diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-28 11:48:37 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-28 11:48:37 +0100 |
| commit | a409b4f6576980f876ec880ddf24dadc5d418c58 (patch) | |
| tree | 58432c2074f8ce8232ffc77c8c039788a690dc42 | |
| parent | 643d339ade252af66259466d9a9f615760471fc1 (diff) | |
Check struct type info is present
3 files changed, 48 insertions, 1 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index 73713a65..bd5b43fd 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -411,7 +411,11 @@ public class TypeChecker { untyped.fieldName() ); - var fieldType = structTypeInfo.orElseThrow().fieldType(untyped.fieldName()); + if (structTypeInfo.isEmpty()) { + throw new MissingStructTypeInfoError(receiverType, untyped.source()); + } + + var fieldType = structTypeInfo.get().fieldType(untyped.fieldName()); if (fieldType.isEmpty()) { throw new NoSuchFieldError(receiverType, untyped.fieldName(), untyped.source()); diff --git a/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java b/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java new file mode 100644 index 00000000..b7582135 --- /dev/null +++ b/src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java @@ -0,0 +1,24 @@ +package org.shedlang.compiler.typechecker.errors; + +import org.shedlang.compiler.errors.InternalCompilerError; +import org.shedlang.compiler.sources.Source; +import org.shedlang.compiler.types.StructType; + +public class MissingStructTypeInfoError extends InternalCompilerError { + private final StructType type; + private final Source source; + + public MissingStructTypeInfoError(StructType type, Source source) { + super("Missing type info for struct " + type); + this.type = type; + this.source = source; + } + + public StructType type() { + return type; + } + + public Source source() { + return source; + } +} diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFieldAccessTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFieldAccessTests.java index 88ffa75a..7f8ad5fb 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFieldAccessTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFieldAccessTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; +import org.shedlang.compiler.typechecker.errors.MissingStructTypeInfoError; import org.shedlang.compiler.typechecker.errors.NoSuchFieldError; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.StructTypeInfo; @@ -73,4 +74,22 @@ public class TypeCheckerFieldAccessTests { assertThat(error.type(), equalTo(structType)); assertThat(error.fieldName(), equalTo("name")); } + + @Test + public void whenStructTypeInfoIsMissingThenErrorIsThrown() { + var untyped = Untyped.fieldAccess( + Untyped.reference("user"), + "name" + ); + var structType = Types.struct(NamespaceName.of("Example"), "User"); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map(Map.entry("user", structType))); + + var error = assertThrows( + MissingStructTypeInfoError.class, + () -> TypeChecker.typeCheckExpression(untyped, context) + ); + + assertThat(error.type(), equalTo(structType)); + } } |
