From c01f9acf6a48b0ea4c32c3f3f1881d3b965a8db5 Mon Sep 17 00:00:00 2001 From: Grzegorz Nowak Date: Sat, 14 Nov 2026 14:53:30 +0000 Subject: [PATCH 1/5] fix: resolve local self-crate aliases to the target root Admit only exact extern crate self bindings as internal aliases and distinguish their crate-root meaning from ordinary self:: paths. Add eight bounded regressions without extending inherited or chained alias resolution. Keep regressions in a focused test-only module for a small resolver diff. This fork-only inspection PR has no tracking issue; no upstream publication or merge is requested. --- src/common/extraction/extract_dependencies.rs | 14 +- .../extraction/self_crate_alias_tests.rs | 139 ++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/common/extraction/self_crate_alias_tests.rs diff --git a/src/common/extraction/extract_dependencies.rs b/src/common/extraction/extract_dependencies.rs index d6df0a8..e934af0 100644 --- a/src/common/extraction/extract_dependencies.rs +++ b/src/common/extraction/extract_dependencies.rs @@ -208,7 +208,9 @@ fn collect_internal_aliases( ) -> AliasMap { let mut aliases = AliasMap::new(); for reference in references { - if !matches!( + let self_crate_alias = reference.kind == ImportKind::ExternCrate + && reference.segments == ["self"]; + if !self_crate_alias && !matches!( reference.kind, ImportKind::Use | ImportKind::PubUse | ImportKind::Mod ) { @@ -265,6 +267,12 @@ fn resolve_direct( candidates.insert(found); } } + // Unlike a `self::` path, `extern crate self` names this target's crate root. + "self" if reference.kind == ImportKind::ExternCrate => { + if let Some(found) = longest_prefix(index, &reference.module, &[], 0) { + candidates.insert(found); + } + } "self" => { let mut path = reference.module.segments.clone(); path.extend_from_slice(&reference.segments[1..]); @@ -372,6 +380,10 @@ enum ResolutionOutcome { Unresolved, } +#[cfg(test)] +#[path = "self_crate_alias_tests.rs"] +mod self_crate_alias_tests; + #[cfg(test)] mod tests { use std::collections::BTreeMap; diff --git a/src/common/extraction/self_crate_alias_tests.rs b/src/common/extraction/self_crate_alias_tests.rs new file mode 100644 index 0000000..09b932a --- /dev/null +++ b/src/common/extraction/self_crate_alias_tests.rs @@ -0,0 +1,139 @@ +//! Bounded extraction regressions: not a compiler-complete name resolver. +use std::collections::BTreeMap; + +use super::{AliasMap, ResolutionOutcome, collect_internal_aliases, resolve_reference}; +use crate::common::extraction::{ + ImportKind, + cargo_project::CargoDependencyScope, + dependency::{LogicalModule, RawReference}, +}; + +fn module(segments: &[&str]) -> LogicalModule { + LogicalModule { + package: "fixture".to_owned(), + dependency_scope: CargoDependencyScope::Normal, + target: "lib".to_owned(), + segments: segments.iter().map(ToString::to_string).collect(), + } +} + +fn index() -> BTreeMap { + [ + (module(&[]), "src/lib.rs"), + (module(&["consumer"]), "src/consumer.rs"), + (module(&["sibling"]), "src/sibling.rs"), + (module(&["api"]), "src/api.rs"), + (module(&["api", "model"]), "src/api/model.rs"), + (module(&["facade"]), "src/facade.rs"), + ] + .into_iter() + .map(|(module, source)| (module, source.to_owned())) + .collect() +} + +fn reference(current: &[&str], segments: &[&str], kind: ImportKind) -> RawReference { + RawReference { + source: "src/consumer.rs".to_owned(), + module: module(current), + segments: segments.iter().map(ToString::to_string).collect(), + leading_colon: false, + kind, + line: 1, + binding: None, + declaration: None, + ignored: false, + } +} + +fn alias(current: &[&str], binding: &str) -> RawReference { + let mut declaration = reference(current, &["self"], ImportKind::ExternCrate); + declaration.binding = Some(binding.to_owned()); + declaration +} + +fn assert_target(reference: &RawReference, aliases: &AliasMap, expected: &str) { + let ResolutionOutcome::Found(found) = resolve_reference(reference, &index(), aliases) else { + panic!("expected unique target {expected} for {}", reference.rendered_path()); + }; + assert_eq!(found.source, expected); +} + +#[test] +fn root_self_crate_alias_resolves_root_reexport() { + let aliases = collect_internal_aliases(&[alias(&[], "this_crate")], &index()); + assert_target( + &reference(&[], &["this_crate", "Risk"], ImportKind::PathReference), + &aliases, + "src/lib.rs", + ); +} + +#[test] +fn nested_self_crate_alias_and_its_declaration_resolve_crate_not_module() { + let declaration = alias(&["consumer"], "this_crate"); + assert_target(&declaration, &AliasMap::new(), "src/lib.rs"); + let aliases = collect_internal_aliases(&[declaration], &index()); + assert_target( + &reference(&["consumer"], &["this_crate", "Risk"], ImportKind::PathReference), + &aliases, + "src/lib.rs", + ); +} + +#[test] +fn self_crate_alias_keeps_existing_longest_module_prefix_semantics() { + let aliases = collect_internal_aliases(&[alias(&["consumer"], "this_crate")], &index()); + assert_target( + &reference(&["consumer"], &["this_crate", "api", "model", "User"], ImportKind::PathReference), + &aliases, + "src/api/model.rs", + ); +} + +#[test] +fn use_via_renamed_self_crate_alias_resolves_root() { + let aliases = collect_internal_aliases(&[alias(&["consumer"], "renamed_crate")], &index()); + let mut import = reference(&["consumer"], &["renamed_crate", "Risk"], ImportKind::Use); + import.binding = Some("RenamedRisk".to_owned()); + assert_target(&import, &aliases, "src/lib.rs"); +} + +#[test] +fn nested_explicit_self_crate_binding_shadows_bare_root_module() { + let aliases = collect_internal_aliases(&[alias(&["consumer"], "facade")], &index()); + assert_target( + &reference(&["consumer"], &["facade", "Risk"], ImportKind::PathReference), + &aliases, + "src/lib.rs", + ); +} + +#[test] +fn ordinary_self_path_still_means_current_module() { + assert_target( + &reference(&["consumer"], &["self", "Risk"], ImportKind::PathReference), + &AliasMap::new(), + "src/consumer.rs", + ); +} + +#[test] +fn other_extern_crates_do_not_become_internal_aliases() { + let mut declaration = reference(&["consumer"], &["api"], ImportKind::ExternCrate); + declaration.binding = Some("other_crate".to_owned()); + let aliases = collect_internal_aliases(&[declaration], &index()); + assert!(aliases.is_empty()); +} + +#[test] +fn nested_alias_does_not_leak_into_sibling_module() { + let aliases = collect_internal_aliases(&[alias(&["consumer"], "this_crate")], &index()); + assert!(matches!( + resolve_reference( + &reference(&["sibling"], &["this_crate", "Risk"], ImportKind::PathReference), + &index(), + &aliases, + ), + ResolutionOutcome::Unresolved + )); +} From 5cf22eccca72326e84b1ed3ea46d1c506e44073d Mon Sep 17 00:00:00 2001 From: Grzegorz Nowak Date: Sat, 14 Nov 2026 15:17:02 +0000 Subject: [PATCH 2/5] test: cover self-crate aliases through production extraction Add a compilable nested-alias fixture and public extraction assertions for the declaration, each qualified root reference, module suffix, diagnostics, and graph edges. Include competing lib/bin root and alias-isolation coverage. Validate the integration test against the unpatched resolver and an alias-disabled variant that retains declaration-root resolution, ensuring a declaration edge cannot hide missing qualified references. --- .../extraction/self_crate_alias_tests.rs | 50 ++++++++++++ .../self_crate_alias_project/.gitignore | 2 + .../self_crate_alias_project/Cargo.toml | 7 ++ .../self_crate_alias_project/src/api.rs | 1 + .../self_crate_alias_project/src/consumer.rs | 5 ++ .../self_crate_alias_project/src/lib.rs | 4 + tests/public_self_crate_alias_extraction.rs | 79 +++++++++++++++++++ 7 files changed, 148 insertions(+) create mode 100644 tests/fixtures/self_crate_alias_project/.gitignore create mode 100644 tests/fixtures/self_crate_alias_project/Cargo.toml create mode 100644 tests/fixtures/self_crate_alias_project/src/api.rs create mode 100644 tests/fixtures/self_crate_alias_project/src/consumer.rs create mode 100644 tests/fixtures/self_crate_alias_project/src/lib.rs create mode 100644 tests/public_self_crate_alias_extraction.rs diff --git a/src/common/extraction/self_crate_alias_tests.rs b/src/common/extraction/self_crate_alias_tests.rs index 09b932a..f28dd86 100644 --- a/src/common/extraction/self_crate_alias_tests.rs +++ b/src/common/extraction/self_crate_alias_tests.rs @@ -80,6 +80,56 @@ fn nested_self_crate_alias_and_its_declaration_resolve_crate_not_module() { ); } +#[test] +fn self_crate_aliases_respect_competing_library_and_binary_roots() { + let targets = [ + ("lib", "src/lib.rs", "src/consumer.rs"), + ("bin", "src/main.rs", "src/bin_consumer.rs"), + ]; + let mut index = BTreeMap::new(); + let mut declarations = Vec::new(); + for (target, root, consumer) in targets { + let mut root_module = module(&[]); + root_module.target = target.to_owned(); + index.insert(root_module, root.to_owned()); + + let mut declaration = alias(&["consumer"], "local_crate"); + declaration.module.target = target.to_owned(); + declaration.source = consumer.to_owned(); + index.insert(declaration.module.clone(), consumer.to_owned()); + declarations.push(declaration); + } + let mut library_only = declarations[0].clone(); + library_only.binding = Some("library_only".to_owned()); + let mut aliases = collect_internal_aliases(&declarations, &index); + aliases.extend(collect_internal_aliases(&[library_only], &index)); + + for (declaration, (target, root, _)) in declarations.iter().zip(targets) { + let mut qualified = declaration.clone(); + qualified.kind = ImportKind::PathReference; + qualified.binding = None; + qualified.segments = vec!["local_crate".to_owned(), "Item".to_owned()]; + for reference in [declaration, &qualified] { + let ResolutionOutcome::Found(found) = resolve_reference(reference, &index, &aliases) + else { + panic!( + "expected {} to resolve in target {target}", + reference.rendered_path(), + ); + }; + assert_eq!(found.source, root, "wrong root for target {target}"); + } + + qualified.segments[0] = "library_only".to_owned(); + let outcome = resolve_reference(&qualified, &index, &aliases); + if target == "lib" { + assert!(matches!(outcome, ResolutionOutcome::Found(found) if found.source == root)); + } else { + assert!(matches!(outcome, ResolutionOutcome::Unresolved)); + } + } +} + #[test] fn self_crate_alias_keeps_existing_longest_module_prefix_semantics() { let aliases = collect_internal_aliases(&[alias(&["consumer"], "this_crate")], &index()); diff --git a/tests/fixtures/self_crate_alias_project/.gitignore b/tests/fixtures/self_crate_alias_project/.gitignore new file mode 100644 index 0000000..80faede --- /dev/null +++ b/tests/fixtures/self_crate_alias_project/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +/target/ diff --git a/tests/fixtures/self_crate_alias_project/Cargo.toml b/tests/fixtures/self_crate_alias_project/Cargo.toml new file mode 100644 index 0000000..8632de8 --- /dev/null +++ b/tests/fixtures/self_crate_alias_project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "archunit-self-crate-alias-fixture" +version = "0.0.0" +edition = "2024" +publish = false + +[workspace] diff --git a/tests/fixtures/self_crate_alias_project/src/api.rs b/tests/fixtures/self_crate_alias_project/src/api.rs new file mode 100644 index 0000000..71f713e --- /dev/null +++ b/tests/fixtures/self_crate_alias_project/src/api.rs @@ -0,0 +1 @@ +pub struct Model; diff --git a/tests/fixtures/self_crate_alias_project/src/consumer.rs b/tests/fixtures/self_crate_alias_project/src/consumer.rs new file mode 100644 index 0000000..9bfee07 --- /dev/null +++ b/tests/fixtures/self_crate_alias_project/src/consumer.rs @@ -0,0 +1,5 @@ +extern crate self as local_crate; + +pub fn item(_model: local_crate::api::Model) -> local_crate::Item { + local_crate::Item +} diff --git a/tests/fixtures/self_crate_alias_project/src/lib.rs b/tests/fixtures/self_crate_alias_project/src/lib.rs new file mode 100644 index 0000000..9180289 --- /dev/null +++ b/tests/fixtures/self_crate_alias_project/src/lib.rs @@ -0,0 +1,4 @@ +pub mod api; +pub mod consumer; + +pub struct Item; diff --git a/tests/public_self_crate_alias_extraction.rs b/tests/public_self_crate_alias_extraction.rs new file mode 100644 index 0000000..4f98a78 --- /dev/null +++ b/tests/public_self_crate_alias_extraction.rs @@ -0,0 +1,79 @@ +use std::path::PathBuf; + +use archunit::{ + ImportKind, ProjectLocator, SourceOptions, extract_dependencies, extract_graph, + locate_project_from, +}; + +#[test] +fn nested_self_crate_alias_resolves_source_references_and_graph_edges() { + let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests/fixtures/self_crate_alias_project"); + let project = locate_project_from(&ProjectLocator::from_path(fixture)) + .expect("self-crate alias fixture should be discoverable"); + let dependencies = extract_dependencies(&project, SourceOptions::default()) + .expect("self-crate alias dependencies should extract"); + + for (path, target, kind, count) in [ + ("self", "src/lib.rs", ImportKind::ExternCrate, 1), + ("local_crate::Item", "src/lib.rs", ImportKind::PathReference, 2), + ( + "local_crate::api::Model", + "src/api.rs", + ImportKind::PathReference, + 1, + ), + ] { + let references = dependencies + .references() + .iter() + .filter(|reference| { + reference.source() == "src/consumer.rs" + && reference.referenced_path() == path + && reference.kind() == kind + }) + .collect::>(); + assert_eq!( + references.len(), + count, + "expected {count} {kind:?} references to {path}", + ); + for reference in references { + assert_eq!( + reference.internal_target(), + Some(target), + "{path} at line {} should resolve to {target}", + reference.line(), + ); + assert!(reference.line() > 0); + } + } + assert!( + dependencies.diagnostics().is_empty(), + "unexpected dependency diagnostics: {:?}", + dependencies.diagnostics(), + ); + + let graph = extract_graph(&project, SourceOptions::default()) + .expect("self-crate alias graph should extract"); + assert!( + graph.diagnostics().is_empty(), + "unexpected graph diagnostics: {:?}", + graph.diagnostics(), + ); + for (target, kinds) in [ + ( + "src/lib.rs", + vec![ImportKind::ExternCrate, ImportKind::PathReference], + ), + ("src/api.rs", vec![ImportKind::PathReference]), + ] { + let edge = graph + .graph() + .iter() + .find(|edge| edge.source == "src/consumer.rs" && edge.target == target) + .unwrap_or_else(|| panic!("expected consumer-to-{target} edge")); + assert!(!edge.external); + assert_eq!(edge.import_kinds.iter().collect::>(), kinds); + } +} From 16dfffe6616486570ac641e7bfcf5c596a7e7188 Mon Sep 17 00:00:00 2001 From: Grzegorz Nowak Date: Sat, 14 Nov 2026 15:38:52 +0000 Subject: [PATCH 3/5] test: inline self-crate alias regressions into existing suites Move the self-crate alias unit regressions into the inline extraction test module and the source-backed regression into the existing graph extraction suite, so the fix reuses established repository test mechanisms. Fold the alias-resolution and binding-precedence cases into the existing tables, drop the nested-only and ordinary-`self::` duplicates now covered through real source, and drop a vacuous line assertion plus a diagnostics check that `extract_graph` copies by construction. Coverage is unchanged: declaration and alias root resolution, competing lib/bin roots with alias isolation, module-suffix resolution, import via a renamed alias, root-module shadowing, non-self extern exclusion, and sibling isolation. The graph regression fails when alias collection is disabled while declaration-root resolution stays. Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, cargo test --workspace --all-features (326 unit, 148 integration, 55 doctests). --- src/common/extraction/extract_dependencies.rs | 228 ++++++++++++++---- .../extraction/self_crate_alias_tests.rs | 189 --------------- tests/public_graph_extraction.rs | 78 +++++- tests/public_self_crate_alias_extraction.rs | 79 ------ 4 files changed, 258 insertions(+), 316 deletions(-) delete mode 100644 src/common/extraction/self_crate_alias_tests.rs delete mode 100644 tests/public_self_crate_alias_extraction.rs diff --git a/src/common/extraction/extract_dependencies.rs b/src/common/extraction/extract_dependencies.rs index e934af0..2886ede 100644 --- a/src/common/extraction/extract_dependencies.rs +++ b/src/common/extraction/extract_dependencies.rs @@ -208,12 +208,14 @@ fn collect_internal_aliases( ) -> AliasMap { let mut aliases = AliasMap::new(); for reference in references { - let self_crate_alias = reference.kind == ImportKind::ExternCrate - && reference.segments == ["self"]; - if !self_crate_alias && !matches!( - reference.kind, - ImportKind::Use | ImportKind::PubUse | ImportKind::Mod - ) { + let self_crate_alias = + reference.kind == ImportKind::ExternCrate && reference.segments == ["self"]; + if !self_crate_alias + && !matches!( + reference.kind, + ImportKind::Use | ImportKind::PubUse | ImportKind::Mod + ) + { continue; } let Some(binding) = &reference.binding else { @@ -380,10 +382,6 @@ enum ResolutionOutcome { Unresolved, } -#[cfg(test)] -#[path = "self_crate_alias_tests.rs"] -mod self_crate_alias_tests; - #[cfg(test)] mod tests { use std::collections::BTreeMap; @@ -428,12 +426,31 @@ mod tests { module("lib", &["consumer", "local"]), "src/consumer/local.rs", ), + (module("lib", &["sibling"]), "src/sibling.rs"), ] .into_iter() .map(|(module, source)| (module, source.to_owned())) .collect() } + fn alias(current: LogicalModule, binding: &str) -> RawReference { + let mut declaration = reference(current, &["self"]); + declaration.kind = ImportKind::ExternCrate; + declaration.binding = Some(binding.to_owned()); + declaration + } + + fn assert_alias_target(reference: &RawReference, aliases: &AliasMap, expected: &str) { + let ResolutionOutcome::Found(found) = resolve_reference(reference, &index(), aliases) + else { + panic!( + "expected {expected} for {}, got no unique target", + reference.rendered_path() + ); + }; + assert_eq!(found.source, expected); + } + #[test] fn resolves_explicit_roots_and_longest_internal_prefix() { let reference = reference( @@ -467,52 +484,169 @@ mod tests { } #[test] - fn resolves_paths_through_internal_use_aliases() { + fn resolves_paths_through_internal_aliases() { let current = module("lib", &["consumer"]); - let import = RawReference { - source: "src/consumer.rs".to_owned(), - module: current.clone(), - segments: vec!["crate".to_owned(), "api".to_owned()], - leading_colon: false, - kind: ImportKind::Use, - line: 1, - binding: Some("public_api".to_owned()), - declaration: None, - ignored: false, - }; - let aliases = collect_internal_aliases(&[import], &index()); - let reference = reference(current, &["public_api", "model", "User"]); + let mut crate_api = reference(current.clone(), &["crate", "api"]); + crate_api.kind = ImportKind::Use; + crate_api.binding = Some("public_api".to_owned()); - let ResolutionOutcome::Found(found) = resolve_reference(&reference, &index(), &aliases) - else { - panic!("fixture alias should resolve uniquely"); - }; + for (declaration, binding, segments, expected) in [ + ( + crate_api, + "public_api", + &["public_api", "model", "User"][..], + "src/api/model.rs", + ), + ( + alias(current.clone(), "this_crate"), + "this_crate", + &["this_crate", "api", "model", "User"][..], + "src/api/model.rs", + ), + ] { + let aliases = collect_internal_aliases(&[declaration], &index()); + let reference = reference(current.clone(), segments); - assert_eq!(found.source, "src/api/model.rs"); + let ResolutionOutcome::Found(found) = resolve_reference(&reference, &index(), &aliases) + else { + panic!("fixture alias {binding} should resolve uniquely"); + }; + + assert_eq!(found.source, expected, "wrong target for alias {binding}"); + } } #[test] fn explicit_bindings_take_precedence_over_bare_module_candidates() { let current = module("lib", &["consumer"]); - let import = RawReference { - source: "src/consumer.rs".to_owned(), - module: current.clone(), - segments: vec!["crate".to_owned(), "api".to_owned()], - leading_colon: false, - kind: ImportKind::Use, - line: 1, - binding: Some("local".to_owned()), - declaration: None, - ignored: false, - }; - let aliases = collect_internal_aliases(&[import], &index()); - let reference = reference(current, &["local", "Thing"]); + let mut crate_api = reference(current.clone(), &["crate", "api"]); + crate_api.kind = ImportKind::Use; + crate_api.binding = Some("local".to_owned()); - let ResolutionOutcome::Found(found) = resolve_reference(&reference, &index(), &aliases) - else { - panic!("the explicit local binding should win"); - }; + for (declaration, binding, segments, expected) in [ + (crate_api, "local", &["local", "Thing"][..], "src/api.rs"), + ( + alias(current.clone(), "api"), + "api", + &["api", "Risk"][..], + "src/lib.rs", + ), + ] { + let aliases = collect_internal_aliases(&[declaration], &index()); + let reference = reference(current.clone(), segments); + + let ResolutionOutcome::Found(found) = resolve_reference(&reference, &index(), &aliases) + else { + panic!("the explicit {binding} binding should win"); + }; + + assert_eq!(found.source, expected, "wrong precedence for {binding}"); + } + } + + #[test] + fn root_self_crate_alias_resolves_root_reexport() { + let root = module("lib", &[]); + let aliases = collect_internal_aliases(&[alias(root.clone(), "this_crate")], &index()); + + assert_alias_target( + &reference(root, &["this_crate", "Risk"]), + &aliases, + "src/lib.rs", + ); + } + + #[test] + fn self_crate_aliases_respect_competing_library_and_binary_roots() { + let targets = [ + ("lib", "src/lib.rs", "src/consumer.rs"), + ("bin", "src/main.rs", "src/bin_consumer.rs"), + ]; + let mut index = BTreeMap::new(); + let mut declarations = Vec::new(); + for (target, root, consumer) in targets { + index.insert(module(target, &[]), root.to_owned()); + + let mut declaration = alias(module(target, &["consumer"]), "local_crate"); + declaration.source = consumer.to_owned(); + index.insert(declaration.module.clone(), consumer.to_owned()); + declarations.push(declaration); + } + let mut library_only = declarations[0].clone(); + library_only.binding = Some("library_only".to_owned()); + let mut aliases = collect_internal_aliases(&declarations, &index); + aliases.extend(collect_internal_aliases(&[library_only], &index)); + + for (declaration, (target, root, _)) in declarations.iter().zip(targets) { + let mut qualified = declaration.clone(); + qualified.kind = ImportKind::PathReference; + qualified.binding = None; + qualified.segments = vec!["local_crate".to_owned(), "Item".to_owned()]; + for reference in [declaration, &qualified] { + let ResolutionOutcome::Found(found) = + resolve_reference(reference, &index, &aliases) + else { + panic!( + "expected {} to resolve in target {target}", + reference.rendered_path(), + ); + }; + assert_eq!(found.source, root, "wrong root for target {target}"); + } + + qualified.segments[0] = "library_only".to_owned(); + let outcome = resolve_reference(&qualified, &index, &aliases); + if target == "lib" { + assert!( + matches!(outcome, ResolutionOutcome::Found(found) if found.source == root), + "the library-only alias should resolve inside lib", + ); + } else { + assert!( + matches!(outcome, ResolutionOutcome::Unresolved), + "a library-only alias must not leak into the bin target", + ); + } + } + } + + #[test] + fn use_via_renamed_self_crate_alias_resolves_root() { + let current = module("lib", &["consumer"]); + let aliases = + collect_internal_aliases(&[alias(current.clone(), "renamed_crate")], &index()); + let mut import = reference(current, &["renamed_crate", "Risk"]); + import.kind = ImportKind::Use; + import.binding = Some("RenamedRisk".to_owned()); + + assert_alias_target(&import, &aliases, "src/lib.rs"); + } + + #[test] + fn other_extern_crates_do_not_become_internal_aliases() { + let mut declaration = reference(module("lib", &["consumer"]), &["api"]); + declaration.kind = ImportKind::ExternCrate; + declaration.binding = Some("other_crate".to_owned()); - assert_eq!(found.source, "src/api.rs"); + let aliases = collect_internal_aliases(&[declaration], &index()); + + assert!(aliases.is_empty()); + } + + #[test] + fn nested_alias_does_not_leak_into_sibling_module() { + let aliases = collect_internal_aliases( + &[alias(module("lib", &["consumer"]), "this_crate")], + &index(), + ); + + assert!(matches!( + resolve_reference( + &reference(module("lib", &["sibling"]), &["this_crate", "Risk"]), + &index(), + &aliases, + ), + ResolutionOutcome::Unresolved + )); } } diff --git a/src/common/extraction/self_crate_alias_tests.rs b/src/common/extraction/self_crate_alias_tests.rs deleted file mode 100644 index f28dd86..0000000 --- a/src/common/extraction/self_crate_alias_tests.rs +++ /dev/null @@ -1,189 +0,0 @@ -//! Bounded extraction regressions: not a compiler-complete name resolver. -use std::collections::BTreeMap; - -use super::{AliasMap, ResolutionOutcome, collect_internal_aliases, resolve_reference}; -use crate::common::extraction::{ - ImportKind, - cargo_project::CargoDependencyScope, - dependency::{LogicalModule, RawReference}, -}; - -fn module(segments: &[&str]) -> LogicalModule { - LogicalModule { - package: "fixture".to_owned(), - dependency_scope: CargoDependencyScope::Normal, - target: "lib".to_owned(), - segments: segments.iter().map(ToString::to_string).collect(), - } -} - -fn index() -> BTreeMap { - [ - (module(&[]), "src/lib.rs"), - (module(&["consumer"]), "src/consumer.rs"), - (module(&["sibling"]), "src/sibling.rs"), - (module(&["api"]), "src/api.rs"), - (module(&["api", "model"]), "src/api/model.rs"), - (module(&["facade"]), "src/facade.rs"), - ] - .into_iter() - .map(|(module, source)| (module, source.to_owned())) - .collect() -} - -fn reference(current: &[&str], segments: &[&str], kind: ImportKind) -> RawReference { - RawReference { - source: "src/consumer.rs".to_owned(), - module: module(current), - segments: segments.iter().map(ToString::to_string).collect(), - leading_colon: false, - kind, - line: 1, - binding: None, - declaration: None, - ignored: false, - } -} - -fn alias(current: &[&str], binding: &str) -> RawReference { - let mut declaration = reference(current, &["self"], ImportKind::ExternCrate); - declaration.binding = Some(binding.to_owned()); - declaration -} - -fn assert_target(reference: &RawReference, aliases: &AliasMap, expected: &str) { - let ResolutionOutcome::Found(found) = resolve_reference(reference, &index(), aliases) else { - panic!("expected unique target {expected} for {}", reference.rendered_path()); - }; - assert_eq!(found.source, expected); -} - -#[test] -fn root_self_crate_alias_resolves_root_reexport() { - let aliases = collect_internal_aliases(&[alias(&[], "this_crate")], &index()); - assert_target( - &reference(&[], &["this_crate", "Risk"], ImportKind::PathReference), - &aliases, - "src/lib.rs", - ); -} - -#[test] -fn nested_self_crate_alias_and_its_declaration_resolve_crate_not_module() { - let declaration = alias(&["consumer"], "this_crate"); - assert_target(&declaration, &AliasMap::new(), "src/lib.rs"); - let aliases = collect_internal_aliases(&[declaration], &index()); - assert_target( - &reference(&["consumer"], &["this_crate", "Risk"], ImportKind::PathReference), - &aliases, - "src/lib.rs", - ); -} - -#[test] -fn self_crate_aliases_respect_competing_library_and_binary_roots() { - let targets = [ - ("lib", "src/lib.rs", "src/consumer.rs"), - ("bin", "src/main.rs", "src/bin_consumer.rs"), - ]; - let mut index = BTreeMap::new(); - let mut declarations = Vec::new(); - for (target, root, consumer) in targets { - let mut root_module = module(&[]); - root_module.target = target.to_owned(); - index.insert(root_module, root.to_owned()); - - let mut declaration = alias(&["consumer"], "local_crate"); - declaration.module.target = target.to_owned(); - declaration.source = consumer.to_owned(); - index.insert(declaration.module.clone(), consumer.to_owned()); - declarations.push(declaration); - } - let mut library_only = declarations[0].clone(); - library_only.binding = Some("library_only".to_owned()); - let mut aliases = collect_internal_aliases(&declarations, &index); - aliases.extend(collect_internal_aliases(&[library_only], &index)); - - for (declaration, (target, root, _)) in declarations.iter().zip(targets) { - let mut qualified = declaration.clone(); - qualified.kind = ImportKind::PathReference; - qualified.binding = None; - qualified.segments = vec!["local_crate".to_owned(), "Item".to_owned()]; - for reference in [declaration, &qualified] { - let ResolutionOutcome::Found(found) = resolve_reference(reference, &index, &aliases) - else { - panic!( - "expected {} to resolve in target {target}", - reference.rendered_path(), - ); - }; - assert_eq!(found.source, root, "wrong root for target {target}"); - } - - qualified.segments[0] = "library_only".to_owned(); - let outcome = resolve_reference(&qualified, &index, &aliases); - if target == "lib" { - assert!(matches!(outcome, ResolutionOutcome::Found(found) if found.source == root)); - } else { - assert!(matches!(outcome, ResolutionOutcome::Unresolved)); - } - } -} - -#[test] -fn self_crate_alias_keeps_existing_longest_module_prefix_semantics() { - let aliases = collect_internal_aliases(&[alias(&["consumer"], "this_crate")], &index()); - assert_target( - &reference(&["consumer"], &["this_crate", "api", "model", "User"], ImportKind::PathReference), - &aliases, - "src/api/model.rs", - ); -} - -#[test] -fn use_via_renamed_self_crate_alias_resolves_root() { - let aliases = collect_internal_aliases(&[alias(&["consumer"], "renamed_crate")], &index()); - let mut import = reference(&["consumer"], &["renamed_crate", "Risk"], ImportKind::Use); - import.binding = Some("RenamedRisk".to_owned()); - assert_target(&import, &aliases, "src/lib.rs"); -} - -#[test] -fn nested_explicit_self_crate_binding_shadows_bare_root_module() { - let aliases = collect_internal_aliases(&[alias(&["consumer"], "facade")], &index()); - assert_target( - &reference(&["consumer"], &["facade", "Risk"], ImportKind::PathReference), - &aliases, - "src/lib.rs", - ); -} - -#[test] -fn ordinary_self_path_still_means_current_module() { - assert_target( - &reference(&["consumer"], &["self", "Risk"], ImportKind::PathReference), - &AliasMap::new(), - "src/consumer.rs", - ); -} - -#[test] -fn other_extern_crates_do_not_become_internal_aliases() { - let mut declaration = reference(&["consumer"], &["api"], ImportKind::ExternCrate); - declaration.binding = Some("other_crate".to_owned()); - let aliases = collect_internal_aliases(&[declaration], &index()); - assert!(aliases.is_empty()); -} - -#[test] -fn nested_alias_does_not_leak_into_sibling_module() { - let aliases = collect_internal_aliases(&[alias(&["consumer"], "this_crate")], &index()); - assert!(matches!( - resolve_reference( - &reference(&["sibling"], &["this_crate", "Risk"], ImportKind::PathReference), - &index(), - &aliases, - ), - ResolutionOutcome::Unresolved - )); -} diff --git a/tests/public_graph_extraction.rs b/tests/public_graph_extraction.rs index 7ec4fbf..00a595c 100644 --- a/tests/public_graph_extraction.rs +++ b/tests/public_graph_extraction.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeSet, path::PathBuf}; use archunit::{ Edge, ExtractionDiagnosticKind, ImportKind, ProjectLocator, SourceOptions, - enumerate_source_files, extract_graph, locate_project_from, + enumerate_source_files, extract_dependencies, extract_graph, locate_project_from, }; fn fixture_project() -> archunit::CargoProject { @@ -12,6 +12,13 @@ fn fixture_project() -> archunit::CargoProject { .expect("fixture workspace should be discoverable") } +fn self_crate_alias_project() -> archunit::CargoProject { + let fixture = + PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/self_crate_alias_project"); + locate_project_from(&ProjectLocator::from_path(fixture)) + .expect("self-crate alias fixture should be discoverable") +} + #[test] fn every_discovered_file_has_exactly_one_canonical_self_edge() { let project = fixture_project(); @@ -135,3 +142,72 @@ fn development_sources_receive_self_edges_only_when_selected() { + 3 ); } + +#[test] +fn nested_self_crate_alias_resolves_source_references_and_graph_edges() { + let project = self_crate_alias_project(); + let dependencies = extract_dependencies(&project, SourceOptions::default()) + .expect("self-crate alias dependencies should extract"); + + for (path, target, kind, count) in [ + ("self", "src/lib.rs", ImportKind::ExternCrate, 1), + ( + "local_crate::Item", + "src/lib.rs", + ImportKind::PathReference, + 2, + ), + ( + "local_crate::api::Model", + "src/api.rs", + ImportKind::PathReference, + 1, + ), + ] { + let references = dependencies + .references() + .iter() + .filter(|reference| { + reference.source() == "src/consumer.rs" + && reference.referenced_path() == path + && reference.kind() == kind + }) + .collect::>(); + assert_eq!( + references.len(), + count, + "expected {count} {kind:?} references to {path}", + ); + for reference in references { + assert_eq!( + reference.internal_target(), + Some(target), + "{path} at line {} should resolve to {target}", + reference.line(), + ); + } + } + + let graph = extract_graph(&project, SourceOptions::default()) + .expect("self-crate alias graph should extract"); + assert!( + graph.diagnostics().is_empty(), + "unexpected extraction diagnostics: {:?}", + graph.diagnostics(), + ); + for (target, kinds) in [ + ( + "src/lib.rs", + vec![ImportKind::ExternCrate, ImportKind::PathReference], + ), + ("src/api.rs", vec![ImportKind::PathReference]), + ] { + let edge = graph + .graph() + .iter() + .find(|edge| edge.source == "src/consumer.rs" && edge.target == target) + .unwrap_or_else(|| panic!("expected consumer-to-{target} edge")); + assert!(!edge.external); + assert_eq!(edge.import_kinds.iter().collect::>(), kinds); + } +} diff --git a/tests/public_self_crate_alias_extraction.rs b/tests/public_self_crate_alias_extraction.rs deleted file mode 100644 index 4f98a78..0000000 --- a/tests/public_self_crate_alias_extraction.rs +++ /dev/null @@ -1,79 +0,0 @@ -use std::path::PathBuf; - -use archunit::{ - ImportKind, ProjectLocator, SourceOptions, extract_dependencies, extract_graph, - locate_project_from, -}; - -#[test] -fn nested_self_crate_alias_resolves_source_references_and_graph_edges() { - let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("tests/fixtures/self_crate_alias_project"); - let project = locate_project_from(&ProjectLocator::from_path(fixture)) - .expect("self-crate alias fixture should be discoverable"); - let dependencies = extract_dependencies(&project, SourceOptions::default()) - .expect("self-crate alias dependencies should extract"); - - for (path, target, kind, count) in [ - ("self", "src/lib.rs", ImportKind::ExternCrate, 1), - ("local_crate::Item", "src/lib.rs", ImportKind::PathReference, 2), - ( - "local_crate::api::Model", - "src/api.rs", - ImportKind::PathReference, - 1, - ), - ] { - let references = dependencies - .references() - .iter() - .filter(|reference| { - reference.source() == "src/consumer.rs" - && reference.referenced_path() == path - && reference.kind() == kind - }) - .collect::>(); - assert_eq!( - references.len(), - count, - "expected {count} {kind:?} references to {path}", - ); - for reference in references { - assert_eq!( - reference.internal_target(), - Some(target), - "{path} at line {} should resolve to {target}", - reference.line(), - ); - assert!(reference.line() > 0); - } - } - assert!( - dependencies.diagnostics().is_empty(), - "unexpected dependency diagnostics: {:?}", - dependencies.diagnostics(), - ); - - let graph = extract_graph(&project, SourceOptions::default()) - .expect("self-crate alias graph should extract"); - assert!( - graph.diagnostics().is_empty(), - "unexpected graph diagnostics: {:?}", - graph.diagnostics(), - ); - for (target, kinds) in [ - ( - "src/lib.rs", - vec![ImportKind::ExternCrate, ImportKind::PathReference], - ), - ("src/api.rs", vec![ImportKind::PathReference]), - ] { - let edge = graph - .graph() - .iter() - .find(|edge| edge.source == "src/consumer.rs" && edge.target == target) - .unwrap_or_else(|| panic!("expected consumer-to-{target} edge")); - assert!(!edge.external); - assert_eq!(edge.import_kinds.iter().collect::>(), kinds); - } -} From 659b317e4b54615b671168aa8aaba52526655ea2 Mon Sep 17 00:00:00 2001 From: Grzegorz Nowak Date: Sun, 15 Nov 2026 14:35:57 +0000 Subject: [PATCH 4/5] test: build use-alias references through a dedicated helper Introduce `use_alias` in the extraction test module so `use`-style references with a bound name are constructed in one expression instead of starting from a plain reference and mutating kind/binding afterward. Three call sites in the alias-resolution, binding-precedence, and renamed alias tests switch to the helper. No production code or other test helpers change. Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, cargo test --workspace --all-features (326 unit, 148 integration, 55 doctests). --- src/common/extraction/extract_dependencies.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/common/extraction/extract_dependencies.rs b/src/common/extraction/extract_dependencies.rs index 2886ede..47d1bfb 100644 --- a/src/common/extraction/extract_dependencies.rs +++ b/src/common/extraction/extract_dependencies.rs @@ -416,6 +416,13 @@ mod tests { } } + fn use_alias(current: LogicalModule, segments: &[&str], binding: &str) -> RawReference { + let mut reference = reference(current, segments); + reference.kind = ImportKind::Use; + reference.binding = Some(binding.to_owned()); + reference + } + fn index() -> BTreeMap { [ (module("lib", &[]), "src/lib.rs"), @@ -486,9 +493,7 @@ mod tests { #[test] fn resolves_paths_through_internal_aliases() { let current = module("lib", &["consumer"]); - let mut crate_api = reference(current.clone(), &["crate", "api"]); - crate_api.kind = ImportKind::Use; - crate_api.binding = Some("public_api".to_owned()); + let crate_api = use_alias(current.clone(), &["crate", "api"], "public_api"); for (declaration, binding, segments, expected) in [ ( @@ -519,9 +524,7 @@ mod tests { #[test] fn explicit_bindings_take_precedence_over_bare_module_candidates() { let current = module("lib", &["consumer"]); - let mut crate_api = reference(current.clone(), &["crate", "api"]); - crate_api.kind = ImportKind::Use; - crate_api.binding = Some("local".to_owned()); + let crate_api = use_alias(current.clone(), &["crate", "api"], "local"); for (declaration, binding, segments, expected) in [ (crate_api, "local", &["local", "Thing"][..], "src/api.rs"), @@ -615,9 +618,7 @@ mod tests { let current = module("lib", &["consumer"]); let aliases = collect_internal_aliases(&[alias(current.clone(), "renamed_crate")], &index()); - let mut import = reference(current, &["renamed_crate", "Risk"]); - import.kind = ImportKind::Use; - import.binding = Some("RenamedRisk".to_owned()); + let import = use_alias(current, &["renamed_crate", "Risk"], "RenamedRisk"); assert_alias_target(&import, &aliases, "src/lib.rs"); } From 9d35a84efcd6084e6c6f5d8cd8e7891cdcc8c989 Mon Sep 17 00:00:00 2001 From: Grzegorz Nowak Date: Sun, 15 Nov 2026 14:40:41 +0000 Subject: [PATCH 5/5] test: place use_alias adjacent to its alias sibling helper Reorder the extraction test helpers so the two specialised reference builders sit together. No behavior change. Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets --all-features --locked -- -D warnings, cargo test --workspace --all-features (326 unit, 148 integration, 55 doctests). --- src/common/extraction/extract_dependencies.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/common/extraction/extract_dependencies.rs b/src/common/extraction/extract_dependencies.rs index 47d1bfb..e422873 100644 --- a/src/common/extraction/extract_dependencies.rs +++ b/src/common/extraction/extract_dependencies.rs @@ -416,13 +416,6 @@ mod tests { } } - fn use_alias(current: LogicalModule, segments: &[&str], binding: &str) -> RawReference { - let mut reference = reference(current, segments); - reference.kind = ImportKind::Use; - reference.binding = Some(binding.to_owned()); - reference - } - fn index() -> BTreeMap { [ (module("lib", &[]), "src/lib.rs"), @@ -447,6 +440,13 @@ mod tests { declaration } + fn use_alias(current: LogicalModule, segments: &[&str], binding: &str) -> RawReference { + let mut reference = reference(current, segments); + reference.kind = ImportKind::Use; + reference.binding = Some(binding.to_owned()); + reference + } + fn assert_alias_target(reference: &RawReference, aliases: &AliasMap, expected: &str) { let ResolutionOutcome::Found(found) = resolve_reference(reference, &index(), aliases) else {