use std::collections::{HashMap, HashSet};
use std::panic::Location;
use std::sync::Arc;
use crate::binding::{BindingRecord, Qualifier, Recipe, instance_of};
use crate::dependency::{Dependencies, ReadKind};
use crate::error::LoadRefusal;
use crate::error::wiring::{BoundItem, InputOrigin, WiringError, WiringErrors};
use crate::graph::register::{Differing, Import, Registry, Site, later_repeats};
use crate::graph::{
BindingId, EdgeTarget, Effective, FrozenBinding, FrozenModule, Graph, InputDecl, ModuleId, Role, Visible,
boundary_key, cycles, order, record_key, register, scopes, visibility,
};
use crate::key::{BindingKind, Key, KeyName};
use crate::module::def::{ExportRecord, InputRecord, ModuleNode};
use crate::module::meta::FrozenMeta;
use crate::module::{Module, ModuleIdentity, ModuleName};
use crate::redact::redact;
use crate::scope::ScopeKind;
use crate::testing::{CollectionOverride, Override, OverrideTarget, Replacement, TestPlan};
use crate::runtime::Runtime;
use crate::timer::{Bound, Timer};
use crate::transport::controller::{EnhancerDep, HandlerDecl, HandlerRecord, Mount};
use crate::transport::inputs::Inputs;
use crate::type_name::TypeName;
pub(crate) struct WireEnv {
pub(crate) timer: Option<Arc<dyn Timer>>,
pub(crate) runtime: Option<Arc<dyn Runtime>>,
pub(crate) knobs_set: Vec<&'static str>,
pub(crate) zero_defaults: Vec<(&'static str, &'static Location<'static>)>,
}
pub(crate) fn wire(root: Box<dyn Module>, env: &WireEnv, plan: Option<TestPlan>) -> Result<Graph, WiringErrors> {
let mut registry = register::register_all(root, plan.as_ref());
if let Some(timer) = &env.timer {
add_timer_module(&mut registry, timer, env.runtime.as_ref());
}
let mut graph = Graph::empty();
let mut steps = Steps::default();
let declared = freeze(&mut graph, registry, None, plan, &mut steps);
graph.root = declared.root;
check(&mut graph, &declared, env, &mut steps);
let errors = steps.into_errors();
if !errors.is_empty() {
return Err(WiringErrors::new(errors));
}
graph.connect_order = order::connect_order(&graph);
Ok(graph)
}
pub(crate) fn wire_lazy(
base: &Graph,
module: Box<dyn Module>,
env: &WireEnv,
at: &'static Location<'static>,
) -> Result<LazyWiring, LazyFailure> {
if let Some(&existing) = base.by_identity.get(&module.identity()) {
if let Some(errors) = reload_differs(base, existing, &*module, at) {
return Err(LazyFailure::Wiring(errors));
}
return Ok(LazyWiring { graph: base.clone(), module: existing, singletons: Vec::new() });
}
let registry = register::register_lazy(base, module, at);
if let Some(refusal) = refusal(base, ®istry) {
return Err(LazyFailure::Refused(refusal));
}
let mut graph = base.clone();
let load = base.modules.iter().filter_map(|m| m.loaded).max().map_or(0, |last| last + 1);
let mut steps = Steps::default();
let declared = freeze(&mut graph, registry, Some(load), None, &mut steps);
check(&mut graph, &declared, env, &mut steps);
let errors = steps.into_errors();
if !errors.is_empty() {
return Err(LazyFailure::Wiring(WiringErrors::new(errors)));
}
graph.connect_order = order::connect_order(&graph);
let singletons =
graph.connect_order.iter().copied().filter(|id| id.0 as usize >= declared.first_binding).collect();
Ok(LazyWiring { graph, module: declared.root, singletons })
}
pub(crate) fn reload_differs(
base: &Graph,
existing: ModuleId,
module: &dyn Module,
at: &'static Location<'static>,
) -> Option<WiringErrors> {
let frozen = base.module(existing);
let settings = module.settings();
let mut errors: Vec<WiringError> =
Differing::of(Import::Existing(existing), (&frozen.settings, frozen.site), (&settings, Site::Load { at }))
.into_iter()
.map(|differing| differing.into_error(&[], base))
.collect();
let mut repeats = Vec::new();
later_repeats(&mut repeats, Import::Existing(existing), &frozen.settings, &settings);
errors.extend(repeats.into_iter().map(|(_, repeat)| WiringError::DuplicateSetting {
setting: repeat.name,
module: frozen.name.clone(),
first: repeat.first,
second: repeat.second,
}));
(!errors.is_empty()).then(|| WiringErrors::new(errors))
}
pub(crate) struct LazyWiring {
pub(crate) graph: Graph,
pub(crate) module: ModuleId,
pub(crate) singletons: Vec<BindingId>,
}
pub(crate) enum LazyFailure {
Wiring(WiringErrors),
Refused(LoadRefusal),
}
pub(crate) struct Declared {
pub(crate) root: ModuleId,
pub(crate) first_module: usize,
pub(crate) first_binding: usize,
pub(crate) first_handler: usize,
pub(crate) exports: Vec<(ModuleId, ExportRecord)>,
pub(crate) inputs: Vec<(ModuleId, InputRecord)>,
pub(crate) transport_inputs: Vec<InputDecl>,
pub(crate) meta: Vec<(ModuleId, &'static str, Dependencies)>,
}
#[derive(Default)]
struct Steps {
modules: Vec<WiringError>,
bindings: Vec<WiringError>,
dependencies: Vec<WiringError>,
cycles: Vec<WiringError>,
scopes: Vec<WiringError>,
environment: Vec<WiringError>,
}
impl Steps {
fn into_errors(self) -> Vec<WiringError> {
let mut errors = self.modules;
errors.extend(self.bindings);
errors.extend(self.dependencies);
errors.extend(self.cycles);
errors.extend(self.scopes);
errors.extend(self.environment);
errors
}
}
fn check(graph: &mut Graph, declared: &Declared, env: &WireEnv, steps: &mut Steps) {
visibility::build_tables(graph, declared);
check_modules(graph, declared, &mut steps.modules);
check_bindings(graph, declared, &mut steps.bindings);
visibility::resolve_dependencies(graph, declared, &mut steps.dependencies);
visibility::check_controller_reads(graph, declared, &mut steps.dependencies);
cycles::dependency_cycles(graph, &mut steps.cycles);
scopes::assign_roles(graph);
scopes::needs_execution(graph);
scopes::check_scopes(graph, &mut steps.scopes);
scopes::closure_scopes(graph, declared, &mut steps.scopes);
scopes::check_closures(graph, declared, &mut steps.scopes);
scopes::check_inputs(graph, &mut steps.scopes);
check_environment(graph, env, declared, &mut steps.environment);
}
struct TimerModule;
fn add_timer_module(registry: &mut Registry, timer: &Arc<dyn Timer>, runtime: Option<&Arc<dyn Runtime>>) {
let label = if runtime.is_some() { "AppBuilder::runtime" } else { "AppBuilder::timer" };
let identity = ModuleIdentity::of_type::<TimerModule>().label(label);
let mut node = ModuleNode::new(identity.clone());
node.global = true;
let location = Location::caller();
let key = Key::of::<dyn Timer, ()>();
node.bindings.push(BindingRecord::new(
key,
TypeName::of::<dyn Timer>(),
BindingKind::Single,
ScopeKind::Singleton,
Recipe::Value(instance_of::<dyn Timer>(Arc::clone(timer))),
Dependencies::default(),
location,
));
node.exports.push(ExportRecord { key, reexport: false, location });
if let Some(runtime) = runtime {
let key = Key::of::<dyn Runtime, ()>();
node.bindings.push(BindingRecord::new(
key,
TypeName::of::<dyn Runtime>(),
BindingKind::Single,
ScopeKind::Singleton,
Recipe::Value(instance_of::<dyn Runtime>(Arc::clone(runtime))),
Dependencies::default(),
location,
));
node.exports.push(ExportRecord { key, reexport: false, location });
}
let index = registry.nodes.len();
registry.nodes.push(node);
registry.by_identity.insert(identity, index);
registry.imports.push(Vec::new());
registry.post_order.insert(0, index);
}
fn freeze(
graph: &mut Graph,
mut registry: Registry,
load: Option<u32>,
plan: Option<TestPlan>,
steps: &mut Steps,
) -> Declared {
let names = registry.names(Some(&*graph));
cycles::self_imports(®istry, &names, &mut steps.modules);
for text in registry.nodes.iter().flat_map(|node| &node.secrets) {
if !text.is_empty() {
graph.secrets.register(text.clone());
}
}
for differing in std::mem::take(&mut registry.differing) {
steps.modules.push(differing.into_error(&names, graph));
}
for (module, repeat) in std::mem::take(&mut registry.repeats) {
let module = match module {
Import::New(node) => names[node].clone(),
Import::Existing(id) => graph.module(id).name.clone(),
};
steps.modules.push(WiringError::DuplicateSetting { setting: repeat.name, module, first: repeat.first, second: repeat.second });
}
for replaced in ®istry.replaced {
let Some(node) = registry.nodes.get(replaced.replacement) else { continue };
let offered: HashSet<Key> = node.exports.iter().map(|export| boundary_key(node.keyed, export.key)).collect();
let missing: Vec<KeyName> =
replaced.exports.iter().filter(|key| !offered.contains(*key)).map(|key| key.name(BindingKind::Single)).collect();
if !missing.is_empty() {
steps.bindings.push(WiringError::ReplacementMissingExports {
original: ModuleName::of(&replaced.original, 0),
replacement: names[replaced.replacement].clone(),
missing,
});
}
}
let replacements: &[Replacement] = plan.as_ref().map(|plan| plan.replacements.as_slice()).unwrap_or(&[]);
for (position, replacement) in replacements.iter().enumerate() {
if let Some(first) = replacements[..position].iter().find(|earlier| earlier.original == replacement.original) {
steps.bindings.push(WiringError::DuplicateReplacement {
original: ModuleName::of(&replacement.original, 0),
first: first.location,
second: replacement.location,
});
} else if !registry.replaced.iter().any(|replaced| replaced.original == replacement.original) {
steps.bindings.push(WiringError::ReplacementUnmatched {
original: ModuleName::of(&replacement.original, 0),
at: replacement.location,
});
}
}
let mut failures = Vec::new();
for (index, node) in registry.nodes.iter_mut().enumerate() {
let failed: Vec<usize> = node
.bindings
.iter()
.enumerate()
.filter(|(_, record)| matches!(record.recipe, Recipe::Failed))
.map(|(position, _)| position)
.collect();
for (position, failure) in failed.into_iter().zip(node.failures.drain(..)) {
failures.push((index, position, failure));
}
}
let (overrides, collections) = match plan {
Some(TestPlan { overrides, collections, .. }) => (overrides, collections),
None => (Vec::new(), Vec::new()),
};
apply_overrides(&mut registry, &names, overrides, &mut steps.bindings);
let replaced_collections: HashSet<Key> = collections.iter().map(|co| co.key).collect();
for (index, position, failure) in failures {
let Some(record) = registry.nodes[index].bindings.get(position) else { continue };
if !matches!(record.recipe, Recipe::Failed) {
continue;
}
if record.kind == BindingKind::Collection && replaced_collections.contains(&record_key(record)) {
continue;
}
steps.bindings.push(WiringError::ValueFailed {
module: names[index].clone(),
key: record_key(record).name(record.kind),
error: redact(&graph.secrets, failure.error),
at: failure.location,
});
}
apply_collections(&mut registry, collections, &mut steps.bindings);
let first_module = graph.modules.len();
let mut module_of = vec![ModuleId(first_module as u32); registry.nodes.len()];
for (position, &node) in registry.post_order.iter().enumerate() {
module_of[node] = ModuleId((first_module + position) as u32);
}
let mut declared = Declared {
root: module_of.first().copied().unwrap_or(ModuleId(first_module as u32)),
first_module,
first_binding: graph.bindings.len(),
first_handler: graph.handlers.len(),
exports: Vec::new(),
inputs: Vec::new(),
transport_inputs: Vec::new(),
meta: Vec::new(),
};
let mut contributions: HashMap<Key, Vec<BindingId>> = HashMap::new();
let mut transports_seen: HashSet<TypeName> = graph.handlers.iter().map(|handler| handler.decl.transport).collect();
let mut nodes: Vec<Option<ModuleNode>> = registry.nodes.into_iter().map(Some).collect();
for (position, &index) in registry.post_order.iter().enumerate() {
let Some(node) = nodes[index].take() else { continue };
let id = ModuleId((first_module + position) as u32);
let ModuleNode {
identity,
global,
keyed,
nested_keyed,
bindings,
enhancers,
exports,
controllers,
inputs,
hooks,
meta,
settings,
site,
..
} = node;
for repeat in settings.repeats() {
steps.modules.push(WiringError::DuplicateSetting {
setting: repeat.name,
module: names[index].clone(),
first: repeat.first,
second: repeat.second,
});
}
if let Some((outer, inner)) = nested_keyed {
steps.modules.push(WiringError::NestedKeyed {
module: names[index].clone(),
outer: TypeName::from_parts(outer.id, outer.name),
inner: TypeName::from_parts(inner.id, inner.name),
});
}
let mut binding_ids = Vec::with_capacity(bindings.len());
for (slot, record) in bindings.into_iter().enumerate() {
let binding = BindingId(graph.bindings.len() as u32);
if record.kind == BindingKind::Collection {
contributions.entry(record_key(&record)).or_default().push(binding);
}
let role = if record.controller {
Role::Controller
} else if enhancers.contains(&slot) {
Role::Enhancer
} else {
Role::Provider
};
graph.bindings.push(FrozenBinding {
id: binding,
origin: id,
record,
role,
effective: Effective::Singleton,
needs_execution: false,
edges: Vec::new(),
});
binding_ids.push(binding);
}
for controller in controllers {
let Some(&binding) = binding_ids.get(controller.binding) else { continue };
let controller_key = graph.key_name(binding);
let mut decls = Vec::new();
(controller.mount)(&mut Mount::new(&mut decls));
for decl in &decls {
declare_transport_inputs(graph, decl, &mut transports_seen, &mut declared.transport_inputs);
}
if let Some((prefix, at)) = &controller.prefix {
if !decls.iter().any(|decl| decl.reads_prefix) {
let mut transports: Vec<TypeName> = Vec::new();
for decl in &decls {
if !transports.contains(&decl.transport) {
transports.push(decl.transport);
}
}
steps.bindings.push(WiringError::UnreadPrefix {
controller: controller_key,
module: names[index].clone(),
prefix: prefix.to_string(),
transports,
at,
});
}
}
let prefix = controller.prefix.as_ref().map(|(prefix, _)| prefix.clone());
graph.handlers.extend(
decls.into_iter().map(|decl| HandlerRecord::new(binding, controller_key, id, prefix.clone(), decl)),
);
}
for input in inputs {
graph.inputs.entry(input.key).or_insert_with(|| InputDecl {
key: input.key,
seeders: vec![input.seeder],
origin: InputOrigin::Module {
module: names[index].clone(),
seeders: vec![input.seeder],
at: input.location,
},
});
declared.inputs.push((id, input));
}
let mut frozen_meta = FrozenMeta::new();
let mut entries: Vec<_> = meta.values.into_iter().collect();
entries.sort_by_key(|(_, entry)| entry.name);
for (ty, entry) in entries {
let mut dependencies = Dependencies::default();
(entry.dependencies)(&*entry.value, &mut dependencies);
declared.meta.push((id, entry.name, dependencies));
frozen_meta.insert(ty, Arc::from(entry.value));
}
declared.exports.extend(exports.into_iter().map(|export| (id, export)));
let imports: Vec<ModuleId> = registry.imports[index]
.iter()
.map(|import| match *import {
Import::New(node) => module_of[node],
Import::Existing(module) => module,
})
.collect();
graph.by_identity.insert(identity.clone(), id);
graph.modules.push(FrozenModule {
id,
identity,
name: names[index].clone(),
global,
keyed,
imports,
bindings: binding_ids,
hooks,
meta: frozen_meta,
loaded: load,
settings,
site: match site {
Site::Import { importer: Import::New(node), at } => Site::Import { importer: Import::Existing(module_of[node]), at },
other => other,
},
});
}
for (key, ids) in contributions {
let mut merged: Vec<BindingId> = graph.collections.get(&key).map(|existing| existing.to_vec()).unwrap_or_default();
merged.extend(ids);
graph.collections.insert(key, Arc::from(merged));
}
scopes::mark_role_contributions(graph, declared.first_binding, &mut steps.bindings);
declared
}
fn apply_overrides(registry: &mut Registry, names: &[ModuleName], overrides: Vec<Override>, errors: &mut Vec<WiringError>) {
let timer = Key::of::<dyn Timer, ()>();
let runtime = Key::of::<dyn Runtime, ()>();
for ov in overrides {
if ov.key == timer {
errors.push(WiringError::TimerOverride { at: ov.location });
continue;
}
if ov.key == runtime {
errors.push(WiringError::RuntimeOverride { at: ov.location });
continue;
}
let modules: Vec<usize> = match &ov.target {
OverrideTarget::Unscoped | OverrideTarget::Everywhere => registry.post_order.clone(),
OverrideTarget::ModuleType { ty, name } => match one_instance(registry, names, *ty, None, *name, &ov, errors) {
Some(module) => vec![module],
None => continue,
},
OverrideTarget::ModuleKeyed { ty, name, qualifier } => {
match one_instance(registry, names, *ty, Some(*qualifier), *name, &ov, errors) {
Some(module) => vec![module],
None => continue,
}
}
OverrideTarget::Module(identity) => match registry.by_identity.get(identity) {
Some(&module) => vec![module],
None => {
errors.push(WiringError::OverrideUnmatched {
key: ov.key.name(BindingKind::Single),
keyed: None,
at: ov.location,
});
continue;
}
},
};
let matches: Vec<(usize, usize)> = modules
.iter()
.flat_map(|&module| {
registry.nodes[module]
.bindings
.iter()
.enumerate()
.filter(|(_, record)| record.kind == BindingKind::Single && record.keys().any(|key| key == ov.key))
.map(move |(position, _)| (module, position))
})
.collect();
if matches.is_empty() {
let collection = modules.iter().any(|&module| {
registry.nodes[module]
.bindings
.iter()
.any(|record| record.kind == BindingKind::Collection && record_key(record) == ov.key)
});
errors.push(if collection {
WiringError::OverrideKind {
key: ov.key.name(BindingKind::Single),
expected: BindingKind::Collection,
found: BindingKind::Single,
at: ov.location,
}
} else {
WiringError::OverrideUnmatched {
key: ov.key.name(BindingKind::Single),
keyed: keyed_binder(registry, names, &modules, ov.key),
at: ov.location,
}
});
continue;
}
if matches.len() > 1 && matches!(ov.target, OverrideTarget::Unscoped) {
let mut matched: Vec<ModuleName> = matches.iter().map(|&(module, _)| names[module].clone()).collect();
matched.dedup();
errors.push(WiringError::OverrideAmbiguous { key: ov.key.name(BindingKind::Single), matches: matched, at: ov.location });
continue;
}
for (module, position) in matches {
replace_recipe(&mut registry.nodes[module], position, &ov);
}
}
}
fn one_instance(
registry: &Registry,
names: &[ModuleName],
ty: std::any::TypeId,
qualifier: Option<Qualifier>,
name: &'static str,
ov: &Override,
errors: &mut Vec<WiringError>,
) -> Option<usize> {
let found: Vec<usize> = registry
.post_order
.iter()
.copied()
.filter(|&node| {
let identity = ®istry.nodes[node].identity;
identity.type_id() == ty && (qualifier.is_none() || identity.qualifier() == qualifier)
})
.collect();
match found.as_slice() {
[one] => Some(*one),
[] => {
errors.push(WiringError::OverrideUnmatched { key: ov.key.name(BindingKind::Single), keyed: None, at: ov.location });
None
}
several => {
errors.push(WiringError::OverrideModuleAmbiguous {
module: name,
candidates: several.iter().map(|&node| names[node].clone()).collect(),
at: ov.location,
});
None
}
}
}
fn keyed_binder(registry: &Registry, names: &[ModuleName], modules: &[usize], key: Key) -> Option<ModuleName> {
if key.is_unqualified() {
return None;
}
let bare = unqualified(key);
modules
.iter()
.copied()
.find(|&module| {
let node = ®istry.nodes[module];
node.keyed.is_some_and(|q| q.id == key.qualifier_id())
&& node.bindings.iter().any(|record| record.kind == BindingKind::Single && record.keys().any(|k| k == bare))
})
.map(|module| names[module].clone())
}
fn replace_recipe(node: &mut ModuleNode, position: usize, ov: &Override) {
let Some(record) = node.bindings.get_mut(position) else { return };
if record_key(record) == ov.key {
record.recipe = ov.recipe.clone();
record.dependencies = ov.dependencies.clone();
return;
}
let qualifier = record.qualifier;
let scope = record.scope;
record.also.retain(|also| also.key.with_qualifier(qualifier.id, qualifier.name) != ov.key);
let mut split = BindingRecord::new(
unqualified(ov.key),
ov.key.type_name(),
BindingKind::Single,
scope,
ov.recipe.clone(),
ov.dependencies.clone(),
ov.location,
);
split.qualifier = qualifier;
node.bindings.push(split);
}
fn apply_collections(registry: &mut Registry, collections: Vec<CollectionOverride>, errors: &mut Vec<WiringError>) {
for co in collections {
let removed: usize = registry.nodes.iter_mut().map(|node| remove_contributions(node, co.key)).sum();
if removed == 0 {
let single = registry
.nodes
.iter()
.any(|node| node.bindings.iter().any(|r| r.kind == BindingKind::Single && r.keys().any(|key| key == co.key)));
errors.push(if single {
WiringError::OverrideKind {
key: co.key.name(BindingKind::Collection),
expected: BindingKind::Single,
found: BindingKind::Collection,
at: co.location,
}
} else {
WiringError::OverrideUnmatched { key: co.key.name(BindingKind::Collection), keyed: None, at: co.location }
});
continue;
}
let Some(root) = registry.nodes.first_mut() else { continue };
for item in co.items {
let mut record = BindingRecord::new(
unqualified(co.key),
co.key.type_name(),
BindingKind::Collection,
ScopeKind::Singleton,
Recipe::Value(item),
Dependencies::default(),
co.location,
);
record.qualifier = qualifier_of(co.key);
root.bindings.push(record);
}
}
}
fn remove_contributions(node: &mut ModuleNode, key: Key) -> usize {
let mut removed_before = Vec::with_capacity(node.bindings.len());
let mut gone = Vec::with_capacity(node.bindings.len());
let mut kept = Vec::with_capacity(node.bindings.len());
let mut removed = 0;
for record in node.bindings.drain(..) {
removed_before.push(removed);
let remove = record.kind == BindingKind::Collection && record_key(&record) == key;
gone.push(remove);
if remove {
removed += 1;
} else {
kept.push(record);
}
}
node.bindings = kept;
for controller in &mut node.controllers {
if let Some(&shift) = removed_before.get(controller.binding) {
controller.binding -= shift;
}
}
node.enhancers.retain(|&position| !gone.get(position).copied().unwrap_or(false));
for position in &mut node.enhancers {
if let Some(&shift) = removed_before.get(*position) {
*position -= shift;
}
}
removed
}
fn unqualified(key: Key) -> Key {
let none = Qualifier::none();
key.with_qualifier(none.id, none.name)
}
fn qualifier_of(key: Key) -> Qualifier {
let none = Qualifier::none();
Qualifier { id: key.qualifier_id(), name: key.qualifier_name().unwrap_or(none.name) }
}
fn declare_transport_inputs(graph: &mut Graph, decl: &HandlerDecl, seen: &mut HashSet<TypeName>, declared: &mut Vec<InputDecl>) {
if !seen.insert(decl.transport) {
return;
}
let mut inputs = Inputs::new();
(decl.inputs)(&mut inputs);
for input in inputs.keys {
let mut seeders = vec![decl.transport];
seeders.extend(input.also.iter().copied().filter(|&seeder| seeder != decl.transport));
let declaration = InputDecl {
key: input.key,
seeders,
origin: InputOrigin::Transport { name: decl.transport, at: input.location },
};
let entry = graph.inputs.entry(input.key).or_insert_with(|| declaration.clone());
let named = entry.seeders.iter().all(|seeder| declaration.seeders.contains(seeder));
if named && matches!(entry.origin, InputOrigin::Module { .. }) {
entry.seeders.clone_from(&declaration.seeders);
}
declared.push(declaration);
}
}
fn check_modules(graph: &Graph, declared: &Declared, errors: &mut Vec<WiringError>) {
for (module, export) in &declared.exports {
let key = export.key.name(BindingKind::Single);
if export.reexport {
match graph.lookup(*module, export.key) {
Some(Visible::Binding(_)) => {}
Some(Visible::Ambiguous(list)) => errors.push(WiringError::ReexportAmbiguous {
module: graph.module_name(*module),
key,
sources: list.iter().map(|&(source, _)| graph.module_name(source)).collect(),
at: export.location,
}),
Some(Visible::Input(_)) | None => {
errors.push(WiringError::ReexportNotVisible { module: graph.module_name(*module), key, at: export.location })
}
}
} else if visibility::own_binding(graph, *module, export.key).is_none() {
let imported = matches!(graph.lookup(*module, export.key), Some(Visible::Binding(_) | Visible::Ambiguous(_)));
errors.push(WiringError::ExportNotBound {
module: graph.module_name(*module),
key,
imported,
near: visibility::own_near_spelling(graph, *module, export.key).map(|near| near.name(BindingKind::Single)),
at: export.location,
});
}
}
for (module, input) in &declared.inputs {
if graph.module(*module).keyed.is_some() {
errors.push(WiringError::KeyedInput {
module: graph.module_name(*module),
key: input.key.name(BindingKind::Single),
at: input.location,
});
}
}
}
fn check_bindings(graph: &Graph, declared: &Declared, errors: &mut Vec<WiringError>) {
for module in graph.modules.iter().skip(declared.first_module) {
let mut first: HashMap<Key, BindingId> = HashMap::new();
let mut pairs: HashSet<(BindingId, BindingId)> = HashSet::new();
for &id in &module.bindings {
let record = &graph.binding(id).record;
if record.kind != BindingKind::Single {
continue;
}
for key in record.keys() {
match first.get(&key) {
Some(&earlier) if earlier != id => {
if pairs.insert((earlier, id)) {
errors.push(WiringError::DuplicateBinding {
key: key.name(BindingKind::Single),
module: module.name.clone(),
first: graph.binding(earlier).record.location,
second: record.location,
});
}
}
Some(_) => {}
None => {
first.insert(key, id);
}
}
}
}
}
let roles = scopes::role_types(graph, &graph.bindings);
let mut mixed: HashSet<Key> = HashSet::new();
for binding in &graph.bindings {
if binding.record.kind != BindingKind::Single {
continue;
}
for key in binding.record.keys() {
if roles.contains(&key.type_id()) {
continue;
}
let Some(contributions) = graph.collections.get(&key) else { continue };
let new = binding.id.0 as usize >= declared.first_binding
|| contributions.iter().any(|id| id.0 as usize >= declared.first_binding);
if new && mixed.insert(key) {
if let Some(&first) = contributions.first() {
errors.push(WiringError::KindMix {
key: key.name(BindingKind::Single),
module: graph.module_name(binding.origin),
single: binding.record.location,
collection: graph.binding(first).record.location,
});
}
}
}
}
let mut controllers: HashMap<std::any::TypeId, BindingId> = HashMap::new();
for binding in graph.bindings.iter().filter(|binding| binding.record.controller) {
controllers.entry(binding.record.built.id()).or_insert(binding.id);
}
for binding in &graph.bindings {
let record = &binding.record;
if record.controller || matches!(record.recipe, Recipe::Alias { .. }) {
continue;
}
let new = binding.id.0 as usize >= declared.first_binding;
let controller_at = match controllers.get(&record.built.id()) {
Some(&controller) if new || controller.0 as usize >= declared.first_binding => {
let controller = graph.binding(controller);
Some((graph.module_name(controller.origin), controller.record.location))
}
Some(_) => continue,
None if new && record.controller_type.is_some() => None,
None => continue,
};
errors.push(WiringError::ControllerProvided {
key: graph.key_name(binding.id),
controller: record.controller_type.unwrap_or(record.built),
module: graph.module_name(binding.origin),
at: record.location,
controller_at,
});
}
for binding in graph.bindings.iter().skip(declared.first_binding) {
let record = &binding.record;
if let Recipe::Alias { target } = &record.recipe {
match graph.lookup(binding.origin, *target) {
Some(Visible::Binding(_)) => {}
Some(Visible::Ambiguous(list)) => errors.push(WiringError::Ambiguous {
key: target.name(BindingKind::Single),
consumer: format!("the alias `{}`", graph.key_name(binding.id)),
module: graph.module_name(binding.origin),
sources: list
.iter()
.map(|&(source, id)| (graph.module_name(source), graph.binding(id).record.location))
.collect(),
}),
Some(Visible::Input(_)) | None => errors.push(WiringError::DanglingAlias {
alias: graph.key_name(binding.id),
target: target.name(BindingKind::Single),
module: graph.module_name(binding.origin),
at: record.location,
}),
}
}
if let Some(&first) = record.replaced_ready.first() {
let second = record.replaced_ready.get(1).copied().or_else(|| record.ready.as_ref().map(|ready| ready.location));
if let Some(second) = second {
errors.push(WiringError::DuplicateReadiness {
key: graph.key_name(binding.id),
module: graph.module_name(binding.origin),
first,
second,
});
}
}
}
let mut first_input: HashMap<Key, &'static Location<'static>> = HashMap::new();
for (module, input) in &declared.inputs {
let key = input.key.name(BindingKind::Single);
match first_input.get(&input.key) {
Some(&first) => errors.push(WiringError::DuplicateBinding {
key,
module: graph.module_name(*module),
first,
second: input.location,
}),
None => {
first_input.insert(input.key, input.location);
}
}
for binding in &graph.bindings {
if binding.record.kind == BindingKind::Single && binding.record.keys().any(|bound| bound == input.key) {
errors.push(WiringError::DuplicateBinding {
key,
module: graph.module_name(binding.origin),
first: input.location,
second: binding.record.location,
});
}
}
}
for (index, input) in declared.transport_inputs.iter().enumerate() {
let key = || input.key.name(BindingKind::Single);
for (module, other) in
declared.inputs.iter().filter(|(_, other)| other.key == input.key && !input.seeders.contains(&other.seeder))
{
let second = InputOrigin::Module {
module: graph.module_name(*module),
seeders: vec![other.seeder],
at: other.location,
};
errors.push(WiringError::InputConflict { key: key(), first: input.origin.clone(), second });
}
for other in
declared.transport_inputs[..index].iter().filter(|other| other.key == input.key && !other.same_seeders(&input.seeders))
{
errors.push(WiringError::InputConflict { key: key(), first: other.origin.clone(), second: input.origin.clone() });
}
let reported = declared.inputs.iter().any(|(_, other)| other.key == input.key)
|| declared.transport_inputs[..index].iter().any(|other| other.key == input.key);
if reported {
continue;
}
for binding in &graph.bindings {
if binding.record.kind == BindingKind::Single && binding.record.keys().any(|bound| bound == input.key) {
let second = InputOrigin::Binding { module: graph.module_name(binding.origin), at: binding.record.location };
errors.push(WiringError::InputConflict { key: key(), first: input.origin.clone(), second });
}
}
}
}
fn check_environment(graph: &Graph, env: &WireEnv, declared: &Declared, errors: &mut Vec<WiringError>) {
let timed = env.timer.is_some();
if !timed && declared.first_module == 0 {
errors.extend(env.knobs_set.iter().map(|&knob| WiringError::KnobWithoutTimer { knob }));
}
if declared.first_module == 0 {
errors.extend(env.zero_defaults.iter().map(|&(knob, at)| WiringError::ZeroDefault { knob, at }));
}
for binding in graph.bindings.iter().skip(declared.first_binding) {
let record = &binding.record;
let name = graph.label(binding.id);
let item = |item: BoundItem, bound: Bound, at: &'static Location<'static>, text: &dyn Fn() -> String| {
written_bound(bound, timed, || WiringError::ZeroBound {
item,
binding: Some(graph.key_name(binding.id)),
module: graph.module_name(binding.origin),
at,
}, || WiringError::BoundWithoutTimer { item: text(), at })
};
errors.extend(item(
BoundItem::Construction,
record.construct_bound,
record.construct_bound_at.unwrap_or(record.location),
&|| format!("construction of `{name}`"),
));
if let Some(ready) = &record.ready {
if !timed && !ready.backoff.is_zero() {
errors.push(WiringError::BackoffWithoutTimer {
binding: name.clone(),
at: ready.backoff_location.unwrap_or(ready.location),
});
}
errors.extend(item(
BoundItem::ReadinessWhole,
ready.whole,
ready.whole_at.unwrap_or(ready.location),
&|| format!("readiness `.timeout` of `{name}`"),
));
errors.extend(item(
BoundItem::ReadinessAttempt,
ready.attempt,
ready.attempt_at.unwrap_or(ready.location),
&|| format!("readiness `.attempt_timeout` of `{name}`"),
));
}
for hook in &record.hooks {
errors.extend(item(
BoundItem::Hook(hook.kind),
hook.bound,
hook.bound_at.unwrap_or(hook.location),
&|| format!("`{}` hook of `{name}`", hook.kind),
));
}
}
for module in graph.modules.iter().skip(declared.first_module) {
for hook in &module.hooks {
let at = hook.bound_at.unwrap_or(hook.location);
errors.extend(written_bound(
hook.bound,
timed,
|| WiringError::ZeroBound { item: BoundItem::Hook(hook.kind), binding: None, module: module.name.clone(), at },
|| WiringError::BoundWithoutTimer { item: format!("`{}` hook of module {}", hook.kind, module.name), at },
));
}
}
}
fn written_bound(
bound: Bound,
timed: bool,
zero: impl FnOnce() -> WiringError,
untimed: impl FnOnce() -> WiringError,
) -> Option<WiringError> {
match bound {
Bound::After(after) if after.is_zero() => Some(zero()),
Bound::After(_) if !timed => Some(untimed()),
Bound::After(_) | Bound::Default | Bound::Unbounded => None,
}
}
fn refusal(base: &Graph, registry: &Registry) -> Option<LoadRefusal> {
let names = registry.names(Some(base));
for &index in ®istry.post_order {
let node = ®istry.nodes[index];
let module = || names[index].clone();
if !node.controllers.is_empty() {
return Some(LoadRefusal::Controllers { module: module() });
}
if !node.meta.is_empty() {
return Some(LoadRefusal::Middleware { module: module() });
}
if let Some(input) = node.inputs.first() {
return Some(LoadRefusal::Input { module: module(), key: input.key.name(BindingKind::Single) });
}
if node.global {
if let Some(export) = node.exports.first() {
let key = boundary_key(node.keyed, export.key).name(BindingKind::Single);
return Some(LoadRefusal::GlobalExport { module: module(), key });
}
}
for record in node.bindings.iter().filter(|record| record.kind == BindingKind::Collection) {
let key = record_key(record);
if base.collections.contains_key(&key) || reads_collection(base, key) {
return Some(LoadRefusal::Contribution { module: module(), key: key.name(BindingKind::Collection) });
}
}
}
None
}
fn reads_collection(base: &Graph, key: Key) -> bool {
let in_dependencies = |dependencies: &Dependencies| {
dependencies.list.iter().flat_map(|d| &d.requirement.reads).any(|read| matches!(&read.kind, ReadKind::Collection(k) if *k == key))
};
base.bindings.iter().any(|binding| {
binding.edges.iter().any(|edge| matches!(&edge.target, EdgeTarget::Collection(k) if *k == key))
|| binding.record.ready.as_ref().is_some_and(|ready| in_dependencies(&ready.dependencies))
|| binding.record.hooks.iter().any(|hook| in_dependencies(&hook.dependencies))
}) || base.modules.iter().any(|module| module.hooks.iter().any(|hook| in_dependencies(&hook.dependencies)))
|| base.handlers.iter().any(|handler| {
handler.decl.role_keys.contains(&key)
|| in_dependencies(&handler.decl.dependencies)
|| handler.decl.enhancer_deps.iter().any(|dep| match dep {
EnhancerDep::Closure(closure) => in_dependencies(&closure.dependencies),
EnhancerDep::Type(_) => false,
})
})
}