Skip to content

Unified: Add ClassLikeElement, support unqualified and static access to MaD members - #23211

Draft
asgerf wants to merge 12 commits into
mainfrom
asgerf-swift-mad-static-ext
Draft

asgerf wants to merge 12 commits into
mainfrom
asgerf-swift-mad-static-ext

Conversation

@asgerf

@asgerf asgerf commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

ClassLikeElement

Introduces ClassLikeElement as a representative for a class/type. This serves two purposes:

  • Extensions and the main declaration of class now map to ClassLikeElement. Predicates like getAMemberIncludingExtensions are refactored to go through ClassLikeElement.
  • Types only mentioned in a model, i.e. not declared in the database, also get a ClassLikeElement, making it possible to write code that handles both situations under one pipeline.

Unqualified access to members from models

Members from models are now also present in static name binding. The direct benefit of this is that we can detect unqualified member accesses properly. For example, take the following locally-ambiguous situation:

extension E {
  func blah() {
    bar() // is this a call to self.bar() or top-level bar() from an imported module?
  }
}

If we have a model for E.bar() then the bar access is now classified as UnqualifiedMemberAccess and a corresponding self access is generated, with the usual post-update nodes etc.

Static members and meta-types

For the above to work properly, we also start distinguishing between static and non-static members in the MaD models. We do this by seeing static members as members of the meta-type, i.e. the type describing "the class itself" as opposed to instances of the class. A static member of E is written as a member of E.Type. We don't add full meta-type support to type inference, but add a representative for the meta type, so all member accesses can be assigned a "base type", where static accesses get the meta-type as its base.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…cesses

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… type

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf
asgerf force-pushed the asgerf-swift-mad-static-ext branch from 7bbb39a to 07b4f7b Compare November 23, 2026 14:35
@asgerf
asgerf force-pushed the asgerf-swift-mad-static-ext branch 3 times, most recently from 1946ff8 to 213c70d Compare November 24, 2026 11:15
…n meta-type values

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@asgerf asgerf changed the title Unified: Track static members in legacy MaD models Unified: Add ClassLikeElement, support unqualified access to MaD models, and distinguish static members in MaD Nov 24, 2026
@asgerf asgerf changed the title Unified: Add ClassLikeElement, support unqualified access to MaD models, and distinguish static members in MaD Unified: Add ClassLikeElement, support unqualified and static access to MaD members Nov 24, 2026
@asgerf
asgerf requested a max review from Copilot November 24, 2026 15:33

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

Unresolved name-binding and receiver-typing defects miss modeled accesses or incorrectly match instance-only models, while generated expectations still contain test-failure entries.

5 open issues
What changed in this PR

This PR extends Unified’s Swift analysis with a canonical representation for declared types, extensions, and model-only types. It improves modeled member resolution and adds lightweight metatype support for static access.

Changes:

  • Introduce and export ClassLikeElement, and refactor member lookup around it.
  • Include modeled members in name binding and distinguish static access through metatypes.
  • Migrate static Swift models to .Type selectors and expand extractor and library regression fixtures.
