From c82fcaa28e77fce24db5956862ca6355c1a8397d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 17 Jul 2026 18:39:02 +0100 Subject: Support integer literal with explicit type in Rust AST --- hobgoblin/src/output/lang/rust/ast.hob | 1 + .../rusttransient0/RustTransient0Generator.java | 4 ++-- .../compiler/output/lang/rust/RustWriter.java | 3 +++ .../output/lang/rust/ast/RustIntegerLiteral.java | 22 +++++++++++++++++----- .../compiler/output/lang/rust/RustWriterTests.java | 14 +++++++++++++- .../lang/rust/ast/RustIntegerLiteralMatcher.java | 6 ++++++ 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/hobgoblin/src/output/lang/rust/ast.hob b/hobgoblin/src/output/lang/rust/ast.hob index ac903e1..7a405d4 100644 --- a/hobgoblin/src/output/lang/rust/ast.hob +++ b/hobgoblin/src/output/lang/rust/ast.hob @@ -168,6 +168,7 @@ struct RustIfExpression { struct RustIntegerLiteral { field value: Int64; + field type: Option[RustIdentifier]; } struct RustPrefixExpression { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java index 8fc9b61..d96130b 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/rusttransient0/RustTransient0Generator.java @@ -419,8 +419,8 @@ public class RustTransient0Generator implements Generator { bytes, true, new RustArrayRepeatExpression( - new RustIntegerLiteral(0), - new RustIntegerLiteral(length) + new RustIntegerLiteral(0, Optional.empty()), + new RustIntegerLiteral(length, Optional.empty()) ) ), new RustExpressionStatement(new RustTryPropagationExpression(new RustCallExpression( diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java index 06c35c6..0f026a2 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java @@ -426,6 +426,9 @@ public class RustWriter implements AutoCloseable { private void writeIntegerLiteral(RustIntegerLiteral integerLiteral) throws IOException { this.writer.write(Long.toString(integerLiteral.value())); + if (integerLiteral.type().isPresent()) { + this.writeIdentifier(integerLiteral.type().get()); + } } private void writePrefixExpression(RustPrefixExpression prefixExpression) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java index 8034c2b..eb297b0 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteral.java @@ -5,18 +5,30 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral imports -public record RustIntegerLiteral(long value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { +public record RustIntegerLiteral(long value, java.util.Optional type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(0); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(0, java.util.Optional.empty()); } - public record Builder(long value) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { + public record Builder(long value, java.util.Optional type) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustExpression.Builder { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral build() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral(value); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral(value, type); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder withValue(long value) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(value); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(value, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder withType(java.util.Optional type) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(value, type); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier type) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(value, java.util.Optional.of(type)); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder withType(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIdentifier.Builder type) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder(value, java.util.Optional.of(type.build())); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral.Builder body diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index eebc834..67f3f30 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -560,7 +560,7 @@ public class RustWriterTests { // === Integer literals === @Test - public void integerLiteral() throws IOException { + public void integerLiteralWithoutExplicitType() throws IOException { var rust = RustIntegerLiteral.arbitrary().withValue(123).build(); var string = write(writer -> writer.writeExpression(rust)); @@ -568,6 +568,18 @@ public class RustWriterTests { assertThat(string, equalTo("123")); } + @Test + public void integerLiteralWithExplicitType() throws IOException { + var rust = RustIntegerLiteral.arbitrary() + .withValue(123) + .withType(RustIdentifier.of("i32")) + .build(); + + var string = write(writer -> writer.writeExpression(rust)); + + assertThat(string, equalTo("123i32")); + } + // === Prefix expressions === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteralMatcher.java index f27b21f..4e31a41 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteralMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustIntegerLiteralMatcher.java @@ -34,6 +34,12 @@ public class RustIntegerLiteralMatcher implements org.zwobble.precisely.Matcher< return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteralMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteralMatcher withType(org.zwobble.precisely.Matcher> type) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteral::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteralMatcher(submatchers); + } + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteralMatcher body // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustIntegerLiteralMatcher body } -- cgit v1.2.3