Skip to content

Commit e9c1d6d

Browse files
committed
Suggest proper vis type in the help message
1 parent 8ec1279 commit e9c1d6d

File tree

8 files changed

+30
-8
lines changed

8 files changed

+30
-8
lines changed

compiler/rustc_privacy/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ privacy_from_private_dep_in_public_interface =
88
privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interface
99
.label = can't leak {$vis_descr} {$kind}
1010
.visibility_label = `{$descr}` declared as {$vis_descr}
11-
.suggestion = consider adding `pub` in front of it
11+
.suggestion = consider adding `{$vis_sugg}` in front of it
1212
1313
privacy_item_is_private = {$kind} `{$descr}` is private
1414
.label = private {$kind}

compiler/rustc_privacy/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub struct InPublicInterfaceTraits<'a> {
5656
pub span: Span,
5757
pub vis_descr: &'static str,
5858
pub kind: &'a str,
59+
pub vis_sugg: &'static str,
5960
pub descr: DiagnosticArgFromDisplay<'a>,
6061
#[label(privacy_visibility_label)]
6162
pub vis_span: Span,
@@ -70,6 +71,7 @@ pub struct InPublicInterface<'a> {
7071
pub span: Span,
7172
pub vis_descr: &'static str,
7273
pub kind: &'a str,
74+
pub vis_sugg: &'static str,
7375
pub descr: DiagnosticArgFromDisplay<'a>,
7476
#[label(privacy_visibility_label)]
7577
pub vis_span: Span,

compiler/rustc_privacy/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1814,11 +1814,27 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
18141814
|| self.in_assoc_ty
18151815
|| self.tcx.resolutions(()).has_pub_restricted
18161816
{
1817+
// FIXME: this code was adapted from the above `vis_descr` computation,
1818+
// but it's not clear if it's correct.
1819+
let vis_sugg = match self.required_visibility {
1820+
ty::Visibility::Public => "pub",
1821+
ty::Visibility::Restricted(vis_def_id) => {
1822+
if vis_def_id == self.tcx.parent_module(hir_id) {
1823+
"???FIXME???"
1824+
} else if vis_def_id.is_top_level_module() {
1825+
"pub(crate)"
1826+
} else {
1827+
"???FIXME???"
1828+
}
1829+
}
1830+
};
1831+
18171832
if kind == "trait" {
18181833
self.tcx.sess.emit_err(InPublicInterfaceTraits {
18191834
span,
18201835
vis_descr,
18211836
kind,
1837+
vis_sugg,
18221838
descr: descr.into(),
18231839
vis_span,
18241840
});
@@ -1827,6 +1843,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
18271843
span,
18281844
vis_descr,
18291845
kind,
1846+
vis_sugg,
18301847
descr: descr.into(),
18311848
vis_span,
18321849
suggestion: vis_span,

tests/ui/privacy/issue-30079.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ LL | struct Priv;
1515
| -----------
1616
| |
1717
| `m2::Priv` declared as private
18-
| help: consider adding `pub` in front of it
18+
| help: consider adding `pub(crate)` in front of it
1919
LL | impl ::std::ops::Deref for ::SemiPriv {
2020
LL | type Target = Priv;
2121
| ^^^^^^^^^^^ can't leak private type
@@ -27,7 +27,7 @@ LL | struct Priv;
2727
| -----------
2828
| |
2929
| `m3::Priv` declared as private
30-
| help: consider adding `pub` in front of it
30+
| help: consider adding `pub(crate)` in front of it
3131
LL | impl ::SemiPrivTrait for () {
3232
LL | type Assoc = Priv;
3333
| ^^^^^^^^^^ can't leak private type

tests/ui/privacy/restricted/private-in-public.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct Priv;
55
| -----------
66
| |
77
| `Priv` declared as private
8-
| help: consider adding `pub` in front of it
8+
| help: consider adding `pub(crate)` in front of it
99
...
1010
LL | pub(crate) fn g(_: Priv) {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
@@ -17,7 +17,7 @@ LL | struct Priv;
1717
| -----------
1818
| |
1919
| `Priv` declared as private
20-
| help: consider adding `pub` in front of it
20+
| help: consider adding `pub(crate)` in front of it
2121
...
2222
LL | pub(crate) fn h(_: Priv) {}
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

tests/ui/transmutability/visibility/assume/should_accept_if_dst_has_unreachable_field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | #[repr(C)] pub(self) struct Zst; // <- unreachable type
55
| --------------------
66
| |
77
| `dst::Zst` declared as private
8-
| help: consider adding `pub` in front of it
8+
| help: consider adding `pub(crate)` in front of it
99
...
1010
LL | pub(in super) field: Zst,
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

tests/ui/transmutability/visibility/should_accept_if_src_has_unreachable_field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | #[repr(C)] pub(self) struct Zst; // <- unreachable type
55
| --------------------
66
| |
77
| `src::Zst` declared as private
8-
| help: consider adding `pub` in front of it
8+
| help: consider adding `pub(crate)` in front of it
99
...
1010
LL | pub(in super) field: Zst,
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type

tests/ui/type-alias-impl-trait/privacy.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0446]: private type alias `Foo` in public interface
22
--> $DIR/privacy.rs:4:1
33
|
44
LL | type Foo = (impl Sized, u8);
5-
| -------- `Foo` declared as private
5+
| --------
6+
| |
7+
| `Foo` declared as private
8+
| help: consider adding `pub` in front of it
69
LL | pub fn foo() -> Foo {
710
| ^^^^^^^^^^^^^^^^^^^ can't leak private type alias
811

0 commit comments

Comments
 (0)