Skip to content

fix(swift): keep _name/name member twins apart in node ids - #5204

Closed
rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/swift-underscore-ids
Closed

rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/swift-underscore-ids

Conversation

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor

Closes #5203

What does this PR do?

Swift members whose names differ only by a leading _ or by case now get separate node ids, the same way as Python and C# (#3302, #4415), Solidity (#5058) and Java (#5152): the one public name keeps its id and the others get a salted id.

  • A type's class body is grouped with all its extensions in the same file (they share one id), then salted with the existing _underscore_salted_nid.
  • The Swift cross-file resolver now matches the exact spelling before the folded name (both lookup sites).

_AST_CACHE_SCHEMA 45 -> 46 (Swift output changes).

Numbers

The answer key is a separate tree-sitter scan that counts declarations per class/type.

repo groups no node: v8 -> this PR nodes gained calls fixed v8 ids changed
Alamofire 3 3 -> 0 3 15 0
swift-nio 59 59 -> 0 65 147 1 (hexdumpDetailed/hexDumpDetailed)
swift-collections 55 52 -> 0 69 192 0
  • swift-collections also has 7 names missing on both sides because of tree-sitter parse errors in two files.
  • 22 edges outside the groups changed in swift-collections: 7 test calls now go to the public append/insert/... instead of _append/...; 6 v8 guesses that only worked because a twin had vanished are gone; 1 correct edge was lost (BigString.append -> _append is now ambiguous with _Chunk._append); 1 call now goes to the wrong method (s.index() lands on _index), because TreeSet.index itself collides with the nested type TreeSet.Index, a separate existing bug ([Bug]: Swift: same-named nested types in one file collapse into one node (e.g. two CodingKeys enums) #4575 is a related nested-type one).
  • Extensions in another file use a different id prefix, so they are not grouped with the type.

Repro

class Foo {
  func bar() { helper() }
  func _bar() { other() }
  func caller() { _bar(); bar() }
}
  • v8: no ._bar() node; .bar() calls both helper and other.
  • This PR: foo_foo_bar_2c4c06 (._bar()) exists and every call lands on the right method.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: every Swift member declaration gets its own node, and a call is credited to the member it is written in. Ids outside a colliding group are unchanged. Persisted state: the AST cache is bumped, so cached Swift extractions re-run.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

How was this tested?

uv run --frozen pytest tests/test_swift_underscore_ids.py -q   # 4 tests (one covers the cross-file resolver); all 4 fail on v8
PYTHONPATH="$PWD" uv run --frozen pytest tests -q              # 11152 passed (v8: 11148), 0 failed
uv run --frozen ruff check .                                   # clean
pyright on the changed files                                   # no new errors vs v8

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

…ode ids

make_id strips a leading `_` and folds case, so the methods of one Swift
type `_flatMap()` / `flatMap()` or `hexdumpDetailed()` / `hexDumpDetailed()`
minted ONE id. add_node kept the first declaration and dropped the
second: the method vanished, the calls it makes were credited to its
twin, and calls to it landed on the twin.

Apply the Python/C#/Solidity/Java rule (Graphify-Labs#3302, Graphify-Labs#4415, Graphify-Labs#5058, Graphify-Labs#5151): a
type's id is its file stem plus its name, so its class body and every
same-file `extension` share one id space; group the methods of all of
them by the id they would get and run each through
`_underscore_salted_nid`. The one public name keeps the plain id, every
other spelling is salted. Members that collide with nothing keep their
ids; overloads share one name, so they are not a collision.

The cross-file member-call pass indexed methods by a folded key, so with
both twins present `foo.bar()` from another file bound to whichever came
last. It now binds the exact spelling first and falls back to the folded
key only when the type has no member so named.

Measured on Alamofire, swift-nio and swift-collections: 117 colliding
groups, 137 methods that had no node on v8 now do, and 354 call edges
that v8 credited to a twin now hit the right method. One v8 id changed,
inside a case-only group where both names are public. The resolver
change also re-points 7 test calls from `_append`/`_insert`/... to the
public `append`/`insert`/... they name.

Bump _AST_CACHE_SCHEMA to 46 so cached Swift extractions re-run.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @rohit-jsfreaky. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 13, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (2 not verified).

Formal verification. PR-changed functions: 0/2 verified (0 proven, 0 may-equivalent, 0 distinguished) · 2 not verified (1 vacuous, 1 unsupported).

Not verified on this run: \_resolve\_swift\_member\_calls (vacuous: never exercised), \_extract\_generic (unsupported).


Graphify review — findings

Bumps the AST cache schema to 45 so cached C#, Java and SQL extractions re-run. C# fields and primary-constructor parameters with non-capitalised type names (_Pair, pair, bool) now enter the receiver table, and a member call on an undeclared receiver is tagged receiver_undeclared, so _Pair.Make() binds as a static call. A Java call through a class-qualified static field resolves via the field's declared type, SQL routine bodies keep comma joins and @dblink suffixes, and Swift typed receivers bind to the exact underscore-twin across files, though Swift collision groups are keyed on the simple name and can merge distinct types that share one.

Review partial — this diff was larger than one review pass covers, so later files were not reviewed; some findings may be missing.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 5638 functions depend on the 942 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: walk() — 1 callers, 93 callees

Verification — 5638 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 4938 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

352 of 565 test file(s) selected (62%) via static blast radius.

  • tests/test_4874_jvm_resolution.py — impact
  • tests/test_4978_reconcile_cost.py — impact
  • tests/test_4992_ts_import_types.py — impact
  • tests/test_affected_overrides.py — impact
  • tests/test_alias_folding.py — impact
  • tests/test_analyze.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_binary_media_words.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_ghost_fold_4955.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_cache_long_paths.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_cache_windows_case_4925.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_case_only_decl_ids_4904.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cluster.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_code_only_scope_4923.py — impact
  • tests/test_confidence.py — impact
  • tests/test_confidence_score_output.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cpp_alias_member_resolution.py — impact
  • tests/test_cpp_anonymous_namespace_receivers.py — impact
  • tests/test_cpp_conditional_initializers.py — impact
  • tests/test_cpp_forward_declarations.py — impact
  • tests/test_cpp_forward_receiver_completion.py — impact
  • tests/test_cpp_inherited_member_resolution.py — impact
  • tests/test_cpp_local_callable_shadowing.py — impact
  • tests/test_cpp_member_call_by_receiver_type.py — impact
  • tests/test_cpp_member_declaration_identity.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_receiver_chains.py — impact
  • tests/test_cpp_receiver_parameters.py — impact
  • tests/test_cpp_split_header_owner.py — impact
  • … and 302 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Docs that may be stale (advisory)

…and 10 more.

Formal verification

Could not verify: Could not verify \_resolve\_swift\_member\_calls.

The verifier did not have enough to check \_resolve\_swift\_member\_calls, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: non-vacuity: domain too small (only 1 distinct inputs exercised, need 3) — 'no divergence' would be near-vacuous

Could not verify: Could not verify \_extract\_generic.

The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed on v8 as e68e728 via authorship-preserving cherry-pick, released in graphify 0.9.109. Thanks @rohit-jsfreaky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Swift: _bar() and bar() share one node id, and cross-file foo._bar() resolves to bar()

2 participants