fix(resolve): don't bind a bare call to a class its caller can't reach (#4935)
`class Screen : Activity() { fun title() = getString(1) }` got an INFERRED
`calls` edge to `Prefs.getString`, a method of an unrelated class. A bare call
can only name a member of the caller's own class, a superclass or an enclosing
class, but the corpus-wide name match accepted the only `getString` it found.
Activity is outside the corpus, so nothing in the graph says it declares
getString, and the guess landed on the wrong class.
When a class in the caller's chain extends a class with no source file, keep
only candidates that are not class members, that belong to the chain, or that
the file imports by name (`import pkg.Obj.member`, `import static`). Callers
whose whole chain is in the corpus are untouched, so a receiver lambda such as
`with(p) { getString() }` keeps the edge it has today, and top-level functions
still resolve. This mirrors the unresolved-base guard the C# resolver already
uses.
(cherry picked from commit f4723706a7)
This commit is contained in:
1 parent
3844e681f2
commit
d7bff93019
2 files changed
+140
No files matched your search
@@ -7925,6 +7925,76 @@ def _resolve_elixir_qualified_calls(
|
||||
})
|
||||
|
||||
|
||||
def _jvm_scope_index(
|
||||
all_nodes: list[dict], all_edges: list[dict]
|
||||
) -> tuple[dict[str, str], dict[str, list[str]], dict[str, str], set[str]]:
|
||||
"""Per-corpus tables for deciding which classes a bare Kotlin/Java call can reach.
|
||||
|
||||
Returns ``(owner_of, bases_of, enclosing_of, sourceless)``: method id ->
|
||||
declaring class, class -> its ``inherits`` targets, nested class -> its
|
||||
enclosing class, and the ids of nodes with no ``source_file`` (the stubs
|
||||
left for classes declared outside the corpus).
|
||||
"""
|
||||
owner_of: dict[str, str] = {}
|
||||
bases_of: dict[str, list[str]] = {}
|
||||
enclosing_of: dict[str, str] = {}
|
||||
for e in all_edges:
|
||||
rel = e.get("relation")
|
||||
if rel == "method":
|
||||
owner_of[e["target"]] = e["source"]
|
||||
elif rel == "inherits":
|
||||
bases_of.setdefault(e["source"], []).append(e["target"])
|
||||
elif rel == "contains":
|
||||
enclosing_of[e["target"]] = e["source"]
|
||||
sourceless = {n["id"] for n in all_nodes if not n.get("source_file")}
|
||||
return owner_of, bases_of, enclosing_of, sourceless
|
||||
|
||||
|
||||
def _jvm_reachable_candidates(
|
||||
candidates: list[str],
|
||||
caller: str,
|
||||
callee: str,
|
||||
index: tuple[dict[str, str], dict[str, list[str]], dict[str, str], set[str]],
|
||||
imported_symbols: set[str],
|
||||
) -> list[str]:
|
||||
"""Drop bare-call candidates the caller's class cannot reach (#4935).
|
||||
|
||||
A bare ``getString(1)`` can name a member of the caller's own class, of a
|
||||
superclass, or of an enclosing class. When a class in that chain extends a
|
||||
class outside the corpus, an unresolved base may declare the member, and
|
||||
the corpus-wide name match then binds the call to a same-named method of an
|
||||
unrelated class (``Activity.getString`` became ``Prefs.getString``). In that
|
||||
case keep only candidates that are not class members, that belong to the
|
||||
chain, or that the file imports by name (``import pkg.Obj.member``,
|
||||
``import static``). A caller whose chain is fully in the corpus is left
|
||||
alone, so a receiver lambda such as ``with(p) { getString() }`` keeps the
|
||||
edge it has today.
|
||||
"""
|
||||
owner_of, bases_of, enclosing_of, sourceless = index
|
||||
chain: set[str] = set()
|
||||
todo = [owner_of.get(caller, caller)]
|
||||
external_base = False
|
||||
while todo:
|
||||
cls = todo.pop()
|
||||
if cls in chain:
|
||||
continue
|
||||
chain.add(cls)
|
||||
for base in bases_of.get(cls, ()):
|
||||
if base in sourceless:
|
||||
external_base = True
|
||||
else:
|
||||
todo.append(base)
|
||||
if cls in enclosing_of:
|
||||
todo.append(enclosing_of[cls])
|
||||
if not external_base:
|
||||
return candidates
|
||||
imported_by_name = _make_id(callee) in imported_symbols
|
||||
return [
|
||||
c for c in candidates
|
||||
if c not in owner_of or owner_of[c] in chain or c in imported_symbols or imported_by_name
|
||||
]
|
||||
|
||||
|
||||
def _resolve_kotlin_member_calls(
|
||||
per_file: list[dict],
|
||||
all_nodes: list[dict],
|
||||
@@ -11138,6 +11208,7 @@ def extract(
|
||||
_csharp_method_owner: dict[str, str] | None = None # method nid -> type nid, built on first use
|
||||
_csharp_outer_type: dict[str, str] | None = None # nested type nid -> outer type label, built on first use
|
||||
_csharp_overload_cache: dict = {} # filled on first use (#4584)
|
||||
_jvm_index = None # built on first Kotlin/Java bare call (#4935)
|
||||
for rc in all_raw_calls:
|
||||
if rc.get("_ambiguous_python_import"):
|
||||
continue
|
||||
@@ -11339,6 +11410,14 @@ def extract(
|
||||
)
|
||||
imported_symbols = file_to_symbol_imports.get(caller_file_nid, set())
|
||||
imported_modules = file_to_module_imports.get(caller_file_nid, set())
|
||||
if caller_family == "jvm":
|
||||
if _jvm_index is None:
|
||||
_jvm_index = _jvm_scope_index(all_nodes, all_edges)
|
||||
candidates = _jvm_reachable_candidates(
|
||||
candidates, caller, callee, _jvm_index, imported_symbols
|
||||
)
|
||||
if not candidates:
|
||||
continue
|
||||
|
||||
def _has_import_evidence(candidate_id: str) -> bool:
|
||||
# Direct symbol import (`import { foo }`) is the strongest evidence:
|
||||
|
||||
@@ -183,3 +183,64 @@ def test_object_and_companion_init_blocks_produce_an_edge(tmp_path: Path) -> Non
|
||||
# The owner of a companion's init is not pinned: the extractor gives the
|
||||
# enclosing class or the file node depending on the layout.
|
||||
assert any(s.startswith("Widget:") and t == "Logger:.single()" for s, t in edges)
|
||||
|
||||
|
||||
# ---- inherited external methods ----------------------------------------------
|
||||
|
||||
def test_an_external_base_does_not_bind_a_bare_call_to_an_unrelated_class(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/Prefs.java": PREFS_JAVA,
|
||||
"app/Screen.kt": "package app\nimport android.app.Activity\n"
|
||||
"class Screen : Activity() { fun title(): String = getString(1) }\n",
|
||||
})
|
||||
assert ("Screen:.title()", "Prefs:.getString()") not in edges
|
||||
|
||||
|
||||
def test_a_java_class_with_an_external_base_gets_the_same_guard(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/Prefs.java": PREFS_JAVA,
|
||||
"app/Screen.java": "package app;\nimport android.app.Activity;\n"
|
||||
"public class Screen extends Activity { public void t() { getString(1); } }\n",
|
||||
})
|
||||
assert ("Screen:.t()", "Prefs:.getString()") not in edges
|
||||
|
||||
|
||||
def test_an_in_repo_base_that_extends_an_external_class_still_taints_its_children(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/Prefs.java": PREFS_JAVA,
|
||||
"util/BaseScreen.kt": "package util\nimport android.app.Activity\n"
|
||||
"open class BaseScreen : Activity() { fun baseHelper() {} }\n",
|
||||
"app/Screen.kt": "package app\nimport util.BaseScreen\n"
|
||||
"class Screen : BaseScreen() {\n"
|
||||
" fun a() { baseHelper() }\n"
|
||||
" fun b(): String = getString(1)\n}\n",
|
||||
})
|
||||
assert ("Screen:.a()", "BaseScreen:.baseHelper()") in edges # in-repo member kept
|
||||
assert ("Screen:.b()", "Prefs:.getString()") not in edges # external one dropped
|
||||
|
||||
|
||||
def test_a_top_level_function_is_still_called_from_a_class_with_an_external_base(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/Top.kt": "package util\nfun topHelper() {}\n",
|
||||
"app/Screen.kt": "package app\nimport android.app.Activity\nimport util.topHelper\n"
|
||||
"class Screen : Activity() { fun t() = topHelper() }\n",
|
||||
})
|
||||
assert ("Screen:.t()", "Top:topHelper()") in edges
|
||||
|
||||
|
||||
def test_a_member_imported_by_name_survives_an_external_base_in_kotlin(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/Settings.kt": SETTINGS_KT,
|
||||
"app/Screen.kt": "package app\nimport android.app.Activity\nimport util.Settings.fetchValue\n"
|
||||
"class Screen : Activity() { fun t() = fetchValue(\"k\") }\n",
|
||||
})
|
||||
assert ("Screen:.t()", "Settings:.fetchValue()") in edges
|
||||
|
||||
|
||||
def test_a_member_imported_by_name_survives_an_external_base_in_java(tmp_path: Path) -> None:
|
||||
edges = _calls(tmp_path, {
|
||||
"util/JSettings.java": "package util;\npublic class JSettings { public static void helperJ() {} }\n",
|
||||
"app/Screen.java": "package app;\nimport android.app.Activity;\nimport static util.JSettings.helperJ;\n"
|
||||
"public class Screen extends Activity { public void t() { helperJ(); } }\n",
|
||||
})
|
||||
assert ("Screen:.t()", "JSettings:.helperJ()") in edges
|
||||
Reference in new issue
Block a user