Repository navigation
test(ast): diff the java and kotlin decoders against the golden capture - #391
Merged
Merged
Conversation
Peefy
force-pushed
the
test/ast-diff-java-kotlin
branch
from
October 6, 2026 07:34
ff05ad1 to
aaf8435
Compare
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>
Peefy
force-pushed
the
test/ast-diff-java-kotlin
branch
from
October 6, 2026 07:54
aaf8435 to
486fab3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
hack/ast_diff.rbruns each binding's real decoder overtestdata/ast/alignment.jsonand diffs the result against the golden. It exists to catch the one bug the static check inast-shape-test.yamlis blind to: a decoder that names every field correctly and then reads it one level of nesting wrong. That bug has shipped five times, and every occurrence producedundefined/None/""rather than raising, so every per-language suite stayed green.Twelve bindings are covered. Two more were listed in
bindings.rbas unrunnable, and both reasons were wrong — they said "no JRE on this machine", which is a fact about one laptop rather than about the binding.What this adds
java — decodes with the same Jackson classes every other JVM binding uses, so
hack/dump/java/Dump.javawalks them reflectively and reads each field's name off the@JsonPropertythe decoder was given. The binding's own tag comes from the@JsonSubTypestable on the base class, which covers theAs.PROPERTYbases (Stmt/Expr/Type) and theAs.EXTERNAL_PROPERTYones (LiteralTypeValue/NumberLitValue) without a special case each.kotlin — compiles those same
com.kcl.astsources, so the shared decoder was never the part in doubt. What is kotlin's own isAstWire.kt, 649 lines of hand-written shape restoration written to mirrordotnet/KclLib.AST/Wire.cs, andAstBuild.kt'sNew*constructors. 1370 lines with nothing checking them, and the round-trip is the only thing that can.Result
Neither binding differs from the golden, so
CLASS_TAGSgains the eleven wrapper classes java adds for a payload the wire has no name for (UnionType.UnionTypeValueand the rest, one 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.java reaches 1012/1012 golden objects with R4 ×72. kotlin reaches 1012/1012 with R4 ×61 and R12 ×321 — the same counts dotnet and zig produce, which is the evidence the two mirror files really are in sync.
Worth knowing: java has no
parseModuleJsonUtilexposes exactly one decoder,deserializeProgram, which wants the{"root":…,"pkgs":{…}}envelope thatParseProgramResult.ast_jsoncarries.ParseFileResult.ast_jsonis a bareModule, so a Java caller who parses one file and asks for the typed AST has to build anObjectMapperthemselves. Every other binding here has both entry points. Not fixed here — out of scope for a test-only change — but it is the reason the dumper readsModuledirectly.CI
Both legs run on the default ubuntu runner and build nothing: the dumpers compile the AST sources directly, so unlike
java-test.yamlandkotlin-test.yamlthere is no JNI shim and no Rust toolchain. Both scripts pinjavac --release 8to the language level the poms declare, so a Java 9+ API is a compile error in the harness rather than something that passes on a new JDK and fails on the zulu 8 the build matrix uses.Two things that would have failed on a cold runner, found and fixed while wiring the legs up: resolving the declared classpath through maven has to be online (
-ofails against an empty~/.m2, as a classpath error before the dumper has even run), andkotlin-compileris a plugin dependency, so a fresh local repository does not have it —hack/dump/kotlin.shnow fetches it itself, with a workflow step doing the same so the failure is attributable.Still uncovered
go,candcppremain without a dumper. go's reason is structural and accurate — it has no typed AST package at all. For c the reason holds too:KCL_DECLARE_NODEmakes every node avoid*, so a dumper would be a hand-written second traversal. The cpp reason is inaccurate —cpp/include/kcl_ast.hppis 1987 lines of its own typed AST with avirtual tag()on every node, not a wrapper over the C header — but a dumper there would still need a per-class member list, which is the maintainability objection wearing the wrong explanation. Probing c and cpp directly on the golden (body=63, comments=33, all 33 non-empty) found neither broken.🤖 Generated with Claude Code