use super::path_vocab::{PathSite, identity_modules, readable_module};
use super::resolve::{CrateScopes, Named, Namespace};
use super::scope_tree::ScopeTable;
#[cfg(test)]
use super::token_tree::Edition;
#[cfg(test)]
use super::token_tree::TokenTree;
use super::use_tree::{UseLeaf, UseStatement};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct ImportedPath {
pub path: String,
pub is_glob: bool,
pub is_self_leaf: bool,
}
impl ImportedPath {
pub(crate) fn can_bind_a_value(&self) -> bool {
!self.is_glob && !self.is_self_leaf
}
#[cfg(test)]
pub(crate) fn plain(path: impl Into<String>) -> Self {
ImportedPath {
path: path.into(),
is_glob: false,
is_self_leaf: false,
}
}
#[cfg(test)]
pub(crate) fn self_leaf(path: impl Into<String>) -> Self {
ImportedPath {
path: path.into(),
is_glob: false,
is_self_leaf: true,
}
}
#[cfg(test)]
pub(crate) fn glob(path: impl Into<String>) -> Self {
ImportedPath {
path: path.into(),
is_glob: true,
is_self_leaf: false,
}
}
}
impl std::ops::Deref for ImportedPath {
type Target = str;
fn deref(&self) -> &str {
&self.path
}
}
#[cfg(test)]
fn file_alone(
source: &str,
current_module: &str,
edition: Edition,
) -> Result<Vec<ClassifiedLeaf>, String> {
let tree = TokenTree::lex(source, edition);
let table = ScopeTable::build(&tree, current_module, 0);
let uses = file_uses(super::use_tree::use_statements(&tree), &table);
classify_uses(&CrateScopes::new(vec![table], edition), 0, &uses)
}
#[cfg(test)]
pub(super) fn imports_with_importers(
source: &str,
current_module: &str,
edition: Edition,
) -> Result<Vec<(String, ImportedPath)>, String> {
Ok(internal_imports(&file_alone(
source,
current_module,
edition,
)?))
}
#[cfg(test)]
pub(super) fn imported_module_paths(
source: &str,
current_module: &str,
) -> Result<Vec<ImportedPath>, String> {
let mut paths: Vec<ImportedPath> =
imports_with_importers(source, current_module, Edition::Rust2021)?
.into_iter()
.map(|(_importer, import)| import)
.collect();
paths.sort();
paths.dedup();
Ok(paths)
}
#[cfg(test)]
fn external_imports_with_importers(
source: &str,
current_module: &str,
edition: Edition,
) -> Result<Vec<(String, String)>, String> {
Ok(external_imports(&file_alone(
source,
current_module,
edition,
)?))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum UseTarget {
Internal(String),
External(String),
}
fn classify(
scopes: &CrateScopes,
t: usize,
scope: u32,
written: &str,
ns: Namespace,
) -> Result<Vec<UseTarget>, String> {
let head_of = |path: &str| {
let path = path.trim_start_matches("::");
path.split_once("::")
.map_or(path, |(head, _)| head)
.to_string()
};
let target_of = |path: &str| {
if path == "crate" || path.starts_with("crate::") {
UseTarget::Internal(readable_module(path))
} else {
UseTarget::External(head_of(path))
}
};
Ok(
match scopes.head_names(t, scope, written, PathSite::Use, ns) {
Named::Paths(paths) => paths.iter().map(|path| target_of(path)).collect(),
Named::External(path) => vec![UseTarget::External(head_of(&path))],
Named::Unbound { head, also, .. } => std::iter::once(UseTarget::External(head))
.chain(also.iter().map(|path| target_of(path)))
.collect(),
Named::Local | Named::Invalid => Vec::new(),
Named::PastCap(refusal) => return Err(refusal),
},
)
}
pub(super) struct ClassifiedLeaf {
importer: String,
target: UseTarget,
is_glob: bool,
is_self_leaf: bool,
}
pub(super) fn classify_uses(
scopes: &CrateScopes,
t: usize,
uses: &[FileUse],
) -> Result<Vec<ClassifiedLeaf>, String> {
let mut out = Vec::new();
for file_use in uses {
let leaves = file_use.leaves.as_ref().map_err(Clone::clone)?;
for leaf in leaves {
let (written, is_glob, is_self_leaf, ns) = match leaf {
UseLeaf::Name { path, .. } => (path, false, false, Namespace::Either),
UseLeaf::Glob(base) => (base, true, false, Namespace::Type),
UseLeaf::SelfLeaf { module, .. } => (module, false, true, Namespace::Type),
UseLeaf::Empty(_) => continue,
};
for target in classify(scopes, t, file_use.scope, written, ns)? {
out.push(ClassifiedLeaf {
importer: file_use.importer.clone(),
target,
is_glob,
is_self_leaf,
});
}
}
}
Ok(out)
}
pub(super) struct FileUse {
pub importer: String,
pub scope: u32,
pub leaves: Result<Vec<UseLeaf>, String>,
}
pub(super) fn file_uses(statements: Vec<UseStatement>, table: &ScopeTable) -> Vec<FileUse> {
let identities = identity_modules(table.scopes.iter().map(|scope| scope.module.as_str()));
statements
.into_iter()
.map(|statement| {
let scope = table.scope_at(statement.at);
FileUse {
importer: identities[&table.scopes[scope as usize].module].clone(),
scope,
leaves: statement.leaves,
}
})
.collect()
}
pub(super) fn internal_imports(classified: &[ClassifiedLeaf]) -> Vec<(String, ImportedPath)> {
let mut pairs: Vec<(String, ImportedPath)> = classified
.iter()
.filter_map(|leaf| match &leaf.target {
UseTarget::Internal(path) => Some((
leaf.importer.clone(),
ImportedPath {
path: path.clone(),
is_glob: leaf.is_glob,
is_self_leaf: leaf.is_self_leaf,
},
)),
UseTarget::External(_) => None,
})
.collect();
pairs.sort();
pairs.dedup();
pairs
}
pub(super) fn external_imports(classified: &[ClassifiedLeaf]) -> Vec<(String, String)> {
let mut pairs: Vec<(String, String)> = classified
.iter()
.filter_map(|leaf| match &leaf.target {
UseTarget::External(head) => Some((leaf.importer.clone(), head.clone())),
UseTarget::Internal(_) => None,
})
.collect();
pairs.sort();
pairs.dedup();
pairs
}
#[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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::a::b")),
"{imports:?}"
);
assert!(
imports.contains(&ImportedPath::plain("crate::a::c::d")),
"{imports:?}"
);
assert!(
imports.contains(&ImportedPath::plain("crate::sibling::X")),
"{imports:?}"
);
assert!(
imports.contains(&ImportedPath::plain("crate::kernel::inner::Y")),
"{imports:?}"
);
assert!(
imports.contains(&ImportedPath::glob("crate::z")),
"{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").unwrap(),
vec![ImportedPath::plain("crate::secret::Thing")],
"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").unwrap();
assert_eq!(
imports,
vec![
ImportedPath::self_leaf("crate::config"),
ImportedPath::plain("crate::config::Setting"),
],
"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").unwrap(),
vec![ImportedPath::self_leaf("crate::config")],
);
assert!(
!imports[1].is_self_leaf && imports[1].can_bind_a_value(),
"a sibling leaf keeps the plain form: {imports:?}"
);
}
#[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").unwrap().is_empty(),
"over-popped super must yield no import: {:?}",
imported_module_paths(over, "crate::a").unwrap()
);
let ok = "use super::other::X;\n";
assert_eq!(
imported_module_paths(ok, "crate::a").unwrap(),
vec![ImportedPath::plain("crate::other::X")],
);
}
#[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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::real::A")),
"{imports:?}"
);
let in_string = r#"let s = "use crate::ghost::Z;";"#;
assert!(
imported_module_paths(in_string, "crate::kernel")
.unwrap()
.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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::real::B")),
"{imports:?}"
);
let lifetime = "fn f<'a>(x: &'a str) {} use crate::a::b;";
let imports = imported_module_paths(lifetime, "crate::kernel").unwrap();
assert_eq!(
imports,
vec![ImportedPath::plain("crate::a::b")],
"{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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::current::A")),
"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 imports = imported_module_paths(&format!("mod kernel;\n{source}"), "crate").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::kernel::Thing")),
"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")
.unwrap()
.iter()
.all(|p| !p.contains("kernel")),
"with no `mod kernel` in scope, the bare path is treated as external"
);
}
#[test]
fn scanner_treats_leading_colon_path_as_external() {
assert!(
imported_module_paths("mod serde;\nuse ::serde::Deserialize;", "crate")
.unwrap()
.is_empty(),
"a leading-:: path must be external even when its head matches a root module"
);
assert!(
imported_module_paths("mod serde;\nuse serde::Deserialize;", "crate")
.unwrap()
.contains(&ImportedPath::plain("crate::serde::Deserialize")),
"a bare head matching a root module resolves locally (shadowing rule)"
);
}
#[test]
fn scanner_resolves_a_bare_head_in_the_scope_it_is_written_in() {
assert!(
imported_module_paths("mod serde;\nuse serde::Value;", "crate")
.unwrap()
.contains(&ImportedPath::plain("crate::serde::Value")),
"at the crate root, a bare crate-root-module path resolves locally"
);
assert!(
imported_module_paths("use serde::Value;", "crate::sub")
.unwrap()
.is_empty(),
"in a submodule, a bare first segment its scope does not bind is external"
);
assert_eq!(
imported_module_paths("mod inner;\nuse inner::X;", "crate::sub").unwrap(),
vec![ImportedPath::plain("crate::sub::inner::X")],
"a submodule's own child is in its scope, so a bare head naming it is that module, as a uniform path is"
);
}
#[test]
fn scanner_preserves_non_ascii_module_paths() {
let source = "use crate::café::Item;";
let imports = imported_module_paths(source, "crate::kernel").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::café::Item")),
"{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")
.unwrap()
.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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::real::C")),
"{imports:?}"
);
let idents = "let r = 1; let b = 2; use crate::real::D;";
assert_eq!(
imported_module_paths(idents, "crate::kernel").unwrap(),
vec![ImportedPath::plain("crate::real::D")]
);
}
#[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")
.unwrap()
.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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::real::C")),
"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").unwrap(),
vec![ImportedPath::plain("crate::real::E")]
);
let idents = "let c = 1; use crate::real::D;";
assert_eq!(
imported_module_paths(idents, "crate::kernel").unwrap(),
vec![ImportedPath::plain("crate::real::D")]
);
}
#[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").unwrap();
assert_eq!(
imports,
vec![ImportedPath::plain("crate::real::A")],
"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").unwrap().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")
.unwrap()
.contains(&ImportedPath::plain("crate::real::B")),
"`!=` / 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").unwrap(),
vec![ImportedPath::plain("crate::forbidden::Thing")],
"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").unwrap(),
vec![ImportedPath::plain("crate::x::Y")],
"a plain use is unaffected by the bound-skip"
);
assert_eq!(
imported_module_paths("use crate::x::Y;\nfn f() -> impl Sized + use<>", "crate",)
.unwrap(),
vec![ImportedPath::plain("crate::x::Y")],
"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").unwrap();
assert!(
imports.contains(&ImportedPath::plain("crate::a::inner::leaf::Thing")),
"self must resolve against the inline submodule: {imports:?}"
);
assert!(
imports.contains(&ImportedPath::plain("crate::a::sibling::X")),
"super from the inline submodule resolves to crate::a: {imports:?}"
);
let bare = imported_module_paths("mod kernel;\nmod inner { use kernel::Thing; }", "crate")
.unwrap();
assert!(
bare.is_empty(),
"a bare use of a crate-root module inside an inline submodule that binds no such name is external: {bare:?}"
);
assert_eq!(
imported_module_paths("use self::leaf::Thing;", "crate::a").unwrap(),
vec![ImportedPath::plain("crate::a::leaf::Thing")]
);
}
#[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").unwrap(),
vec![ImportedPath::plain("crate::real::A")],
"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",
Edition::Rust2018,
)
.unwrap();
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(
"mod libc;\nuse libc::helper;",
"crate",
Edition::Rust2018,
)
.unwrap();
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",
Edition::Rust2018,
)
.unwrap();
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",
Edition::Rust2018,
).unwrap();
assert!(
masked.is_empty(),
"a use inside a string or macro body is not an observed external head: {masked:?}"
);
}
#[test]
fn cfg_if_macro_body_is_read_and_observed() {
let src = r#"
cfg_if::cfg_if! {
if #[cfg(unix)] {
use crate::unix_module::UnixThing;
} else {
use crate::win_module::WinThing;
}
}
"#;
let paths = imported_module_paths(src, "crate").unwrap();
assert_eq!(
paths,
vec![
ImportedPath::plain("crate::unix_module::UnixThing"),
ImportedPath::plain("crate::win_module::WinThing"),
],
"imports inside cfg_if! macro bodies are observed: {paths:?}"
);
}
}