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(rust): bind a scoped call to its own type, not the file's last (#2409)

Raghav Chamadiya37d55c1b5703
diff --git a/packages/core/src/repowise/core/ingestion/call_resolver.py b/packages/core/src/repowise/core/ingestion/call_resolver.py
index c6a74e633..ce5f9c6f6 100644
--- a/packages/core/src/repowise/core/ingestion/call_resolver.py
+++ b/packages/core/src/repowise/core/ingestion/call_resolver.py
@@ -1033,6 +1033,25 @@ class CallResolver:
local = parsed.export_aliases.get(name) if parsed else None
return symbols.get(local) if local else None
+ def _published_by(self, file_path: str, owner: str, name: str) -> str | None:
+ """What *file_path* publishes under *name*, owned by *owner* where it can be.
+
+ ``_file_symbols`` is flat and last-wins, so a file that declares ``new``
+ on four types answers every ``Type::new()`` lookup with whichever came
+ last. That is the right file and the wrong owner. ``_file_methods``
+ already carries the owner, and until now was only ever asked about the
+ caller's own file.
+
+ A real module qualifier owns nothing — ``config::limits()`` has no
+ ``(config, limits)`` entry anywhere — so it falls through to the flat
+ lookup unchanged. This can only re-point an edge that was already
+ landing on the wrong owner of the right file.
+ """
+ owned = self._file_methods.get(file_path, {}).get((owner, name))
+ if owned is not None:
+ return owned
+ return self._published(file_path, name)
+
def _merged_symbols_for(self, file_path: str) -> dict[str, str]:
"""Merged ``{name → symbol_id}`` across every file *file_path* imports.
@@ -1574,7 +1593,7 @@ class CallResolver:
# Strategy 1: receiver is a module alias (e.g. "import models" → "models.User()")
module_file = self._module_aliases.get(file_path, {}).get(receiver_name)
if module_file:
- published = self._published(module_file, method_name)
+ published = self._published_by(module_file, receiver_name, method_name)
if published is not None:
return ResolvedCall(caller_id, published, 0.88, call.line, "module_alias")
# A namespace over a barrel names a file that declares nothing of
@@ -1598,7 +1617,7 @@ class CallResolver:
name_to_file = self._import_names.get(file_path, {})
if receiver_name in name_to_file and not module_file:
source_file = name_to_file[receiver_name]
- published = self._published(source_file, method_name)
+ published = self._published_by(source_file, receiver_name, method_name)
if published is not None:
return ResolvedCall(caller_id, published, 0.88, call.line, "module_alias")
diff --git a/tests/unit/ingestion/test_call_resolver_strategies.py b/tests/unit/ingestion/test_call_resolver_strategies.py
index 807e73d3c..bbe734f1b 100644
--- a/tests/unit/ingestion/test_call_resolver_strategies.py
+++ b/tests/unit/ingestion/test_call_resolver_strategies.py
@@ -720,3 +720,116 @@ class TestForeignReceiverTypes:
if get_external_receiver_types(spec.tag)
}
assert populated == {"rust"}
+
+
+class TestOwnerAwareModuleTier:
+ """The module and import-name tiers pick an owner, not just a file.
+
+ Both end in a lookup on ``_file_symbols``, which is flat and last-wins. A
+ file declaring ``new`` on several types answered every ``Type::new()`` with
+ whichever came last: the right file, the wrong owner.
+ """
+
+ def test_a_scoped_call_reaches_its_own_type_not_the_files_last(
+ self, tmp_path: Path
+ ) -> None:
+ """Without the owner lookup this lands on ``Second::new``."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "shapes.rs": (
+ "rust",
+ "pub struct First;\n"
+ "pub struct Second;\n"
+ "\n"
+ "impl First {\n"
+ " pub fn new() -> First { First }\n"
+ "}\n"
+ "\n"
+ "impl Second {\n"
+ " pub fn new() -> Second { Second }\n"
+ "}\n",
+ ),
+ "main.rs": (
+ "rust",
+ "use crate::shapes::First;\n"
+ "\n"
+ "pub fn run() -> First {\n"
+ " First::new()\n"
+ "}\n",
+ ),
+ },
+ )
+ _link_imports(parsed, {"main.rs": {"crate::shapes::First": "shapes.rs"}})
+ assert (
+ "main.rs::run",
+ "shapes.rs::First::new",
+ 0.88,
+ "module_alias",
+ ) in _edges(parsed, tmp_path)
+
+ def test_a_module_qualifier_still_reaches_the_free_function(
+ self, tmp_path: Path
+ ) -> None:
+ """``util::helper()`` names a module, and a module owns nothing."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "util.rs": (
+ "rust",
+ "pub struct Thing;\n"
+ "\n"
+ "impl Thing {\n"
+ " pub fn helper() -> u8 { 1 }\n"
+ "}\n"
+ "\n"
+ "pub fn helper() -> u8 { 2 }\n",
+ ),
+ "main.rs": (
+ "rust",
+ "use crate::util;\n"
+ "\n"
+ "pub fn run() -> u8 {\n"
+ " util::helper()\n"
+ "}\n",
+ ),
+ },
+ )
+ _link_imports(parsed, {"main.rs": {"crate::util": "util.rs"}})
+ assert (
+ "main.rs::run",
+ "util.rs::helper",
+ 0.88,
+ "module_alias",
+ ) in _edges(parsed, tmp_path)
+
+ def test_a_python_module_alias_is_not_hijacked_by_a_method_of_that_name(
+ self, tmp_path: Path
+ ) -> None:
+ """Strategy 1 carries the same lookup, and a real alias owns nothing."""
+ parsed = _parse_all(
+ tmp_path,
+ {
+ "models.py": (
+ "python",
+ "class Widget:\n"
+ " def build(self):\n"
+ " return 2\n"
+ "\n"
+ "\n"
+ "def build():\n"
+ " return 1\n",
+ ),
+ "caller.py": (
+ "python",
+ "import models\n\ndef run():\n return models.build()\n",
+ ),
+ },
+ )
+ _link_imports(parsed, {"caller.py": {"models": "models.py"}})
+ assert (
+ "caller.py::run",
+ "models.py::build",
+ 0.88,
+ "module_alias",
+ ) in _edges(parsed, tmp_path)