fix(rust): only land a use rooted in this crate

The review bot is right that `use serde::Serialize;` should not land on a
local `Serialize`. Leaving out std/core/alloc was not enough: any other
crate has the same problem. A `use` is now resolved only when its path
starts at `crate`, `self` or `super`.

(cherry picked from commit ee27336a4f)
This commit is contained in:
berce authored and safishamsi committed 2026-10-14 22:34:17 +01:00
1 parent ef1d256ec8
commit 5faa05c852
2 files changed
+9 -6

No files matched your search

+3 -2
View File
@@ -723,8 +723,9 @@ def extract_rust(path: Path) -> dict:
if arg:
def name_use(written: str, path: str) -> None:
# The name as written, for the corpus rewire (#4439): the id folds its
# case. Not for the standard library, whose `File` is never the corpus's.
if path.lstrip(": ").split("::")[0].strip() not in ("std", "core", "alloc"):
# case. Only for a path rooted in this crate: `serde::Serialize` or
# `std::fs::File` is never the corpus's own `Serialize` or `File`.
if path.lstrip(": ").split("::")[0].strip() in ("crate", "self", "super"):
edges[-1]["_rust_use_name"] = written.split(" as ")[0].strip()
module_name = _rust_use_module(_read_text(arg, source))
grouped = arg.type in ("use_list", "scoped_use_list")
+6 -4
View File
@@ -236,8 +236,10 @@ def test_a_use_of_a_name_declared_twice_stays_unresolved(tmp_path: Path):
assert _imports(result, "user.rs") == ["thing"]
def test_a_std_use_does_not_land_on_a_same_named_declaration(tmp_path: Path):
def test_a_use_from_another_crate_does_not_land_on_a_same_named_declaration(tmp_path: Path):
"""Only a path rooted at `crate`, `self` or `super` names this crate's own item."""
result = _extract(tmp_path, {
"src/lib.rs": "pub mod a;\npub mod user;\n", "src/a.rs": "pub struct File;\npub struct Command;\n",
"src/user.rs": "use std::fs::File;\nuse std::{process::Command, io};\n"})
assert _imports(result, "user.rs") == ["command", "file", "io"]
"src/lib.rs": "pub mod a;\npub mod user;\n",
"src/a.rs": "pub struct File;\npub struct Command;\npub struct Serialize;\n",
"src/user.rs": "use std::fs::File;\nuse std::{process::Command, io};\nuse serde::Serialize;\n"})
assert _imports(result, "user.rs") == ["command", "file", "io", "serialize"]