Skip to content

Unified: Support Swift subscript calls and declarations - #23111

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

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 is extracted as a FunctionDeclaration named subscript, and the setter becomes an AccessorDeclaration with the accessor-kind setter_method. We clone the 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 just let the call resolve as normal (resolves to the getter).

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
}

/** Gets the node corresponding to the field `type_parameter`. */
final F::TypeParameter getTypeParameter(int i) {
* The new value is passed as an extra positional argument after the explicit ones.
*/
pragma[nomagic]
private predicate setterCallSelector(CallExpr call, string name, string argLabels) {
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch 2 times, most recently from ff2cdda to 7ecb52d Compare November 18, 2026 13:07
Extract subscript accesses as calls to a 'subscript' member, and subscript
declarations as a 'subscript' getter method followed by a 'subscript'
accessor of kind 'setter_method'. The setter clones the subscript parameters
and takes the new value as an extra trailing positional parameter.

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 as 'name:set', with the value as the last
argument label, e.g. 'subscript:set(_:_:)'.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf force-pushed the asgerf-swift-subscript-support branch 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)

Comment on lines +34 to 36
node1.isReceiverPostUpdateEx(call) and
step.value() and
node2.isPostUpdate(receiverExpr)
Comment on lines +116 to +119
pos.asPositional() =
count(Argument arg |
arg = call.asSetterCall().(CallExpr).getAnArgument() and arg.isPositional()
)
CachedStage::ref() and
exists(MemberAccessExpr callee, ClassLikeDeclaration cls, NamespaceNode ns |
callee = ce.getCallee() and
ns.getMember(callee.getMemberName()).isIdentifier(result.getNameNode())
@asgerf asgerf added the no-change-note-required This PR does not need a change note label Nov 18, 2026

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