summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-03 14:57:30 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-03 14:57:30 +0100
commit139dc93bb10daea87eba99e8f28d8a668c94e13b (patch)
tree95b3778099493b58381b21d3d2636b5b82320ce4 /src
parent79721bda16ed9a941a714f1cdb1db9b1cf40316c (diff)
Add self type path segment
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriter.java4
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfType.java24
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePath.java21
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustTypePathSegment.java4
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java9
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfTypeMatcher.java33
7 files changed, 85 insertions, 12 deletions
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 1d9244a..6db480e 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
@@ -348,6 +348,10 @@ public class RustWriter implements AutoCloseable {
case RustPathIdentSegmentIdentifier pathIdentSegmentIdentifier -> {
this.writeIdentifier(pathIdentSegmentIdentifier.name());
}
+
+ case RustPathIdentSegmentSelfType _ -> {
+ this.writer.write("Self");
+ }
}
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java
index f6686bf..bc25612 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegment.java
@@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.rust.ast;
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment imports
-public sealed interface RustPathIdentSegment permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentGlobal, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier {
+public sealed interface RustPathIdentSegment permits org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentCrate, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentGlobal, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentIdentifier, org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType {
public interface Builder {
public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfType.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfType.java
new file mode 100644
index 0000000..186fc42
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfType.java
@@ -0,0 +1,24 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.rust.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType imports
+
+public record RustPathIdentSegmentSelfType() implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType.Builder();
+ }
+
+ public record Builder() implements org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegment.Builder {
+ public org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType build() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType();
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType body
+}
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 8b0adf0..797f770 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
@@ -66,21 +66,20 @@ public record RustTypePath(java.util.List<org.zwobble.hobgoblin.compiler.output.
}
public static RustType crate(String... names) {
- var segments = Stream.concat(
- Stream.of(RustTypePathSegment.crate()),
- Arrays.stream(names)
- .map(name -> new RustTypePathSegment(
- new RustPathIdentSegmentIdentifier(new RustIdentifier(name)),
- Optional.empty()
- ))
- )
- .toList();
- return new RustTypePath(segments);
+ return qualified(RustTypePathSegment.crate(), names);
}
public static RustTypePath global(String... names) {
+ return qualified(RustTypePathSegment.global(), names);
+ }
+
+ public static RustTypePath selfType(String... names) {
+ return qualified(RustTypePathSegment.selfType(), names);
+ }
+
+ private static RustTypePath qualified(RustTypePathSegment qualifier, String... names) {
var segments = Stream.concat(
- Stream.of(RustTypePathSegment.global()),
+ Stream.of(qualifier),
Arrays.stream(names)
.map(name -> new RustTypePathSegment(
new RustPathIdentSegmentIdentifier(new RustIdentifier(name)),
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 cffef8d..6531925 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
@@ -50,6 +50,10 @@ public record RustTypePathSegment(org.zwobble.hobgoblin.compiler.output.lang.rus
return new RustTypePathSegment(new RustPathIdentSegmentGlobal(), Optional.empty());
}
+ public static RustTypePathSegment selfType() {
+ return new RustTypePathSegment(new RustPathIdentSegmentSelfType(), Optional.empty());
+ }
+
public RustTypePathSegment withArgs(List<RustType> args) {
if (this.args.isPresent()) {
throw new IllegalArgumentException("Type path segment already has args");
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 d139a3b..4c337d5 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
@@ -439,6 +439,15 @@ public class RustWriterTests {
assertThat(string, equalTo("::a::b"));
}
+ @Test
+ public void selfTypeSegmentIsWritten() throws IOException {
+ var rust = RustTypePath.selfType();
+
+ var string = write(writer -> writer.writeType(rust));
+
+ assertThat(string, equalTo("Self"));
+ }
+
// == Identifiers ==
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfTypeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfTypeMatcher.java
new file mode 100644
index 0000000..18ab20a
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/ast/RustPathIdentSegmentSelfTypeMatcher.java
@@ -0,0 +1,33 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.rust.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher imports
+
+public class RustPathIdentSegmentSelfTypeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher isRustPathIdentSegmentSelfType() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType>> submatchers;
+
+ private RustPathIdentSegmentSelfTypeMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType>> submatchers) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfType.class, this.submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.rust.ast.RustPathIdentSegmentSelfTypeMatcher body
+}