Skip to content

Commit c98257c

Browse files
committed
fix: Implement missing exported_private_dependencies checks
1 parent 33c245b commit c98257c

File tree

4 files changed

+124
-27
lines changed

4 files changed

+124
-27
lines changed

compiler/rustc_privacy/src/lib.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,13 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
16461646
.generics()
16471647
.predicates();
16481648
}
1649+
1650+
// Check the impl trait refs and impl trait targets
1651+
if let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id.def_id) {
1652+
self.check(item.owner_id.def_id, impl_vis, None)
1653+
.visit_trait(trait_ref.instantiate_identity());
1654+
}
1655+
16491656
for impl_item_ref in impl_.items {
16501657
let impl_item_vis = if impl_.of_trait.is_none() {
16511658
min(
@@ -1673,6 +1680,33 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
16731680
}
16741681
}
16751682
}
1683+
DefKind::Use => {
1684+
let item = tcx.hir().item(id);
1685+
if let hir::ItemKind::Use(path, use_kind) = item.kind
1686+
// List imports are desugared as single ones so skip fake `ListStem`s
1687+
&& use_kind != rustc_hir::UseKind::ListStem
1688+
{
1689+
for def_id in path.res.iter().filter_map(Res::opt_def_id) {
1690+
self.check(item.owner_id.def_id, item_visibility, None).check_def_id(
1691+
def_id,
1692+
item.kind.descr(),
1693+
&LazyDefPathStr { def_id, tcx },
1694+
);
1695+
}
1696+
}
1697+
}
1698+
DefKind::ExternCrate => {
1699+
let item = tcx.hir().item(id);
1700+
if let Some(cnum) = tcx.extern_mod_stmt_cnum(item.owner_id.def_id) {
1701+
let def_id = cnum.as_def_id();
1702+
// Since extern crate is available only in the crate root, just omit effective vis
1703+
self.check(item.owner_id.def_id, item_visibility, None).visit_def_id(
1704+
def_id,
1705+
item.kind.descr(),
1706+
&LazyDefPathStr { def_id, tcx },
1707+
);
1708+
}
1709+
}
16761710
_ => {}
16771711
}
16781712
}

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ hashbrown = { version = "0.15", default-features = false, features = [
2424
] }
2525
std_detect = { path = "../stdarch/crates/std_detect", default-features = false, features = [
2626
'rustc-dep-of-std',
27-
] }
27+
], public = true }
2828

2929
# Dependencies of the `backtrace` crate
3030
rustc-demangle = { version = "0.1.24", features = ['rustc-dep-of-std'] }

tests/ui/privacy/pub-priv-dep/pub-priv1.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
// dependency or a private one.
88

99
#![deny(exported_private_dependencies)]
10+
#![allow(hidden_glob_reexports)]
1011

1112
// This crate is a private dependency
12-
// FIXME: This should trigger.
1313
pub extern crate priv_dep;
14+
//~^ ERROR extern crate `priv_dep` from private dependency 'priv_dep' in public interface
1415
// This crate is a public dependency
1516
extern crate pub_dep;
1617
// This crate is a private dependency
@@ -77,31 +78,31 @@ pub type Alias = OtherType;
7778

7879
pub struct PublicWithPrivateImpl;
7980

80-
// FIXME: This should trigger.
81-
// See https://github.com/rust-lang/rust/issues/71043
8281
impl OtherTrait for PublicWithPrivateImpl {}
82+
//~^ ERROR trait `OtherTrait` from private dependency 'priv_dep' in public interface
8383

8484
pub trait PubTraitOnPrivate {}
8585

86-
// FIXME: This should trigger.
87-
// See https://github.com/rust-lang/rust/issues/71043
8886
impl PubTraitOnPrivate for OtherType {}
87+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
8988

9089
pub struct AllowedPrivType {
9190
#[allow(exported_private_dependencies)]
9291
pub allowed: OtherType,
9392
}
9493

95-
// FIXME: This should trigger.
9694
pub use priv_dep::m;
97-
// FIXME: This should trigger.
95+
//~^ ERROR `use` import `m` from private dependency 'priv_dep' in public interface
9896
pub use pm::fn_like;
99-
// FIXME: This should trigger.
97+
//~^ ERROR `use` import `fn_like` from private dependency 'pm' in public interface
10098
pub use pm::PmDerive;
101-
// FIXME: This should trigger.
99+
//~^ ERROR `use` import `PmDerive` from private dependency 'pm' in public interface
102100
pub use pm::pm_attr;
103-
104-
// FIXME: This should trigger.
101+
//~^ ERROR `use` import `pm_attr` from private dependency 'pm' in public interface
105102
pub use priv_dep::E::V1;
103+
//~^ ERROR `use` import `V1` from private dependency 'priv_dep' in public interface
104+
//~^^ ERROR `use` import `V1` from private dependency 'priv_dep' in public interface
105+
pub use priv_dep::*;
106+
//~^ ERROR `use` import `priv_dep` from private dependency 'priv_dep' in public interface
106107

