summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-28 13:52:34 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-28 13:52:34 +0100
commitaf0f173fcc0bf84316e188a0e6d4da4dfe11b021 (patch)
treeb9b24c3422ee113913fed383d99f372cd1560bfd
parent3ce11f3a242c2287a14d5f5a4ae230b4c79beaaa (diff)
Throw appropriate error if enum variant is not recognised
-rw-r--r--examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java6
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java46
2 files changed, 41 insertions, 11 deletions
diff --git a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java
index 0cd25d8..13e5044 100644
--- a/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java
+++ b/examples/16-transient-0/output/java/src/main/java/org/zwobble/example/types/data/transient0/HobgoblinTransient0Data.java
@@ -184,7 +184,11 @@ public final class HobgoblinTransient0Data {
public static org.zwobble.example.types.data.EnumWithVariants decodeEnumWithVariants(java.io.InputStream inputStream, java.util.List<java.lang.Object> sharedValues) throws java.io.IOException {
var ordinal = org.zwobble.example.types.transient0.HobgoblinTransient0Builtins.decodeInt32(inputStream, sharedValues);
- return EnumWithVariants$values[ordinal];
+ if (ordinal >= 0 && ordinal < 3) {
+ return EnumWithVariants$values[ordinal];
+ } else {
+ throw new java.io.IOException("unrecognised variant tag for data.EnumWithVariants: " + ordinal);
+ }
}
public static void encodeStructWithEnum(org.zwobble.example.types.data.StructWithEnum value, java.io.OutputStream outputStream, java.util.Map<org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue, java.lang.Long> sharedValues) throws java.io.IOException {
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
index 3a7f0ff..4197a24 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
@@ -653,11 +653,30 @@ public class JavaTransient0Generator implements Generator {
enumDefinition.type(),
new JavaBlock(Stream.concat(
generateDecode(ordinal, NativeTypes.INT_32, context).stream(),
- // TODO: handle ordinal not in array
- Stream.of(new JavaReturn(new JavaArrayAccess(
- new JavaRef(enumValues),
- new JavaRef(ordinal)
- )))
+ Stream.of(new JavaIfStatement(
+ new JavaBinaryOperation(
+ JavaBinaryOperator.LOGICAL_AND,
+ new JavaBinaryOperation(
+ JavaBinaryOperator.GREATER_THAN_OR_EQUAL,
+ new JavaRef(ordinal),
+ new JavaIntegerLiteral(0)
+ ),
+ new JavaBinaryOperation(
+ JavaBinaryOperator.LESS_THAN,
+ new JavaRef(ordinal),
+ new JavaIntegerLiteral(enumDefinition.variants().size())
+ )
+ ),
+ new JavaBlock(List.of(
+ new JavaReturn(new JavaArrayAccess(
+ new JavaRef(enumValues),
+ new JavaRef(ordinal)
+ ))
+ )),
+ new JavaBlock(List.of(
+ throwUnrecognisedVariantException(enumDefinition.type(), new JavaRef(ordinal))
+ ))
+ ))
).toList())
)
);
@@ -804,11 +823,7 @@ public class JavaTransient0Generator implements Generator {
cases.add(new JavaSwitchRule(
new JavaCaseDefault(),
new JavaBlock(List.of(
- throwIoException(new JavaBinaryOperation(
- JavaBinaryOperator.ADD,
- new JavaStringLiteral("unrecognised variant tag for " + sumDefinition.type().describe() + ": "),
- new JavaRef(variantTag)
- ))
+ throwUnrecognisedVariantException(sumDefinition.type(), new JavaRef(variantTag))
))
));
@@ -1631,6 +1646,17 @@ public class JavaTransient0Generator implements Generator {
);
}
+ private static JavaThrowStatement throwUnrecognisedVariantException(
+ Type type,
+ JavaExpression tag
+ ) {
+ return throwIoException(new JavaBinaryOperation(
+ JavaBinaryOperator.ADD,
+ new JavaStringLiteral("unrecognised variant tag for " + type.describe() + ": "),
+ tag
+ ));
+ }
+
private static JavaThrowStatement throwIoException(String errorMessage) {
return throwIoException(new JavaStringLiteral(errorMessage));
}