use std::collections::{HashMap, HashSet};
use crate::binding::Recipe;
use crate::dependency::{Dependencies, ReadKind};
use crate::error::wiring::WiringError;
use crate::graph::wire::Declared;
use crate::graph::{
BindingId, Edge, EdgeTarget, Graph, ModuleId, Visible, VisibilityTable, boundary_key, dependency_label, record_key,
};
use crate::key::{BindingKind, Key};
use crate::transport::controller::EnhancerDep;
use crate::type_name::short_type_name;
pub(crate) fn build_tables(graph: &mut Graph, declared: &Declared) {
let count = graph.modules.len();
graph.visibility.resize_with(count, VisibilityTable::default);
graph.exported.resize_with(count, Vec::new);
for (module, export) in declared.exports.iter().filter(|(_, export)| !export.reexport) {
if let Some(id) = own_binding(graph, *module, export.key) {
let key = boundary_key(graph.module(*module).keyed, export.key);
graph.exported[module.0 as usize].push((key, id));
}
}
let mut pending: Vec<&(ModuleId, crate::module::def::ExportRecord)> =
declared.exports.iter().filter(|(_, export)| export.reexport).collect();
loop {
let view: &Graph = graph;
let mut resolved = Vec::new();
pending.retain(|entry| {
let (module, export) = &**entry;
match sources(view, *module, export.key).as_slice() {
[(_, id)] => {
resolved.push((*module, boundary_key(view.module(*module).keyed, export.key), *id));
false
}
_ => true,
}
});
if resolved.is_empty() {
break;
}
for (module, key, id) in resolved {
graph.exported[module.0 as usize].push((key, id));
}
}
for index in declared.first_module..count {
let table = table_for(graph, ModuleId(index as u32));
graph.visibility[index] = table;
}
}
pub(crate) fn own_binding(graph: &Graph, module: ModuleId, key: Key) -> Option<BindingId> {
graph.module(module).bindings.iter().copied().find(|&id| {
let record = &graph.binding(id).record;
record.kind == BindingKind::Single && record.keys().any(|own| own == key)
})
}
pub(crate) fn sources(graph: &Graph, module: ModuleId, key: Key) -> Vec<(ModuleId, BindingId)> {
let mut found: Vec<(ModuleId, BindingId)> = own_binding(graph, module, key).map(|id| (module, id)).into_iter().collect();
for exporter in exporters(graph, module) {
for &(exported, id) in &graph.exported[exporter.0 as usize] {
if exported == key && !found.iter().any(|&(_, seen)| seen == id) {
found.push((exporter, id));
}
}
}
found
}
fn exporters(graph: &Graph, module: ModuleId) -> impl Iterator<Item = ModuleId> + '_ {
let imports = graph.module(module).imports.iter().copied();
let globals = graph.modules.iter().filter(|m| m.global).map(|m| m.id);
imports.chain(globals)
}
fn table_for(graph: &Graph, module: ModuleId) -> VisibilityTable {
let mut sources: HashMap<Key, Vec<(ModuleId, BindingId)>> = HashMap::new();
for &id in &graph.module(module).bindings {
let record = &graph.binding(id).record;
if record.kind != BindingKind::Single {
continue;
}
for key in record.keys() {
sources.entry(key).or_insert_with(|| vec![(module, id)]);
}
}
for exporter in exporters(graph, module) {
for &(key, id) in &graph.exported[exporter.0 as usize] {
let list = sources.entry(key).or_default();
if !list.iter().any(|&(_, seen)| seen == id) {
list.push((exporter, id));
}
}
}
let mut entries: HashMap<Key, Visible> = sources
.into_iter()
.map(|(key, list)| {
let visible = if list.len() == 1 { Visible::Binding(list[0].1) } else { Visible::Ambiguous(list) };
(key, visible)
})
.collect();
for &key in graph.inputs.keys() {
entries.entry(key).or_insert(Visible::Input(key));
}
VisibilityTable { entries }
}
pub(crate) fn resolve_dependencies(graph: &mut Graph, declared: &Declared, errors: &mut Vec<WiringError>) {
let mut reported: HashSet<(ModuleId, Key)> = HashSet::new();
for index in declared.first_binding..graph.bindings.len() {
let edges = binding_edges(graph, BindingId(index as u32), errors, &mut reported);
graph.bindings[index].edges = edges;
}
let graph = &*graph;
for binding in graph.bindings.iter().skip(declared.first_binding) {
if let Some(ready) = &binding.record.ready {
let owner = format!("readiness check of {}", graph.label(binding.id));
check_dependencies(graph, binding.origin, &ready.dependencies, &owner, errors, &mut reported);
}
for hook in &binding.record.hooks {
let owner = format!("{} hook of {}", hook.kind, graph.label(binding.id));
check_dependencies(graph, binding.origin, &hook.dependencies, &owner, errors, &mut reported);
}
}
for module in graph.modules.iter().skip(declared.first_module) {
for hook in &module.hooks {
let owner = format!("{} hook of {}", hook.kind, module.name);
check_dependencies(graph, module.id, &hook.dependencies, &owner, errors, &mut reported);
}
}
for (module, name, dependencies) in &declared.meta {
let owner = format!("metadata `{}` of {}", short_type_name(*name), graph.module_name(*module));
check_dependencies(graph, *module, dependencies, &owner, errors, &mut reported);
}
for handler in graph.handlers.iter().skip(declared.first_handler) {
let name = graph.handler_name(handler);
let controller = record_key(&graph.binding(handler.controller).record);
if let Some(Visible::Ambiguous(list)) = graph.lookup(handler.module, controller) {
if reported.insert((handler.module, controller)) {
errors.push(WiringError::Ambiguous {
key: controller.name(BindingKind::Single),
consumer: format!("{name} (its controller)"),
module: graph.module_name(handler.module),
sources: ambiguous_sources(graph, list),
});
}
}
check_dependencies(graph, handler.module, &handler.decl.dependencies, &name, errors, &mut reported);
for dep in &handler.decl.enhancer_deps {
match dep {
EnhancerDep::Type(key) => {
let consumer = || format!("{name} (enhancer `{}`)", key.type_name());
check_key(graph, handler.module, *key, false, consumer, errors, &mut reported);
}
EnhancerDep::Closure(closure) => {
let owner = format!("{name} (enhancer closure)");
check_dependencies(graph, handler.module, &closure.dependencies, &owner, errors, &mut reported);
}
}
}
}
if declared.first_module == 0 {
let root = graph.root;
let mut unread: Vec<(&Key, &Vec<(ModuleId, BindingId)>)> = graph.visibility[root.0 as usize]
.entries
.iter()
.filter_map(|(key, visible)| match visible {
Visible::Ambiguous(list) if !reported.contains(&(root, *key)) => Some((key, list)),
_ => None,
})
.collect();
unread.sort_by_key(|(key, _)| (key.type_name().full(), key.qualifier_name()));
for (key, list) in unread {
errors.push(WiringError::Ambiguous {
key: key.name(BindingKind::Single),
consumer: "a lookup that names no module".to_owned(),
module: graph.module_name(root),
sources: ambiguous_sources(graph, list),
});
}
}
}
pub(crate) fn check_controller_reads(graph: &Graph, declared: &Declared, errors: &mut Vec<WiringError>) {
let controller_of = |module: ModuleId, key: Key| match graph.lookup(module, key) {
Some(Visible::Binding(id)) if graph.binding(*id).record.controller => Some(*id),
_ => None,
};
let report = |reader_module: ModuleId, controller: BindingId, consumer: String, errors: &mut Vec<WiringError>| {
errors.push(WiringError::ControllerRead {
controller: graph.key_name(controller),
consumer,
module: graph.module_name(reader_module),
controller_module: graph.module_name(graph.binding(controller).origin),
});
};
for reader in graph.bindings.iter().skip(declared.first_binding).filter(|binding| !binding.record.controller) {
for edge in &reader.edges {
if let EdgeTarget::Binding(id) = edge.target
&& graph.binding(id).record.controller
{
report(reader.origin, id, graph.consumer(reader.id, edge.dependency), errors);
}
}
if let Recipe::Alias { target } = &reader.record.recipe
&& let Some(id) = controller_of(reader.origin, *target)
{
report(reader.origin, id, format!("the alias `{}`", graph.key_name(reader.id)), errors);
}
let ready = reader.record.ready.iter().map(|ready| (format!("readiness check of {}", graph.label(reader.id)), &ready.dependencies));
let hooks =
reader.record.hooks.iter().map(|hook| (format!("{} hook of {}", hook.kind, graph.label(reader.id)), &hook.dependencies));
for (owner, dependencies) in ready.chain(hooks) {
for dependency in &dependencies.list {
for read in &dependency.requirement.reads {
if let ReadKind::Single(key) = &read.kind
&& let Some(id) = controller_of(reader.origin, *key)
{
report(reader.origin, id, format!("{owner} ({})", dependency_label(dependency.label)), errors);
}
}
}
}
}
let closure_reads = |module: ModuleId, owner: &str, dependencies: &Dependencies, errors: &mut Vec<WiringError>| {
for dependency in &dependencies.list {
for read in &dependency.requirement.reads {
if let ReadKind::Single(key) = &read.kind
&& let Some(id) = controller_of(module, *key)
{
report(module, id, format!("{owner} ({})", dependency_label(dependency.label)), errors);
}
}
}
};
for module in graph.modules.iter().skip(declared.first_module) {
for hook in &module.hooks {
closure_reads(module.id, &format!("{} hook of {}", hook.kind, module.name), &hook.dependencies, errors);
}
}
for handler in graph.handlers.iter().skip(declared.first_handler) {
for dep in &handler.decl.enhancer_deps {
if let EnhancerDep::Closure(closure) = dep {
let owner = format!("{} (enhancer closure)", graph.handler_name(handler));
closure_reads(handler.module, &owner, &closure.dependencies, errors);
}
}
}
}
fn binding_edges(
graph: &Graph,
id: BindingId,
errors: &mut Vec<WiringError>,
reported: &mut HashSet<(ModuleId, Key)>,
) -> Vec<Edge> {
let binding = graph.binding(id);
let mut edges = Vec::new();
for (index, dependency) in binding.record.dependencies.list.iter().enumerate() {
for read in &dependency.requirement.reads {
let target = match &read.kind {
ReadKind::Single(key) => {
let consumer = || graph.consumer(id, index);
check_key(graph, binding.origin, *key, read.optional, consumer, errors, reported)
}
ReadKind::Collection(key) => Some(EdgeTarget::Collection(*key)),
ReadKind::Extension(_) | ReadKind::Execution => Some(EdgeTarget::Execution),
ReadKind::Module => Some(EdgeTarget::Module),
};
if let Some(target) = target {
edges.push(Edge { target, dependency: index, optional: read.optional });
}
}
}
edges
}
fn check_dependencies(
graph: &Graph,
module: ModuleId,
dependencies: &Dependencies,
owner: &str,
errors: &mut Vec<WiringError>,
reported: &mut HashSet<(ModuleId, Key)>,
) {
for dependency in &dependencies.list {
for read in &dependency.requirement.reads {
if let ReadKind::Single(key) = &read.kind {
let consumer = || format!("{owner} ({})", dependency_label(dependency.label));
check_key(graph, module, *key, read.optional, consumer, errors, reported);
}
}
}
}
fn check_key(
graph: &Graph,
module: ModuleId,
key: Key,
optional: bool,
consumer: impl FnOnce() -> String,
errors: &mut Vec<WiringError>,
reported: &mut HashSet<(ModuleId, Key)>,
) -> Option<EdgeTarget> {
match graph.lookup(module, key) {
Some(Visible::Binding(id)) => Some(EdgeTarget::Binding(*id)),
Some(Visible::Input(input)) => Some(EdgeTarget::Input(*input)),
Some(Visible::Ambiguous(list)) => {
reported.insert((module, key));
errors.push(WiringError::Ambiguous {
key: key.name(BindingKind::Single),
consumer: consumer(),
module: graph.module_name(module),
sources: ambiguous_sources(graph, list),
});
None
}
None => {
if !optional {
errors.push(WiringError::Missing {
key: key.name(BindingKind::Single),
consumer: consumer(),
module: graph.module_name(module),
near: near_spelling(graph, module, key)
.map(|(near, exporter)| (near.name(BindingKind::Single), graph.module_name(exporter))),
});
}
None
}
}
}
fn ambiguous_sources(
graph: &Graph,
list: &[(ModuleId, BindingId)],
) -> Vec<(crate::module::ModuleName, &'static std::panic::Location<'static>)> {
list.iter().map(|&(module, id)| (graph.module_name(module), graph.binding(id).record.location)).collect()
}
pub(crate) fn near_spelling(graph: &Graph, module: ModuleId, missing: Key) -> Option<(Key, ModuleId)> {
let near = |key: &Key| spelled_alike(*key, missing);
let mut visible: Vec<(Key, ModuleId)> = graph.visibility[module.0 as usize]
.entries
.iter()
.filter(|(key, _)| near(*key))
.filter_map(|(key, visible)| match visible {
Visible::Binding(id) => Some((*key, graph.binding(*id).origin)),
Visible::Ambiguous(list) => list.first().map(|&(source, _)| (*key, source)),
Visible::Input(_) => None,
})
.collect();
visible.sort_by_key(|(key, _)| (!two_versions(*key, missing), key.type_name().full()));
if let Some(found) = visible.into_iter().next() {
return Some(found);
}
let anywhere = || graph.bindings.iter().flat_map(|binding| binding.record.keys().map(move |key| (key, binding.origin)));
anywhere()
.find(|(key, _)| near(key) && two_versions(*key, missing))
.or_else(|| anywhere().find(|(key, _)| near(key)))
}
pub(crate) fn own_near_spelling(graph: &Graph, module: ModuleId, exported: Key) -> Option<Key> {
graph.module(module).bindings.iter().find_map(|&id| {
let record = &graph.binding(id).record;
if record.kind != BindingKind::Single {
return None;
}
record.keys().find(|key| spelled_alike(*key, exported))
})
}
fn spelled_alike(key: Key, other: Key) -> bool {
key.qualifier_id() == other.qualifier_id()
&& (two_versions(key, other)
|| (key.type_name().full() != other.type_name().full()
&& short_type_name(strip_auto_traits(key.type_name().full()))
== short_type_name(strip_auto_traits(other.type_name().full()))))
}
pub(crate) fn two_versions(key: Key, other: Key) -> bool {
key.type_id() != other.type_id() && key.type_name().full() == other.type_name().full()
}
fn strip_auto_traits(name: &str) -> &str {
const SUFFIXES: [&str; 4] = [" + core::marker::Send", " + core::marker::Sync", " + Send", " + Sync"];
let mut name = name;
while let Some(rest) = SUFFIXES.iter().find_map(|suffix| name.strip_suffix(*suffix)) {
name = rest;
}
name
}