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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 8 additions & 2 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 10 additions & 0 deletions
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

0 commit comments

Comments
 (0)