Skip to content

Commit 6df1d7e

Browse files
Auto merge of #144479 - cjgillot:incr-privacy-mod, r=<try>
Perform check_private_in_public by module.
2 parents 606dcc0 + 1b01dec commit 6df1d7e

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) {
11511151

11521152
parallel!(
11531153
{
1154-
tcx.ensure_ok().check_private_in_public(());
1154+
tcx.par_hir_for_each_module(|module| {
1155+
tcx.ensure_ok().check_private_in_public(module)
1156+
})
11551157
},
11561158
{
11571159
tcx.par_hir_for_each_module(|module| {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,11 @@ rustc_queries! {
13901390
eval_always
13911391
desc { "checking effective visibilities" }
13921392
}
1393-
query check_private_in_public(_: ()) {
1394-
desc { "checking for private elements in public interfaces" }
1393+
query check_private_in_public(module_def_id: LocalModDefId) {
1394+
desc { |tcx|
1395+
"checking for private elements in public interfaces for {}",
1396+
describe_as_module(module_def_id, tcx)
1397+
}
13951398
}
13961399

13971400
query reachable_set(_: ()) -> &'tcx LocalDefIdSet {

compiler/rustc_privacy/src/lib.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
14221422
};
14231423

14241424
let vis = self.tcx.local_visibility(local_def_id);
1425-
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1426-
let vis_span = self.tcx.def_span(def_id);
14271425
if self.in_assoc_ty && !vis.is_at_least(self.required_visibility, self.tcx) {
14281426
let vis_descr = match vis {
14291427
ty::Visibility::Public => "public",
@@ -1440,6 +1438,8 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
14401438
}
14411439
};
14421440

1441+
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1442+
let vis_span = self.tcx.def_span(def_id);
14431443
self.tcx.dcx().emit_err(InPublicInterface {
14441444
span,
14451445
vis_descr,
@@ -1462,6 +1462,8 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
14621462
} else {
14631463
lint::builtin::PRIVATE_BOUNDS
14641464
};
1465+
let span = self.tcx.def_span(self.item_def_id.to_def_id());
1466+
let vis_span = self.tcx.def_span(def_id);
14651467
self.tcx.emit_node_span_lint(
14661468
lint,
14671469
self.tcx.local_def_id_to_hir_id(self.item_def_id),
@@ -1593,7 +1595,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
15931595
self.effective_visibilities.effective_vis(def_id).copied()
15941596
}
15951597

1596-
fn check_item(&mut self, id: ItemId) {
1598+
fn check_item(&self, id: ItemId) {
15971599
let tcx = self.tcx;
15981600
let def_id = id.owner_id.def_id;
15991601
let item_visibility = tcx.local_visibility(def_id);
@@ -1721,7 +1723,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> {
17211723
}
17221724
}
17231725

1724-
fn check_foreign_item(&mut self, id: ForeignItemId) {
1726+
fn check_foreign_item(&self, id: ForeignItemId) {
17251727
let tcx = self.tcx;
17261728
let def_id = id.owner_id.def_id;
17271729
let item_visibility = tcx.local_visibility(def_id);
@@ -1853,16 +1855,12 @@ fn effective_visibilities(tcx: TyCtxt<'_>, (): ()) -> &EffectiveVisibilities {
18531855
tcx.arena.alloc(visitor.effective_visibilities)
18541856
}
18551857

1856-
fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
1858+
fn check_private_in_public(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
18571859
let effective_visibilities = tcx.effective_visibilities(());
18581860
// Check for private types in public interfaces.
1859-
let mut checker = PrivateItemsInPublicInterfacesChecker { tcx, effective_visibilities };
1861+
let checker = PrivateItemsInPublicInterfacesChecker { tcx, effective_visibilities };
18601862

1861-
let crate_items = tcx.hir_crate_items(());
1862-
for id in crate_items.free_items() {
1863-
checker.check_item(id);
1864-
}
1865-
for id in crate_items.foreign_items() {
1866-
checker.check_foreign_item(id);
1867-
}
1863+
let crate_items = tcx.hir_module_items(module_def_id);
1864+
let _ = crate_items.par_items(|id| Ok(checker.check_item(id)));
1865+
let _ = crate_items.par_foreign_items(|id| Ok(checker.check_foreign_item(id)));
18681866
}

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,42 @@ LL | struct Priv;
9393
LL | type Alias = Priv;
9494
| ^^^^^^^^^^ can't leak private type
9595

96+
error: type `types::Priv` is more private than the item `types::ES`
97+
--> $DIR/private-in-public-warn.rs:27:9
98+
|
99+
LL | pub static ES: Priv;
100+
| ^^^^^^^^^^^^^^^^^^^ static `types::ES` is reachable at visibility `pub(crate)`
101+
|
102+
note: but type `types::Priv` is only usable at visibility `pub(self)`
103+
--> $DIR/private-in-public-warn.rs:9:5
104+
|
105+
LL | struct Priv;
106+
| ^^^^^^^^^^^
107+
108+
error: type `types::Priv` is more private than the item `types::ef1`
109+
--> $DIR/private-in-public-warn.rs:28:9
110+
|
111+
LL | pub fn ef1(arg: Priv);
112+
| ^^^^^^^^^^^^^^^^^^^^^^ function `types::ef1` is reachable at visibility `pub(crate)`
113+
|
114+
note: but type `types::Priv` is only usable at visibility `pub(self)`
115+
--> $DIR/private-in-public-warn.rs:9:5
116+
|
117+
LL | struct Priv;
118+
| ^^^^^^^^^^^
119+
120+
error: type `types::Priv` is more private than the item `types::ef2`
121+
--> $DIR/private-in-public-warn.rs:29:9
122+
|
123+
LL | pub fn ef2() -> Priv;
124+
| ^^^^^^^^^^^^^^^^^^^^^ function `types::ef2` is reachable at visibility `pub(crate)`
125+
|
126+
note: but type `types::Priv` is only usable at visibility `pub(self)`
127+
--> $DIR/private-in-public-warn.rs:9:5
128+
|
129+
LL | struct Priv;
130+
| ^^^^^^^^^^^
131+
96132
error: trait `traits::PrivTr` is more private than the item `traits::Alias`
97133
--> $DIR/private-in-public-warn.rs:42:5
98134
|
@@ -359,42 +395,6 @@ note: but type `Priv2` is only usable at visibility `pub(self)`
359395
LL | struct Priv2;
360396
| ^^^^^^^^^^^^
361397

362-
error: type `types::Priv` is more private than the item `types::ES`
363-
--> $DIR/private-in-public-warn.rs:27:9
364-
|
365-
LL | pub static ES: Priv;
366-
| ^^^^^^^^^^^^^^^^^^^ static `types::ES` is reachable at visibility `pub(crate)`
367-
|
368-
note: but type `types::Priv` is only usable at visibility `pub(self)`
369-
--> $DIR/private-in-public-warn.rs:9:5
370-
|
371-
LL | struct Priv;
372-
| ^^^^^^^^^^^
373-
374-
error: type `types::Priv` is more private than the item `types::ef1`
375-
--> $DIR/private-in-public-warn.rs:28:9
376-
|
377-
LL | pub fn ef1(arg: Priv);
378-
| ^^^^^^^^^^^^^^^^^^^^^^ function `types::ef1` is reachable at visibility `pub(crate)`
379-
|
380-
note: but type `types::Priv` is only usable at visibility `pub(self)`
381-
--> $DIR/private-in-public-warn.rs:9:5
382-
|
383-
LL | struct Priv;
384-
| ^^^^^^^^^^^
385-
386-
error: type `types::Priv` is more private than the item `types::ef2`
387-
--> $DIR/private-in-public-warn.rs:29:9
388-
|
389-
LL | pub fn ef2() -> Priv;
390-
| ^^^^^^^^^^^^^^^^^^^^^ function `types::ef2` is reachable at visibility `pub(crate)`
391-
|
392-
note: but type `types::Priv` is only usable at visibility `pub(self)`
393-
--> $DIR/private-in-public-warn.rs:9:5
394-
|
395-
LL | struct Priv;
396-
| ^^^^^^^^^^^
397-
398398
warning: bounds on generic parameters in type aliases are not enforced
399399
--> $DIR/private-in-public-warn.rs:42:23
400400
|

tests/ui/privacy/projections.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
warning: type `Priv` is more private than the item `Leak`
2-
--> $DIR/projections.rs:3:5
3-
|
4-
LL | pub type Leak = Priv;
5-
| ^^^^^^^^^^^^^ type alias `Leak` is reachable at visibility `pub(crate)`
6-
|
7-
note: but type `Priv` is only usable at visibility `pub(self)`
8-
--> $DIR/projections.rs:2:5
9-
|
10-
LL | struct Priv;
11-
| ^^^^^^^^^^^
12-
= note: `#[warn(private_interfaces)]` on by default
13-
141
error[E0446]: private type `Priv` in public interface
152
--> $DIR/projections.rs:24:5
163
|
@@ -29,6 +16,19 @@ LL | struct Priv;
2916
LL | type A<T: Trait> = T::A<m::Leak>;
3017
| ^^^^^^^^^^^^^^^^ can't leak private type
3118

19+
warning: type `Priv` is more private than the item `Leak`
20+
--> $DIR/projections.rs:3:5
21+
|
22+
LL | pub type Leak = Priv;
23+
| ^^^^^^^^^^^^^ type alias `Leak` is reachable at visibility `pub(crate)`
24+
|
25+
note: but type `Priv` is only usable at visibility `pub(self)`
26+
--> $DIR/projections.rs:2:5
27+
|
28+
LL | struct Priv;
29+
| ^^^^^^^^^^^
30+
= note: `#[warn(private_interfaces)]` on by default
31+
3232
error: type `Priv` is private
3333
--> $DIR/projections.rs:14:15
3434
|

0 commit comments

Comments
 (0)