Repository navigation
test(ast): diff the c and c++ decoders against the golden capture - #392
Merged
Merged
Conversation
Both were listed in `bindings.rb` as unrunnable, and both reasons were
wrong — as descriptions of the bindings, not just of this machine. The c
one said a dumper "would have to re-encode the same traversal as the
decoder, which is a second decoder to get wrong rather than a check", and
the cpp one said the C++ AST was "the C header's union tree under RAII
wrappers". Neither is true of the repository as it stands. `cpp` has its
own 1987-line typed AST in `cpp/include/kcl_ast.hpp` with its own inline
decoder and a `virtual const char* tag()` per variant, sharing nothing
with the C header but the language. Neither has a serializer, so both are
`:reflect`, and neither has runtime reflection, so both dumpers are a
second hand-written statement of what the header declares — which is the
same bargain the C++ `cmake` build already makes and the diff is what
catches the two drifting.
The reduction counts say the two models do land on the wire. c and cpp
differ only where the C decoder's grouping is genuinely deeper than the
Rust one: c fires R8 7 more times than cpp, all of it `Expr::Identifier`,
`Expr::Target` and `Expr::Keyword`, whose newtype payloads the C struct
holds in a named field of its own where the C++ variant holds them
directly. c fires R7 3 more times, once per `MemberOrIndex` in the
golden, because the C decoder unpacks that union into a `kind` plus a
`member` or an `index` field and the wire's `value` key has no counterpart
in it.
Three naming decisions in the c dumper are worth stating here rather than
only in its header, because each is a place where the dump could have been
made to agree by renaming something the binding owns:
* `@cls` is the C name with the `kcl_` prefix and the `_t` suffix off.
That decoration is a convention, not part of the name, and stripping
it is what keeps 24 C spellings out of `CLASS_TAGS` — a table whose
entries only undo a prefix is not an independent claim about
anything. CLASS_TAGS gains two entries instead, for the arms the C
decoder names after the union field rather than the variant.
* `LiteralType`'s four arms and `NumberLitValue`'s two are an enum and
a `value` field inside a union, not types, so they carry `@tag` and
nothing else. For `LiteralType::Int` the payload's fields are spread
as siblings of `value` rather than nested one level deeper as the
struct groups them, which is R15.
* `MemberOrIndex` keeps its own `member` / `index` field names instead
of being re-tagged into the wire's `value`. R7 (ii) is the rule that
reconciles a payload the binding kept in a single wrapper field of
its own, and firing it is the evidence the field really is the
payload's.
Neither binding introduces a real difference against the golden. Both
reach 1012/1012 objects and 3657 leaves, and both probes now resolve the
same three compiler names the scripts fall back through, so a runner
that ships only clang or only g++ is not reported as a missing toolchain.
The CI legs need no toolchain step: both decoders are self-contained, so
`hack/dump/{c,cpp}.sh` compile their sources against a stock cc / c++
already on the runner image — not `cargo build`, not `libkcl`, and not
either binding's Makefile or cmake, which build the protobuf and JNI
layers around the parts that were never in question.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Peefy <xpf6677@163.com>
Peefy
force-pushed
the
test/ast-diff-c-cpp
branch
from
October 6, 2026 07:34
01137b8 to
838149a
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.
Summary
candcpptohack/ast_diff.rb, which runs each binding's real decoder overtestdata/ast/alignment.jsonand diffs the result against the golden. Both were listed inAstDiff.unrunnable; both entries are now gone and the map is down to the three that remain (go, and — on this branch — nothing else).hack/dump/c/Dump.c+hack/dump/c.sh, andhack/dump/cpp/Dump.cpp+hack/dump/cpp.sh. Both are:reflect..github/workflows/ast-diff-test.yaml, plus the decoder paths they watch.Why the old reasons were wrong
Neither was accurate about the repository.
csaid a dumper "would have to re-encode the same traversal as the decoder, which is a second decoder to get wrong rather than a check" — butc/lib/kcl_lib_ast.cis a real decoder with a real traversal, and reading its output is not that.cppsaid the C++ AST was "the C header's union tree under RAII wrappers" — it is not:cpp/include/kcl_ast.hppis 1987 lines of its own typed AST with its own inline decoder and avirtual const char* tag()per variant, sharing nothing with the C header but the language.What the two dumpers agree with
Both reach 1012/1012 golden objects and 3657 leaves. Full suite on this branch: 12 agreed, 0 disagreed, 0 failed to run, 0 skipped, 0 vacuous;
hack/test_ast_diff.rb11/11.The reduction counts show where the two models genuinely differ, and it is only where the C decoder's grouping is deeper than the Rust one:
c's extra 7 R8 firings are
Expr::Identifier/Target/Keyword, whose newtype payloads the C struct holds in a named field of its own where the C++ variant holds them directly. c's extra 3 R7 firings are exactly the 3MemberOrIndexelements in the golden.Three places the dump could have been made to agree by renaming something the binding owns
Each was resolved the other way, and each is explained in
hack/dump/c/Dump.c's header:@clsis the C name with thekcl_prefix and_tsuffix off. That decoration is a convention, not part of the name, and stripping it is what keeps 24 C spellings out ofCLASS_TAGS— a table whose entries only undo a prefix is not an independent claim about anything.CLASS_TAGSgains two entries instead, for the arms C names after the union field (subscriptexpr,compareexpr).LiteralType/NumberLitValuearms are an enum and avaluefield inside a union, not types, so they carry@tagand nothing else.LiteralType::Intspreads its payload's fields as siblings ofvaluerather than nesting them one level deeper as the struct groups them — R15.MemberOrIndexkeeps its ownmember/indexfield names instead of being re-tagged into the wire'svalue. R7 (ii) reconciles a payload the binding kept in a single wrapper field of its own, and its firing is the evidence the field really is the payload's.idis never emitted: C does read it off the wire, butast.rsgates it behindSHOULD_SERIALIZE_ID, which is off, so everyiddecodes to NULL and the wire has no key for it.Both probes fixed
They now resolve the same three compiler names the scripts fall back through (
cc/clang/gccandc++/g++/clang++) instead of asking forccandc++directly, so a runner that ships only clang or only g++ is not reported as a missing toolchain.Test plan
ruby hack/ast_diff.rb— 12 agreed, 0 disagreed, 0 failed, 0 skipped, 0 vacuousruby hack/test_ast_diff.rb— 11 exercised, 0 failedquant_opnames in the c dumper flips it to1 disagreednamingmodule.body[52].node.value.node.op: expected "All", got "Any"— the check is not passing vacuouslyast-diff-test.yamlc and cpp legs on the runnerNeither leg needs a toolchain step. Both decoders are self-contained, so the scripts compile their sources against a stock
cc/c++already on the runner image — notcargo build, notlibkcl, and not either binding's Makefile or cmake, which build the protobuf and JNI layers around the parts that were never in question.🤖 Generated with Claude Code