From 5d41166750e0f05da436bf30c7a642749f7a71c5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 10 Aug 2026 10:12:48 +0100 Subject: Prevent singleton struct from being generic --- .../compiler/ast/untyped/UntypedStructDefinitionNode.java | 7 +++++++ .../zwobble/hobgoblin/compiler/typechecker/TypeChecker.java | 4 ++++ .../errors/SingletonStructCannotHaveTypeParamsError.java | 10 ++++++++++ 3 files changed, 21 insertions(+) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SingletonStructCannotHaveTypeParamsError.java (limited to 'src/main/java') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java index 9742068..29cb885 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java @@ -60,5 +60,12 @@ public record UntypedStructDefinitionNode( } // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode body + public boolean isGeneric() { + return this.typeParams.isPresent(); + } + + public boolean isSingleton() { + return this.fields.isEmpty(); + } // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode body } 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 63fc357..95ce840 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java @@ -227,6 +227,10 @@ public class TypeChecker { UntypedStructDefinitionNode untyped, TypeCheckerNamespaceContext context ) { + if (untyped.isSingleton() && untyped.isGeneric()) { + throw new SingletonStructCannotHaveTypeParamsError(untyped.source()); + } + var metaType = lookupTypeLevelValue(untyped.name(), untyped.source(), context); var typeOrConstructor = switch (metaType) { case SimpleStructType type -> diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SingletonStructCannotHaveTypeParamsError.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SingletonStructCannotHaveTypeParamsError.java new file mode 100644 index 0000000..a630646 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SingletonStructCannotHaveTypeParamsError.java @@ -0,0 +1,10 @@ +package org.zwobble.hobgoblin.compiler.typechecker.errors; + +import org.zwobble.hobgoblin.compiler.errors.SourceError; +import org.zwobble.hobgoblin.compiler.sources.Source; + +public class SingletonStructCannotHaveTypeParamsError extends SourceError { + public SingletonStructCannotHaveTypeParamsError(Source source) { + super("singleton struct cannot have type params", source); + } +} -- cgit v1.2.3