From d605c6ffff3b8e967433cdf8b8efc5c4755397ef Mon Sep 17 00:00:00 2001 From: MCR-3 Date: Tue, 22 Sep 2026 10:52:23 +0200 Subject: [PATCH] fix: bare module::func() call resolution for src-less Rust crates nested in a picked folder A src-less Rust tree is treated as one crate named "crate", with every module one level below the crate root - but when such a tree sits nested under a picked folder (a monorepo containing several flat-layout crates, say), the whole nesting prefix ends up inside every module name instead, and the bare-module-path resolver's one-level-below-crate-root guess (module::func() with no use needed) silently misses. The use-imported branch just above it already falls back to a crate-wide search when its own guess misses; the bare-path branch gets the same fallback now, tightened to require base itself be a real, unique submodule of the crate (not just any function of that name anywhere in it) - a bare std type call like Vec::new() must not get wired to an unrelated same-named top-level function elsewhere in the crate. Added sample_rust_nested_project/ as a minimal src-less two-file regression fixture, nested one level under app/ so picking the fixture's own root folder reproduces the bug directly. Closes #22 --- backend/languages/rust.py | 69 ++++++++++++++++++++--------- sample_rust_nested_project/app/a.rs | 12 +++++ sample_rust_nested_project/app/b.rs | 4 ++ 3 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 sample_rust_nested_project/app/a.rs create mode 100644 sample_rust_nested_project/app/b.rs diff --git a/backend/languages/rust.py b/backend/languages/rust.py index 9f3028e..a3d6675 100644 --- a/backend/languages/rust.py +++ b/backend/languages/rust.py @@ -17,12 +17,16 @@ inference (e.g. through a struct field, or a reassignment) is out of scope, same as python.py's own resolver only handling direct references. -Two further real limits: `use` paths are resolved by name, not by -following actual re-exports, so a type imported through a `pub use` chain -is found via a crate-wide fallback search rather than the exact -re-exporting module. And calls made inside a macro invocation -(`println!(...)`, `format!(...)`, etc.) are invisible: tree-sitter parses a -macro's arguments as an opaque token stream, not as expressions. +Two further real limits: `use` paths and bare module paths alike are +resolved by name, not by following actual re-exports or the real `mod` +tree, so a callee that isn't exactly one level below its crate root - a +type reached through a `pub use` chain, or any module of a `src/`-less +crate that's nested under a picked folder (the whole nesting prefix ends up +inside the module name, not just the crate name) - is found via a +crate-wide fallback search instead. And calls made inside a macro +invocation (`println!(...)`, `format!(...)`, etc.) are invisible: +tree-sitter parses a macro's arguments as an opaque token stream, not as +expressions. """ from __future__ import annotations @@ -471,6 +475,27 @@ def _find_fn_in_crate(self, crate_name: str, fn_name: str) -> RustModuleInfo | N return candidate return None + def _find_submodule_in_crate(self, crate_name: str, base: str) -> RustModuleInfo | None: + """The one module in `crate_name` whose own path ends in `::base` - i.e. `base` really is a + submodule, just not exactly one level below the crate root as the direct guess assumes. + + Unlike `_find_fn_in_crate`, this doesn't search by function name: `base` in `module::func()` + might not name a module at all (a std type used without a `use`, e.g. `Vec::new()`), and + matching by name alone there would wire the call to an unrelated same-named function anywhere + in the crate. Requiring `base` itself to be a real, unique module name avoids that. + """ + matches = [ + candidate + for candidate in self.modules.values() + if candidate.module.startswith(crate_name + "::") and candidate.module.rsplit("::", 1)[-1] == base + ] + return matches[0] if len(matches) == 1 else None + + @staticmethod + def _top_level_fn(module_info: RustModuleInfo | None, fn_name: str) -> str | None: + """`fn_name`'s id among `module_info`'s top-level functions, or None if either isn't there.""" + return module_info.top_level.get(fn_name) if module_info else None + def _make_resolver(self, mod: RustModuleInfo, local_types: dict[str, str]): def resolve(func_node: Node, class_name: str | None) -> str | None: source = self.sources_bytes[mod.rel_path] @@ -482,12 +507,8 @@ def resolve(func_node: Node, class_name: str | None) -> str | None: return mod.top_level[name] if name in mod.from_imports: module_hint, original = mod.from_imports[name] - target_mod = self.modules.get(module_hint) - if target_mod and original in target_mod.top_level: - return target_mod.top_level[original] - fallback_mod = self._find_fn_in_crate(module_hint.split("::")[0], original) - if fallback_mod: - return fallback_mod.top_level[original] + found = self._top_level_fn(self.modules.get(module_hint), original) + return found or self._top_level_fn(self._find_fn_in_crate(module_hint.split("::")[0], original), original) return None # Type::method(...) / module::func(...) - single path segment only, @@ -511,21 +532,27 @@ def resolve(func_node: Node, class_name: str | None) -> str | None: # not a type - maybe `base` is a module alias: module::func() if base in mod.from_imports: module_hint, original = mod.from_imports[base] - target_mod = self.modules.get(_join_module(module_hint, original)) - if target_mod and method in target_mod.top_level: - return target_mod.top_level[method] - fallback_mod = self._find_fn_in_crate(module_hint.split("::")[0], method) - if fallback_mod: - return fallback_mod.top_level[method] + found = self._top_level_fn(self.modules.get(_join_module(module_hint, original)), method) + found = found or self._top_level_fn(self._find_fn_in_crate(module_hint.split("::")[0], method), method) + if found: + return found # bare module path with no `use` needed, e.g. `helpers::helper_fn()` if base in self.crate_names: direct_mod = self.modules.get(base) else: direct_mod = self.modules.get(mod.crate_name if base == "crate" else f"{mod.crate_name}::{base}") - if direct_mod and method in direct_mod.top_level: - return direct_mod.top_level[method] - return None + found = self._top_level_fn(direct_mod, method) + if found: + return found + if base in self.crate_names: + return None # a whole sibling crate nested deeper: Cargo-workspace-aware detection, out of scope + # the one-level-below-crate-root guess missed - e.g. a src-less crate nested + # under a picked folder puts the whole nesting prefix inside every module name + # here. Only worth another look when `base` is a genuine submodule of this crate, + # not just any function of that name - `base` may not be a module at all (a std + # type used bare, e.g. `Vec::new()`, already failed the type check above). + return self._top_level_fn(self._find_submodule_in_crate(mod.crate_name, base), method) # self.method(...) / variable.method() when the variable's type is known if func_node.type == "field_expression": diff --git a/sample_rust_nested_project/app/a.rs b/sample_rust_nested_project/app/a.rs new file mode 100644 index 0000000..1e6abf8 --- /dev/null +++ b/sample_rust_nested_project/app/a.rs @@ -0,0 +1,12 @@ +// Regression fixture for #22: this project has no `src/` layout, so picking +// it directly names its one crate "crate" and every module sits one level +// below the crate root ("crate::a", "crate::b"). Picking *this directory's +// parent* instead - as the app/ prefix here simulates - keeps the same crate +// name but pushes "app" into every module name too ("crate::app::a", +// "crate::app::b"), which is what used to break a bare module path. +mod b; + +/// Calls b's function via a bare module path - no `use` needed or written. +pub fn from_a() -> i32 { + b::from_b() +} diff --git a/sample_rust_nested_project/app/b.rs b/sample_rust_nested_project/app/b.rs new file mode 100644 index 0000000..da1a969 --- /dev/null +++ b/sample_rust_nested_project/app/b.rs @@ -0,0 +1,4 @@ +/// Returns a fixed number, standing in for real work. +pub fn from_b() -> i32 { + 42 +}