Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(swift): an extension is not a constructible class (#3619)

Swati Ahuja617889c89a00
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index c26666b9c..c190eced1 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -134,6 +134,12 @@ _MAX_ANCESTOR_EXPAND_DEPTH = 3
# other language spells its fields ``variable``, indistinguishable by kind.
_NON_CALLABLE_KINDS = frozenset({"property"})
+# A Rust ``impl`` or Swift ``extension`` block is named for the type it adds
+# members to and declares nothing itself, so no bare name means it: ``Text(..)``
+# constructs SwiftUI's ``Text``, not the repo's ``extension Text``. Kept out of
+# the bare-name indices; its members still resolve through the method index.
+_EXTENSION_KINDS = frozenset({"impl"})
+
# A getter and its setter are two declarations under one id, which reads as an
# overload set and is not one: the name is an attribute, not a callable.
_PROPERTY_DECORATORS = frozenset({"property", "cached_property"})
@@ -561,9 +567,10 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
else:
definitions[decl_key].append((path, index_id))
# File-level symbol index (top-level symbols and methods)
- file_syms[sym.name] = index_id
- if sym.parent_name is None:
- top_level[sym.name] = index_id
+ if sym.kind not in _EXTENSION_KINDS:
+ file_syms[sym.name] = index_id
+ if sym.parent_name is None:
+ top_level[sym.name] = index_id
# Method index: (class_name, method_name) → symbol_id
if sym.parent_name:
@@ -597,8 +604,10 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
self._non_callable_ids.add(index_id)
if _is_property_accessor(sym):
self._property_accessor_ids.add(index_id)
- # Same rule as the per-file index, for the global-unique tier.
- if not (sym.is_declaration and sym.parent_name is not None):
+ # Same rules as the per-file index, for the global-unique tier.
+ if not (sym.is_declaration and sym.parent_name is not None) and (
+ sym.kind not in _EXTENSION_KINDS
+ ):
self._global_symbols[sym.name].append(index_id)
def _index_overload_sets(self, parsed: ParsedFile) -> dict[str, str]:
diff --git a/packages/core/src/repowise/core/ingestion/extractors/__init__.py b/packages/core/src/repowise/core/ingestion/extractors/__init__.py
index db1d512b9..9e0a8a4e0 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/__init__.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/__init__.py
@@ -11,6 +11,7 @@ from .helpers import (
refine_go_type_kind,
refine_kotlin_class_kind,
refine_pascal_type_kind,
+ refine_swift_class_kind,
)
from .heritage import HERITAGE_EXTRACTORS, extract_heritage
from .signatures import build_signature
@@ -32,4 +33,5 @@ __all__ = [
"refine_go_type_kind",
"refine_kotlin_class_kind",
"refine_pascal_type_kind",
+ "refine_swift_class_kind",
]
diff --git a/packages/core/src/repowise/core/ingestion/extractors/helpers.py b/packages/core/src/repowise/core/ingestion/extractors/helpers.py
index fed9b4525..dec5ae101 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/helpers.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/helpers.py
@@ -59,6 +59,12 @@ def refine_kotlin_class_kind(class_node: Node) -> str:
return "class"
+def refine_swift_class_kind(class_node: Node) -> str:
+ """``impl`` for a Swift ``extension``, which adds members to a type it does not declare."""
+ keyword = class_node.child_by_field_name("declaration_kind")
+ return "impl" if keyword is not None and keyword.type == "extension" else "class"
+
+
# Elixir definition keyword -> SymbolKind. Every definition is a `call` node,
# so the node type says nothing; the keyword in the call's target says all.
_ELIXIR_DEFINITION_KINDS = {
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index 16e6a9727..59b5d8027 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -55,6 +55,7 @@ from .extractors import (
refine_go_type_kind,
refine_kotlin_class_kind,
refine_pascal_type_kind,
+ refine_swift_class_kind,
)
from .extractors.bindings.elixir import elixir_import_modules
from .extractors.bindings.python import expand_bare_relative_imports
@@ -567,6 +568,16 @@ def _keep_ts_overloads_beside_a_body(symbols: list[Symbol], node_types: list[str
]
+def _drop_swift_extensions_of_local_types(symbols: list[Symbol]) -> list[Symbol]:
+ """Drop an ``extension`` of a type the same file declares.
+
+ It shares the type's id, so it would replace the type's node and kind; its
+ members already name the type as their parent.
+ """
+ declared = {s.id for s in symbols if s.kind != "impl"}
+ return [s for s in symbols if s.kind != "impl" or s.id not in declared]
+
+
def _is_declaration(
def_node: Node,
config: LanguageConfig,
@@ -723,6 +734,9 @@ def _refine_symbol_kind(
if kind == "class" and language == "kotlin" and node_type == "class_declaration":
kind = refine_kotlin_class_kind(def_node)
+ if kind == "class" and language == "swift" and node_type == "class_declaration":
+ kind = refine_swift_class_kind(def_node)
+
# Pascal ``declType`` wraps class, record, interface, enum and more.
if kind == "class" and language == "pascal" and node_type == "declType":
kind = refine_pascal_type_kind(def_node)
@@ -1661,6 +1675,9 @@ class ASTParser:
if language in _TS_JS_LANGUAGES:
symbols = _keep_ts_overloads_beside_a_body(symbols, node_types)
+ if language == "swift":
+ symbols = _drop_swift_extensions_of_local_types(symbols)
+
# A .m file routinely declares its private methods in a class
# extension and defines them below in the @implementation, which
# builds each symbol id twice in one file.
diff --git a/tests/unit/ingestion/test_bare_name_index_kinds.py b/tests/unit/ingestion/test_bare_name_index_kinds.py
index b402a0e1e..2a84df800 100644
--- a/tests/unit/ingestion/test_bare_name_index_kinds.py
+++ b/tests/unit/ingestion/test_bare_name_index_kinds.py
@@ -329,3 +329,74 @@ class TestTheImportMergedTierRefusesAStdName:
assert [
e for e in edges if e[1] == "parser.py::unwrap" and e[2] == "import_merged"
]
+
+
+class TestSwiftExtensionsAreNotConstructors:
+ """``extension Text {}`` adds members to a type; it declares no ``Text``.
+
+ Stored as a ``class`` it answered every ``Text(..)``, ``Label(..)`` and
+ ``Color(..)`` call: 363 of 1,952 Swift call edges on IceCubesApp.
+ """
+
+ _EXT = "extension Text {\n func styled() -> Text { self }\n}\n"
+
+ def test_an_extension_of_an_external_type_takes_no_constructor_call(
+ self, tmp_path: Path
+ ) -> None:
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "Ext.swift": ("swift", self._EXT + "func local() { _ = Text(\"a\") }\n"),
+ "Use.swift": ("swift", "func use() { _ = Text(\"hi\") }\n"),
+ },
+ )
+ assert _kinds(parsed, "Text") == {"impl"}
+ assert not [e for e in _edges(parsed, tmp_path) if e[1] == "Ext.swift::Text"]
+
+ def test_an_extension_of_a_repo_type_leaves_its_constructor_to_the_type(
+ self, tmp_path: Path
+ ) -> None:
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "Foo.swift": ("swift", "struct Foo {\n init(x: Int) {}\n}\n"),
+ "Ext.swift": ("swift", "extension Foo {\n func bar() {}\n}\n"),
+ "Use.swift": ("swift", "func use() { _ = Foo(x: 1) }\n"),
+ },
+ )
+ assert ("Use.swift::use", "Foo.swift::Foo", "global_unique") in _edges(
+ parsed, tmp_path
+ )
+
+ def test_extension_methods_still_resolve(self, tmp_path: Path) -> None:
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "Ext.swift": ("swift", self._EXT),
+ "Use.swift": (
+ "swift",
+ "func use() {\n let t = Text(\"hi\")\n t.styled()\n}\n",
+ ),
+ },
+ )
+ callees = {e[1] for e in _edges(parsed, tmp_path)}
+ assert callees == {"Ext.swift::Text::styled"}
+
+ def test_an_extension_in_its_types_own_file_leaves_the_type_alone(
+ self, tmp_path: Path
+ ) -> None:
+ """Both share ``Foo.swift::Foo``; the type keeps the symbol and its kind."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "Foo.swift": (
+ "swift",
+ "struct Foo {}\nextension Foo {\n func bar() {}\n}\n",
+ ),
+ "Use.swift": ("swift", "func use() { _ = Foo() }\n"),
+ },
+ )
+ assert _kinds(parsed, "Foo") == {"class"}
+ assert ("Use.swift::use", "Foo.swift::Foo", "global_unique") in _edges(
+ parsed, tmp_path
+ )