Skip to content

Commit a9f6bab

Browse files
committed
convert privacy access levels into a query
1 parent 69c9d9b commit a9f6bab

File tree

10 files changed

+68
-35
lines changed

10 files changed

+68
-35
lines changed

src/librustc/dep_graph/dep_node.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use hir::def_id::CrateNum;
1112
use std::fmt::Debug;
1213
use std::sync::Arc;
1314

@@ -81,7 +82,7 @@ pub enum DepNode<D: Clone + Debug> {
8182
TypeckItemType(D),
8283
UnusedTraitCheck,
8384
CheckConst(D),
84-
Privacy,
85+
PrivacyAccessLevels(CrateNum),
8586
IntrinsicCheck(D),
8687
MatchCheck(D),
8788

@@ -230,7 +231,7 @@ impl<D: Clone + Debug> DepNode<D> {
230231
CheckEntryFn => Some(CheckEntryFn),
231232
Variance => Some(Variance),
232233
UnusedTraitCheck => Some(UnusedTraitCheck),
233-
Privacy => Some(Privacy),
234+
PrivacyAccessLevels(k) => Some(PrivacyAccessLevels(k)),
234235
Reachability => Some(Reachability),
235236
DeadCheck => Some(DeadCheck),
236237
LateLintCheck => Some(LateLintCheck),

src/librustc/lint/context.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ use std::ops::Deref;
4444
use syntax::attr;
4545
use syntax::ast;
4646
use syntax::symbol::Symbol;
47-
use syntax_pos::{MultiSpan, Span};
47+
use syntax_pos::{DUMMY_SP, MultiSpan, Span};
4848
use errors::{self, Diagnostic, DiagnosticBuilder};
4949
use hir;
50+
use hir::def_id::LOCAL_CRATE;
5051
use hir::intravisit as hir_visit;
5152
use syntax::visit as ast_visit;
5253

@@ -1231,10 +1232,11 @@ fn check_lint_name_cmdline(sess: &Session, lint_cx: &LintStore,
12311232
/// Perform lint checking on a crate.
12321233
///
12331234
/// Consumes the `lint_store` field of the `Session`.
1234-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1235-
access_levels: &AccessLevels) {
1235+
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
12361236
let _task = tcx.dep_graph.in_task(DepNode::LateLintCheck);
12371237

1238+
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
1239+
12381240
let krate = tcx.hir.krate();
12391241

12401242
// We want to own the lint store, so move it out of the session.

src/librustc/middle/dead.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ use hir::itemlikevisit::ItemLikeVisitor;
2121
use middle::privacy;
2222
use ty::{self, TyCtxt};
2323
use hir::def::Def;
24-
use hir::def_id::{DefId};
24+
use hir::def_id::{DefId, LOCAL_CRATE};
2525
use lint;
2626
use util::nodemap::FxHashSet;
2727

2828
use syntax::{ast, codemap};
2929
use syntax::attr;
30+
use syntax::codemap::DUMMY_SP;
3031
use syntax_pos;
3132

3233
// Any local node that may call something in its body block should be
@@ -592,9 +593,9 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
592593
}
593594
}
594595

595-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
596-
access_levels: &privacy::AccessLevels) {
596+
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
597597
let _task = tcx.dep_graph.in_task(DepNode::DeadCheck);
598+
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
598599
let krate = tcx.hir.krate();
599600
let live_symbols = find_live(tcx, access_levels, krate);
600601
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };

src/librustc/middle/reachable.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use util::nodemap::{NodeSet, FxHashSet};
2727
use syntax::abi::Abi;
2828
use syntax::ast;
2929
use syntax::attr;
30+
use syntax::codemap::DUMMY_SP;
3031
use hir;
32+
use hir::def_id::LOCAL_CRATE;
3133
use hir::intravisit::{Visitor, NestedVisitorMap};
3234
use hir::itemlikevisit::ItemLikeVisitor;
3335
use hir::intravisit;
@@ -359,11 +361,11 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
359361
}
360362
}
361363

