Skip to content

fix(swift): recover switch-case uses of modifier-named locals (#5006) - #5641

Closed
Komorebi-GZM wants to merge 2 commits into
Graphify-Labs:v8from
Komorebi-GZM:fix/swift-switch-modifier-idents
Closed

Komorebi-GZM wants to merge 2 commits into
Graphify-Labs:v8from
Komorebi-GZM:fix/swift-switch-modifier-idents

Conversation

@Komorebi-GZM

Copy link
Copy Markdown
Contributor

Closes #5006.

What

tree-sitter-swift 0.7.4 (the newest pin under >=0.7,<0.9) reads a contextual-modifier identifier used as a value after return in a switch case body — return prefix + "a", return prefix.count — as a declaration modifier waiting for a declaration. The switch collapses into ERROR nodes and the file is only partially extracted (parse_errors, and switch kind { ... } is misread as a call to kind).

Fail-closed fix (CONTRIBUTING item 5), in extract_swift:

  • If the file already parses, it is left untouched.
  • Otherwise, identifiers that the broken tree classified as modifiers under an ERROR node are grouped with parentheses (valid Swift) and the file is re-parsed.
  • The rewrite is kept only when the whole file then parses cleanly. A file that still has other syntax errors keeps today's behaviour.

Real declaration modifiers (prefix func, open class, final class, override func) are not rewritten: they are not children of ERROR.

The AST cache schema bumps 59 → 60 so cached Swift inputs re-extract.

Tests

tests/test_swift_switch_modifier_idents.py:

  • 20 modifier names × (return ident + "a" and return ident.count) have no parse_errors and no fabricated kind call.
  • Controls: bare return prefix, if (not switch), and real prefix/open/final/override declarations still parse and are not rewritten away.
  • A file with another remaining syntax error is refused (parse_errors stay).

On tree-sitter-swift 0.7.4, the operator/member cases fail without the change and pass with it. Existing Swift suites still pass (65 tests in the focused run).

Base: Graphify-Labs/graphify v8 @ 5d962d2 (0.9.123).

…fy-Labs#5006)

tree-sitter-swift 0.7.4 reads a contextual-modifier identifier used as a
value after `return` in a switch case body (`return prefix + "a"`) as a
declaration modifier, so the switch collapses into ERROR nodes and the
file is only partially extracted.

Group those misread identifiers with parentheses (valid Swift) before
parsing. The rewrite is kept only when it makes the whole file parse
cleanly; a file that already parses, or that still has errors afterwards,
is left untouched. Bump the AST cache schema to 60 so cached Swift
inputs re-extract.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Komorebi-GZM. 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.11 and 3.13, and that the change keeps extraction deterministic.

@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 review View in Graphify

Nothing blocking · 8 possible issues to check · Merge confidence 4/5

8 possible issues to check · High impact

Swift extraction now retries parsing when modifier-named identifiers in switch cases cause syntax errors, and reuses the recovered parse tree.

8 possible issues we couldn't confirm

They don't change the score.

What this review covered
  • 1 more candidate was not reported: the cited line wasn't in code the review read.
  • We didn't review 1 file that isn't source code, such as docs, config, images, or generated or third-party code.
  • A changed file's diff was too long to show in full, so the review may not have seen every changed line in it.
  • 4 of 4 changed files inspected. This is scope, not a correctness guarantee.
Code graph · 6751 functions depend on this change
  • 1 function became more coupled. extract_swift() (34 callers).
  • 659 of 659 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed 62e2aa5 · 1 of 1 earlier findings resolved · 6 earlier findings not re-checked

Comment thread graphify/extract.py Resolved
@Komorebi-GZM

Copy link
Copy Markdown
Contributor Author

Coordination notes for this PR (from the same account's other workstream)

Posting here so the notes travel with the PR notifications:

  1. Repo discipline: while this PR is open, no new issue claims in this repo from the same account (one line at a time, consistent with the maintainer's expectations).
  2. Bot review findings worth addressing (from the graphify-labs investigation on this PR):
    • Parenthesize modifier-like words only inside switch cases under parse errors — not file-wide.
    • Overlapping modifier spans: handle offset shifts from earlier insertions (process right-to-left or recompute), otherwise later wraps land at wrong offsets.
    • Skip the extra parse for Swift files with no parse errors.
  3. Regression-test discipline that has worked well on the sibling TS PR (fix(typescript): resolve calls through immutable local const aliases #5622): prove the old failure first (red), then fix (green); the graphify-labs bot reviews each push, so expect one finding per push and fix in the same round.

Our sibling PR #5622 (TypeScript const-alias call edges) is parked waiting for maintainer review with 4 rounds of red-green fixes and a semantic matrix posted — see its issue thread for the reviewer-tested pattern.

…cases (Graphify-Labs#5006)

Accept the graphify-labs review on Graphify-Labs#5641: a clean Swift file is extracted
from the normalizer's tree instead of being parsed twice, and modifier
parentheses are inserted only inside a switch case. Collected spans are
disjoint identifier ranges, so right-to-left insertion is unchanged.
AST cache schema 60 -> 61.
safishamsi added a commit that referenced this pull request Nov 30, 2026
Extractor accuracy (Swift #5641, Apex #5627, Python #5647, TypeScript
#5604/#5614/#5612, Bash caller-var #5602), GitHub Actions run-step
extraction (#5603), incremental correctness (#5634/#5638/#5636), symlink
and semantic-cache fixes (#5617/#5618), export/report hardening and perf
(#5643/#5644/#5645/#5646/#5619/#5620/#5621), and CLI/docs/skill/install
work (#5629/#5628/#5626/#5624/#5623/#5625/#5642/#5583). AST cache schema
59 to 60; Swift/Apex/Python/Bash/GHA inputs re-extract on first build.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.124 (just released to PyPI), cherry-picked with your authorship preserved so it shows under your name in the history. Thanks @Komorebi-GZM for recovering switch-case uses of modifier-named Swift locals — verified green in the full suite and, where applicable, smoke-tested against the published package. 🙏

@safishamsi safishamsi closed this Nov 30, 2026
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.

[Bug]: Swift: a local with same name as a modifier (prefix, open, final...) used in a switch case body gives a syntax-parse error from tree-sitter-swift

2 participants