summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-01 11:29:17 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-01 11:29:17 +0100
commit755379506a06b34fee36f1227d1367ec3ec0a4da (patch)
tree4e0e4bded3078af7f50d421b4fbbd8c4e1faf803 /src/main/java/org/zwobble
parentcb76da7ffcf550c68103fe864defaa489e970eb0 (diff)
Document safety of generated Java casts in java-transient-0
Diffstat (limited to 'src/main/java/org/zwobble')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java12
1 files changed, 12 insertions, 0 deletions
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 78cbca4..1999cc8 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
@@ -225,6 +225,8 @@ public class JavaTransient0Generator implements Generator {
private JavaMethodDeclaration generateDecodeInt8Method() {
var statements = List.<JavaBlockStatement>of(
+ // InputStream.read() returns a byte of data as an int, so casting
+ // to a byte is safe.
new JavaReturn(new JavaCast(
JavaTypeRef.BYTE,
generateInputStreamRead()
@@ -267,6 +269,8 @@ public class JavaTransient0Generator implements Generator {
);
if (bits > 32) {
+ // byteToWrite should only represent a byte of data, so
+ // casting to int is safe.
byteToWrite = new JavaCast(JavaTypeRef.INT, byteToWrite);
}
@@ -334,6 +338,7 @@ public class JavaTransient0Generator implements Generator {
));
statements.addAll(generateEncode(
+ // The length of a string is an int, so casting to long is safe.
new JavaCast(JavaTypeRef.LONG, new JavaFieldAccess(
new JavaRef(bytesVariableName),
JavaIdentifier.of("length")
@@ -383,6 +388,10 @@ public class JavaTransient0Generator implements Generator {
Optional.of(new JavaMethodCall(
new JavaRef(INPUT_STREAM_NAME),
JavaIdentifier.of("readNBytes"),
+ // We've checked above that the length is less than
+ // Integer.MAX_VALUE, so casting to an int is safe.
+ // (This doesn't check for negatives, but readNBytes() fails for
+ // negative values anyway.)
List.of(new JavaCast(JavaTypeRef.INT, new JavaRef(bytesLengthVariableName)))
))
));
@@ -872,6 +881,7 @@ public class JavaTransient0Generator implements Generator {
var statements = new ArrayList<JavaBlockStatement>();
statements.addAll(generateEncode(
+ // The size of the list is an int, so casting to a long is safe.
new JavaCast(JavaTypeRef.LONG, new JavaMethodCall(
value,
JavaIdentifier.of("size"),
@@ -1069,6 +1079,8 @@ public class JavaTransient0Generator implements Generator {
JavaIdentifier.of("put"),
List.of(
this.taggedSharedValue(value, type),
+ // The size of the shared values map is an
+ // int, so casting to a long is safe.
new JavaCast(JavaTypeRef.LONG, new JavaMethodCall(
new JavaRef(SHARED_VALUES_NAME),
JavaIdentifier.of("size"),