Skip to content

Add test cases for namespaced crates #142437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod utils {
pub fn root_helper() {
println!("root_helper");
}
}

pub fn root_function() -> String {
"my_api root!".to_string()
}
3 changes: 3 additions & 0 deletions tests/ui/resolve/auxiliary/open-ns-my_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn root_function() -> String {
"my_api root!".to_string()
}
15 changes: 15 additions & 0 deletions tests/ui/resolve/auxiliary/open-ns-my_api_core.rs
Original file line number Diff line number Diff line change
@@ -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!",)
}
9 changes: 9 additions & 0 deletions tests/ui/resolve/auxiliary/open-ns-my_api_utils.rs
Original file line number Diff line number Diff line change
@@ -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!",)
}
19 changes: 19 additions & 0 deletions tests/ui/resolve/open-ns-1.rs
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an ambiguity error.
my_api::utils refers both to my_api(crate)::utils(mod) and my_api(open_ns)::utils(crate), and they have different definitions.

Alternatively my_api(crate)::utils(mod) will shadow my_api(open_ns)::utils(crate) in the 3-scope model, if we do not report ambiguities even for explicit uses.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean, there is no utils(mod) in my_api. The my_api crate only has a public function named root_function.

Copy link
Contributor

@petrochenkov petrochenkov Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay, I mixed up tests/ui/resolve/auxiliary/open-ns-my_api.rs and tests/ui/resolve/auxiliary/open-ns-mod-my_api.rs, the files are named too similarly.
I'll need to re-review all the cases.


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();
}
19 changes: 19 additions & 0 deletions tests/ui/resolve/open-ns-2.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also an ambiguity, like in tests/ui/resolve/open-ns-1.rs.

use my_api::*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the 3-scope model this would import from both my_api(crate) and my_api(open_ns).
Therefore it will introduce names utils(mod), root_function and core into this module, but not utils(crate) because it would be shadowed by utils(mod).
Need to test this behavior.


fn main() {
let _ = root_function();
let _ = utils_helper();
let _ = core_fn();
let _ = core_fn2();
}
18 changes: 18 additions & 0 deletions tests/ui/resolve/open-ns-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// This test should fail with `utils` being defined multiple times, since open-ns-mod-my_api.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm? There's no open-ns-mod-my_api.rs in this test.

// 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();
}
15 changes: 15 additions & 0 deletions tests/ui/resolve/open-ns-4.rs
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also an ambiguity, like in tests/ui/resolve/open-ns-1.rs.

}
13 changes: 13 additions & 0 deletions tests/ui/resolve/open-ns-5.rs
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 19 additions & 0 deletions tests/ui/resolve/open-ns-6.rs
Original file line number Diff line number Diff line change
@@ -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)
}
13 changes: 13 additions & 0 deletions tests/ui/resolve/open-ns-7.rs
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use my_api; // FIXME can be resolved even though it (maybe?) shouldn't be
use my_api;

We should decide the desired behavior before merging this. In this test case, my_api is an open namespace module. I think it makes sense to resolve it, as it's fine to have a module that only contains another module.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should generally work, unless there are specific reasons for not supporting it (like issues with reexporting things like this to other crates due to them lacking identity).

use my_api::utils::utils_helper;

fn main() {
let _ = utils_helper();
}
Loading