Skip to content

Commit b20e49f

Browse files
Rollup merge of #152543 - petrochenkov:rprivtit2, r=fmease
privacy: Fix type privacy holes in RPITITs A subset of #146470. Private types in RPITITs now report hard errors. Private types in bounds of associated types still only report the `private_bounds` lint due to unacceptable amount of breakage (#146470 (comment)). Closes #144139
2 parents bbe8536 + ef8c95c commit b20e49f

3 files changed

Lines changed: 39 additions & 56 deletions

File tree

compiler/rustc_privacy/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,13 +1595,14 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
15951595
let mut check = self.check(item.def_id.expect_local(), vis, effective_vis);
15961596

15971597
let is_assoc_ty = item.is_type();
1598-
check.hard_error = is_assoc_ty && !item.is_impl_trait_in_trait();
1598+
check.hard_error = is_assoc_ty;
15991599
check.generics().predicates();
16001600
if assoc_has_type_of(self.tcx, item) {
1601-
check.hard_error = check.hard_error && item.defaultness(self.tcx).has_value();
16021601
check.ty();
16031602
}
16041603
if is_assoc_ty && item.container == AssocContainer::Trait {
1604+
// FIXME: too much breakage from reporting hard errors here, better wait for a fix
1605+
// from proper associated type normalization.
16051606
check.hard_error = false;
16061607
check.bounds();
16071608
}