File Description
unified/​ql/​test/​library-tests/​type-inference/​type-inference.expected Update metatype inference expectations.
unified/​ql/​test/​library-tests/​type-inference/​metatypes.swift Test metatype values and member calls.
unified/​ql/​test/​library-tests/​static-name-binding/​test.ql Adapt unqualified-access checks to target nodes.
unified/​ql/​test/​library-tests/​mad/​test.swift Test static, instance, and implicit modeled access.
unified/​ql/​test/​library-tests/​mad/​test.ext.json Define static and instance test models.
unified/​ql/​test/​library-tests/​mad/​test.expected Record modeled-member matching results.
unified/​ql/​test/​library-tests/​mad-static-members/​test.swift Test modeled member namespaces and inheritance.
unified/​ql/​test/​library-tests/​mad-static-members/​test.qlref Enable inline-expectation postprocessing.
unified/​ql/​test/​library-tests/​mad-static-members/​test.ql Check model bindings and metatype bases.
unified/​ql/​test/​library-tests/​mad-static-members/​test.ext.json Supply static and instance member models.
unified/​ql/​test/​library-tests/​mad-static-members/​test.expected Record binding and base-type expectations.
unified/​ql/​test/​library-tests/​mad-builtins/​test.swift Update implicit Process-property sink expectations.
unified/​ql/​test/​library-tests/​mad-builtins/​test.expected Record newly resolved implicit sinks.
unified/​ql/​test/​library-tests/​dataflow/​test.expected Record metatype call data-flow results.
unified/​ql/​test/​library-tests/​dataflow/​metatypes.swift Test metatype calls and class overrides.
unified/​ql/​lib/​utils/​test/​TestUtils.qll Name extension members through canonical elements.
unified/​ql/​lib/​unified.qll Export ClassLikeElement.
unified/​ql/​lib/​ext/​legacy-swift.model.json Mark static Swift APIs with .Type.
unified/​ql/​lib/​ext/​format-string.model.yml Move static format-string models to .Type.
unified/​ql/​lib/​ext/​database.model.json Move static database models to .Type.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeMention.qll Resolve class-like and metatype mentions.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInferencePluginSwift.qll Infer X.self and recognize X.Type.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInferencePlugin.qll Add a metatype-operand plugin hook.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeInference.qll Resolve static members and infer metatype bases.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​TypeAbstraction.qll Update documentation for the ClassLikeType rename.
unified/​ql/​lib/​codeql/​unified/​internal/​typeinference/​Type.qll Unify class-like types and introduce MetaType.
unified/​ql/​lib/​codeql/​unified/​internal/​StaticNameBinding.qll Track modeled members and extension targets.
unified/​ql/​lib/​codeql/​unified/​internal/​mad/​LegacyMaD.qll Distinguish static and instance model selectors.
unified/​ql/​lib/​codeql/​unified/​internal/​FacadeAst.qll Map declarations and extensions to canonical elements.
unified/​ql/​lib/​codeql/​unified/​internal/​dataflow/​FlowSummary.qll Use the renamed MemberSelector abstraction.
unified/​ql/​lib/​codeql/​unified/​internal/​ClassLikeElement.qll Introduce canonical declared/model-only type representatives.
unified/​ql/​lib/​codeql/​unified/​internal/​AnalysisQuality.qll Include modeled unqualified-access targets in statistics.
unified/​extractor/​tests/​corpus/​swift/​types/​metatype.swift Add metatype syntax corpus coverage.
unified/​extractor/​tests/​corpus/​swift/​types/​metatype.output Record parsed and translated metatype trees.
unified/​extractor/​src/​languages/​swift/​swift.rs Translate metatype syntax into member accesses.

Review effort: Max

pragma[nomagic]
private MaD::TypeSelector getExtendedTypeSelector(ClassLikeDeclaration ext) {
not exists(N::getExtendedDeclaration(ext)) and
N::trackTypeSelector(result) = N::getNodeFromRef(ext.getExtensionTarget())
exists(MaD::MemberSelector member |
node1.isMemberSelector(member) and
name = member.getName() and
node2.isTypeSelector(member.getTypeString())
Type inferBaseType(MemberAccess access) {
result = inferType(access.(MemberAccessExpr).getBase())
or
result.(MetaType).getInstanceType() = access.(MemberAccessExpr).getBase().(TypeMention).getType()
or
result.(MetaType).getInstanceType() = access.(MemberAccessExpr).getBase().(TypeMention).getType()
or
result = inferVariableType(access.(UnqualifiedMemberAccess).getImplicitQualifierVariable())
| implicit-self.swift:80:14:80:19 | ... .x | implicit-self.swift:78:24:78:38 | source(...) | implicit-self.swift:80:14:80:19 | ... .x | $@ | implicit-self.swift:78:24:78:38 | source(...) | source(...) |
| implicit-self.swift:86:14:86:14 | x | implicit-self.swift:85:29:85:43 | source(...) | implicit-self.swift:86:14:86:14 | x | $@ | implicit-self.swift:85:29:85:43 | source(...) | source(...) |
| implicit-self.swift:87:14:87:19 | ... .x | implicit-self.swift:85:29:85:43 | source(...) | implicit-self.swift:87:14:87:19 | ... .x | $@ | implicit-self.swift:85:29:85:43 | source(...) | source(...) |
| metatypes.swift:7:14:7:14 | x | metatypes.swift:20:18:20:43 | source(...) | metatypes.swift:7:14:7:14 | x | $@ | metatypes.swift:20:18:20:43 | source(...) | source(...) |

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.

2 participants