From 6752ace624c60ca0ee4a251d1001766acad9b4cb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 30 Jun 2026 20:56:33 +0100 Subject: Support Rust type paths with generic args --- .../compiler/output/lang/rust/RustWriter.java | 10 +++++++++ .../output/lang/rust/ast/RustTypePath.java | 26 ++++++++++++++++++---- .../output/lang/rust/ast/RustTypePathSegment.java | 26 +++++++++++++++++----- 3 files changed, 52 insertions(+), 10 deletions(-) (limited to 'src/main/java') 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 9153d83..6cd67c6 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 @@ -110,6 +110,16 @@ public class RustWriter implements AutoCloseable { private void writeTypePathSegment(RustTypePathSegment segment) throws IOException { writePathIdentSegment(segment.pathIdentSegment()); + + if (segment.args().isPresent()) { + this.writer.write("<"); + writeWithSeparator( + segment.args().get(), + this::writeType, + () -> this.writer.write(", ") + ); + this.writer.write(">"); + } } private void writePathIdentSegment(RustPathIdentSegment pathIdentSegment) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java index c55b53d..9b44334 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java @@ -3,8 +3,10 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath imports +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Optional; // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath imports public record RustTypePath(java.util.List segments) implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustType { @@ -28,22 +30,38 @@ public record RustTypePath(java.util.List new RustTypePathSegment(new RustPathIdentSegmentIdentifier(new RustIdentifier(name)))) + .map(name -> new RustTypePathSegment( + new RustPathIdentSegmentIdentifier(new RustIdentifier(name)), + Optional.empty() + )) .toList(); return new RustTypePath(segments); } public static RustType of(RustIdentifier... names) { var segments = Arrays.stream(names) - .map(name -> new RustTypePathSegment(new RustPathIdentSegmentIdentifier(name))) + .map(name -> new RustTypePathSegment( + new RustPathIdentSegmentIdentifier(name), + Optional.empty() + )) .toList(); return new RustTypePath(segments); } + + public RustType withArgs(List args) { + var segments = new ArrayList<>(this.segments); + var lastSegment = segments.removeLast().withArgs(args); + segments.add(lastSegment); + return new RustTypePath(segments); + } // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePath body } diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java index cbd1c69..4473aa3 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java @@ -5,22 +5,29 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast; // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment imports // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment imports -public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment) { +import java.util.List; +import java.util.Optional; + +public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder arbitrary() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier.arbitrary().build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier.arbitrary().build(), java.util.Optional.empty()); } - public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment) { + public record Builder(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment, java.util.Optional> args) { public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment build() { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment(pathIdentSegment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment(pathIdentSegment, args); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder withPathIdentSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment pathIdentSegment) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(pathIdentSegment); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(pathIdentSegment, args); } public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder withPathIdentSegment(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment.Builder pathIdentSegment) { - return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(pathIdentSegment.build()); + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(pathIdentSegment.build(), args); + } + + public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder withArgs(java.util.Optional> args) { + return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder(pathIdentSegment, args); } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment.Builder body @@ -28,5 +35,12 @@ public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rus } // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment body + public RustTypePathSegment withArgs(List args) { + if (this.args.isPresent()) { + throw new IllegalArgumentException("Type path segment already has args"); + } + + return new RustTypePathSegment(this.pathIdentSegment, Optional.of(args)); + } // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustTypePathSegment body } -- cgit v1.2.3