Skip to content

Commit 22f914a

Browse files
authored
Rollup merge of #40771 - nikomatsakis:issue-40746-privacy-access-levels, r=eddyb
"on-demandify" privacy and access levels r? @eddyb cc @cramertj #40746
2 parents 03d54a4 + a9f6bab commit 22f914a

File tree

19 files changed

+108
-85
lines changed

19 files changed

+108
-85
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/dep_graph/edges.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ impl<D: Clone + Debug + Eq + Hash> DepGraphEdges<D> {
101101
}
102102

103103
/// Indicates that the current task `C` reads `v` by adding an
104-
/// edge from `v` to `C`. If there is no current task, panics. If
105-
/// you want to suppress this edge, use `ignore`.
104+
/// edge from `v` to `C`. If there is no current task, has no
105+
/// effect. Note that *reading* from tracked state is harmless if
106+
/// you are not in a task; what is bad is *writing* to tracked
107+
/// state (and leaking data that you read into a tracked task).
106108
pub fn read(&mut self, v: DepNode<D>) {
107-
let source = self.make_node(v);
108-
self.add_edge_from_current_node(|current| (source, current))
109+
if self.current_node().is_some() {
110+
let source = self.make_node(v);
111+
self.add_edge_from_current_node(|current| (source, current))
112+
}
109113
}
110114

111115
/// Indicates that the current task `C` writes `v` by adding an

src/librustc/dep_graph/shadow.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ impl ShadowGraph {
8080

8181
let mut stack = self.stack.borrow_mut();
8282
match *message {
83-
DepMessage::Read(ref n) => self.check_edge(Some(Some(n)), top(&stack)),
83+
// It is ok to READ shared state outside of a
84+
// task. That can't do any harm (at least, the only
85+
// way it can do harm is by leaking that data into a
86+
// query or task, which would be a problem
87+
// anyway). What would be bad is WRITING to that
88+
// state.
89+
DepMessage::Read(_) => { }
8490
DepMessage::Write(ref n) => self.check_edge(top(&stack), Some(Some(n))),
8591
DepMessage::PushTask(ref n) => stack.push(Some(n.clone())),
8692
DepMessage::PushIgnore => stack.push(None),
@@ -116,7 +122,7 @@ impl ShadowGraph {
116122
(None, None) => unreachable!(),
117123

118124
// nothing on top of the stack
119-
(None, Some(n)) | (Some(n), None) => bug!("read/write of {:?} but no current task", n),
125+
(None, Some(n)) | (Some(n), None) => bug!("write of {:?} but no current task", n),
120126

121127
// this corresponds to an Ignore being top of the stack
122128
(Some(None), _) | (_, Some(None)) => (),

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/cstore.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ pub trait CrateStore {
255255
fn used_crates(&self, prefer: LinkagePreference) -> Vec<(CrateNum, LibSource)>;
256256
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource;
257257
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
258-
fn encode_metadata<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
259-
reexports: &def::ExportMap,
258+
fn encode_metadata<'a, 'tcx>(&self,
259+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
260260
link_meta: &LinkMeta,
261261
reachable: &NodeSet) -> Vec<u8>;
262262
fn metadata_encoding_version(&self) -> &[u8];
@@ -412,10 +412,10 @@ impl CrateStore for DummyCrateStore {
412412
{ vec![] }
413413
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource { bug!("used_crate_source") }
414414
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> { None }
415-
fn encode_metadata<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
416-
reexports: &def::ExportMap,
417-
link_meta: &LinkMeta,
418-
reachable: &NodeSet) -> Vec<u8> { vec![] }
415+
fn encode_metadata<'a, 'tcx>(&self,
416+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
417+
link_meta: &LinkMeta,
418+
reachable: &NodeSet) -> Vec<u8> { vec![] }
419419
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
420420
}
421421

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/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use session::Session;
1515
use lint;
1616
use middle;
1717
use hir::TraitMap;
18-
use hir::def::Def;
18+
use hir::def::{Def, ExportMap};
1919
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
2020
use hir::map as hir_map;
2121
use hir::map::DisambiguatedDefPathData;
@@ -416,6 +416,9 @@ pub struct GlobalCtxt<'tcx> {
416416
/// is relevant; generated by resolve.
417417
pub trait_map: TraitMap,
418418

419+
/// Export map produced by name resolution.
420+
pub export_map: ExportMap,
421+
419422
pub named_region_map: resolve_lifetime::NamedRegionMap,
420423

421424
pub region_maps: RegionMaps,
@@ -698,6 +701,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
698701
region_maps: region_maps,
699702
variance_computed: Cell::new(false),
700703
trait_map: resolutions.trait_map,
704+
export_map: resolutions.export_map,
701705
fulfilled_predicates: RefCell::new(fulfilled_predicates),
702706
hir: hir,
703707
maps: maps::Maps::new(dep_graph, providers),

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

+6-3
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,10 +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 export_map: ExportMap,
114-
pub access_levels: middle::privacy::AccessLevels,
116+
pub access_levels: Rc<AccessLevels>,
115117
pub reachable: NodeSet,
116118
pub name: String,
117119
pub glob_map: Option<hir::GlobMap>,
@@ -122,6 +124,7 @@ pub struct Resolutions {
122124
pub freevars: FreevarMap,
123125
pub trait_map: TraitMap,
124126
pub maybe_unused_trait_imports: NodeSet,
127+
pub export_map: ExportMap,
125128
}
126129

127130
#[derive(Clone, Copy, PartialEq, Eq, Debug)]

src/librustc_driver/driver.rs

+10-14
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,18 +808,18 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
807808
expanded_crate: krate,
808809
defs: resolver.definitions,
809810
analysis: ty::CrateAnalysis {
810-
export_map: resolver.export_map,
811-
access_levels: AccessLevels::default(),
811+
access_levels: Rc::new(AccessLevels::default()),
812812
reachable: NodeSet(),
813813
name: crate_name.to_string(),
814814
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
815815
},
816816
resolutions: Resolutions {
817817
freevars: resolver.freevars,
818+
export_map: resolver.export_map,
818819
trait_map: resolver.trait_map,
819820
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
820821
},
821-
hir_forest: hir_forest
822+
hir_forest: hir_forest,
822823
})
823824
}
824825

@@ -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, &analysis.export_map)
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_metadata/cstore_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,12 @@ impl CrateStore for cstore::CStore {
496496
self.do_extern_mod_stmt_cnum(emod_id)
497497
}
498498

499-
fn encode_metadata<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
500-
reexports: &def::ExportMap,
499+
fn encode_metadata<'a, 'tcx>(&self,
500+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
501501
link_meta: &LinkMeta,
502502
reachable: &NodeSet) -> Vec<u8>
503503
{
504-
encoder::encode_metadata(tcx, self, reexports, link_meta, reachable)
504+
encoder::encode_metadata(tcx, self, link_meta, reachable)
505505
}
506506

507507
fn metadata_encoding_version(&self) -> &[u8]

0 commit comments

Comments
 (0)