use super::lexer::{keyword_starts_at, strip_comments_and_strings, strip_macro_bodies};
use super::path_vocab::{
brace_content, canonical_segment, effective_module, inline_mod_at, is_crate_root_shadow,
resolve_self_super, split_top_commas,
};
pub(crate) fn imported_module_paths(
source: &str,
current_module: &str,
root_modules: &[String],
) -> Vec<String> {
let mut paths: Vec<String> = imports_with_importers(source, current_module, root_modules)
.into_iter()
.map(|(_importer, import)| import)
.collect();
paths.sort();
paths.dedup();
paths
}
pub(crate) fn imports_with_importers(
source: &str,
current_module: &str,
root_modules: &[String],
) -> Vec<(String, String)> {
let cleaned = strip_macro_bodies(&strip_comments_and_strings(source));
let mut pairs = Vec::new();
for (module, tree) in use_trees_with_modules(&cleaned, current_module) {
for leaf in expand_use_tree(&tree) {
if let Some(absolute) = normalize_module_path(&leaf, &module, root_modules) {
pairs.push((module.clone(), absolute));
}
}
}
pairs.sort();
pairs.dedup();
pairs
}
pub(crate) fn external_imports_with_importers(
source: &str,
current_module: &str,
root_modules: &[String],
) -> Vec<(String, String)> {
let cleaned = strip_macro_bodies(&strip_comments_and_strings(source));
let mut pairs = Vec::new();
for (module, tree) in use_trees_with_modules(&cleaned, current_module) {
for leaf in expand_use_tree(&tree) {
if let Some(head) = external_crate_head(&leaf, &module, root_modules) {
pairs.push((module.clone(), head));
}
}
}
pairs.sort();
pairs.dedup();
pairs
}
fn use_trees_with_modules(source: &str, base_module: &str) -> Vec<(String, String)> {
let bytes = source.as_bytes();
let mut trees = Vec::new();
let mut mod_stack: Vec<(String, usize)> = Vec::new();
let mut depth = 0usize;
let mut i = 0;
while i < bytes.len() {
if keyword_starts_at(bytes, i, b"use") {
let start = i + 3;
let mut p = start;
while p < bytes.len() && bytes[p].is_ascii_whitespace() {
p += 1;
}
if bytes.get(p) == Some(&b'<') {
i = start;
continue;
}
match source[start..].find(';') {
Some(rel) => {
trees.push((
effective_module(base_module, &mod_stack),
source[start..start + rel].trim().to_string(),
));
i = start + rel + 1;
continue;
}
None => break,
}
}
if let Some((name_start, name_end, brace)) = inline_mod_at(bytes, i) {
mod_stack.push((
canonical_segment(source[name_start..name_end].trim()).to_string(),
depth,
));
i = brace; continue;
}
match bytes[i] {
b'{' => depth += 1,
b'}' => {
depth = depth.saturating_sub(1);
while mod_stack.last().is_some_and(|(_, d)| *d == depth) {
mod_stack.pop();
}
}
_ => {}
}
i += 1;
}
trees
}
fn expand_use_tree(tree: &str) -> Vec<String> {
expand_use_tree_depth(tree, 0)
}
const MAX_USE_NEST_DEPTH: usize = 128;
fn expand_use_tree_depth(tree: &str, depth: usize) -> Vec<String> {
if depth >= MAX_USE_NEST_DEPTH {
return Vec::new();
}
let tree = tree.trim();
match tree.find('{') {
Some(open) => {
let prefix = tree[..open].trim();
let inner = brace_content(&tree[open..]);
let mut out = Vec::new();
for part in split_top_commas(&inner) {
let part = part.trim();
if part.is_empty() {
continue;
}
let head = match part.find(" as ") {
Some(idx) => part[..idx].trim(),
None => part,
};
if head == "self" {
let module = prefix.trim_end_matches(':').trim();
if !module.is_empty() {
out.push(module.to_string());
}
} else {
out.extend(expand_use_tree_depth(&format!("{prefix}{part}"), depth + 1));
}
}
out
}
None => {
let leaf = match tree.find(" as ") {
Some(idx) => &tree[..idx],
None => tree,
};
let leaf = leaf.trim().strip_suffix("::*").unwrap_or(leaf.trim());
let leaf = leaf.trim_end_matches(':');
if leaf.is_empty() {
Vec::new()
} else {
vec![leaf.to_string()]
}
}
}
}
fn normalize_module_path(
path: &str,
current_module: &str,
root_modules: &[String],
) -> Option<String> {
if path.trim_start().starts_with("::") {
return None;
}
let segments: Vec<&str> = path
.split("::")
.map(|segment| canonical_segment(segment.trim()))
.filter(|segment| !segment.is_empty())
.collect();
let (first, _rest) = segments.split_first()?;
match *first {
"crate" => Some(segments.join("::")),
"self" | "super" => resolve_self_super(current_module, &segments),
other => {
if is_crate_root_shadow(current_module, other, root_modules) {
let mut out = vec!["crate"];
out.extend(segments.iter().copied());
Some(out.join("::"))
} else {
None
}
}
}
}
fn external_crate_head(
path: &str,
current_module: &str,
root_modules: &[String],
) -> Option<String> {
if path.trim_start().starts_with("::") {
return path
.split("::")
.map(|segment| canonical_segment(segment.trim()))
.find(|segment| !segment.is_empty())
.map(str::to_string);
}
let segments: Vec<&str> = path
.split("::")
.map(|segment| canonical_segment(segment.trim()))
.filter(|segment| !segment.is_empty())
.collect();
let (first, _rest) = segments.split_first()?;
match *first {
"crate" | "self" | "super" => None,
other => {
if is_crate_root_shadow(current_module, other, root_modules) {
None
} else {
Some(other.to_string())
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn scanner_expands_groups_and_resolves_relative_imports() {
let source = r#"
// a line comment mentioning use crate::ignored::me;
use crate::a::{b, c::d};
use super::sibling::X;
use self::inner::Y;
use serde::Deserialize;
use crate::z::*;
"#;
let imports = imported_module_paths(source, "crate::kernel", &[]);
assert!(imports.contains(&"crate::a::b".to_string()), "{imports:?}");
assert!(
imports.contains(&"crate::a::c::d".to_string()),
"{imports:?}"
);
assert!(
imports.contains(&"crate::sibling::X".to_string()),
"{imports:?}"
);
assert!(
imports.contains(&"crate::kernel::inner::Y".to_string()),
"{imports:?}"
);
assert!(imports.contains(&"crate::z".to_string()), "{imports:?}");
assert!(!imports.iter().any(|p| p.contains("serde")), "{imports:?}");
assert!(
!imports.iter().any(|p| p.contains("ignored")),
"{imports:?}"
);
}
#[test]
fn a_block_comment_between_use_and_its_path_does_not_fuse_the_keyword() {
assert_eq!(
imported_module_paths("use/*re-export*/crate::secret::Thing;", "crate", &[]),
vec!["crate::secret::Thing".to_string()],
"a block comment after `use` must not swallow the import",
);
}
#[test]
fn a_self_alias_in_a_use_group_resolves_to_the_prefix_module() {
let source = "use crate::config::{self as cfg, Setting};";
let imports = imported_module_paths(source, "crate", &[]);
assert_eq!(
imports,
vec![
"crate::config".to_string(),
"crate::config::Setting".to_string(),
],
"a `self as alias` in a group resolves to the prefix module: {imports:?}"
);
assert_eq!(
imported_module_paths("use crate::config::{self as cfg};", "crate", &[]),
vec!["crate::config".to_string()],
);
}
#[test]
fn super_past_the_crate_root_is_not_an_internal_module() {
let over = "use super::super::other::X;\n";
assert!(
imported_module_paths(over, "crate::a", &[]).is_empty(),
"over-popped super must yield no import: {:?}",
imported_module_paths(over, "crate::a", &[])
);
let ok = "use super::other::X;\n";
assert_eq!(
imported_module_paths(ok, "crate::a", &[]),
vec!["crate::other::X".to_string()],
);
}
#[test]
fn scanner_ignores_comments_and_string_literals() {
let url = r#"let u = "http://example.com"; use crate::real::A;"#;
let imports = imported_module_paths(url, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::real::A".to_string()),
"{imports:?}"
);
let in_string = r#"let s = "use crate::ghost::Z;";"#;
assert!(
imported_module_paths(in_string, "crate::kernel", &[]).is_empty(),
"a use inside a string must not be observed"
);
let quote_char = r#"let q = '"'; use crate::real::B;"#;
let imports = imported_module_paths(quote_char, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::real::B".to_string()),
"{imports:?}"
);
let lifetime = "fn f<'a>(x: &'a str) {} use crate::a::b;";
let imports = imported_module_paths(lifetime, "crate::kernel", &[]);
assert_eq!(imports, vec!["crate::a::b".to_string()], "{imports:?}");
}
#[test]
fn scanner_handles_nested_block_comments() {
let source = r#"
/*
fn old() {
/* tweak later */
use crate::legacy::Thing;
}
*/
use crate::current::A;
"#;
let imports = imported_module_paths(source, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::current::A".to_string()),
"the real use after the nested comment must be observed: {imports:?}"
);
assert!(
!imports.iter().any(|p| p.contains("legacy")),
"a use inside a nested block comment must not be observed: {imports:?}"
);
}
#[test]
fn scanner_resolves_root_relative_bare_use() {
let source = "use kernel::Thing; use serde::Deserialize;";
let roots = vec!["kernel".to_string()];
let imports = imported_module_paths(source, "crate", &roots);
assert!(
imports.contains(&"crate::kernel::Thing".to_string()),
"a bare use of a crate-root module must resolve to crate::…: {imports:?}"
);
assert!(
!imports.iter().any(|p| p.contains("serde")),
"an unknown first segment stays external: {imports:?}"
);
assert!(
imported_module_paths(source, "crate", &[])
.iter()
.all(|p| !p.contains("kernel")),
"with no root modules known, the bare path is treated as external"
);
}
#[test]
fn scanner_treats_leading_colon_path_as_external() {
let roots = vec!["serde".to_string()];
assert!(
imported_module_paths("use ::serde::Deserialize;", "crate", &roots).is_empty(),
"a leading-:: path must be external even when its head matches a root module"
);
assert!(
imported_module_paths("use serde::Deserialize;", "crate", &roots)
.contains(&"crate::serde::Deserialize".to_string()),
"a bare head matching a root module resolves locally (shadowing rule)"
);
}
#[test]
fn scanner_resolves_root_relative_use_only_at_crate_root() {
let roots = vec!["serde".to_string()];
assert!(
imported_module_paths("use serde::Value;", "crate", &roots)
.contains(&"crate::serde::Value".to_string()),
"at the crate root, a bare crate-root-module path resolves locally"
);
assert!(
imported_module_paths("use serde::Value;", "crate::sub", &roots).is_empty(),
"in a submodule, a bare first segment is external even if it matches a root module"
);
}
#[test]
fn scanner_preserves_non_ascii_module_paths() {
let source = "use crate::café::Item;";
let imports = imported_module_paths(source, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::café::Item".to_string()),
"{imports:?}"
);
}
#[test]
fn scanner_ignores_raw_and_byte_strings() {
for src in [
r##"let s = r"use crate::ghost::Z;";"##,
r##"let s = r#"use crate::ghost::Z;"#;"##,
r##"let s = br#"use crate::ghost::Z;"#;"##,
r#"let s = b"use crate::ghost::Z;";"#,
] {
assert!(
imported_module_paths(src, "crate::kernel", &[]).is_empty(),
"a use inside a (raw/byte) string must not be observed: {src}"
);
}
let tricky = r####"let s = r##"http://x "# inside"##; use crate::real::C;"####;
let imports = imported_module_paths(tricky, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::real::C".to_string()),
"{imports:?}"
);
let idents = "let r = 1; let b = 2; use crate::real::D;";
assert_eq!(
imported_module_paths(idents, "crate::kernel", &[]),
vec!["crate::real::D".to_string()]
);
}
#[test]
fn scanner_ignores_raw_c_strings() {
for src in [
r##"let s = cr"use crate::ghost::Z;";"##,
r##"let s = cr#"use crate::ghost::Z;"#;"##,
] {
assert!(
imported_module_paths(src, "crate::kernel", &[]).is_empty(),
"a use inside a raw C-string must not be observed: {src}"
);
}
let tricky = r##"let s = cr#"a"b"#; use crate::real::C;"##;
let imports = imported_module_paths(tricky, "crate::kernel", &[]);
assert!(
imports.contains(&"crate::real::C".to_string()),
"a use after a raw C-string with an odd inner-quote count must be observed: {imports:?}"
);
let cstr = r#"let s = c"use crate::ghost::Z;"; use crate::real::E;"#;
assert_eq!(
imported_module_paths(cstr, "crate::kernel", &[]),
vec!["crate::real::E".to_string()]
);
let idents = "let c = 1; use crate::real::D;";
assert_eq!(
imported_module_paths(idents, "crate::kernel", &[]),
vec!["crate::real::D".to_string()]
);
}
#[test]
fn use_inside_a_macro_body_is_not_observed() {
let source = r#"
macro_rules! m {
() => { use crate::ghost::Thing; };
($x:tt) => {{ use crate::ghost::Other; }};
}
with_imports! { use crate::ghost::FromInvocation; }
use crate::real::A;
"#;
let imports = imported_module_paths(source, "crate", &[]);
assert_eq!(
imports,
vec!["crate::real::A".to_string()],
"macro definition and invocation bodies are skipped; the real use is kept: {imports:?}"
);
for body in [
"macro_rules! m { () => { use crate::ghost::T; }; }",
"macro_rules! m ( () => { use crate::ghost::T; }; )",
"macro_rules! m [ () => { use crate::ghost::T; }; ]",
"some_macro! { use crate::ghost::T; }",
"some_macro!( use crate::ghost::T; )",
"some_macro![ use crate::ghost::T; ]",
] {
assert!(
imported_module_paths(body, "crate", &[]).is_empty(),
"no import observed from a macro body: {body}"
);
}
let not_macros = "let _ = a != b; let _ = !flag; use crate::real::B;";
assert!(
imported_module_paths(not_macros, "crate", &[]).contains(&"crate::real::B".to_string()),
"`!=` / unary `!` must not be treated as a macro invocation"
);
}
#[test]
fn a_precise_capturing_use_bound_does_not_swallow_the_following_use() {
for header in [
"fn iter() -> impl Iterator<Item = u8> + use<> { std::iter::empty() }",
"fn iter<'a, T>() -> impl Iterator<Item = &'a T> + use<'a, T> { loop {} }",
"fn iter() -> impl Iterator<Item = u8> + use <> { std::iter::empty() }",
"fn iter() -> impl Iterator<Item = u8> + use /*c*/ <> { std::iter::empty() }",
] {
let src = format!("{header}\nuse crate::forbidden::Thing;");
assert_eq!(
imported_module_paths(&src, "crate", &[]),
vec!["crate::forbidden::Thing".to_string()],
"the `use<…>` bound must be skipped so the following real use is observed: {header:?}"
);
}
}
#[test]
fn a_use_bound_as_the_last_token_neither_panics_nor_drops_a_preceding_use() {
assert_eq!(
imported_module_paths("use crate::x::Y;", "crate", &[]),
vec!["crate::x::Y".to_string()],
"a plain use is unaffected by the bound-skip"
);
assert_eq!(
imported_module_paths(
"use crate::x::Y;\nfn f() -> impl Sized + use<>",
"crate",
&[],
),
vec!["crate::x::Y".to_string()],
"a trailing use<> bound must not drop the preceding real use or panic"
);
}
#[test]
fn use_inside_an_inline_module_is_attributed_to_that_module() {
let source = "mod inner { use self::leaf::Thing; use super::sibling::X; }";
let imports = imported_module_paths(source, "crate::a", &[]);
assert!(
imports.contains(&"crate::a::inner::leaf::Thing".to_string()),
"self must resolve against the inline submodule: {imports:?}"
);
assert!(
imports.contains(&"crate::a::sibling::X".to_string()),
"super from the inline submodule resolves to crate::a: {imports:?}"
);
let bare = imported_module_paths(
"mod inner { use kernel::Thing; }",
"crate",
&["kernel".to_string()],
);
assert!(
bare.is_empty(),
"a bare use inside an inline submodule is external: {bare:?}"
);
assert_eq!(
imported_module_paths("use self::leaf::Thing;", "crate::a", &[]),
vec!["crate::a::leaf::Thing".to_string()]
);
}
#[test]
fn escaped_quote_char_literal_is_consumed_whole() {
let src = r#"let _q = '\''; let _s = "use crate::ghost::Z;"; use crate::real::A;"#;
assert_eq!(
imported_module_paths(src, "crate::kernel", &[]),
vec!["crate::real::A".to_string()],
"an escaped-quote char literal must not leak and expose a fake use"
);
}
#[test]
fn external_scan_captures_external_heads_with_the_shared_resolution() {
let pairs = external_imports_with_importers(
"use libc::c_int;\nuse ::winapi::HANDLE;\nuse crate::domain::Thing;",
"crate::service",
&[],
);
assert_eq!(
pairs,
vec![
("crate::service".to_string(), "libc".to_string()),
("crate::service".to_string(), "winapi".to_string()),
],
"external heads captured, the internal `crate::…` import dropped: {pairs:?}"
);
let shadowed =
external_imports_with_importers("use libc::helper;", "crate", &["libc".to_string()]);
assert!(
shadowed.is_empty(),
"a shadowed crate-root module is internal, not an external head: {shadowed:?}"
);
let internal = external_imports_with_importers(
"use crate::a::B;\nuse self::x::Y;\nuse super::z::W;",
"crate::m",
&[],
);
assert!(
internal.is_empty(),
"internal roots yield no external heads: {internal:?}"
);
let masked = external_imports_with_importers(
"fn f() { let _s = \"use libc::c_int;\"; }\nmacro_rules! m { () => { use libc::c_void; }; }",
"crate::service",
&[],
);
assert!(
masked.is_empty(),
"a use inside a string or macro body is not an observed external head: {masked:?}"
);
}
}