From a409b4f6576980f876ec880ddf24dadc5d418c58 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 28 Sep 2023 11:48:37 +0100 Subject: Check struct type info is present --- .../shedlang/compiler/typechecker/TypeChecker.java | 6 +++++- .../errors/MissingStructTypeInfoError.java | 24 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/shedlang/compiler/typechecker/errors/MissingStructTypeInfoError.java (limited to 'src/main/java') 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; + } +} -- cgit v1.2.3