Skip to content

Make the tree-sitter adapters' language overrides actually run - #4

Merged
ethanzhoucool merged 3 commits into
mainfrom
claude/fix-treesitter-overrides
Nov 3, 2026
Merged

ethanzhoucool merged 3 commits into
mainfrom
claude/fix-treesitter-overrides

Conversation

@ethanzhoucool

Copy link
Copy Markdown
Owner

Summary

Two bugs in the shared tree-sitter walk (adapters/treesitter.py) meant that per-language code in the Java, C++ and JavaScript adapters silently never ran.

1. Language-specific meta builders were dead code. _COMMON_META stored the base-class functions (TreeSitterAdapter._meta_call), and meta_for called them directly. So the overrides were never used:

  • JavaAdapter._meta_call. Java puts the callee in name, not function, so every Java call had callee == "" and receiver == "". Visible effects in the bundled Java sample:
    • The walkthrough said Calls `the function` on `the object` with 1 argument. for System.out.println(...).
    • Java KNOWN_COSTS (e.g. Collections.sort in a loop) never applied.
    • The naive fib in the Java sample came out as O(n), "takes part in a recursive cycle through another function". The identical Python and C++ samples are O(2^n) with a high-severity finding.
  • CppAdapter._meta_member / _meta_index. C++ calls the object argument. For p->x the IR had object: "" and no via_pointer. For v[i] it had container: "" and key: "v" (the key was the container's name).

2. Definitions repeated their own name as a child. recurse_into is meant to drop the name field ("emitting it again as a NAME child buries every definition under a duplicate of itself"). It compared with c is not name_field, but py-tree-sitter returns a fresh wrapper object on every access, so identity never matches. (I checked: c is nf is False and c == nf is True for the same node.) The symbol table reads that NAME child as a use. As a result, no JavaScript variable could ever be reported as "assigned but never read": const unused = 2 was always counted as used by its own declaration. The bundled JS sample's slugify is defined and never called, and is now correctly reported. C++ functions had the same duplicate through their declarator chain.

Changes

  • _COMMON_META now maps Kind to the builder's method name, and meta_for resolves it on self, so a subclass override replaces the shared builder. Existing meta_<kind> hooks work as before.
  • recurse_into (base and the JS parameter filter) compares with !=.
  • CppAdapter.recurse_into descends only into a function's parameter list instead of the whole declarator, so the name at the bottom of the chain isn't emitted again.

Diff of the four bundled web samples (web/static/samples.js) at all three levels:

  • Java: call lines now name the callee and receiver (Calls `println` on `System.out` , `cache.clear()`), and fib is O(2^n) with the exponential finding.
  • JS: slugify is reported as unused.
  • Halstead and MI move slightly, because definitions no longer count their own name as an operand.
  • Python and SQL: no change.

Testing

  • pytest on Python 3.13, 3.12 and 3.11: 160 passed, 4 skipped. ruff check explain_my_code tests is clean. mypy reports the same 26 errors as before.
  • New tests. All of them fail on main, except the cases for languages that were already correct:
    • test_calls_name_their_callee, across all tree-sitter languages; failed for Java;
    • test_definitions_do_not_repeat_their_own_name_as_a_child; failed for JS, Java and C++;
    • test_java_calls_keep_their_receiver;
    • test_cpp_member_and_index_read_cpp_field_names;
    • test_unused_local_is_reported_in_javascript;
    • test_naive_recursion_is_exponential_in_java_too.

Notes

This is independent of the cost-model PR (#3), and the two touch different files. #3 deliberately left Java out of its tests because of the empty callee this PR fixes.


Generated by Claude Code

claude and others added 3 commits November 2, 2026 06:43
Two bugs in the shared tree-sitter walk meant per-language code never ran:

- `_COMMON_META` bound the base-class builder functions, so
  `JavaAdapter._meta_call` and `CppAdapter._meta_member`/`_meta_index`
  were dead code. Every Java call had an empty callee and receiver, so
  the UI said "Calls `the function` on `the object`", known costs never
  applied, and naive Java `fib` was not flagged exponential the way the
  Python and C++ ones are. C++ member and index access read the wrong
  fields (index key was the container name).
- `recurse_into` dropped the `name` child with `c is not name_field`,
  but py-tree-sitter returns a new wrapper on every access, so the
  check never matched. Each definition got a NAME child of its own name,
  which counted as a use: no JavaScript variable could ever be reported
  as unused. C++ functions had the same duplicate via their declarator.

The builder table now holds method names resolved on `self`, node
comparison uses `!=`, and C++ callables descend into their parameter
list rather than the whole declarator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
They sat at the same spot as the Python lowering tests in another open
PR; at the end of the file the two merge cleanly in either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UigC7feaY8NkAy9oay3GDW
@ethanzhoucool
ethanzhoucool marked this pull request as ready for review November 3, 2026 17:14
@ethanzhoucool
ethanzhoucool merged commit e38f5dd into main Nov 3, 2026
6 checks passed
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.

2 participants