Skip to content

Correct warning message in restricted visibility #140614

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

Merged
merged 1 commit into from
May 8, 2025
Merged
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
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,10 @@ impl Visibility {
} else if restricted_id == tcx.parent_module_from_def_id(def_id).to_local_def_id() {
"pub(self)".to_string()
} else {
format!("pub({})", tcx.item_name(restricted_id.to_def_id()))
format!(
"pub(in crate{})",
tcx.def_path(restricted_id.to_def_id()).to_string_no_crate_verbose()
)
}
}
ty::Visibility::Public => "pub".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/reexports.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ warning: glob import doesn't reexport anything with visibility `pub` because no
LL | pub use super::*;
| ^^^^^^^^
|
note: the most public imported item is `pub(a)`
note: the most public imported item is `pub(in crate::a)`
--> $DIR/reexports.rs:11:17
|
LL | pub use super::*;
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/pub/pub-restricted-warning.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//@ check-pass

#![allow(dead_code)]

mod outer {
pub mod inner {
pub(in crate::outer) struct Foo;
pub fn bar() -> Foo {
//~^ WARNING type `Foo` is more private than the item `outer::inner::bar` [private_interfaces]
Foo
}
}

pub mod nested {
pub mod inner {
pub(in crate::outer::nested) struct NestedFoo;
pub fn bar() -> NestedFoo {
//~^ WARNING type `NestedFoo` is more private than the item `nested::inner::bar` [private_interfaces]
NestedFoo
}
}
}
}

fn main() {}
27 changes: 27 additions & 0 deletions tests/ui/pub/pub-restricted-warning.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
warning: type `Foo` is more private than the item `outer::inner::bar`
--> $DIR/pub-restricted-warning.rs:8:9
|
LL | pub fn bar() -> Foo {
| ^^^^^^^^^^^^^^^^^^^ function `outer::inner::bar` is reachable at visibility `pub(crate)`
|
note: but type `Foo` is only usable at visibility `pub(in crate::outer)`
--> $DIR/pub-restricted-warning.rs:7:9
|
LL | pub(in crate::outer) struct Foo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[warn(private_interfaces)]` on by default

warning: type `NestedFoo` is more private than the item `nested::inner::bar`
--> $DIR/pub-restricted-warning.rs:17:13
|
LL | pub fn bar() -> NestedFoo {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `nested::inner::bar` is reachable at visibility `pub(crate)`
|
note: but type `NestedFoo` is only usable at visibility `pub(in crate::outer::nested)`
--> $DIR/pub-restricted-warning.rs:16:13
|
LL | pub(in crate::outer::nested) struct NestedFoo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 2 warnings emitted

Loading