mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 13:17:41 +00:00
Testkit: a nested library loaded by its own targets stays a crate root
A `src/lib.rs` below another package's `src` folder was treated as a module whenever any declaration named it, including one in its own package's binary. Its `extern crate muda as m;` then did not reach its modules, and a `::m::PredefinedMenuItem::quit` call there was missed. Only a declaration outside the package's `build.rs` and its `src`, `tests`, `examples` and `benches` folders now makes it a module.
This commit is contained in:
@@ -1517,7 +1517,7 @@ fn extern_crates<'a>(toks: &'a [Token], never: &[bool]) -> Vec<(bool, &'a str, u
|
||||
/// to the crate's extern prelude, so that a path starting with `::m` names muda anywhere in the crate.
|
||||
///
|
||||
/// The crate roots follow Cargo's layout: `src/lib.rs` and `src/main.rs`, except one below another package's
|
||||
/// `src` folder that a declaration names; each file that [`cargo_target`]
|
||||
/// `src` folder that a declaration outside its own package folder names; each file that [`cargo_target`]
|
||||
/// accepts; another `build.rs`, `lib.rs` or `main.rs` when no `mod` declaration in another of the files names
|
||||
/// it, plainly or through a `#[path]` attribute; and each other file directly in a `bin`, `examples`, `tests`
|
||||
/// or `benches` folder other than `mod.rs` when no package folder holds it and no declaration without a
|
||||
@@ -1553,6 +1553,9 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec<BTreeSet<Str
|
||||
// through a `#[path]` attribute. Each node lists the nodes its declarations name.
|
||||
let mut named = vec![false; paths.len()];
|
||||
let mut named_plainly = vec![false; paths.len()];
|
||||
// Whether a `src/lib.rs` or `src/main.rs` is named by a declaration in a file other than its package's
|
||||
// `build.rs` and the files in its `src`, `tests`, `examples` and `benches` folders.
|
||||
let mut named_from_outside = vec![false; paths.len()];
|
||||
let edges: Vec<Vec<usize>> = (0..paths.len() * 2)
|
||||
.map(|node| {
|
||||
let (file, path_loaded) = (node / 2, node % 2 == 1);
|
||||
@@ -1564,6 +1567,13 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec<BTreeSet<Str
|
||||
if j != file {
|
||||
named[j] = true;
|
||||
named_plainly[j] |= by_path == 0;
|
||||
let package = paths[j].strip_suffix("src/lib.rs").or_else(|| paths[j].strip_suffix("src/main.rs"));
|
||||
let own = |package: &str| {
|
||||
paths[file].strip_prefix(package).is_some_and(|rest| {
|
||||
rest == "build.rs" || ["src/", "tests/", "examples/", "benches/"].iter().any(|dir| rest.starts_with(dir))
|
||||
})
|
||||
};
|
||||
named_from_outside[j] |= package.is_none_or(|package| !own(package));
|
||||
targets.push(2 * j + by_path);
|
||||
}
|
||||
}
|
||||
@@ -1587,7 +1597,7 @@ fn extern_prelude_names(paths: &[String], contents: &[&str]) -> Vec<BTreeSet<Str
|
||||
let mut segments = paths[file].rsplit('/');
|
||||
match (segments.next(), segments.next()) {
|
||||
// A `src/lib.rs` that a declaration in a package's `src` folder loads is a module there.
|
||||
(Some("lib.rs" | "main.rs"), Some("src")) => !(named[file] && in_package_src(&paths[file])),
|
||||
(Some("lib.rs" | "main.rs"), Some("src")) => !(named_from_outside[file] && in_package_src(&paths[file])),
|
||||
(Some("lib.rs" | "main.rs" | "build.rs"), _) => !named[file],
|
||||
// In a known package, `cargo_target` has already placed the target folders.
|
||||
(Some(name), Some("bin" | "examples" | "tests" | "benches")) => {
|
||||
|
||||
@@ -720,6 +720,23 @@ fn predefined_quit_reads_target_folders_below_the_package_folder_only() {
|
||||
let lib = "extern crate other_menu as menu;\n#[path = \"nested/src/lib.rs\"]\nmod nested;\n";
|
||||
let found = predefined_quit([("src/lib.rs", lib), ("src/nested/src/lib.rs", local)]);
|
||||
assert!(found.is_empty(), "{found:?}");
|
||||
// A nested package's library that only its own binary loads stays a crate root, so its alias reaches its modules.
|
||||
let nested = "extern crate muda as menu;\nmod child;\n";
|
||||
let found = predefined_quit([
|
||||
("src/lib.rs", ""),
|
||||
("src/nested/src/lib.rs", nested),
|
||||
("src/nested/src/child.rs", "fn f() { ::menu::PredefinedMenuItem::quit(None); }\n"),
|
||||
("src/nested/src/bin/tool.rs", "#[path = \"../lib.rs\"]\nmod lib;\n"),
|
||||
]);
|
||||
assert_eq!(found.iter().map(|f| (f.path.as_str(), f.line)).collect::<Vec<_>>(), vec![("src/nested/src/child.rs", 1)]);
|
||||
// One that a module beside its `src` folder loads is a module of the outer crate.
|
||||
let found = predefined_quit([
|
||||
("src/lib.rs", "mod nested;\n"),
|
||||
("src/nested/mod.rs", "#[path = \"src/lib.rs\"]\nmod inner;\n"),
|
||||
("src/nested/src/lib.rs", nested),
|
||||
("src/nested/src/child.rs", "fn f() { ::menu::PredefinedMenuItem::quit(None); }\n"),
|
||||
]);
|
||||
assert!(found.is_empty(), "{found:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user