summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-10 10:12:48 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-10 10:12:48 +0100
commit5d41166750e0f05da436bf30c7a642749f7a71c5 (patch)
tree3a300ac4a091adcd7f8e66dee64062065f995030 /src/main/java/org/zwobble
parenteb145887ab57b75299a55c85c753d3b4707105be (diff)
Prevent singleton struct from being generic
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNode.java7
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/errors/SingletonStructCannotHaveTypeParamsError.java10
3 files changed, 21 insertions, 0 deletions
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);
+ }
+}