Skip to content

fix(swift): index protocol method requirements as Method nodes - #1763

Merged
DeusData merged 1 commit into
mainfrom
feat/swift-protocol-methods
Aug 20, 2026
Merged

fix(swift): index protocol method requirements as Method nodes#1763
DeusData merged 1 commit into
mainfrom
feat/swift-protocol-methods

Conversation

@DeusData

Copy link
Copy Markdown
Owner

fix(swift): index protocol method requirements as Method nodes

Advances #43. Distilled from #613 by @xbsjason, credited via Co-authored-by.

The gap

A Swift protocol requirement — a bodyless func generate() -> String inside a protocol — produced no node at all. Swift codebases are heavily protocol-driven, so the requirement is frequently the declaration a reader actually wants, and "who declares generate()" simply had no answer.

What delivers it

Three edits, and I verified each is load-bearing by removing it and watching the test fail:

  1. protocol_body joins the class-body types in find_class_body — without it, a protocol's members are never walked.
  2. protocol_function_declaration joins swift_func_typesextract_class_methods gates on that set, so without the entry the requirement gets walked and then discarded.
  3. Both name resolvers accept the node. It has the same simple_identifier shape as function_declaration, and since it now reaches the free-function path too, naming it in only one place would leave a latent unnamed-def bug.

What I dropped from #613, and why

The enum/struct half. #613 also tried to make bare struct/enum produce Class/Enum nodes, and both of its assertions fail on CI (lsan-macos, test-unix, and Windows alike). The cause is worth recording: tree-sitter-swift has no struct_declaration or enum_declaration node type — it models both as class_declaration:

class_declaration:             2
protocol_declaration:          1
protocol_function_declaration: 1
protocol_body:                 1
struct_declaration:            0   ← not a symbol
enum_declaration:              0   ← not a symbol

So those two entries in swift_class_types have been inert since they were written, class_label_for_kind's enum_declaration → "Enum" branch never fires for Swift, and a bare Swift enum is labeled Class. That is a genuine pre-existing modelling gap. I've left the dead entries in place with a comment naming the problem — deleting them would tidy the code while erasing the only marker that the gap exists — and it stays out of this PR.

A find_first_descendant_by_kind rescue in extract_class_methods. Removing it changes no test outcome once the function-type entry is present, so it was carrying nothing.

The result is three production edits instead of six, with no behaviour left unproven.

Tests

swift_protocol asserts Interface:StudyRunning + Method:generate. It is binding: applied against unmodified main, it fails on exactly ASSERT(has_def(r, "Method", "generate")).

Verification

macOS ASan/UBSan: extraction, grammar_labels, grammar_probe_g, grammar_regression, lang_contract, parse_coverage — 384 passed, 0 failed. lint-ci green.

A Swift protocol requirement — a bodyless `func generate() -> String`
inside a protocol — was absent from the graph entirely. Swift codebases
are heavily protocol-driven, so the requirement is very often the
declaration a reader is looking for, and "who declares generate()" had no
answer.

Three edits deliver it: protocol_body joins the class-body types so a
protocol's members are walked at all; protocol_function_declaration joins
swift_func_types, because extract_class_methods gates on that set and
would otherwise walk the requirement and discard it; and both name
resolvers accept the node, which has the same simple_identifier shape as
function_declaration.

Distilled from #613 by xbsjason. That PR also carried an enum/struct half
whose two assertions fail on CI: tree-sitter-swift has no
struct_declaration or enum_declaration node type (it models both as
class_declaration), so the corresponding swift_class_types entries are
inert and a bare enum is still labeled Class. That is a real pre-existing
modelling gap, left untouched here and now documented at the dead entries
rather than silently deleted.

Also dropped from the original: a find_first_descendant_by_kind rescue in
extract_class_methods. Removing it changes no test outcome once the
function-type entry is present, so it was carrying nothing.

Advances #43.

Co-authored-by: xbsjason <xbsjason@gmail.com>
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
@DeusData
DeusData enabled auto-merge August 20, 2026 11:40
@DeusData
DeusData merged commit 5e9c633 into main Aug 20, 2026
36 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.

1 participant