Skip to content

Commit 7a99b18

Browse files
Peefyclaude
andauthored
test(ast): diff the java and kotlin decoders against the golden capture
Both were listed in `bindings.rb` as unrunnable, and both reasons were wrong. They said "no JRE on this machine", which is an observation about one laptop rather than about the binding — a JDK is all they need, and `ast_diff.rb` has no way to run either decoder until now. java decodes the capture with the same Jackson classes every other JVM binding uses, so the dumper walks them reflectively and reads each field's name off the `@JsonProperty` the decoder was given, rather than from a list written out here. Its own tag comes from the `@JsonSubTypes` table on the base class, which covers both the `As.PROPERTY` bases (`Stmt`/`Expr`/`Type`) and the `As.EXTERNAL_PROPERTY` ones (`LiteralTypeValue`/`NumberLitValue`) without a special case each. kotlin compiles those same `com.kcl.ast` sources, so the decoder it shares with java was never the part in doubt. What is kotlin's own is `AstWire.kt` — 649 lines of hand-written shape restoration, written to mirror `dotnet/KclLib.AST/Wire.cs`, whose docstring claims a decode/encode cycle is a fixed point — and `AstBuild.kt`'s `New*` constructors. 1370 lines with nothing checking them, and the round-trip is the only thing that can. It passes, and its reduction counts (R4 x61, R12 x321) come out identical to dotnet's and zig's, which is the evidence the two mirror files really are in sync. Neither binding introduces a real difference against the golden, so CLASS_TAGS gains the eleven wrapper classes java adds for a payload the wire has no name for (`UnionType.UnionTypeValue` and the rest, a level deeper again for the literal arms). kotlin and dotnet collapse the same shapes on the write side, which is why they never needed naming; java has no writer, so the reflective walk meets the wrapper where it stands. Worth knowing from the java dumper, recorded here rather than only in the dumper's header: the binding has no `parseModule`. `JsonUtil` exposes exactly one decoder, `deserializeProgram`, which wants the `{"root":…,"pkgs":{…}}` envelope that `ParseProgramResult.ast_json` carries — but `ParseFileResult.ast_json` is a bare `Module`, so a caller who parses one file and asks for the typed AST has to build an ObjectMapper themselves. Every other binding here has both entry points. The CI legs build nothing: unlike java-test and kotlin-test, the dumpers compile the AST sources directly, so no JNI shim and no Rust toolchain. Both scripts pin `javac --release 8` to the language level the poms declare, so a Java 9+ API is a compile error here rather than something that passes on a new JDK and fails on the zulu 8 the build matrix uses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Peefy <xpf6677@163.com>
1 parent 014c4e2 commit 7a99b18

7 files changed

Lines changed: 771 additions & 3 deletions

File tree

‎.github/workflows/ast-diff-test.yaml‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ on:
5151
- "c/lib/kcl_lib_ast.c"
5252
- "cpp/include/kcl_ast.hpp"
5353
- "cpp/include/kcl_ast_json.hpp"
54+
# Java and Kotlin share one decoder (`kotlin` compiles the same
55+
# `com.kcl.ast` sources java does) but each has its own on top of it, so
56+
# both roots are listed. `.java` under the kotlin source root is that
57+
# shared decoder; the `.kt` files next to it are the writer and the
58+
# `New*` builders, which are kotlin's own and are the part nothing else
59+
# checks.
60+
- "java/src/main/java/com/kcl/ast/**"
61+
- "kotlin/src/main/java/com/kcl/ast/**"
62+
- "kotlin/src/main/kotlin/com/kcl/ast/**"
5463
- ".github/workflows/ast-diff-test.yaml"
5564
workflow_dispatch:
5665

