Skip to content

test(ast): diff the c and c++ decoders against the golden capture - #392

Merged
Peefy merged 1 commit into
mainfrom
test/ast-diff-c-cpp
Oct 6, 2026
Merged

Peefy merged 1 commit into
mainfrom
test/ast-diff-c-cpp

Conversation

@Peefy

@Peefy Peefy commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds c and cpp to hack/ast_diff.rb, which runs each binding's real decoder over testdata/ast/alignment.json and diffs the result against the golden. Both were listed in AstDiff.unrunnable; both entries are now gone and the map is down to the three that remain (go, and — on this branch — nothing else).
  • New dumpers: hack/dump/c/Dump.c + hack/dump/c.sh, and hack/dump/cpp/Dump.cpp + hack/dump/cpp.sh. Both are :reflect.
  • New CI legs in .github/workflows/ast-diff-test.yaml, plus the decoder paths they watch.

Why the old reasons were wrong

Neither was accurate about the repository. c 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" — but c/lib/kcl_lib_ast.c is a real decoder with a real traversal, and reading its output is not that. cpp said the C++ AST was "the C header's union tree under RAII wrappers" — it is not: cpp/include/kcl_ast.hpp is 1987 lines of its own typed AST with its own inline decoder and a virtual 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.rb 11/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:

R7 R8 R15
c ×26 ×65 ×1
cpp ×23 ×58 ×1

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 3 MemberOrIndex elements 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:

  • @cls is the C name with the kcl_ prefix and _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 C names after the union field (subscriptexpr, compareexpr).
  • LiteralType / NumberLitValue arms are an enum and a value field inside a union, not types, so they carry @tag and nothing else. LiteralType::Int spreads its payload's fields as siblings of value rather than nesting them one level deeper as the struct groups them — R15.
  • MemberOrIndex keeps its own member / index field names instead of being re-tagged into the wire's value. 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.

id is never emitted: C does read it off the wire, but ast.rs gates it behind SHOULD_SERIALIZE_ID, which is off, so every id decodes 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/gcc and c++/g++/clang++) instead of asking for cc and c++ 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 vacuous
  • ruby hack/test_ast_diff.rb — 11 exercised, 0 failed
  • Negative control: swapping two quant_op names in the c dumper flips it to 1 disagreed naming module.body[52].node.value.node.op: expected "All", got "Any" — the check is not passing vacuously
  • ast-diff-test.yaml c and cpp legs on the runner

Neither 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 — 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.

🤖 Generated with Claude Code

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
Peefy force-pushed the test/ast-diff-c-cpp branch from 01137b8 to 838149a Compare October 6, 2026 07:34
@Peefy
Peefy merged commit 014c4e2 into main Oct 6, 2026
15 of 16 checks passed
@Peefy
Peefy deleted the test/ast-diff-c-cpp branch October 6, 2026 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant