Skip to content

Unified: Support Swift subscript calls and declarations - #23111

Draft
asgerf wants to merge 1 commit into
github:mainfrom
asgerf:asgerf-swift-subscript-support
Draft

asgerf wants to merge 1 commit into
github:mainfrom
asgerf:asgerf-swift-subscript-support

Conversation

@asgerf

@asgerf asgerf commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

xs[i] is now seen as a call to a member named subscript, i.e. xs.subscript(i).

A subscript declaration can contain a getter and a setter, with a shared parameter list:

class C {
  subscript(x: Int) -> Int {
    get { .. }
    set { ... }
  }

The getter and setter are extracted as a pair of FunctionDeclarations both named subscript, with the setter having an extra parameter with external name #newValue (a "reserved" name that cannot clash with a user identifier). The use of a named parameter plays nicely with overload resolution and overriding. We clone the shared parameter list so both callables get their own Parameter nodes (crude, but gets the job done). The setter is recognised on the QL side as a SetterMethodDeclaration, and is meant as a language-agnostic concept.

When a call appears as an assignment target (foo() = x), we use the existing TSetterCall branch of DataFlowCall to represent the call to a SetterMethodDeclaration. For type inference, we internally let the call resolve as normal (resolves to the getter) but expose a predicate specifically for resolving setter calls to their setter method.

The PR also contains a very basic model of flow through subscript calls, where they are always seen as accesses to the "array element" content.

Relationship to Ruby

My thinking is that perhaps Ruby's foo= methods can also be handled by being mapped to "setter methods". I've been going back and forth on the idea of fully adopting Ruby's convention, and representing the subscript-setter as a method named subscript=, but eventually decided against it as it lead to the proliferation of ad-hoc string manipulation in the codebase.

@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch 4 times, most recently from 8a57ce8 to b4ac4b1 Compare November 16, 2026 10:44
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch 3 times, most recently from 7ecb52d to eeab6e5 Compare November 18, 2026 13:52
@asgerf
asgerf marked this pull request as ready for review November 18, 2026 19:50
@asgerf
asgerf requested review from a team as code owners November 18, 2026 19:50
Copilot AI adaptive review requested due to automatic review settings November 18, 2026 19:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Swift subscript setter handling has shadowing collisions, incorrect argument mapping when defaults are omitted, and premature receiver updates, causing missed or false data-flow results.

3 open issues
What changed in this PR

Adds Swift subscript support to the unified extractor and analysis libraries, representing reads as subscript method calls and writes as setter-method calls.

Changes:

  • Extracts subscript getters and setters with separate parameters and generic type parameters.
  • Extends call resolution, data flow, and model matching to support setters and basic array-element flow.
  • Adds extraction and analysis tests, including a newly detected path-injection flow.
File Description
unified/​ql/​test/​query-tests/​security/​CWE-022/​PathInjection/​testPathInjection.swift Expects the newly detected subscript flow.
unified/​ql/​test/​query-tests/​security/​CWE-022/​PathInjection/​PathInjectionTest.expected Records the additional alert and flow path.
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Updates subscript expression labels.
unified/​ql/​test/​library-tests/​mad/​test.swift Tests subscript source and sink models.
unified/​ql/​test/​library-tests/​mad/​test.ext.json Defines getter and setter models.
unified/​ql/​test/​library-tests/​mad/​test.expected Records model-matching results.
unified/​ql/​test/​library-tests/​dataflow/​test.ext.json Adds subscript flow summaries.
unified/​ql/​test/​library-tests/​dataflow/​test.expected Records subscript data-flow results.
unified/​ql/​test/​library-tests/​dataflow/​subscripts.swift Tests getters, setters, and write-back flows.
unified/​ql/​test/​library-tests/​controlflow/​basicblock-slices.expected Updates subscript control-flow snapshots.
unified/​ql/​test/​library-tests/​BasicTest/​test.expected Records synthetic subscript identifiers.
unified/​ql/​lib/​unified.dbscheme Adds accessor type-parameter storage.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Resolves setter methods and accessor generics.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​Type.qll Recognizes accessor-owned type parameters.
unified/​ql/​lib/​codeql/​unified/​internal/​mad/​LegacyMaD.qll Matches setter models and their arguments.
unified/​ql/​lib/​codeql/​unified/​internal/​FacadeAst.qll Defines SetterMethodDeclaration.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​FlowSummary.qll Includes setter calls in summary matching.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowPluginSwift.qll Adds subscript element flow and write-back propagation.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowInstantiation.qll Maps setter arguments into data flow.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowGraph.qll Connects setter receivers and updates.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​DataFlowCall.qll Represents assignment-target calls as setters.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​CallGraph.qll Adds setter targets to the call graph.
unified/​ql/​lib/​codeql/​unified/​internal/​Ast.qll Exposes accessor type parameters.
unified/​ql/​consistency-queries/​DataFlowConsistency.ql Allows arguments shared by getter and setter calls.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-protocol-requirement.swift Adds a protocol subscript fixture.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-protocol-requirement.output Records protocol accessor extraction.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-getter-only.swift Adds an implicit-getter fixture.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-getter-only.output Records getter-only extraction.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-get-set.swift Covers labels, modifiers, and generic accessors.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-get-set.output Records getter and setter extraction.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-assignment.swift Adds assignment and nested-subscript fixtures.
unified/​extractor/​tests/​corpus/​swift/​subscripts/​subscript-assignment.output Records assignment extraction.
unified/​extractor/​tests/​corpus/​swift/​expressions/​subscript-call.output Updates subscript-call extraction snapshots.
unified/​extractor/​tests/​corpus/​swift/​collections/​subscript-access.output Records array access as a member call.
unified/​extractor/​tests/​corpus/​swift/​collections/​dictionary-subscript.output Records dictionary access as a member call.
unified/​extractor/​src/​languages/​swift/​swift.rs Implements subscript declaration and call translation.
unified/​extractor/​ast_types.yml Extends accessor schema and documents setter methods.

Review effort: Balanced (adaptive)

@asgerf asgerf added the no-change-note-required This PR does not need a change note label Nov 18, 2026
@asgerf
asgerf marked this pull request as draft November 19, 2026 08:06
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from 9aee525 to 5ae017e Compare November 19, 2026 08:09
Extract subscript accesses as calls to a 'subscript' member, and subscript
declarations as a 'subscript' getter function followed by a 'subscript'
setter function. The setter clones the subscript parameters and takes the
new value as an extra parameter with the reserved external name
'#newValue', which identifies it as a setter method.

A call appearing as an assignment target, or written back after an in-place
update, now invokes the corresponding setter method, generalizing the
existing property setter call logic.

MaD models refer to setter methods by their argument labels, including
'#newValue:', e.g. 'subscript(_:#newValue:)'.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch from 872da71 to 5ebb68a Compare November 19, 2026 09:21
@asgerf
asgerf requested an adaptive review from Copilot November 19, 2026 09:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The PR description still needs final-revision DCA results and a summary of changed results or unexpected regressions for the Swift extraction and unified data-flow changes.

1 open issue
3 resolved since last review

Review effort: Balanced (adaptive)

node2.isResultValue(call)
or
// Simple flow in and out of subscript operators
call.getCallee().(MemberAccess).getMemberName() = "subscript" and

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

no-change-note-required This PR does not need a change note Unified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants