Skip to content

Unified: Basic support for extensions - #22558

Open
asgerf wants to merge 14 commits into
github:mainfrom
asgerf:unified/extensions
Open

asgerf wants to merge 14 commits into
github:mainfrom
asgerf:unified/extensions

Conversation

@asgerf

@asgerf asgerf commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Adds support for Swift extension blocks, aka extension methods.

class A {}
extension A {
  func foo() { ... }
}
A().foo() // calls 'foo' from the extension

This adds a new field, ClassLikeNode.getExtensionTarget() for storing the extension target.

Two features are not yet handled:

  • Scoping: We assume extensions are in scope everywhere, though in reality they are only visible from files the import the file containing the extension. I'd like to build support this together with access levels (protected, etc) as I expect we can use the same mechanism for both. The idea is that an extension method can be seen as having an access level that only permits access within certain files.
  • Type parameters: Type parameters of the target type are in scope within the extension, without being redeclared locally. Not supported yet.

DCA shows an increase in statically-resolvable names by about 2 percentage points.

Inheritance must be resolvable in the main recursive SCC, prior to unqualified member resolution. We now ensure that the static namespace of the extension target is in the local scope.

Note that the instance namespace is not needed here; it is handled in unqualified lookup which is the only case where static name binding needs to worry about instance member lookups.
@asgerf asgerf added the no-change-note-required This PR does not need a change note label Sep 14, 2026
@asgerf
asgerf requested a balanced review from Copilot September 14, 2026 13:43

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.

Copilot review overview

🟡 Changes recommended

A new conformance test uses invalid Swift, and extractor documentation contradicts the new mapping.

Get a fresh assessment by requesting another Copilot review.

Review tier: Balanced
Findings: 1 High severity · 1 Low severity

Open findings (2)
What changed in this PR

Adds structured Swift extension targets and integrates extension members into unified static name binding.

Changes:

  • Adds extension_target to the unified AST schema and extractor.
  • Resolves static and instance members across extensions.
  • Adds extraction and name-binding coverage.
File Description
unified/​extractor/​ast_types.yml Defines the extension-target field.
unified/​extractor/​src/​languages/​swift/​swift.rs Maps Swift extended types structurally.
unified/​extractor/​tests/​corpus/​swift/​types/​extension.output Updates generated extractor expectations.
unified/​ql/​lib/​unified.dbscheme Adds extension-target storage.
unified/​ql/​lib/​codeql/​unified/​internal/​Ast.qll Exposes the generated field accessor.
unified/​ql/​lib/​codeql/​unified/​internal/​StaticNameBinding.qll Connects extension and target namespaces.
unified/​ql/​test/​library-tests/​static-name-binding/​test.swift Updates qualified-target expectations.
unified/​ql/​test/​library-tests/​static-name-binding/​extensions.swift Adds extension name-binding scenarios.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread unified/ql/test/library-tests/static-name-binding/extensions.swift Outdated
Comment thread unified/extractor/src/languages/swift/swift.rs
An extension can add protocols as base, but not classes.

Expanding the test a bit to target a protocol-base and also extension the protocol to add a method with a "default" implementation.
@asgerf asgerf changed the title Unified: Support extensions Unified: Basic support for extensions Sep 14, 2026
@asgerf
asgerf marked this pull request as ready for review September 15, 2026 06:31
@asgerf
asgerf requested review from a team as code owners September 15, 2026 06:31
@asgerf
asgerf requested a review from hvitved September 15, 2026 06:42
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants