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(ingestion): refuse java's package tier when two siblings declare the name (#1870)

Raghav Chamadiya6e241e0970e5
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 9b964d78e..6a6e45427 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -150,7 +150,13 @@ _LANGUAGE_CALL_STRATEGIES: dict[str, _LanguageCallStrategies] = {
# the typed-receiver fallback too. One `name: Type` shape reaches its
# typed vals, vars and parameters alike, so the language gate no longer
# declines the moment the fallback asks.
- "java": replace(_JVM_STRATEGIES, member_fallback=_TYPED_RECEIVER),
+ # Java takes the uniqueness-gated package tier, Kotlin the open one; see
+ # ``_resolve_java_same_package_unique`` for why that is a language rule.
+ "java": replace(
+ _JVM_STRATEGIES,
+ free=("_resolve_java_same_package_unique",),
+ member_fallback=_TYPED_RECEIVER,
+ ),
"kotlin": replace(_JVM_STRATEGIES, member_fallback=_TYPED_RECEIVER),
"csharp": _LanguageCallStrategies(member_fallback=_TYPED_RECEIVER),
"python": _LanguageCallStrategies(member_fallback=_TYPED_RECEIVER),
@@ -578,16 +584,53 @@ class CallResolver:
identifier ``Helper`` may be a class or method defined in any sibling
file of the same package, with no import statement.
"""
+ return self._jvm_same_package(file_path, call, caller_id, unique_only=False)
+
+ def _resolve_java_same_package_unique(
+ self,
+ file_path: str,
+ call: CallSite,
+ caller_id: str,
+ ) -> ResolvedCall | None:
+ """Same tier as ``_resolve_jvm_same_package``, refusing on ambiguity.
+
+ Java-only because Kotlin has package-scope top-level and extension
+ functions, so a bare name there really is a package lookup; Java has
+ none, so it is a static import, an inherited member, or a member call
+ whose receiver the grammar dropped. Hand-read, the removals agree:
+ 20 of 20 wrong on caffeine, 16 of 20 right on exposed and ktor.
+
+ Refusing is not deleting. The chain continues into the import tiers,
+ which answer 14,307 of caffeine's 18,390 refused sites.
+ """
+ return self._jvm_same_package(file_path, call, caller_id, unique_only=True)
+
+ def _jvm_same_package(
+ self,
+ file_path: str,
+ call: CallSite,
+ caller_id: str,
+ *,
+ unique_only: bool,
+ ) -> ResolvedCall | None:
index = self._get_jvm_index()
if index is None:
return None
- siblings = index.same_package_files(file_path)
- for sibling in siblings:
- syms = self._file_symbols.get(sibling, {})
- sym_id = syms.get(call.target_name)
+ found: str | None = None
+ for sibling in index.same_package_files(file_path):
+ sym_id = self._file_symbols.get(sibling, {}).get(call.target_name)
if sym_id is not None and sym_id != caller_id:
- return ResolvedCall(caller_id, sym_id, 0.90, call.line, "same_package")
- return None
+ if not unique_only:
+ return ResolvedCall(caller_id, sym_id, 0.90, call.line, "same_package")
+ if found is not None:
+ # Two siblings declare it and nothing here can tell them
+ # apart; this used to answer with whichever the index
+ # walked first.
+ return None
+ found = sym_id
+ if found is None:
+ return None
+ return ResolvedCall(caller_id, found, 0.90, call.line, "same_package")
def _resolve_jvm_receiver_same_package(
self,
diff --git a/packages/core/src/repowise/core/ingestion/languages/specs/swift.py b/packages/core/src/repowise/core/ingestion/languages/specs/swift.py
index 5953a6a16..bd210b08f 100644
--- a/packages/core/src/repowise/core/ingestion/languages/specs/swift.py
+++ b/packages/core/src/repowise/core/ingestion/languages/specs/swift.py
@@ -50,5 +50,35 @@ SPEC = LanguageSpec(
"Sendable",
}
),
+ # Standard-library and Foundation type names, read only by the bare-name
+ # fallback: an `extension Data` adds members to Foundation's type but does
+ # not own the initializer `Data(contentsOf:)` names.
+ #
+ # Rust's rule, a name a repository plausibly declares itself stays off, is
+ # why `Result`, `Error`, `Task`, `Operation`, `Request` and `Response` are
+ # absent. `OperationQueue` came off on measurement: Alamofire declares a
+ # real `convenience init` on it and the call site invokes that one. A name
+ # match cannot see that, which is this guard's ceiling.
+ builtin_methods=frozenset(
+ {
+ # Foundation value types
+ "Data", "URL", "URLRequest", "URLResponse", "HTTPURLResponse",
+ "URLComponents", "URLQueryItem", "URLSession", "URLSessionConfiguration",
+ "UUID", "Date", "DateComponents", "DateFormatter", "ISO8601DateFormatter",
+ "NumberFormatter", "IndexPath", "IndexSet", "CharacterSet",
+ "Bundle", "Locale", "TimeZone", "Calendar", "FileManager",
+ "JSONDecoder", "JSONEncoder", "JSONSerialization",
+ "PropertyListDecoder", "PropertyListEncoder",
+ "NotificationCenter", "ProcessInfo", "RunLoop", "Thread",
+ "DispatchQueue", "DispatchGroup", "DispatchSemaphore", "DispatchTime",
+ "InputStream", "OutputStream", "Pipe",
+ "NSNumber", "NSString", "NSError", "NSNull", "NSLock", "NSRecursiveLock",
+ # Stdlib containers and scalars
+ "Array", "Dictionary", "Set", "String", "Substring", "Character",
+ "Int", "Int8", "Int16", "Int32", "Int64",
+ "UInt", "UInt8", "UInt16", "UInt32", "UInt64",
+ "Double", "Float", "Bool", "Range", "ClosedRange",
+ }
+ ),
color_hex="#F05138",
)
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index b6ec4577a..b045e8a96 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -207,6 +207,7 @@ _LANGUAGE_STRATEGIES = (
"_resolve_go_same_package",
"_resolve_go_package_call",
"_resolve_jvm_same_package",
+ "_resolve_java_same_package_unique",
"_resolve_jvm_receiver_same_package",
"_resolve_cpp_same_target",
)
@@ -250,7 +251,8 @@ class TestLanguageDispatch:
"Main.java",
"java",
"class Main {\n void run() {\n elsewhere();\n }\n}\n",
- ["_resolve_jvm_same_package"],
+ # java gates the package tier on uniqueness, kotlin does not.
+ ["_resolve_java_same_package_unique"],
),
(
"main.cc",