107108
fn main() {}
+77-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: type `OtherType` from private dependency 'priv_dep' in public interface
2-
--> $DIR/pub-priv1.rs:29:5
1+
error: extern crate `priv_dep` from private dependency 'priv_dep' in public interface
2+
--> $DIR/pub-priv1.rs:13:1
33
|
4-
LL | pub field: OtherType,
5-
| ^^^^^^^^^^^^^^^^^^^^
4+
LL | pub extern crate priv_dep;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/pub-priv1.rs:9:9
@@ -11,64 +11,126 @@ LL | #![deny(exported_private_dependencies)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: type `OtherType` from private dependency 'priv_dep' in public interface
14-
--> $DIR/pub-priv1.rs:36:5
14+
--> $DIR/pub-priv1.rs:30:5
15+
|
16+
LL | pub field: OtherType,
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: type `OtherType` from private dependency 'priv_dep' in public interface
20+
--> $DIR/pub-priv1.rs:37:5
1521
|
1622
LL | pub fn pub_fn_param(param: OtherType) {}
1723
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1824

1925
error: type `OtherType` from private dependency 'priv_dep' in public interface
20-
--> $DIR/pub-priv1.rs:39:5
26+
--> $DIR/pub-priv1.rs:40:5
2127
|
2228
LL | pub fn pub_fn_return() -> OtherType { OtherType }
2329
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2430

2531
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
26-
--> $DIR/pub-priv1.rs:46:5
32+
--> $DIR/pub-priv1.rs:47:5
2733
|
2834
LL | type Foo: OtherTrait;
2935
| ^^^^^^^^^^^^^^^^^^^^
3036

3137
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
32-
--> $DIR/pub-priv1.rs:50:1
38+
--> $DIR/pub-priv1.rs:51:1
3339
|
3440
LL | pub trait WithSuperTrait: OtherTrait {}
3541
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3642

3743
error: type `OtherType` from private dependency 'priv_dep' in public interface
38-
--> $DIR/pub-priv1.rs:59:5
44+
--> $DIR/pub-priv1.rs:60:5
3945
|
4046
LL | type X = OtherType;
4147
| ^^^^^^
4248

4349
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
44-
--> $DIR/pub-priv1.rs:63:1
50+
--> $DIR/pub-priv1.rs:64:1
4551
|
4652
LL | pub fn in_bounds<T: OtherTrait>(x: T) { unimplemented!() }
4753
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4854

4955
error: type `OtherType` from private dependency 'priv_dep' in public interface
50-
--> $DIR/pub-priv1.rs:66:1
56+
--> $DIR/pub-priv1.rs:67:1
5157
|
5258
LL | pub fn private_in_generic() -> std::num::Saturating<OtherType> { unimplemented!() }
5359
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5460

5561
error: type `OtherType` from private dependency 'priv_dep' in public interface
56-
--> $DIR/pub-priv1.rs:69:1
62+
--> $DIR/pub-priv1.rs:70:1
5763
|
5864
LL | pub static STATIC: OtherType = OtherType;
5965
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6066

6167
error: type `OtherType` from private dependency 'priv_dep' in public interface
62-
--> $DIR/pub-priv1.rs:72:1
68+
--> $DIR/pub-priv1.rs:73:1
6369
|
6470
LL | pub const CONST: OtherType = OtherType;
6571
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6672

6773
error: type `OtherType` from private dependency 'priv_dep' in public interface
68-
--> $DIR/pub-priv1.rs:75:1
74+
--> $DIR/pub-priv1.rs:76:1
6975
|
7076
LL | pub type Alias = OtherType;
7177
| ^^^^^^^^^^^^^^
7278

73-
error: aborting due to 11 previous errors
79+
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
80+
--> $DIR/pub-priv1.rs:81:1
81+
|
82+
LL | impl OtherTrait for PublicWithPrivateImpl {}
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84+
85+
error: type `OtherType` from private dependency 'priv_dep' in public interface
86+
--> $DIR/pub-priv1.rs:86:1
87+
|
88+
LL | impl PubTraitOnPrivate for OtherType {}
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
91+
error: `use` import `m` from private dependency 'priv_dep' in public interface
92+
--> $DIR/pub-priv1.rs:94:9
93+
|
94+
LL | pub use priv_dep::m;
95+
| ^^^^^^^^^^^
96+
97+
error: `use` import `fn_like` from private dependency 'pm' in public interface
98+
--> $DIR/pub-priv1.rs:96:9
99+
|
100+
LL | pub use pm::fn_like;
101+
| ^^^^^^^^^^^
102+
103+
error: `use` import `PmDerive` from private dependency 'pm' in public interface
104+
--> $DIR/pub-priv1.rs:98:9
105+
|
106+
LL | pub use pm::PmDerive;
107+
| ^^^^^^^^^^^^
108+
109+
error: `use` import `pm_attr` from private dependency 'pm' in public interface
110+
--> $DIR/pub-priv1.rs:100:9
111+
|
112+
LL | pub use pm::pm_attr;
113+
| ^^^^^^^^^^^
114+
115+
error: `use` import `V1` from private dependency 'priv_dep' in public interface
116+
--> $DIR/pub-priv1.rs:102:9
117+
|
118+
LL | pub use priv_dep::E::V1;
119+
| ^^^^^^^^^^^^^^^
120+
121+
error: `use` import `V1` from private dependency 'priv_dep' in public interface
122+
--> $DIR/pub-priv1.rs:102:9
123+
|
124+
LL | pub use priv_dep::E::V1;
125+
| ^^^^^^^^^^^^^^^
126+
|
127+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
128+
129+
error: `use` import `priv_dep` from private dependency 'priv_dep' in public interface
130+
--> $DIR/pub-priv1.rs:105:9
131+
|
132+
LL | pub use priv_dep::*;
133+
| ^^^^^^^^
134+
135+
error: aborting due to 21 previous errors
74136

0 commit comments

Comments
 (0)