From 6af47e172db351c227349d9cc803afc8923c797b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 4 Dec 2026 11:02:13 +0000 Subject: Check that receiver is callable --- .../hobgoblin/compiler/typechecker/TypeChecker.java | 4 +++- .../compiler/typechecker/errors/NotCallableError.java | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/NotCallableError.java (limited to 'src/main/java') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java index a796d16..b92fe13 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -807,7 +807,9 @@ public class TypeChecker { var receiver = typeCheckExpression(untyped.receiver(), Optional.empty(), context); - var receiverType = (FunctionType) receiver.type(); + if (!(receiver.type() instanceof FunctionType receiverType)) { + throw new NotCallableError(receiver.type(), untyped.receiver().source()); + } if (receiverType.params().size() != untyped.args().size()) { throw new WrongNumberOfArgsError( diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/NotCallableError.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/NotCallableError.java new file mode 100644 index 0000000..90143fb --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/NotCallableError.java @@ -0,0 +1,18 @@ +package org.zwobble.hobgoblin.compiler.typechecker.errors; + +import org.zwobble.hobgoblin.compiler.errors.SourceError; +import org.zwobble.hobgoblin.compiler.sources.Source; +import org.zwobble.hobgoblin.compiler.types.Type; + +public class NotCallableError extends SourceError { + private final Type type; + + public NotCallableError(Type type, Source source) { + super(type.describe() + " is not callable", source); + this.type = type; + } + + public Type type() { + return type; + } +} -- cgit v1.2.3