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(jvm): the same-package tier no longer offers another class's method (#3620)

Swati Ahuja2da238dce012
diff --git a/packages/core/src/repowise/core/ingestion/call_language_strategies.py b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
index 2e17675e1..788f09f46 100644
--- a/packages/core/src/repowise/core/ingestion/call_language_strategies.py
+++ b/packages/core/src/repowise/core/ingestion/call_language_strategies.py
@@ -9,6 +9,7 @@ from __future__ import annotations
from dataclasses import dataclass, replace
from typing import Any
+from .call_receiver_typing import _TYPE_KINDS
from .models import CallSite, ParsedFile
from .resolved_call import ResolvedCall
@@ -329,8 +330,8 @@ class LanguageStrategiesMixin:
return None
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:
+ sym_id = self._package_symbol(sibling, call.target_name, caller_id)
+ if sym_id is not None:
if not unique_only:
return ResolvedCall(caller_id, sym_id, 0.90, call.line, "same_package")
if found is not None:
@@ -341,6 +342,24 @@ class LanguageStrategiesMixin:
return None
return ResolvedCall(caller_id, found, 0.90, call.line, "same_package")
+ def _package_symbol(self, sibling: str, name: str, caller_id: str) -> str | None:
+ """What *name* means in *sibling* to a bare call from another package file.
+
+ The package shares top-level names, not members: a data class's
+ synthesized ``copy`` or a ViewModel's ``onClick`` is reached through a
+ receiver, an ancestor or a scope function, never by the package alone.
+ A nested type and a constructor (named for its class) are not members.
+ A member indexed last under the name hides no top-level symbol of it.
+ """
+ sym_id = self._file_symbols.get(sibling, {}).get(name)
+ if sym_id is None:
+ return None
+ symbol = self._symbols_by_id[sym_id]
+ parent = symbol.parent_name
+ if parent is not None and symbol.name != parent and symbol.kind not in _TYPE_KINDS:
+ sym_id = self._file_top_level.get(sibling, {}).get(name)
+ return None if sym_id == caller_id else sym_id
+
def _resolve_jvm_receiver_same_package(
self,
file_path: str,
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index 1a49b9598..3b2dec8b3 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -82,13 +82,11 @@ _IMPLICIT_RECEIVER_LANGUAGES = frozenset({"java", "csharp", "cpp", "kotlin"})
# its ancestors, for an explicit ``self``/``this`` receiver and an implicit one.
#
# C++ is absent because its heritage binds a qualified external parent to a
-# same-named local type, putting unrelated siblings in one hierarchy. Java is
-# absent because `java.scm`'s bare-call pattern also matches `this.field.m()`,
-# which then arrives here indistinguishable from a real implicit receiver.
+# same-named local type, putting unrelated siblings in one hierarchy.
#
# C# is present: the tier answers with a name's overload set, and
# ``resolve_file`` narrows it to the member the argument count names.
-_INHERITED_LANGUAGES = frozenset({"kotlin", "python", "typescript", "swift", "csharp"})
+_INHERITED_LANGUAGES = frozenset({"java", "kotlin", "python", "typescript", "swift", "csharp"})
# VB.NET's own-instance receivers, lowercased: the language is case-insensitive.
_VBNET_SELF_RECEIVERS = frozenset({"me", "myclass"})
@@ -1507,6 +1505,11 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
) -> ResolvedCall | None:
"""Tier 3 and what follows it: answers not grounded in this file or its imports."""
target_name = call.target_name
+ # Ahead of tier 3: the caller's own hierarchy is better evidence than
+ # repo-wide uniqueness, and a JVM package sibling's member never answers.
+ inherited = self._implicit_inherited_call(file_path, call, caller_id)
+ if inherited is not None:
+ return inherited
# Tier 3: global unique match, only within the same language.
# Uniqueness is judged before filtering data members, on purpose: filtering
# them out first would re-uniquify a name a field and a method share. A
@@ -1521,13 +1524,7 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
file_path, call, caller_id, target_name, candidates[0]
)
- inherited = self._implicit_inherited_call(file_path, call, caller_id)
- if inherited is not None:
- return inherited
-
- # An overload set is several declarations under one id, not an
- # ambiguity. Asked after the inherited tier, which answers with more
- # confidence when the caller's own hierarchy declares the name.
+ # An overload set is several declarations under one id, not an ambiguity.
collapsed = self._collapse_declarations(candidates)
if len(candidates) <= 1 or len(collapsed) != 1:
return None
@@ -1554,8 +1551,8 @@ class CallResolver(LanguageStrategiesMixin, ReceiverTypingMixin):
) -> ResolvedCall | None:
"""A bare call an ancestor of the caller's class answers.
- Asked after tier 3, so it can only add an edge. A member call on its
- bare fallback is refused, as in ``_enclosing_class_method``.
+ A member call on its bare fallback is refused, as in
+ ``_enclosing_class_method``.
"""
lang = self._language_of(file_path)
if lang not in _IMPLICIT_RECEIVER_LANGUAGES or lang not in _INHERITED_LANGUAGES:
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index 2e3852c38..f5da9a867 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -173,6 +173,41 @@ class TestUnpinnedStrategies:
)
]
+ def test_jvm_bare_call_does_not_reach_a_sibling_classes_member(
+ self, tmp_path: Path
+ ) -> None:
+ """``copy(..)`` in a lambda is its receiver's, not a sibling data class's."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "src/p/Query.kt": ("kotlin", "package p\n\ndata class Query(val a: Int)\n"),
+ "src/p/Other.kt": ("kotlin", "package p\n\ndata class Other(val b: Int)\n"),
+ "src/p/Repo.kt": (
+ "kotlin",
+ "package p\n\nfun sync(v: Versions) {\n v.update { copy(a = 1) }\n}\n",
+ ),
+ },
+ )
+ assert not [e for e in _edges(parsed, tmp_path) if e[1].endswith("::copy")]
+
+ def test_jvm_bare_call_reaches_a_top_level_function_a_later_member_shares(
+ self, tmp_path: Path
+ ) -> None:
+ """A sibling's member indexed after its top-level ``load`` does not hide it."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "src/p/Util.kt": (
+ "kotlin",
+ "package p\n\nfun load() {}\n\nclass Cache {\n fun load() {}\n}\n",
+ ),
+ "src/p/Main.kt": ("kotlin", "package p\n\nfun main() {\n load()\n}\n"),
+ },
+ )
+ assert ("src/p/Main.kt::main", "src/p/Util.kt::load", 0.90, "same_package") in _edges(
+ parsed, tmp_path
+ )
+
def test_a_target_name_declared_nowhere_resolves_to_nothing(self, tmp_path: Path) -> None:
"""The population the cascade spends most of its budget on."""
parsed = _parse_all(
diff --git a/tests/unit/ingestion/test_dispatch_edges.py b/tests/unit/ingestion/test_dispatch_edges.py
index f94d5487e..cc03da251 100644
--- a/tests/unit/ingestion/test_dispatch_edges.py
+++ b/tests/unit/ingestion/test_dispatch_edges.py
@@ -17,6 +17,8 @@ from __future__ import annotations
from datetime import datetime
from pathlib import Path
+import pytest
+
from repowise.core.ingestion.dispatch_edges import resolve_override_dispatch
from repowise.core.ingestion.graph import GraphBuilder
from repowise.core.ingestion.models import FileInfo
@@ -318,6 +320,46 @@ def test_a_bare_call_reaches_an_inherited_method_in_an_implicit_receiver_languag
)
+_SAME_PACKAGE_INHERITANCE = {
+ "java": {
+ "p/A.java": "package p;\n\npublic class A {\n void helper() {}\n}\n",
+ "p/B.java": (
+ "package p;\n\npublic class B extends A {\n"
+ " Other field;\n"
+ " void f() {\n helper();\n this.field.m();\n }\n}\n"
+ ),
+ "p/Other.java": "package p;\n\npublic class Other {\n void m() {}\n}\n",
+ },
+ "kotlin": {
+ "p/A.kt": "package p\n\nopen class A {\n fun helper() {}\n}\n",
+ "p/B.kt": "package p\n\nclass B : A() {\n fun f() {\n helper()\n }\n}\n",
+ },
+}
+_DECOYS = {
+ "java": ("p/Decoy.java", "package p;\n\npublic class Decoy {\n void helper() {}\n}\n"),
+ "kotlin": ("p/Decoy.kt", "package p\n\nclass Decoy {\n fun helper() {}\n}\n"),
+}
+
+
[email protected]("language", ["java", "kotlin"])
[email protected]("decoy", [False, True])
+def test_a_same_package_bare_call_reaches_the_ancestor_it_inherits_from(
+ tmp_path: Path, language: str, decoy: bool
+) -> None:
+ """The same-package tier offers no sibling's member, so the ancestor walk
+ answers, ahead of the global-unique guess and with or without a decoy
+ declaring the same name. Java's ``this.field.m()`` keeps its receiver, so
+ it never reaches this tier as a bare ``m()``."""
+ sources = dict(_SAME_PACKAGE_INHERITANCE[language])
+ if decoy:
+ sources.update([_DECOYS[language]])
+ graph = _build(tmp_path, sources, language)
+ ext = "java" if language == "java" else "kt"
+ assert _calls_by_origin(graph, "enclosing_inherited") == {
+ (f"p/B.{ext}::B::f", f"p/A.{ext}::A::helper")
+ }
+
+
def test_a_python_bare_call_is_not_read_as_an_implicit_receiver(tmp_path: Path) -> None:
"""Python spells the receiver, so a bare ``helper()`` is a free function
and binding it to the class hierarchy would be wrong."""