@@ -86,6 +95,21 @@ jobs:
8695
# layers around them.
8796
- binding: c
8897
- binding: cpp
98+
# Java and Kotlin share a decoder but not a dumper: java is walked by
99+
# reflection over `com.kcl.ast`'s declared fields, and kotlin goes
100+
# through `AstWire.kt`, the writer it owns. Neither needs the JNI
101+
# shim, so unlike java-test.yaml and kotlin-test.yaml these legs build
102+
# nothing — `hack/dump/{java,kotlin}.sh` compile the AST sources
103+
# directly.
104+
#
105+
# JDK 17 rather than the zulu 8 the build matrices use: the dumper
106+
# compiles with `--release 8`, so the language level is pinned by the
107+
# scripts and not by this choice, and 17 is the version the pinned
108+
# kotlin-compiler 1.9.25 is known to run under. `cache: 'maven'` is
109+
# the difference between this leg taking two seconds and taking a
110+
# minute — the dumpers resolve their classpath through maven.
111+
- binding: java
112+
- binding: kotlin
89113
# The only binding that cannot share the default runner. The Swiftly
90114
# installer behind swift-actions/setup-swift@v3 cannot provision a
91115
# Linux runner — the same limitation that keeps swift-test.yaml off
@@ -243,5 +267,26 @@ jobs:
243267
working-directory: "lib/dotnet"
244268
run: cargo build --release
245269

270+
# --- java / kotlin ----------------------------------------------------
271+
# One JDK for both. The dumper scripts do not run maven's `compile` phase,
272+
# so nothing here builds the JNI shim — the AST is plain Java over Jackson
273+
# and, for kotlin, plain Kotlin over the same classes.
274+
- name: Setup JDK
275+
if: matrix.binding == 'java' || matrix.binding == 'kotlin'
276+
uses: actions/setup-java@v6.0.1
277+
with:
278+
distribution: 'temurin'
279+
java-version: '17'
280+
cache: 'maven'
281+
# The kotlin dumper needs `kotlin-compiler` 1.9.25, which is a *plugin*
282+
# dependency and so is absent from a fresh local maven repository. The
283+
# script falls back to `dependency:go-offline` on its own, but doing it
284+
# here turns a silent minute-long resolve inside the harness into a step
285+
# whose failure is attributable.
286+
- name: Resolve the kotlin compiler
287+
if: matrix.binding == 'kotlin'
288+
working-directory: "lib/kotlin"
289+
run: mvn -q dependency:go-offline
290+
246291
- name: Diff this binding against the golden capture
247292
run: ruby hack/ast_diff.rb ${{ matrix.binding }}

‎hack/ast_diff/bindings.rb‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,64 @@ def bindings
331331
"catches the two drifting. Position is nested under `Node::pos`."
332332
),
333333

