Repository navigation
Description
Swift: receiver type inference ignores attribute-typed and factory-derived properties, which silently drops call edges
Summary
graphify gives a Swift call's receiver a type from a per-file name -> type table. It fills
that table from two sources only: an explicit type_annotation, or a direct-child
call_expression constructor. A property that gets its type from any other source stays
untyped. _resolve_swift_member_calls then discards every call through that receiver.
The discard is silent. The graph does not mark the caller as unknown. It shows a smaller set
of callers and gives no indication that the set is incomplete.
The largest example is the SwiftUI Observation idiom:
@Environment(Store.self) private var storeIn my project, 25 files declare the central app-state object this way. graphify explain
reported that one test method was the only caller of a method that two production views call.
An agent that trusts this output concludes that the code is unused.
Environment
| Item | Value |
|---|---|
| graphify | 0.9.36 |
| tree-sitter | 0.25.2 |
| tree-sitter-swift | 0.7.3 |
| Python | 3.13.5 |
| OS | macOS (Darwin 25.3.0) |
Steps to reproduce
Make a directory with these three files:
Store.swift
import Foundation
@Observable final class Store {
var count = 0
func reset() { count = 0 }
}EnvView.swift
import SwiftUI
struct EnvView: View {
@Environment(Store.self) private var store
var body: some View {
Button("Reset") { store.reset() }
}
}AnnotatedView.swift
import SwiftUI
struct AnnotatedView: View {
@EnvironmentObject var store: Store
var body: some View {
Button("Reset") { store.reset() }
}
}Then build the graph and look at the calls edges that point to reset():
graphify update . --no-cluster --forceExpected result
Two calls edges. The two views make the same call. Only the declaration of store is
different.
Actual result
One calls edge.
calls: src_annotatedview_annotatedview_body -> src_store_store_reset
The edge from EnvView is absent.
This continues the sequence of Swift receiver-typing work in #1604 (let x = Type.shared) and
#2181 (computed properties, which made var body and the SwiftUI view layer visible at all).
The Observation idiom is the next declaration form that the type table does not reach.
Cause
The call site is extracted correctly. extract_swift records it with the correct enclosing
node:
{'caller_nid': '..._envview_body', 'callee': 'reset',
'is_member_call': True, 'receiver': 'store'}
The loss is in resolution. The type table for EnvView.swift has no entry for store, so
_resolve_swift_member_calls (extract.py) stops at this test:
type_name = type_table_by_file.get(rc.get("source_file", ""), {}).get(receiver)
type_qualified = False
if not type_name:
continueThe table stays empty because of how tree-sitter-swift parses the declaration. The type is
inside modifiers, not a direct child of property_declaration:
property_declaration |'@Environment(Store.self) private var store'
modifiers |'@Environment(Store.self) private'
attribute |'@Environment(Store.self)'
user_type |'Environment'
navigation_expression |'Store.self'
value_binding_pattern |'var'
pattern > simple_identifier |'store'
The inference block in extractors/engine.py iterates node.children of
property_declaration. It looks for config.call_types and for a navigation_expression
(the Type.shared case from #1604). Both branches are correct, but Store.self is a
grandchild through modifiers, so neither branch reaches it. prop_type stays None and
this guard skips the binding:
if prop_name and prop_type:
type_table[prop_name] = prop_typeScope: @Environment is the largest case, not the whole defect
The same loss occurs whenever the type is not an annotation and not a bare constructor. I
confirmed each of these on 0.9.36:
| Declaration | Receiver typed? |
|---|---|
@Environment(Store.self) private var store |
no |
private var store = Factory.build() |
no |
private let store = Store.make() (static factory) |
no |
@Environment(MyModule.Store.self) var store |
no |
@Environment(Store<Int>.self) var store |
no |
@EnvironmentObject var store: Store |
yes |
@StateObject private var store = Store() |
yes |
let store: Store |
yes |
A fix for @Environment alone leaves the factory-initializer cases broken. The general
statement of the defect is: a Swift property whose type comes from neither a
type_annotation nor a direct-child constructor never enters the receiver type table.
A closure is not a factor. store.reset() inside a plain func body fails the same way.
Suggested fix
Add a third inference source, after the two that exist, so an explicit annotation or a
constructor keeps priority. For the attribute case, descend modifiers > attribute and read
the type from the navigation_expression.
I use this patch locally. It is deliberately narrow.
_SWIFT_ATTRIBUTE_TYPE_RE = re.compile(
r"@Environment\s*\(\s*([A-Z][A-Za-z0-9_]*)\s*\.self\s*\)"
)
def _swift_attribute_type_name(property_node, source: bytes) -> str | None:
for c in property_node.children:
if c.type != "modifiers":
continue
for a in c.children:
if a.type != "attribute":
continue
m = _SWIFT_ATTRIBUTE_TYPE_RE.search(_read_text(a, source))
if m:
return m.group(1)
return NoneCalled as the last resort in the Swift property_declaration branch:
if prop_type is None:
prop_type = _swift_attribute_type_name(node, source)Measured on a 156-file Swift tree, with the AST cache cleared for both runs: 9 calls
edges added, 0 removed, node count unchanged. I read the source for all 9 and each one is a
real call. Graphs for the TypeScript, JavaScript and Node trees in the same project are
byte-identical. Build time changes by about 2%, which is inside the noise of my measurements.
Two limits of this patch, both deliberate:
- It does not match a dotted type (
@Environment(Mod.Store.self)) or a generic type
(@Environment(Store<Int>.self)). These give missed edges, not incorrect edges. - It excludes
@Query. The argument to@Queryis a model type, but the property is a
collection of that type. An earlier version of this patch included@Queryand made a
falsecallsedge fromitems.someMethod()to a method on the element type.@Bindable
is also excluded, because Swift requires an annotation or an initializer there, so the
existing path already handles it.
It also does nothing for the factory-initializer cases in the table above. Those need a
different inference source.
Related defect: the type table is file-wide
This is separate from the issue above, and it is present in 0.9.36 without any patch.
type_table is one flat dictionary per file, keyed by the bare property name, and each write
overwrites the last. Two same-named receivers of different types in one file therefore
contaminate each other. With this input, on unmodified 0.9.36:
struct CView { @EnvironmentObject var store: Other /* ... */ }
struct DView { @EnvironmentObject var store: Store; func go() { store.reset() } }graphify emits DView.go -> Other.reset. The source says Store.
Java and C# already use method-scoped receiver typing in the same file. Swift does not. I can
open a separate issue for this if that is useful.
Two consequences for anyone testing the main issue:
- A repro that puts two structs in one file can pass by accident, because a sibling
declaration leaks the type. Use one struct per file. - The AST cache under
graphify-out/cache/ast/v<version>/is keyed by package version, not
by extractor source. A patched extractor reuses unpatched output and the change looks like
a no-op. Clear the cache between runs. I shared one cache between a patched and an
unpatched run as a control. The unpatched run then reported the patched edge count, and the
measured difference went to zero.--forcedid not prevent this.
Why this matters for agent use
The README recommends graphify install --project, which makes an assistant query the graph
before it reads source files. That is a good default, and I use it. It also means a missing
edge becomes a confident wrong answer instead of a gap the assistant can see.
In my case the assistant asked which code calls selectContext(), got a test method, and
started to debug the wrong layer. A caller list that is silently incomplete is worse than no
caller list, because the assistant has no signal to fall back to grep.
A full fix can be impractical. If it is, please mark a receiver that failed to resolve. Then
consumers can tell "no callers" apart from "unknown".
Activity
fix: JS/Kotlin/Swift/SQL extractor correctness batch; bump to 0.9.38 (#…
safishamsi commentedon Aug 9, 2026
Fixed in v0.9.38. Swift receiver typing now handles two shapes it missed: a property typed only through an
@Environment(Type.self)attribute, and a binding to an in-corpus factory whose return type is known (let x = ServiceFactory.make()). Both now resolve their member calls.@Query/keypath/dotted@Environmentforms and non-concrete returns (opaquesome P, arrays, out-of-corpus) stay unresolved rather than guessing, so no wrong edge is produced. Thanks for the detailed shapes.fix: JS/Kotlin/Swift/SQL extractor correctness batch; bump to 0.9.38 (G…