Skip to content

Commit d5bb71c

Browse files
committed
Split up privacy checking so privacy_access_levels only does computations required for AccessLevels
1 parent f22dca0 commit d5bb71c

File tree

7 files changed

+84
-70
lines changed

7 files changed

+84
-70
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ define_dep_nodes!( <'tcx>
456456
[eval_always] CoherenceInherentImplOverlapCheck,
457457
[] CoherenceCheckTrait(DefId),
458458
[eval_always] PrivacyAccessLevels(CrateNum),
459+
[eval_always] CheckPrivacy(CrateNum),
459460
[eval_always] Analysis(CrateNum),
460461

461462
// Represents the MIR for a fn; also used as the task node for

src/librustc/ty/query/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::privacy_access_levels<'tcx> {
369369
}
370370
}
371371

372+
impl<'tcx> QueryDescription<'tcx> for queries::check_privacy<'tcx> {
373+
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
374+
"privacy checking".into()
375+
}
376+
}
377+
372378
impl<'tcx> QueryDescription<'tcx> for queries::typeck_item_bodies<'tcx> {
373379
fn describe(_: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
374380
"type-checking all item bodies".into()

src/librustc/ty/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,9 @@ define_queries! { <'tcx>
350350
[] fn check_match: CheckMatch(DefId)
351351
-> Result<(), ErrorReported>,
352352

353-
/// Performs the privacy check and computes "access levels".
353+
/// Performs part of the privacy check and computes "access levels".
354354
[] fn privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Lrc<AccessLevels>,
355+
[] fn check_privacy: CheckPrivacy(CrateNum) -> (),
355356
},
356357

357358
Other {

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
12511251
force!(crate_inherent_impls_overlap_check, LOCAL_CRATE)
12521252
},
12531253
DepKind::PrivacyAccessLevels => { force!(privacy_access_levels, LOCAL_CRATE); }
1254+
DepKind::CheckPrivacy => { force!(check_privacy, LOCAL_CRATE); }
12541255
DepKind::MirBuilt => { force!(mir_built, def_id!()); }
12551256
DepKind::MirConstQualif => { force!(mir_const_qualif, def_id!()); }
12561257
DepKind::MirConst => { force!(mir_const, def_id!()); }

src/librustc_interface/passes.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_borrowck as borrowck;
2121
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2222
use rustc_data_structures::fingerprint::Fingerprint;
2323
use rustc_data_structures::stable_hasher::StableHasher;
24-
use rustc_data_structures::sync::Lrc;
24+
use rustc_data_structures::sync::{Lrc, ParallelIterator, par_iter};
2525
use rustc_incremental;
2626
use rustc_metadata::creader::CrateLoader;
2727
use rustc_metadata::cstore::{self, CStore};
@@ -278,17 +278,28 @@ fn analysis<'tcx>(
278278

279279
time(sess, "misc checking", || {
280280
parallel!({
281-
time(sess, "privacy checking", || {
282-
rustc_privacy::check_crate(tcx)
281+
time(sess, "privacy access levels", || {
282+
tcx.ensure().privacy_access_levels(LOCAL_CRATE);
283283
});
284-
}, {
285-
time(sess, "death checking", || middle::dead::check_crate(tcx));
286-
}, {
287-
time(sess, "unused lib feature checking", || {
288-
stability::check_unused_or_stable_features(tcx)
284+
parallel!({
285+
time(sess, "privacy checking", || {
286+
tcx.ensure().check_privacy(LOCAL_CRATE);
287+
});
288+
}, {
289+
time(sess, "death checking", || middle::dead::check_crate(tcx));
290+
}, {
291+
time(sess, "unused lib feature checking", || {
292+
stability::check_unused_or_stable_features(tcx)
293+
});
294+
}, {
295+
time(sess, "lint checking", || lint::check_crate(tcx));
289296
});
290297
}, {
291-
time(sess, "lint checking", || lint::check_crate(tcx));
298+
time(sess, "privacy checking modules", || {
299+
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
300+
tcx.ensure().check_mod_privacy(tcx.hir().local_def_id(module));
301+
});
302+
});
292303
});
293304
});
294305

src/librustc_privacy/lib.rs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,19 +1760,15 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
17601760
pub fn provide(providers: &mut Providers<'_>) {
17611761
*providers = Providers {
17621762
privacy_access_levels,
1763+
check_privacy,
17631764
check_mod_privacy,
17641765
..*providers
17651766
};
17661767
}
17671768

1768-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Lrc<AccessLevels> {
1769-
tcx.privacy_access_levels(LOCAL_CRATE)
1770-
}
1771-
17721769
fn check_mod_privacy<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, module_def_id: DefId) {
17731770
let empty_tables = ty::TypeckTables::empty(None);
17741771

1775-
17761772
// Check privacy of names not checked in previous compilation stages.
17771773
let mut visitor = NamePrivacyVisitor {
17781774
tcx,
@@ -1803,18 +1799,6 @@ fn privacy_access_levels<'tcx>(
18031799
) -> Lrc<AccessLevels> {
18041800
assert_eq!(krate, LOCAL_CRATE);
18051801

1806-
let krate = tcx.hir().krate();
1807-
1808-
for &module in krate.modules.keys() {
1809-
tcx.ensure().check_mod_privacy(tcx.hir().local_def_id(module));
1810-
}
1811-
1812-
let private_crates: FxHashSet<CrateNum> = tcx.sess.opts.extern_private.iter()
1813-
.flat_map(|c| {
1814-
tcx.crates().iter().find(|&&krate| &tcx.crate_name(krate) == c).cloned()
1815-
}).collect();
1816-
1817-
18181802
// Build up a set of all exported items in the AST. This is a set of all
18191803
// items which are reachable from external crates based on visibility.
18201804
let mut visitor = EmbargoVisitor {
@@ -1824,7 +1808,7 @@ fn privacy_access_levels<'tcx>(
18241808
changed: false,
18251809
};
18261810
loop {
1827-
intravisit::walk_crate(&mut visitor, krate);
1811+
intravisit::walk_crate(&mut visitor, tcx.hir().krate());
18281812
if visitor.changed {
18291813
visitor.changed = false;
18301814
} else {
@@ -1833,36 +1817,46 @@ fn privacy_access_levels<'tcx>(
18331817
}
18341818
visitor.update(hir::CRATE_HIR_ID, Some(AccessLevel::Public));
18351819

1836-
{
1837-
let mut visitor = ObsoleteVisiblePrivateTypesVisitor {
1838-
tcx,
1839-
access_levels: &visitor.access_levels,
1840-
in_variant: false,
1841-
old_error_set: Default::default(),
1842-
};
1843-
intravisit::walk_crate(&mut visitor, krate);
1820+
Lrc::new(visitor.access_levels)
1821+
}
18441822

1823+
fn check_privacy<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>, krate: CrateNum) {
1824+
assert_eq!(krate, LOCAL_CRATE);
18451825

1846-
let has_pub_restricted = {
1847-
let mut pub_restricted_visitor = PubRestrictedVisitor {
1848-
tcx,
1849-
has_pub_restricted: false
1850-
};
1851-
intravisit::walk_crate(&mut pub_restricted_visitor, krate);
1852-
pub_restricted_visitor.has_pub_restricted
1853-
};
1826+
let access_levels = tcx.privacy_access_levels(LOCAL_CRATE);
18541827

1855-
// Check for private types and traits in public interfaces.
1856-
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
1828+
let krate = tcx.hir().krate();
1829+
1830+
let mut visitor = ObsoleteVisiblePrivateTypesVisitor {
1831+
tcx,
1832+
access_levels: &access_levels,
1833+
in_variant: false,
1834+
old_error_set: Default::default(),
1835+
};
1836+
intravisit::walk_crate(&mut visitor, krate);
1837+
1838+
let has_pub_restricted = {
1839+
let mut pub_restricted_visitor = PubRestrictedVisitor {
18571840
tcx,
1858-
has_pub_restricted,
1859-
old_error_set: &visitor.old_error_set,
1860-
private_crates
1841+
has_pub_restricted: false
18611842
};
1862-
krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
1863-
}
1843+
intravisit::walk_crate(&mut pub_restricted_visitor, krate);
1844+
pub_restricted_visitor.has_pub_restricted
1845+
};
18641846

1865-
Lrc::new(visitor.access_levels)
1847+
let private_crates: FxHashSet<CrateNum> = tcx.sess.opts.extern_private.iter()
1848+
.flat_map(|c| {
1849+
tcx.crates().iter().find(|&&krate| &tcx.crate_name(krate) == c).cloned()
1850+
}).collect();
1851+
1852+
// Check for private types and traits in public interfaces.
1853+
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
1854+
tcx,
1855+
has_pub_restricted,
1856+
old_error_set: &visitor.old_error_set,
1857+
private_crates
1858+
};
1859+
krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
18661860
}
18671861

18681862
__build_diagnostic_array! { librustc_privacy, DIAGNOSTICS }

src/test/ui/privacy/private-inferred-type.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
error[E0446]: private type `m::Priv` in public interface
2+
--> $DIR/private-inferred-type.rs:61:36
3+
|
4+
LL | struct Priv;
5+
| - `m::Priv` declared as private
6+
...
7+
LL | impl TraitWithAssocTy for u8 { type AssocTy = Priv; }
8+
| ^^^^^^^^^^^^^^^^^^^^ can't leak private type
9+
10+
error[E0446]: private type `adjust::S2` in public interface
11+
--> $DIR/private-inferred-type.rs:83:9
12+
|
13+
LL | struct S2;
14+
| - `adjust::S2` declared as private
15+
...
16+
LL | type Target = S2Alias; //~ ERROR private type `adjust::S2` in public interface
17+
| ^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
18+
119
error: type `m::Priv` is private
220
--> $DIR/private-inferred-type.rs:97:9
321
|
@@ -202,24 +220,6 @@ error: type `m::Priv` is private
202220
LL | match a { //~ ERROR type `m::Priv` is private
203221
| ^
204222

205-
error[E0446]: private type `m::Priv` in public interface
206-
--> $DIR/private-inferred-type.rs:61:36
207-
|
208-
LL | struct Priv;
209-
| - `m::Priv` declared as private
210-
...
211-
LL | impl TraitWithAssocTy for u8 { type AssocTy = Priv; }
212-
| ^^^^^^^^^^^^^^^^^^^^ can't leak private type
213-
214-
error[E0446]: private type `adjust::S2` in public interface
215-
--> $DIR/private-inferred-type.rs:83:9
216-
|
217-
LL | struct S2;
218-
| - `adjust::S2` declared as private
219-
...
220-
LL | type Target = S2Alias; //~ ERROR private type `adjust::S2` in public interface
221-
| ^^^^^^^^^^^^^^^^^^^^^^ can't leak private type
222-
223223
error: aborting due to 33 previous errors
224224

225225
For more information about this error, try `rustc --explain E0446`.

0 commit comments

Comments
 (0)