334+
# ---------------------------------------------------------------- java
335+
Binding.new(
336+
name: "java",
337+
mode: :reflect,
338+
probe: lambda {
339+
# A JDK is needed both to build the dumper and to run it, and
340+
# `javac` alone is not enough: on a machine with the stub `javac`
341+
# but no runtime, `javac -version` succeeds and the dumper then
342+
# fails to start. Check the runtime, not the compiler.
343+
javac = which("javac")
344+
java = which("java")
345+
return "no `javac` on PATH" unless javac
346+
return "no `java` on PATH" unless java
347+
348+
out, _err, status = Open3.capture3(java, "-version")
349+
return "probe failed: `java -version` exited #{status.exitstatus}\n#{out}" \
350+
unless status.success?
351+
352+
nil
353+
},
354+
argv: ->(golden, out) { ["bash", "hack/dump/java.sh", golden, out] },
355+
extra_ok: {},
356+
note: "Jackson decoder over `com.kcl.ast.*`, walked by reflection; " \
357+
"position is flat on `Node`. The tag is read out of the " \
358+
"`@JsonSubTypes` table on the base class, so both the " \
359+
"`As.PROPERTY` and `As.EXTERNAL_PROPERTY` spellings are covered."
360+
),
361+
362+
# -------------------------------------------------------------- kotlin
363+
Binding.new(
364+
name: "kotlin",
365+
mode: :wire,
366+
probe: lambda {
367+
java = which("java")
368+
return "no `java` on PATH" unless java
369+
370+
out, _err, status = Open3.capture3(java, "-version")
371+
return "probe failed: `java -version` exited #{status.exitstatus}\n#{out}" \
372+
unless status.success?
373+
374+
# Deliberately *not* checking for the kotlin compiler jar. The probe
375+
# answers "is the toolchain fundamentally absent?", and a missing
376+
# `java` is; the compiler is a plugin dependency that
377+
# `hack/dump/kotlin.sh` fetches itself when the local maven
378+
# repository is cold. Probing for it here would report "no dumper
379+
# written" for a binding that would have run fine — which on a fresh
380+
# CI runner is exactly what would have happened.
381+
nil
382+
},
383+
argv: ->(golden, out) { ["bash", "hack/dump/kotlin.sh", golden, out] },
384+
extra_ok: {},
385+
note: "Has a real serializer — `AstWire.kt`, the mirror of " \
386+
"`dotnet/KclLib.AST/Wire.cs` — so this is a wire round-trip. The " \
387+
"decoder is the same `com.kcl.ast` classes java uses, compiled " \
388+
"from the kotlin source root; the writer and the `New*` builders " \
389+
"in `AstBuild.kt` are kotlin's own and are what this checks."
390+
),
391+
334392
# ---------------------------------------------------------------- zig
335393
Binding.new(
336394
name: "zig",
@@ -371,9 +429,6 @@ def bindings
371429
# than quietly short. `reason` must name the blocker.
372430
def self.unrunnable
373431
{
374-
"java" => "no JRE on this machine (`java -version` reports no runtime), and the " \
375-
"loader is Jackson over com.kcl.ast.* — it needs a JVM to run at all",
376-
"kotlin" => "no JRE on this machine; same Jackson loader, compiled by Maven",
377432
"go" => "the Go binding has no typed AST package at all — go/api/client.go exposes " \
378433
"ast_json as an opaque string, so there is no decoder to run the golden through"
379434
}.freeze

‎hack/ast_diff/canonical.rb‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ module Canonical
323323
# models it as a named struct (`Pos.swift:528`); the others keep the
324324
# payload raw.
325325
"intliteraltype" => nil,
326+
# The other three `LiteralType` arms, same shape and same reason. These
327+
# two entries are about the *arm*, which carries the `Int` / `Str` / …
328+
# tag on the wire as an adjacent key; the payload it wraps is
329+
# registered separately above, as `<arm>value`.
330+
"boolliteraltype" => nil,
331+
"floatliteraltype" => nil,
332+
"strliteraltype" => nil,
326333
"exprcontext" => nil,
327334
"op" => nil,
328335
"unaryop" => nil,
@@ -332,6 +339,36 @@ module Canonical
332339
"configentryoperation" => nil,
333340
"quantoperation" => nil,
334341
"numberbinarysuffix" => nil,
342+
# -- Java's wrapper objects for a payload the wire has no name for -----
343+
# `Type` is `#[serde(tag = "type", content = "value")]` — adjacently
344+
# tagged — so the wire object under `value` has no class of its own. The
345+
# java binding gives it one: a plain holder per variant
346+
# (`UnionType.UnionTypeValue`, `ListType.ListTypeValue`, …) with the
347+
# payload's fields straight on it. Same for the literal/number payload
348+
# arms, which java nests one level deeper again through
349+
# `IntLiteralType.IntLiteralTypeValue` because the arm's own `value`
350+
# field is a scalar and needs a box.
351+
#
352+
# These are untagged, so `nil`: the tag at the level above (`Union`,
353+
# `Int`, …) is the one on the wire, and the wrapper is a Java
354+
# implementation detail the comparator walks *through*. Registering them
355+
# is what lets the comparison be about the payload's fields, which is
356+
# the thing worth checking — the wrapper adds no information, so
357+
# claiming a tag for it would only invent a difference.
358+
#
359+
# The counterpart is kotlin's `AstWire.kt`, which is the mirror of
360+
# `dotnet/KclLib.AST/Wire.cs`: those bindings have a writer and collapse
361+
# the wrapper on the way out, which is why the same class tree never
362+
# needed naming there. java has no writer, so the `:reflect` walk meets
363+
# the wrapper where it stands.
364+
"uniontypevalue" => nil,
365+
"listtypevalue" => nil,
366+
"dicttypevalue" => nil,
367+
"namedtypevalue" => nil,
368+
"functiontypevalue" => nil,
369+
"intliteraltypevalue" => nil,
370+
"intnumberlitvalue" => nil,
371+
"floatnumberlitvalue" => nil,
335372
# The sealed base classes and the marker enums, which only ever appear
336373
# as a wrapper or a scalar and never as a payload object of their own.
337374
"kclstmt" => nil,

‎hack/dump/java.sh‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
# Build and run the Java binding's AST dumper.
3+
#
4+
# hack/dump/java.sh <golden.json> <out.json>
5+
#
6+
# `mvn` is not used. The `compile` phase in java/pom.xml is bound to
7+
# `python3 scripts/build.py`, which builds the Rust JNI shim, and the AST
8+
# dumper needs none of that: `com.kcl.ast` is plain Java over Jackson, and the
9+
# whole point of the exercise is to check the decoder, not the transport. So
10+
# this compiles the AST sources plus the dumper against the Jackson jars
11+
# directly.
12+
#
13+
# It compiles the *sources* rather than reusing `target/classes`, for the same
14+
# reason `hack/dump/swift.sh` compiles the Swift sources and
15+
# `hack/dump/wasm.sh` compiles the TypeScript: a build artifact may be older
16+
# than the code, and a harness that silently checked yesterday's decoder would
17+
# report agreement for a decoder that no longer exists.
18+
#
19+
# The classpath is resolved with `mvn dependency:build-classpath` rather than
20+
# hand-listed, so the dumper cannot drift onto a different Jackson than the one
21+
# the binding's pom declares.
22+
set -euo pipefail
23+
24+
if [ "$#" -ne 2 ]; then
25+
echo "usage: hack/dump/java.sh <golden.json> <out.json>" >&2
26+
exit 2
27+
fi
28+
29+
golden="$1"
30+
out="$2"
31+
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
32+
cd "$root"
33+
34+
# `java` is a stub on a machine with no JRE, so ask for the compiler by name
35+
# and let the shell report it missing rather than running a broken launcher.
36+
javac="$(command -v javac || true)"
37+
java_bin="$(command -v java || true)"
38+
if [ -z "$javac" ] || [ -z "$java_bin" ]; then
39+
echo "no JDK on PATH (both javac and java are needed: javac to build the dumper, java to run it)" >&2
40+
exit 3
41+
fi
42+
if ! "$java_bin" -version >/dev/null 2>&1; then
43+
echo "\`java -version\` fails: there is a javac on PATH but no runtime behind it" >&2
44+
"$java_bin" -version >&2 || true
45+
exit 3
46+
fi
47+
48+
build="$(mktemp -d "${TMPDIR:-/tmp}/kcl-ast-dump-java.XXXXXX")"
49+
trap 'rm -rf "$build"' EXIT
50+
51+
# Online, deliberately. `dependency:build-classpath` is the only way to learn
52+
# the Jackson version the pom actually declares without duplicating it here, and
53+
# a cold CI runner has an empty local maven repository — asking maven to resolve
54+
# that classpath offline would fail before the dumper ever ran, and it would
55+
# fail as a *classpath* error rather than as a decoder error. The CI job sets
56+
# `cache: 'maven'`, so the warm path is the normal one and this is the cold
57+
# fallback.
58+
mvn -q -f java/pom.xml \
59+
dependency:build-classpath -Dmdep.outputFile="$build/cp.txt" >/dev/null
60+
61+
# `com.kcl.ast`, the `com.kcl.loader.*` types `JsonUtil` names, and
62+
# `JsonUtil` itself — the binding's real entry point for turning `ast_json`
63+
# into a tree, which is the code path a Java caller actually goes through.
64+
#
65+
# `com.kcl.util.SematicUtil` is deliberately left out: it is symbol/scope
66+
# resolution over `LoadPackageResult`, which drags in the generated
67+
# `com.kcl.api.Spec` (six figures of protoc output) and has nothing to do with
68+
# decoding an AST. The dumper needs no part of it, and compiling it would put
69+
# the generated protobuf on the harness's critical path for no gain.
70+
find java/src/main/java/com/kcl/ast java/src/main/java/com/kcl/loader \
71+
hack/dump/java -name '*.java' > "$build/sources.txt"
72+
echo java/src/main/java/com/kcl/util/JsonUtil.java >> "$build/sources.txt"
73+
74+
# `--release 8` is the language level java/pom.xml declares, so a Java 9+ API
75+
# in the dumper or in `com.kcl.ast` is a compile error here rather than
76+
# something that passes on a new JDK and fails on the zulu 8 the build matrix
77+
# uses. It is also a slightly stricter check than the default: compiling at the
78+
# running JDK's level would happily accept `List.of` or `var`.
79+
javac -nowarn --release 8 -encoding UTF-8 \
80+
-cp "$(cat "$build/cp.txt")" \
81+
-d "$build/classes" \
82+
@"$build/sources.txt"
83+
84+
"$java_bin" -cp "$build/classes:$(cat "$build/cp.txt")" astdump.Dump "$golden" "$out"

0 commit comments

Comments
 (0)