use super::super::item_head::{BlockModule, block_modules};
use super::super::source_texts::SourceTexts;
use super::super::token_tree::{Edition, TokenTree};
use super::declarations::{DeclaredModule, block_path_modules, declared_modules_in};
use super::paths::module_path_of;
use std::cell::OnceCell;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::rc::Rc;
#[derive(Clone)]
enum ScanSource {
File {
file: PathBuf,
path_base: PathBuf,
child_base: PathBuf,
lineage: Lineage,
},
Body {
file: PathBuf,
start: usize,
end: usize,
path_base: PathBuf,
child_base: PathBuf,
lineage: Lineage,
},
}
#[derive(Clone, Default)]
struct Lineage {
files: HashSet<PathBuf>,
compiled_out: bool,
in_block: bool,
}
impl Lineage {
fn under(&self, compiled_out: bool) -> Lineage {
Lineage {
files: self.files.clone(),
compiled_out: self.compiled_out || compiled_out,
in_block: false,
}
}
fn with_in_block(&self, in_block: bool) -> Lineage {
Lineage {
in_block,
..self.clone()
}
}
}
struct LoadedSource {
file: PathBuf,
range: Option<Range<usize>>,
path_base: PathBuf,
child_base: PathBuf,
lineage: Lineage,
}
impl ScanSource {
fn load(&self) -> LoadedSource {
let (file, range, path_base, child_base, lineage) = match self {
Self::File {
file,
path_base,
child_base,
lineage,
} => (file, None, path_base, child_base, lineage),
Self::Body {
file,
start,
end,
path_base,
child_base,
lineage,
} => (file, Some(*start + 1..*end), path_base, child_base, lineage),
};
LoadedSource {
file: file.clone(),
range,
path_base: path_base.clone(),
child_base: child_base.clone(),
lineage: lineage.clone(),
}
}
}
struct InlineBody {
file: PathBuf,
start: usize,
end: usize,
base: PathBuf,
directory: String,
relocated_base: Option<PathBuf>,
candidate_bases: Vec<PathBuf>,
conventional_compiles: bool,
lineage: Lineage,
}
struct PlainSource {
base: PathBuf,
declared_in: PathBuf,
lineage: Lineage,
is_cfg_conditional: bool,
}
struct DirectPathSource {
relative: PathBuf,
base: PathBuf,
declared_in: PathBuf,
lineage: Lineage,
is_cfg_conditional: bool,
}
struct ConditionalPathSource {
relative: PathBuf,
base: PathBuf,
declared_in: PathBuf,
lineage: Lineage,
}
#[derive(Default)]
struct ChildSources {
seen_plain_file: bool,
bodies: Vec<InlineBody>,
plain: Vec<PlainSource>,
direct: Vec<DirectPathSource>,
conditional: Vec<ConditionalPathSource>,
}
fn collect_children<'t>(
module: &str,
scan_sources: &[ScanSource],
edition: Edition,
sources: &SourceTexts,
texts: &'t HashMap<&PathBuf, OnceCell<Rc<str>>>,
trees: &mut HashMap<PathBuf, (TokenTree<'t>, Vec<BlockModule>)>,
) -> Result<BTreeMap<String, ChildSources>, String> {
let mut children: BTreeMap<String, ChildSources> = Default::default();
for source in scan_sources {
let loaded = source.load();
let read = || {
sources.text(&loaded.file).map_err(|err| {
format!("cannot read source file '{}': {err}", loaded.file.display())
})
};
match texts.get(&loaded.file) {
Some(slot) => {
if !trees.contains_key(&loaded.file) {
let text = match slot.get() {
Some(text) => text,
None => {
let text = read()?;
slot.get_or_init(|| text)
}
};
let tree = TokenTree::lex(text, edition);
let blocks = block_modules(&tree);
trees.insert(loaded.file.clone(), (tree, blocks));
}
let (tree, blocks) = &trees[&loaded.file];
record_declarations(module, &loaded, tree, blocks, &mut children)?;
}
None => {
let text = read()?;
let tree = TokenTree::lex(&text, edition);
let blocks = block_modules(&tree);
record_declarations(module, &loaded, &tree, &blocks, &mut children)?;
}
}
}
Ok(children)
}
fn record_declarations(
module: &str,
loaded: &LoadedSource,
tree: &TokenTree<'_>,
blocks: &[BlockModule],
children: &mut BTreeMap<String, ChildSources>,
) -> Result<(), String> {
let range = loaded.range.clone().unwrap_or(0..tree.len());
for declared in declared_modules_in(tree, range.clone())
.into_iter()
.chain(block_path_modules(tree, blocks, range))
{
let child_sources = children.entry(declared.name.clone()).or_default();
match declared.body {
Some((start, end)) => child_sources
.bodies
.push(inline_body(loaded, &declared, start, end)),
None => record_file_form(module, loaded, &declared, child_sources)?,
}
}
Ok(())
}
fn inline_body(
loaded: &LoadedSource,
declared: &DeclaredModule,
start: usize,
end: usize,
) -> InlineBody {
let in_block = loaded.lineage.in_block || declared.declared_in_block;
let (base, directory) = if declared.declared_in_block {
let own = declared
.name
.rsplit_once("::")
.map_or(&*declared.name, |(_, own)| own);
(loaded.path_base.clone(), own.to_string())
} else {
(loaded.child_base.clone(), declared.name.clone())
};
InlineBody {
file: loaded.file.clone(),
start,
end,
base,
directory,
relocated_base: declared
.direct_path
.clone()
.flatten()
.map(|rel| loaded.path_base.join(rel)),
candidate_bases: declared
.conditional_paths
.iter()
.map(|rel| loaded.path_base.join(rel))
.collect(),
conventional_compiles: !in_block,
lineage: loaded.lineage.with_in_block(
in_block && declared.direct_path.is_none() && declared.conditional_paths.is_empty(),
),
}
}
fn record_file_form(
module: &str,
loaded: &LoadedSource,
declared: &DeclaredModule,
child_sources: &mut ChildSources,
) -> Result<(), String> {
let child_lineage = loaded.lineage.under(declared.is_cfg_conditional);
let compiled_out = child_lineage.compiled_out;
let mut has_backing_conditional_target = false;
for rel in &declared.conditional_paths {
if xingbiao::is_regular_file(&loaded.path_base.join(rel))? {
has_backing_conditional_target = true;
child_sources.conditional.push(ConditionalPathSource {
relative: PathBuf::from(rel),
base: loaded.path_base.clone(),
declared_in: loaded.file.clone(),
lineage: child_lineage.clone(),
});
}
}
match &declared.direct_path {
Some(Some(rel)) => child_sources.direct.push(DirectPathSource {
relative: PathBuf::from(rel),
base: loaded.path_base.clone(),
declared_in: loaded.file.clone(),
lineage: child_lineage,
is_cfg_conditional: compiled_out
|| (declared.direct_path_is_conditional && has_backing_conditional_target),
}),
Some(None) => {}
None if declared.declared_in_block || loaded.lineage.in_block => {
if !has_backing_conditional_target && !compiled_out {
return Err(unbuildable_block_module(module, loaded, declared));
}
}
None => {
child_sources.seen_plain_file = true;
child_sources.plain.push(PlainSource {
base: loaded.child_base.clone(),
declared_in: loaded.file.clone(),
lineage: child_lineage,
is_cfg_conditional: compiled_out || has_backing_conditional_target,
});
}
}
Ok(())
}
fn unbuildable_block_module(
module: &str,
loaded: &LoadedSource,
declared: &DeclaredModule,
) -> String {
let child = format!("{module}::{}", declared.name);
if declared.conditional_paths.is_empty() {
format!(
"module `{child}`, declared in '{}', is a file-form `mod` inside a block with no path \
attribute, which rustc refuses: a module a block holds has no conventional file",
loaded.file.display()
)
} else {
format!(
"module `{child}`, declared in '{}', names no file that exists by any of its \
`cfg_attr` path attributes, and a file-form `mod` inside a block has no conventional \
file, so no configuration builds it",
loaded.file.display()
)
}
}
#[derive(Default)]
struct GraphSources {
by_module: BTreeMap<String, Vec<ScanSource>>,
remapped: Vec<(PathBuf, String)>,
remap_shadowed: BTreeSet<String>,
}
fn register_inline_sources(
child_path: &str,
bodies: Vec<InlineBody>,
graph: &mut GraphSources,
) -> Result<(), String> {
let sources = graph.by_module.entry(child_path.to_string()).or_default();
for body in bodies {
let conventional = body.base.join(&body.directory);
let bases: Vec<PathBuf> = match &body.relocated_base {
Some(base) if body.candidate_bases.is_empty() => vec![base.clone()],
Some(base) => {
let mut present: Vec<PathBuf> = Vec::new();
for candidate in std::iter::once(base).chain(&body.candidate_bases) {
if xingbiao::is_directory(candidate)? && !present.contains(candidate) {
present.push(candidate.clone());
}
}
if present.is_empty() {
vec![base.clone()]
} else {
present
}
}
None if body.candidate_bases.is_empty() => vec![conventional],
None => {
let mut present: Vec<PathBuf> = Vec::new();
for base in body
.candidate_bases
.iter()
.cloned()
.chain(body.conventional_compiles.then(|| conventional.clone()))
{
if xingbiao::is_directory(&base)? {
present.push(base);
}
}
present.sort();
present.dedup();
match (present.is_empty(), body.conventional_compiles) {
(false, _) => present,
(true, true) => vec![conventional],
(true, false) => body.candidate_bases.clone(),
}
}
};
for base in bases {
sources.push(ScanSource::Body {
file: body.file.clone(),
start: body.start,
end: body.end,
path_base: base.clone(),
child_base: base,
lineage: body.lineage.clone(),
});
}
}
Ok(())
}
fn resolve_plain_sources(
child: &str,
child_path: &str,
plain: Vec<PlainSource>,
src_dir: &Path,
files_literal: &HashSet<&PathBuf>,
root_relative: Option<&Path>,
graph: &mut GraphSources,
) -> Result<bool, String> {
let mut already_sourced = HashSet::new();
let mut any_structural_match = false;
let mut unlocated = Vec::new();
for plain_source in plain {
let PlainSource {
base,
declared_in,
lineage: source_lineage,
is_cfg_conditional,
} = plain_source;
let flat = base.join(format!("{child}.rs"));
let nested = base.join(child).join("mod.rs");
let flat_present = xingbiao::is_regular_file(&flat)?;
let nested_present = xingbiao::is_regular_file(&nested)?;
if flat_present && nested_present {
return Err(format!(
"module '{child_path}', declared in '{}', resolves to both '{}' and '{}' — a plain \
`mod {child}` must be backed by exactly one file",
declared_in.display(),
flat.display(),
nested.display()
));
}
if !flat_present && !nested_present {
if !is_cfg_conditional {
unlocated.push(format!(
"declared in '{}', expected '{}' or '{}'",
declared_in.display(),
flat.display(),
nested.display()
));
}
continue;
}
for (candidate, present) in [(flat, flat_present), (nested, nested_present)] {
if !present {
continue;
}
let canon = xingbiao::canonicalize_or_fail(&candidate)?;
if !already_sourced.insert(canon.clone()) {
continue;
}
if source_lineage.files.contains(&canon) {
return Err(format!(
"module '{child_path}', declared in '{}', resolves to '{}', which cycles back to an \
already-open source file",
declared_in.display(),
candidate.display()
));
}
let structurally_matches = files_literal.contains(&candidate)
&& candidate
.strip_prefix(src_dir)
.ok()
.is_some_and(|relative| module_path_of(relative, root_relative) == child_path);
if structurally_matches {
any_structural_match = true;
} else {
graph
.remapped
.push((candidate.clone(), child_path.to_string()));
}
let own_dir = candidate
.parent()
.map(Path::to_path_buf)
.unwrap_or_else(|| base.clone());
let new_child_base = base.join(child);
let mut lineage = source_lineage.clone();
lineage.files.insert(canon);
graph
.by_module
.entry(child_path.to_string())
.or_default()
.push(ScanSource::File {
file: candidate,
path_base: own_dir,
child_base: new_child_base,
lineage,
});
}
}
if !unlocated.is_empty() {
return Err(format!(
"module '{child_path}' is declared (`mod {child};`) but its source file could not be located ({})",
unlocated.join("; ")
));
}
let plain_file_resolved = !already_sourced.is_empty();
if plain_file_resolved && !any_structural_match {
graph.remap_shadowed.insert(child_path.to_string());
}
Ok(plain_file_resolved)
}
enum RemapKind {
Direct,
Conditional,
}
fn register_remapped_source(
child_path: &str,
target: PathBuf,
base: PathBuf,
declared_in: &Path,
target_lineage: Lineage,
kind: RemapKind,
graph: &mut GraphSources,
) -> Result<(), String> {
let canon = xingbiao::canonicalize_or_fail(&target)?;
if target_lineage.files.contains(&canon) {
let attribute = match kind {
RemapKind::Direct => "#[path]",
RemapKind::Conditional => "#[cfg_attr(..., path = ...)]",
};
return Err(format!(
"module '{child_path}', declared in '{}', is remapped by {attribute} to '{}', which cycles back to an \
already-open source file",
declared_in.display(),
target.display()
));
}
graph
.remapped
.push((target.clone(), child_path.to_string()));
let own_dir = target.parent().map(Path::to_path_buf).unwrap_or(base);
let mut lineage = target_lineage;
lineage.files.insert(canon);
graph
.by_module
.entry(child_path.to_string())
.or_default()
.push(ScanSource::File {
file: target,
path_base: own_dir.clone(),
child_base: own_dir,
lineage,
});
Ok(())
}
fn resolve_direct_paths(
child_path: &str,
seen_plain_file: bool,
direct: Vec<DirectPathSource>,
graph: &mut GraphSources,
) -> Result<(), String> {
if direct.is_empty() {
return Ok(());
}
if !seen_plain_file {
graph.remap_shadowed.insert(child_path.to_string());
}
for direct_source in direct {
let DirectPathSource {
relative,
base,
declared_in,
lineage: target_lineage,
is_cfg_conditional,
} = direct_source;
let target = base.join(&relative);
if !xingbiao::is_regular_file(&target)? {
if is_cfg_conditional {
continue;
}
return Err(format!(
"module '{child_path}', declared in '{}', is remapped by #[path = \"{}\"] to a file that does not \
exist: '{}'",
declared_in.display(),
relative.display(),
target.display()
));
}
register_remapped_source(
child_path,
target,
base,
&declared_in,
target_lineage,
RemapKind::Direct,
graph,
)?;
}
Ok(())
}
fn resolve_conditional_paths(
child_path: &str,
seen_plain_file: bool,
conditional: Vec<ConditionalPathSource>,
graph: &mut GraphSources,
) -> Result<(), String> {
if conditional.is_empty() {
return Ok(());
}
if !seen_plain_file {
graph.remap_shadowed.insert(child_path.to_string());
}
for conditional_source in conditional {
let ConditionalPathSource {
relative,
base,
declared_in,
lineage: target_lineage,
} = conditional_source;
let target = base.join(&relative);
if !xingbiao::is_regular_file(&target)? {
continue;
}
register_remapped_source(
child_path,
target,
base,
&declared_in,
target_lineage,
RemapKind::Conditional,
graph,
)?;
}
Ok(())
}
fn index_files_by_module<'a>(
files: &'a [PathBuf],
src_dir: &Path,
root_relative: Option<&Path>,
) -> std::collections::BTreeMap<String, Vec<&'a PathBuf>> {
let mut by_module: std::collections::BTreeMap<String, Vec<&PathBuf>> = Default::default();
for file in files {
if let Ok(relative) = file.strip_prefix(src_dir) {
by_module
.entry(module_path_of(relative, root_relative))
.or_default()
.push(file);
}
}
by_module
}
fn root_scan_sources(root_files: &[&PathBuf], src_dir: &Path) -> Result<Vec<ScanSource>, String> {
let mut root_lineage = Lineage::default();
for f in root_files {
root_lineage
.files
.insert(xingbiao::canonicalize_or_fail(f)?);
}
Ok(root_files
.iter()
.map(|f| ScanSource::File {
file: (*f).clone(),
path_base: src_dir.to_path_buf(),
child_base: src_dir.to_path_buf(),
lineage: root_lineage.clone(),
})
.collect())
}
#[allow(clippy::type_complexity)]
pub(crate) fn reachable_modules(
sources: &SourceTexts,
src_dir: &Path,
files: &[PathBuf],
root_relative: Option<&Path>,
edition: Edition,
) -> Result<
(
std::collections::BTreeSet<String>,
std::collections::BTreeMap<String, PathBuf>,
Vec<(PathBuf, String)>,
std::collections::BTreeSet<String>,
),
String,
> {
let by_module = index_files_by_module(files, src_dir, root_relative);
let files_literal: HashSet<&PathBuf> = files.iter().collect();
let mut reachable = std::collections::BTreeSet::new();
let mut inline_only = std::collections::BTreeMap::new();
let mut graph = GraphSources::default();
reachable.insert("crate".to_string());
if let Some(root_files) = by_module.get("crate") {
graph
.by_module
.insert("crate".to_string(), root_scan_sources(root_files, src_dir)?);
}
let texts: HashMap<&PathBuf, OnceCell<Rc<str>>> =
files.iter().map(|file| (file, OnceCell::new())).collect();
let mut trees = HashMap::new();
let mut queue = vec!["crate".to_string()];
while let Some(module) = queue.pop() {
let Some(scan_sources) = graph.by_module.get(&module).cloned() else {
continue;
};
let children =
collect_children(&module, &scan_sources, edition, sources, &texts, &mut trees)?;
for (child, child_sources) in children {
let ChildSources {
seen_plain_file,
bodies,
plain,
direct,
conditional,
} = child_sources;
let child_path = format!("{module}::{child}");
let inline_extraction_base = bodies.first().map(|b| b.base.clone());
if !bodies.is_empty() {
register_inline_sources(&child_path, bodies, &mut graph)?;
}
let plain_file_resolved = if seen_plain_file {
resolve_plain_sources(
&child,
&child_path,
plain,
src_dir,
&files_literal,
root_relative,
&mut graph,
)?
} else {
false
};
if let Some(base) = inline_extraction_base.filter(|_| !plain_file_resolved) {
inline_only.insert(child_path.clone(), base.join(format!("{child}.rs")));
}
resolve_direct_paths(&child_path, seen_plain_file, direct, &mut graph)?;
resolve_conditional_paths(&child_path, seen_plain_file, conditional, &mut graph)?;
if reachable.insert(child_path.clone()) {
queue.push(child_path);
}
}
}
Ok((reachable, inline_only, graph.remapped, graph.remap_shadowed))
}