From e9f079e4000b3be6287435a4ea0aa60cd7baddf6 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Thu, 12 Jun 2025 12:34:32 -0700 Subject: [PATCH] Add test cases for namespaced crates --- .../resolve/auxiliary/open-ns-mod-my_api.rs | 9 +++++++++ tests/ui/resolve/auxiliary/open-ns-my_api.rs | 3 +++ .../resolve/auxiliary/open-ns-my_api_core.rs | 15 +++++++++++++++ .../resolve/auxiliary/open-ns-my_api_utils.rs | 9 +++++++++ tests/ui/resolve/open-ns-1.rs | 19 +++++++++++++++++++ tests/ui/resolve/open-ns-2.rs | 19 +++++++++++++++++++ tests/ui/resolve/open-ns-3.rs | 18 ++++++++++++++++++ tests/ui/resolve/open-ns-4.rs | 15 +++++++++++++++ tests/ui/resolve/open-ns-5.rs | 13 +++++++++++++ tests/ui/resolve/open-ns-6.rs | 19 +++++++++++++++++++ tests/ui/resolve/open-ns-7.rs | 13 +++++++++++++ 11 files changed, 152 insertions(+) create mode 100644 tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs create mode 100644 tests/ui/resolve/auxiliary/open-ns-my_api.rs create mode 100644 tests/ui/resolve/auxiliary/open-ns-my_api_core.rs create mode 100644 tests/ui/resolve/auxiliary/open-ns-my_api_utils.rs create mode 100644 tests/ui/resolve/open-ns-1.rs create mode 100644 tests/ui/resolve/open-ns-2.rs create mode 100644 tests/ui/resolve/open-ns-3.rs create mode 100644 tests/ui/resolve/open-ns-4.rs create mode 100644 tests/ui/resolve/open-ns-5.rs create mode 100644 tests/ui/resolve/open-ns-6.rs create mode 100644 tests/ui/resolve/open-ns-7.rs diff --git a/tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs b/tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs new file mode 100644 index 0000000000000..dc8b5720c0c10 --- /dev/null +++ b/tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs @@ -0,0 +1,9 @@ +pub mod utils { + pub fn root_helper() { + println!("root_helper"); + } +} + +pub fn root_function() -> String { + "my_api root!".to_string() +} diff --git a/tests/ui/resolve/auxiliary/open-ns-my_api.rs b/tests/ui/resolve/auxiliary/open-ns-my_api.rs new file mode 100644 index 0000000000000..be4bf31f0fbcd --- /dev/null +++ b/tests/ui/resolve/auxiliary/open-ns-my_api.rs @@ -0,0 +1,3 @@ +pub fn root_function() -> String { + "my_api root!".to_string() +} diff --git a/tests/ui/resolve/auxiliary/open-ns-my_api_core.rs b/tests/ui/resolve/auxiliary/open-ns-my_api_core.rs new file mode 100644 index 0000000000000..41418f1516f60 --- /dev/null +++ b/tests/ui/resolve/auxiliary/open-ns-my_api_core.rs @@ -0,0 +1,15 @@ +// #![crate_name = "my_api::core"] + +pub mod util { + pub fn core_mod_fn() -> String { + format!("core_fn from my_api::core::util",) + } +} + +pub fn core_fn() -> String { + format!("core_fn from my_api::core!",) +} + +pub fn core_fn2() -> String { + format!("core_fn2 from my_api::core!",) +} diff --git a/tests/ui/resolve/auxiliary/open-ns-my_api_utils.rs b/tests/ui/resolve/auxiliary/open-ns-my_api_utils.rs new file mode 100644 index 0000000000000..8ac0f46549322 --- /dev/null +++ b/tests/ui/resolve/auxiliary/open-ns-my_api_utils.rs @@ -0,0 +1,9 @@ +pub mod util { + pub fn util_mod_helper() -> String { + format!("Helper from my_api::utils::util",) + } +} + +pub fn utils_helper() -> String { + format!("Helper from my_api::utils!",) +} diff --git a/tests/ui/resolve/open-ns-1.rs b/tests/ui/resolve/open-ns-1.rs new file mode 100644 index 0000000000000..5e317f867bc7a --- /dev/null +++ b/tests/ui/resolve/open-ns-1.rs @@ -0,0 +1,19 @@ +//@ build-pass +//@ aux-crate:my_api=open-ns-my_api.rs +//@ aux-crate:my_api::utils=open-ns-my_api_utils.rs +//@ aux-crate:my_api::core=open-ns-my_api_core.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +use my_api::root_function; +use my_api::utils::util; + +fn main() { + let _ = root_function(); + let _ = my_api::root_function(); + let _ = my_api::utils::utils_helper(); + let _ = util::util_mod_helper(); + let _ = my_api::core::core_fn(); +} diff --git a/tests/ui/resolve/open-ns-2.rs b/tests/ui/resolve/open-ns-2.rs new file mode 100644 index 0000000000000..aa46f2f304436 --- /dev/null +++ b/tests/ui/resolve/open-ns-2.rs @@ -0,0 +1,19 @@ +//@ build-pass +//@ aux-crate: my_api=open-ns-my_api.rs +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ aux-crate: my_api::core=open-ns-my_api_core.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +use my_api::core::{core_fn, core_fn2}; +use my_api::utils::*; +use my_api::*; + +fn main() { + let _ = root_function(); + let _ = utils_helper(); + let _ = core_fn(); + let _ = core_fn2(); +} diff --git a/tests/ui/resolve/open-ns-3.rs b/tests/ui/resolve/open-ns-3.rs new file mode 100644 index 0000000000000..cb0996f2084cb --- /dev/null +++ b/tests/ui/resolve/open-ns-3.rs @@ -0,0 +1,18 @@ +// This test should fail with `utils` being defined multiple times, since open-ns-mod-my_api.rs +// includes a `mod utils` and we also include open-ns-my_api_utils.rs as a namespaced crate at +// my_api::utils. + +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 +//@ build-pass + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +fn main() { + // FIXME test should fail with conflict here, but unsure how to do error annotation + // in auxiliary crate. + // let _ = my_api::root_function(); + // let _ = my_api::utils::root_helper(); + let _ = my_api::utils::utils_helper(); +} diff --git a/tests/ui/resolve/open-ns-4.rs b/tests/ui/resolve/open-ns-4.rs new file mode 100644 index 0000000000000..70404ff5b6483 --- /dev/null +++ b/tests/ui/resolve/open-ns-4.rs @@ -0,0 +1,15 @@ +// This test makes sure namespaced crates work if we don't use any use statements but instead fully +// qualify all references. + +//@ aux-crate: my_api=open-ns-my_api.rs +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 +//@ build-pass + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +fn main() { + let _ = my_api::root_function(); + let _ = my_api::utils::utils_helper(); +} diff --git a/tests/ui/resolve/open-ns-5.rs b/tests/ui/resolve/open-ns-5.rs new file mode 100644 index 0000000000000..07d5207dc45e0 --- /dev/null +++ b/tests/ui/resolve/open-ns-5.rs @@ -0,0 +1,13 @@ +// This test makes sure namespaced crates work if we don't use any use statements but instead fully +// qualify all references and we only have a single namespaced crate with no parent crate. + +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 +//@ build-pass + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +fn main() { + let _ = my_api::utils::utils_helper(); +} diff --git a/tests/ui/resolve/open-ns-6.rs b/tests/ui/resolve/open-ns-6.rs new file mode 100644 index 0000000000000..ccdcad6a84a45 --- /dev/null +++ b/tests/ui/resolve/open-ns-6.rs @@ -0,0 +1,19 @@ +// Tests that namespaced crate names work inside macros. + +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 +//@ build-pass + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +macro_rules! import_and_call { + ($import_path:path, $fn_name:ident) => {{ + use $import_path; + $fn_name(); + }}; +} + +fn main() { + import_and_call!(my_api::utils::utils_helper, utils_helper) +} diff --git a/tests/ui/resolve/open-ns-7.rs b/tests/ui/resolve/open-ns-7.rs new file mode 100644 index 0000000000000..f893801b2a837 --- /dev/null +++ b/tests/ui/resolve/open-ns-7.rs @@ -0,0 +1,13 @@ +//@ aux-crate: my_api::utils=open-ns-my_api_utils.rs +//@ compile-flags: -Z namespaced-crates +//@ edition: 2024 +//@ build-pass + +//@ ignore-test FIXME(packages_as_namespaces): feature not implemented yet + +use my_api; // FIXME can be resolved even though it (maybe?) shouldn't be +use my_api::utils::utils_helper; + +fn main() { + let _ = utils_helper(); +}