Skip to content

Unified: Support Swift subscript calls and declarations - #23111

Draft
asgerf wants to merge 4 commits into
github:mainfrom
asgerf:asgerf-swift-subscript-support
Draft

asgerf wants to merge 4 commits into
github:mainfrom
asgerf:asgerf-swift-subscript-support

Conversation

@asgerf

@asgerf asgerf commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Swift subscripts were not modelled. xs[i] was extracted as a call to xs itself, and subscript declarations became unsupported_node, so no data flowed through them.

Representation

  • xs[i] is now a call to a member named subscript, i.e. xs.subscript(i).
  • A subscript declaration becomes a function_declaration named subscript for the getter. If there is a setter, it is extracted as a separate function_declaration named subscript=, placed next to the getter and tagged chained_declaration.
  • The setter gets its own copy of the subscript's parameters, plus one extra trailing positional parameter: newValue, or the name given in set(v). It has no return type.
  • A subscript parameter with only one name has no argument label, as in Swift.
  • A potential clash with a backticked identifier named subscript is ignored for now.

Data flow

  • A function named like foo= is a SetterMethodDeclaration. resolveSetterCallTarget resolves x.foo(...) to foo= on the receiver type, for both instance and static members.
  • This builds on the existing property getter/setter logic: a call that receives an incoming value or a post-update value also creates a setter call. That covers x[i] = v, x[i] += v, x[i].f = v on value types, and &x[i].
  • The new value is passed after the positional arguments. The receiver flows into setter calls, and its updated value flows back out, the same way as for ordinary calls.
  • The Swift write-back rule now goes through call callees, so c.s[0] = v also writes back through s's setter.

LegacyMaD

  • subscript(...) models already match getter calls, since matching is by member name plus argument labels.
  • Setter calls match subscript=(...) models. The labels are the call's labels plus one extra _: for the value.
  • A call with a member callee that is a direct assignment target always creates a setter call, even when no setter is found in source. This lets models for library types match.

Review notes

  • The consistency query now excludes index arguments shared by the getter and setter calls of x[i] += v.
  • Expected outputs in the CFG, BasicTest and type-inference tests changed only because of the new ... .subscript callee node.
  • New tests are in ql/test/library-tests/dataflow/subscripts.swift and the extractor corpus (tests/corpus/swift/subscripts/).

asgerf and others added 3 commits November 11, 2026 11:58
Extract subscript accesses as calls to a 'subscript' member, and subscript
declarations as getter/setter 'subscript' methods marked with get/set modifiers.
The setter clones the subscript parameters and takes the new value as an extra
trailing positional parameter.

Calls in assignment or in-place update positions now invoke setter methods,
generalizing the existing property setter call logic.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Replace the get/set modifiers on subscript methods with a distinct setter
name, resolve setter calls by looking up 'name=' on the receiver, and let
MaD models match setter calls via the 'name=' method name.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The subscript getter is a plain 'subscript' function declaration, and the
setter an accessor_declaration of kind 'setter_method' with the same name.
Accessor declarations gain a type_parameter field for generic subscripts.
Setter calls resolve to the setter_method accessor with the callee's name;
MaD models still refer to setter calls as 'name='.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from db727ed to 6e07e9e Compare November 12, 2026 11:14
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from 6e07e9e to 82d058d Compare November 12, 2026 19:38

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant