Skip to content
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

Fix type privacy lints error message #113161

Merged
merged 1 commit into from
Jun 30, 2023
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
2 changes: 1 addition & 1 deletion compiler/rustc_privacy/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ privacy_private_in_public_lint =
})

privacy_private_interface_or_bounds_lint = {$ty_kind} `{$ty_descr}` is more private than the item `{$item_descr}`
.item_note = {$item_kind} `{$item_descr}` is reachable at visibility `{$item_vis_descr}`
.item_label = {$item_kind} `{$item_descr}` is reachable at visibility `{$item_vis_descr}`
.ty_note = but {$ty_kind} `{$ty_descr}` is only usable at visibility `{$ty_vis_descr}`
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved

privacy_report_effective_visibility = {$descr}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_privacy/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct UnnameableTypesLint<'a> {
#[derive(LintDiagnostic)]
#[diag(privacy_private_interface_or_bounds_lint)]
pub struct PrivateInterfacesOrBoundsLint<'a> {
#[note(privacy_item_note)]
#[label(privacy_item_label)]
pub item_span: Span,
pub item_kind: &'a str,
pub item_descr: DiagnosticArgFromDisplay<'a>,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,9 +1865,10 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
} else {
lint::builtin::PRIVATE_BOUNDS
};
self.tcx.emit_lint(
self.tcx.emit_spanned_lint(
lint,
hir_id,
span,
PrivateInterfacesOrBoundsLint {
item_span: span,
item_kind: self.tcx.def_descr(self.item_def_id.to_def_id()),
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/associated-inherent-types/private-in-public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
pub type PubAlias0 = PubTy::PrivAssocTy;
//~^ ERROR private associated type `PubTy::PrivAssocTy` in public interface (error E0446)
//~| WARNING this was previously accepted
//~| WARNING associated type `PubTy::PrivAssocTy` is more private than the item `PubAlias0`
pub type PubAlias1 = PrivTy::PubAssocTy;
//~^ ERROR private type `PrivTy` in public interface (error E0446)
//~| WARNING this was previously accepted
//~| WARNING type `PrivTy` is more private than the item `PubAlias1`
pub type PubAlias2 = PubTy::PubAssocTy<PrivTy>;
//~^ ERROR private type `PrivTy` in public interface (error E0446)
//~| WARNING this was previously accepted
//~| WARNING type `PrivTy` is more private than the item `PubAlias2`

pub struct PubTy;
impl PubTy {
Expand Down
29 changes: 13 additions & 16 deletions tests/ui/associated-inherent-types/private-in-public.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ LL | #![deny(private_in_public)]
| ^^^^^^^^^^^^^^^^^

warning: associated type `PubTy::PrivAssocTy` is more private than the item `PubAlias0`
|
note: type alias `PubAlias0` is reachable at visibility `pub`
--> $DIR/private-in-public.rs:12:1
|
LL | pub type PubAlias0 = PubTy::PrivAssocTy;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^ type alias `PubAlias0` is reachable at visibility `pub`
|
note: but associated type `PubTy::PrivAssocTy` is only usable at visibility `pub(crate)`
--> $DIR/private-in-public.rs:24:5
--> $DIR/private-in-public.rs:27:5
|
LL | type PrivAssocTy = ();
| ^^^^^^^^^^^^^^^^
Expand All @@ -31,7 +30,7 @@ LL | #![warn(private_interfaces)]
| ^^^^^^^^^^^^^^^^^^

error: private type `PrivTy` in public interface (error E0446)
--> $DIR/private-in-public.rs:15:1
--> $DIR/private-in-public.rs:16:1
|
LL | pub type PubAlias1 = PrivTy::PubAssocTy;
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -40,20 +39,19 @@ LL | pub type PubAlias1 = PrivTy::PubAssocTy;
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>

warning: type `PrivTy` is more private than the item `PubAlias1`
|
note: type alias `PubAlias1` is reachable at visibility `pub`
--> $DIR/private-in-public.rs:15:1
--> $DIR/private-in-public.rs:16:1
|
LL | pub type PubAlias1 = PrivTy::PubAssocTy;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^ type alias `PubAlias1` is reachable at visibility `pub`
|
note: but type `PrivTy` is only usable at visibility `pub(crate)`
--> $DIR/private-in-public.rs:28:1
--> $DIR/private-in-public.rs:31:1
|
LL | struct PrivTy;
| ^^^^^^^^^^^^^

error: private type `PrivTy` in public interface (error E0446)
--> $DIR/private-in-public.rs:18:1
--> $DIR/private-in-public.rs:20:1
|
LL | pub type PubAlias2 = PubTy::PubAssocTy<PrivTy>;
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -62,14 +60,13 @@ LL | pub type PubAlias2 = PubTy::PubAssocTy<PrivTy>;
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>

warning: type `PrivTy` is more private than the item `PubAlias2`
|
note: type alias `PubAlias2` is reachable at visibility `pub`
--> $DIR/private-in-public.rs:18:1
--> $DIR/private-in-public.rs:20:1
|
LL | pub type PubAlias2 = PubTy::PubAssocTy<PrivTy>;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^ type alias `PubAlias2` is reachable at visibility `pub`
|
note: but type `PrivTy` is only usable at visibility `pub(crate)`
--> $DIR/private-in-public.rs:28:1
--> $DIR/private-in-public.rs:31:1
|
LL | struct PrivTy;
| ^^^^^^^^^^^^^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ where
{
type AssocTy = Const<{ my_const_fn(U) }>;
//~^ ERROR private type
//~| WARNING type `fn(u8) -> u8 {my_const_fn}` is more private than the item `<Const<U> as Trait>::AssocTy`
fn assoc_fn() -> Self::AssocTy {
Const
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ LL | const fn my_const_fn(val: u8) -> u8 {
| ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private

warning: type `fn(u8) -> u8 {my_const_fn}` is more private than the item `<Const<U> as Trait>::AssocTy`
|
note: associated type `<Const<U> as Trait>::AssocTy` is reachable at visibility `pub`
--> $DIR/eval-privacy.rs:22:5
|
LL | type AssocTy = Const<{ my_const_fn(U) }>;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ associated type `<Const<U> as Trait>::AssocTy` is reachable at visibility `pub`
|
note: but type `fn(u8) -> u8 {my_const_fn}` is only usable at visibility `pub(crate)`
--> $DIR/eval-privacy.rs:29:1
--> $DIR/eval-privacy.rs:30:1
|
LL | const fn my_const_fn(val: u8) -> u8 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/error-codes/E0445.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ trait Foo {

pub trait Bar : Foo {}
//~^ ERROR private trait `Foo` in public interface [E0445]
//~| WARNING trait `Foo` is more private than the item `Bar`
pub struct Bar2<T: Foo>(pub T);
//~^ ERROR private trait `Foo` in public interface [E0445]
//~| WARNING trait `Foo` is more private than the item `Bar2`
pub fn foo<T: Foo> (t: T) {}
//~^ ERROR private trait `Foo` in public interface [E0445]
//~| WARNING trait `Foo` is more private than the item `foo`

fn main() {}
23 changes: 10 additions & 13 deletions tests/ui/error-codes/E0445.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ LL | pub trait Bar : Foo {}
| ^^^^^^^^^^^^^^^^^^^ can't leak private trait

warning: trait `Foo` is more private than the item `Bar`
|
note: trait `Bar` is reachable at visibility `pub`
--> $DIR/E0445.rs:13:1
|
LL | pub trait Bar : Foo {}
| ^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^ trait `Bar` is reachable at visibility `pub`
|
note: but trait `Foo` is only usable at visibility `pub(crate)`
--> $DIR/E0445.rs:9:1
|
Expand All @@ -26,7 +25,7 @@ LL | #[warn(private_bounds)]
| ^^^^^^^^^^^^^^

error[E0445]: private trait `Foo` in public interface
--> $DIR/E0445.rs:15:1
--> $DIR/E0445.rs:16:1
|
LL | trait Foo {
| --------- `Foo` declared as private
Expand All @@ -35,20 +34,19 @@ LL | pub struct Bar2<T: Foo>(pub T);
| ^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait

warning: trait `Foo` is more private than the item `Bar2`
|
note: struct `Bar2` is reachable at visibility `pub`
--> $DIR/E0445.rs:15:1
--> $DIR/E0445.rs:16:1
|
LL | pub struct Bar2<T: Foo>(pub T);
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^ struct `Bar2` is reachable at visibility `pub`
|
note: but trait `Foo` is only usable at visibility `pub(crate)`
--> $DIR/E0445.rs:9:1
|
LL | trait Foo {
| ^^^^^^^^^

error[E0445]: private trait `Foo` in public interface
--> $DIR/E0445.rs:17:1
--> $DIR/E0445.rs:19:1
|
LL | trait Foo {
| --------- `Foo` declared as private
Expand All @@ -57,12 +55,11 @@ LL | pub fn foo<T: Foo> (t: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait

warning: trait `Foo` is more private than the item `foo`
|
note: function `foo` is reachable at visibility `pub`
--> $DIR/E0445.rs:17:1
--> $DIR/E0445.rs:19:1
|
LL | pub fn foo<T: Foo> (t: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^ function `foo` is reachable at visibility `pub`
|
note: but trait `Foo` is only usable at visibility `pub(crate)`
--> $DIR/E0445.rs:9:1
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/issues/issue-18389.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ trait Private<P, R> {
}
pub trait Public: Private<
//~^ ERROR private trait `Private<<Self as Public>::P, <Self as Public>::R>` in public interface
//~| WARNING trait `Private<<Self as Public>::P, <Self as Public>::R>` is more private than the item `Public`
<Self as Public>::P,
<Self as Public>::R
> {
Expand Down
7 changes: 4 additions & 3 deletions tests/ui/issues/issue-18389.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ LL | trait Private<P, R> {
...
LL | / pub trait Public: Private<
LL | |
LL | |
LL | | <Self as Public>::P,
LL | | <Self as Public>::R
LL | | > {
| |_^ can't leak private trait

warning: trait `Private<<Self as Public>::P, <Self as Public>::R>` is more private than the item `Public`
|
note: trait `Public` is reachable at visibility `pub`
--> $DIR/issue-18389.rs:14:1
|
LL | / pub trait Public: Private<
LL | |
LL | |
LL | | <Self as Public>::P,
LL | | <Self as Public>::R
LL | | > {
| |_^
| |_^ trait `Public` is reachable at visibility `pub`
|
note: but trait `Private<<Self as Public>::P, <Self as Public>::R>` is only usable at visibility `pub(crate)`
--> $DIR/issue-18389.rs:11:1
|
Expand Down
1 change: 1 addition & 0 deletions tests/ui/privacy/private-in-public-non-principal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ auto trait PrivNonPrincipal {}
pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loop {} }
//~^ WARN private trait `PrivNonPrincipal` in public interface
//~| WARN this was previously accepted
//~| ERROR trait `PrivNonPrincipal` is more private than the item `leak_dyn_nonprincipal`

#[deny(missing_docs)]
fn container() {
Expand Down
9 changes: 4 additions & 5 deletions tests/ui/privacy/private-in-public-non-principal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ LL | pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal>
= note: `#[warn(private_in_public)]` on by default

error: trait `PrivNonPrincipal` is more private than the item `leak_dyn_nonprincipal`
|
note: function `leak_dyn_nonprincipal` is reachable at visibility `pub`
--> $DIR/private-in-public-non-principal.rs:13:1
|
LL | pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loop {} }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function `leak_dyn_nonprincipal` is reachable at visibility `pub`
|
note: but trait `PrivNonPrincipal` is only usable at visibility `pub(crate)`
--> $DIR/private-in-public-non-principal.rs:11:1
|
Expand All @@ -27,13 +26,13 @@ LL | #![deny(private_interfaces)]
| ^^^^^^^^^^^^^^^^^^

error: missing documentation for an associated function
--> $DIR/private-in-public-non-principal.rs:20:9
--> $DIR/private-in-public-non-principal.rs:21:9
|
LL | pub fn check_doc_lint() {}
| ^^^^^^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/private-in-public-non-principal.rs:17:8
--> $DIR/private-in-public-non-principal.rs:18:8
|
LL | #[deny(missing_docs)]
| ^^^^^^^^^^^^
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/privacy/where-priv-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl PubTrWithAssocTy for PrivTy { type AssocTy = PrivTy; }
pub struct S
//~^ WARNING private type `PrivTy` in public interface
//~| WARNING hard error
//~| WARNING type `PrivTy` is more private than the item `S`
where
PrivTy:
{}
Expand All @@ -33,6 +34,7 @@ where
pub enum E
//~^ WARNING private type `PrivTy` in public interface
//~| WARNING hard error
//~| WARNING type `PrivTy` is more private than the item `E`
where
PrivTy:
{}
Expand All @@ -41,19 +43,22 @@ where
pub fn f()
//~^ WARNING private type `PrivTy` in public interface
//~| WARNING hard error
//~| WARNING type `PrivTy` is more private than the item `f`
where
PrivTy:
{}


impl S
//~^ ERROR private type `PrivTy` in public interface
//~| WARNING type `PrivTy` is more private than the item `S`
where
PrivTy:
{
pub fn f()
//~^ WARNING private type `PrivTy` in public interface
//~| WARNING hard error
//~| WARNING type `PrivTy` is more private than the item `S::f`
where
PrivTy:
{}
Expand Down Expand Up @@ -85,6 +90,7 @@ where
{
type AssocTy = Const<{ my_const_fn(U) }>;
//~^ ERROR private type
//~| WARNING type `fn(u8) -> u8 {my_const_fn}` is more private than the item `<Const<U> as Trait>::AssocTy`
fn assoc_fn() -> Self::AssocTy {
Const
}
Expand Down
Loading