362-
pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
363-
access_levels: &privacy::AccessLevels)
364-
-> NodeSet {
364+
pub fn find_reachable<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> NodeSet {
365365
let _task = tcx.dep_graph.in_task(DepNode::Reachability);
366366

367+
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
368+
367369
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
368370
*ty == config::CrateTypeRlib || *ty == config::CrateTypeDylib ||
369371
*ty == config::CrateTypeProcMacro

src/librustc/middle/stability.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
656656
/// Given the list of enabled features that were not language features (i.e. that
657657
/// were expected to be library features), and the list of features used from
658658
/// libraries, identify activated features that don't exist and error about them.
659-
pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
660-
access_levels: &AccessLevels) {
659+
pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
661660
let sess = &tcx.sess;
662661

662+
let access_levels = &ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE);
663+
663664
if tcx.stability.borrow().staged_api[&LOCAL_CRATE] && tcx.sess.features.borrow().staged_api {
664665
let _task = tcx.dep_graph.in_task(DepNode::StabilityIndex);
665666
let krate = tcx.hir.krate();

src/librustc/ty/maps.rs

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use dep_graph::{DepGraph, DepNode, DepTrackingMap, DepTrackingMapConfig};
1212
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1313
use middle::const_val::ConstVal;
14+
use middle::privacy::AccessLevels;
1415
use mir;
1516
use ty::{self, Ty, TyCtxt};
1617

@@ -189,6 +190,12 @@ impl<'tcx> QueryDescription for queries::mir_shims<'tcx> {
189190
}
190191
}
191192

193+
impl<'tcx> QueryDescription for queries::privacy_access_levels<'tcx> {
194+
fn describe(_: TyCtxt, _: CrateNum) -> String {
195+
format!("privacy access levels")
196+
}
197+
}
198+
192199
macro_rules! define_maps {
193200
(<$tcx:tt>
194201
$($(#[$attr:meta])*
@@ -406,6 +413,9 @@ define_maps! { <'tcx>
406413
/// other items, such as enum variant explicit discriminants.
407414
pub monomorphic_const_eval: MonomorphicConstEval(DefId) -> Result<ConstVal<'tcx>, ()>,
408415

416+
/// Performs the privacy check and computes "access levels".
417+
pub privacy_access_levels: PrivacyAccessLevels(CrateNum) -> Rc<AccessLevels>,
418+
409419
pub mir_shims: mir_shim(ty::InstanceDef<'tcx>) -> &'tcx RefCell<mir::Mir<'tcx>>
410420
}
411421

src/librustc/ty/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ pub use self::fold::TypeFoldable;
1717

1818
use dep_graph::{self, DepNode};
1919
use hir::{map as hir_map, FreevarMap, TraitMap};
20-
use middle;
2120
use hir::def::{Def, CtorKind, ExportMap};
2221
use hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2322
use middle::const_val::ConstVal;
2423
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
24+
use middle::privacy::AccessLevels;
2525
use middle::region::{CodeExtent, ROOT_CODE_EXTENT};
2626
use middle::resolve_lifetime::ObjectLifetimeDefault;
2727
use mir::Mir;
@@ -108,9 +108,12 @@ mod sty;
108108

109109
/// The complete set of all analyses described in this module. This is
110110
/// produced by the driver and fed to trans and later passes.
111+
///
112+
/// NB: These contents are being migrated into queries using the
113+
/// *on-demand* infrastructure.
111114
#[derive(Clone)]
112115
pub struct CrateAnalysis {
113-
pub access_levels: middle::privacy::AccessLevels,
116+
pub access_levels: Rc<AccessLevels>,
114117
pub reachable: NodeSet,
115118
pub name: String,
116119
pub glob_map: Option<hir::GlobMap>,

src/librustc_driver/driver.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use std::fs;
4848
use std::io::{self, Write};
4949
use std::iter;
5050
use std::path::{Path, PathBuf};
51+
use std::rc::Rc;
5152
use syntax::{ast, diagnostics, visit};
5253
use syntax::attr;
5354
use syntax::ext::base::ExtCtxt;
@@ -807,7 +808,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
807808
expanded_crate: krate,
808809
defs: resolver.definitions,
809810
analysis: ty::CrateAnalysis {
810-
access_levels: AccessLevels::default(),
811+
access_levels: Rc::new(AccessLevels::default()),
811812
reachable: NodeSet(),
812813
name: crate_name.to_string(),
813814
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
@@ -888,6 +889,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
888889

889890
let mut local_providers = ty::maps::Providers::default();
890891
mir::provide(&mut local_providers);
892+
rustc_privacy::provide(&mut local_providers);
891893
typeck::provide(&mut local_providers);
892894
ty::provide(&mut local_providers);
893895

@@ -931,9 +933,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
931933
|| consts::check_crate(tcx));
932934

933935
analysis.access_levels =
934-
time(time_passes, "privacy checking", || {
935-
rustc_privacy::check_crate(tcx)
936-
});
936+
time(time_passes, "privacy checking", || rustc_privacy::check_crate(tcx));
937937

938938
time(time_passes,
939939
"intrinsic checking",
@@ -1000,19 +1000,15 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
10001000
analysis.reachable =
10011001
time(time_passes,
10021002
"reachability checking",
1003-
|| reachable::find_reachable(tcx, &analysis.access_levels));
1003+
|| reachable::find_reachable(tcx));
10041004

1005-
time(time_passes, "death checking", || {
1006-
middle::dead::check_crate(tcx, &analysis.access_levels);
1007-
});
1005+
time(time_passes, "death checking", || middle::dead::check_crate(tcx));
10081006

10091007
time(time_passes, "unused lib feature checking", || {
1010-
stability::check_unused_or_stable_features(tcx, &analysis.access_levels)
1008+
stability::check_unused_or_stable_features(tcx)
10111009
});
10121010

1013-
time(time_passes,
1014-
"lint checking",
1015-
|| lint::check_crate(tcx, &analysis.access_levels));
1011+
time(time_passes, "lint checking", || lint::check_crate(tcx));
10161012

10171013
// The above three passes generate errors w/o aborting
10181014
if sess.err_count() > 0 {

src/librustc_privacy/lib.rs

+23-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
html_root_url = "https://doc.rust-lang.org/nightly/")]
1818
#![deny(warnings)]
1919

20+
#![cfg_attr(stage0, feature(field_init_shorthand))]
2021
#![feature(rustc_diagnostic_macros)]
2122
#![feature(rustc_private)]
2223
#![feature(staged_api)]
@@ -25,23 +26,24 @@ extern crate rustc;
2526
#[macro_use] extern crate syntax;
2627
extern crate syntax_pos;
2728

28-
use rustc::dep_graph::DepNode;
2929
use rustc::hir::{self, PatKind};
3030
use rustc::hir::def::Def;
31-
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
31+
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, CrateNum, DefId};
3232
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
3333
use rustc::hir::itemlikevisit::DeepVisitor;
3434
use rustc::hir::pat_util::EnumerateAndAdjustIterator;
3535
use rustc::lint;
3636
use rustc::middle::privacy::{AccessLevel, AccessLevels};
3737
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable};
3838
use rustc::ty::fold::TypeVisitor;
39+
use rustc::ty::maps::Providers;
3940
use rustc::util::nodemap::NodeSet;
4041
use syntax::ast;
41-
use syntax_pos::Span;
42+
use syntax_pos::{DUMMY_SP, Span};
4243

4344
use std::cmp;
4445
use std::mem::replace;
46+
use std::rc::Rc;
4547

4648
pub mod diagnostics;
4749

@@ -1203,8 +1205,23 @@ impl<'a, 'tcx> Visitor<'tcx> for PrivateItemsInPublicInterfacesVisitor<'a, 'tcx>
12031205
fn visit_pat(&mut self, _: &'tcx hir::Pat) {}
12041206
}
12051207

1206-
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> AccessLevels {
1207-
let _task = tcx.dep_graph.in_task(DepNode::Privacy);
1208+
pub fn provide(providers: &mut Providers) {
1209+
*providers = Providers {
1210+
privacy_access_levels,
1211+
..*providers
1212+
};
1213+
}
1214+
1215+
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Rc<AccessLevels> {
1216+
tcx.dep_graph.with_ignore(|| { // FIXME
1217+
ty::queries::privacy_access_levels::get(tcx, DUMMY_SP, LOCAL_CRATE)
1218+
})
1219+
}
1220+
1221+
fn privacy_access_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1222+
krate: CrateNum)
1223+
-> Rc<AccessLevels> {
1224+
assert_eq!(krate, LOCAL_CRATE);
12081225

12091226
let krate = tcx.hir.krate();
12101227

@@ -1266,7 +1283,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> AccessLevels {
12661283
krate.visit_all_item_likes(&mut DeepVisitor::new(&mut visitor));
12671284
}
12681285

1269-
visitor.access_levels
1286+
Rc::new(visitor.access_levels)
12701287
}
12711288

12721289
__build_diagnostic_array! { librustc_privacy, DIAGNOSTICS }

src/librustdoc/core.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ pub fn run_core(search_paths: SearchPaths,
184184
// Convert from a NodeId set to a DefId set since we don't always have easy access
185185
// to the map from defid -> nodeid
186186
let access_levels = AccessLevels {
187-
map: access_levels.map.into_iter()
188-
.map(|(k, v)| (tcx.hir.local_def_id(k), v))
187+
map: access_levels.map.iter()
188+
.map(|(&k, &v)| (tcx.hir.local_def_id(k), v))
189189
.collect()
190190
};
191191

0 commit comments

Comments
 (0)