tests/ui/privacy/private-in-public-warn.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ mod traits {
4949
fn f<T: PrivTr>(arg: T) {}
5050
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::f`
5151
fn g() -> impl PrivTr;
52-
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::g::{anon_assoc#0}`
52+
//~^ ERROR private trait `traits::PrivTr` in public interface
5353
//~| ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::g::{anon_assoc#0}`
5454
fn h() -> impl PrivTr {}
55-
//~^ ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::h::{anon_assoc#0}`
55+
//~^ ERROR private trait `traits::PrivTr` in public interface
5656
//~| ERROR trait `traits::PrivTr` is more private than the item `traits::Tr3::h::{anon_assoc#0}`
5757
}
5858
impl<T: PrivTr> Pub<T> {} //~ ERROR trait `traits::PrivTr` is more private than the item `traits::Pub<T>`
@@ -93,13 +93,13 @@ mod generics {
9393

9494
pub trait Tr5 {
9595
fn required() -> impl PrivTr<Priv<()>>;
96-
//~^ ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::required::{anon_assoc#0}`
97-
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::required::{anon_assoc#0}`
96+
//~^ ERROR private trait `generics::PrivTr<generics::Priv<()>>` in public interface
97+
//~| ERROR private type `generics::Priv<()>` in public interface
9898
//~| ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::required::{anon_assoc#0}`
9999
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::required::{anon_assoc#0}`
100100
fn provided() -> impl PrivTr<Priv<()>> {}
101-
//~^ ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::provided::{anon_assoc#0}`
102-
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::provided::{anon_assoc#0}`
101+
//~^ ERROR private trait `generics::PrivTr<generics::Priv<()>>` in public interface
102+
//~| ERROR private type `generics::Priv<()>` in public interface
103103
//~| ERROR trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::provided::{anon_assoc#0}`
104104
//~| ERROR type `generics::Priv<()>` is more private than the item `Tr5::provided::{anon_assoc#0}`
105105
}

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

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,14 @@ note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
194194
LL | trait PrivTr {}
195195
| ^^^^^^^^^^^^
196196

197-
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::g::{anon_assoc#0}`
197+
error[E0446]: private trait `traits::PrivTr` in public interface
198198
--> $DIR/private-in-public-warn.rs:51:19
199199
|
200-
LL | fn g() -> impl PrivTr;
201-
| ^^^^^^^^^^^ opaque type `traits::Tr3::g::{anon_assoc#0}` is reachable at visibility `pub(crate)`
202-
|
203-
note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
204-
--> $DIR/private-in-public-warn.rs:37:5
205-
|
206200
LL | trait PrivTr {}
207-
| ^^^^^^^^^^^^
201+
| ------------ `traits::PrivTr` declared as private
202+
...
203+
LL | fn g() -> impl PrivTr;
204+
| ^^^^^^^^^^^ can't leak private trait
208205

209206
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::g::{anon_assoc#0}`
210207
--> $DIR/private-in-public-warn.rs:51:19
@@ -218,17 +215,14 @@ note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
218215
LL | trait PrivTr {}
219216
| ^^^^^^^^^^^^
220217

221-
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::h::{anon_assoc#0}`
218+
error[E0446]: private trait `traits::PrivTr` in public interface
222219
--> $DIR/private-in-public-warn.rs:54:19
223220
|
224-
LL | fn h() -> impl PrivTr {}
225-
| ^^^^^^^^^^^ opaque type `traits::Tr3::h::{anon_assoc#0}` is reachable at visibility `pub(crate)`
226-
|
227-
note: but trait `traits::PrivTr` is only usable at visibility `pub(self)`
228-
--> $DIR/private-in-public-warn.rs:37:5
229-
|
230221
LL | trait PrivTr {}
231-
| ^^^^^^^^^^^^
222+
| ------------ `traits::PrivTr` declared as private
223+
...
224+
LL | fn h() -> impl PrivTr {}
225+
| ^^^^^^^^^^^ can't leak private trait
232226

233227
error: trait `traits::PrivTr` is more private than the item `traits::Tr3::h::{anon_assoc#0}`
234228
--> $DIR/private-in-public-warn.rs:54:19
@@ -350,29 +344,23 @@ note: but type `generics::Priv` is only usable at visibility `pub(self)`
350344
LL | struct Priv<T = u8>(T);
351345
| ^^^^^^^^^^^^^^^^^^^
352346

353-
error: trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::required::{anon_assoc#0}`
347+
error[E0446]: private trait `generics::PrivTr<generics::Priv<()>>` in public interface
354348
--> $DIR/private-in-public-warn.rs:95:26
355349
|
356-
LL | fn required() -> impl PrivTr<Priv<()>>;
357-
| ^^^^^^^^^^^^^^^^^^^^^ opaque type `Tr5::required::{anon_assoc#0}` is reachable at visibility `pub(crate)`
358-
|
359-
note: but trait `generics::PrivTr<generics::Priv<()>>` is only usable at visibility `pub(self)`
360-
--> $DIR/private-in-public-warn.rs:84:5
361-
|
362350
LL | trait PrivTr<T> {}
363-
| ^^^^^^^^^^^^^^^
351+
| --------------- `generics::PrivTr<generics::Priv<()>>` declared as private
352+
...
353+
LL | fn required() -> impl PrivTr<Priv<()>>;
354+
| ^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
364355

365-
error: type `generics::Priv<()>` is more private than the item `Tr5::required::{anon_assoc#0}`
356+
error[E0446]: private type `generics::Priv<()>` in public interface
366357
--> $DIR/private-in-public-warn.rs:95:26
367358
|
368-
LL | fn required() -> impl PrivTr<Priv<()>>;
369-
| ^^^^^^^^^^^^^^^^^^^^^ opaque type `Tr5::required::{anon_assoc#0}` is reachable at visibility `pub(crate)`
370-
|
371-
note: but type `generics::Priv<()>` is only usable at visibility `pub(self)`
372-
--> $DIR/private-in-public-warn.rs:82:5
373-
|
374359
LL | struct Priv<T = u8>(T);
375-
| ^^^^^^^^^^^^^^^^^^^
360+
| ------------------- `generics::Priv<()>` declared as private
361+
...
362+
LL | fn required() -> impl PrivTr<Priv<()>>;
363+
| ^^^^^^^^^^^^^^^^^^^^^ can't leak private type
376364

377365
error: trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::required::{anon_assoc#0}`
378366
--> $DIR/private-in-public-warn.rs:95:26
@@ -398,29 +386,23 @@ note: but type `generics::Priv<()>` is only usable at visibility `pub(self)`
398386
LL | struct Priv<T = u8>(T);
399387
| ^^^^^^^^^^^^^^^^^^^
400388

401-
error: trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::provided::{anon_assoc#0}`
389+
error[E0446]: private trait `generics::PrivTr<generics::Priv<()>>` in public interface
402390
--> $DIR/private-in-public-warn.rs:100:26
403391
|
404-
LL | fn provided() -> impl PrivTr<Priv<()>> {}
405-
| ^^^^^^^^^^^^^^^^^^^^^ opaque type `Tr5::provided::{anon_assoc#0}` is reachable at visibility `pub(crate)`
406-
|
407-
note: but trait `generics::PrivTr<generics::Priv<()>>` is only usable at visibility `pub(self)`
408-
--> $DIR/private-in-public-warn.rs:84:5
409-
|
410392
LL | trait PrivTr<T> {}
411-
| ^^^^^^^^^^^^^^^
393+
| --------------- `generics::PrivTr<generics::Priv<()>>` declared as private
394+
...
395+
LL | fn provided() -> impl PrivTr<Priv<()>> {}
396+
| ^^^^^^^^^^^^^^^^^^^^^ can't leak private trait
412397

413-
error: type `generics::Priv<()>` is more private than the item `Tr5::provided::{anon_assoc#0}`
398+
error[E0446]: private type `generics::Priv<()>` in public interface
414399
--> $DIR/private-in-public-warn.rs:100:26
415400
|
416-
LL | fn provided() -> impl PrivTr<Priv<()>> {}
417-
| ^^^^^^^^^^^^^^^^^^^^^ opaque type `Tr5::provided::{anon_assoc#0}` is reachable at visibility `pub(crate)`
418-
|
419-
note: but type `generics::Priv<()>` is only usable at visibility `pub(self)`
420-
--> $DIR/private-in-public-warn.rs:82:5
421-
|
422401
LL | struct Priv<T = u8>(T);
423-
| ^^^^^^^^^^^^^^^^^^^
402+
| ------------------- `generics::Priv<()>` declared as private
403+
...
404+
LL | fn provided() -> impl PrivTr<Priv<()>> {}
405+
| ^^^^^^^^^^^^^^^^^^^^^ can't leak private type
424406

425407
error: trait `generics::PrivTr<generics::Priv<()>>` is more private than the item `Tr5::provided::{anon_assoc#0}`
426408
--> $DIR/private-in-public-warn.rs:100:26

0 commit comments

Comments
 (0)