Skip to content

Commit e8c28e2

Browse files
committed
rustc_metadata: Privatize private code and remove dead code
1 parent 446e5e5 commit e8c28e2

File tree

17 files changed

+202
-257
lines changed

17 files changed

+202
-257
lines changed

src/librustc_driver/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl RustcDefaultCalls {
616616
let mut v = Vec::new();
617617
locator::list_file_metadata(&sess.target.target,
618618
path,
619-
&*cstore.metadata_loader,
619+
cstore,
620620
&mut v)
621621
.unwrap();
622622
println!("{}", String::from_utf8(v).unwrap());

src/librustc_interface/passes.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,15 @@ fn write_out_deps(compiler: &Compiler, outputs: &OutputFilenames, out_filenames:
663663

664664
if sess.binary_dep_depinfo() {
665665
for cnum in compiler.cstore.crates_untracked() {
666-
let metadata = compiler.cstore.crate_data_as_rc_any(cnum);
667-
let metadata = metadata.downcast_ref::<cstore::CrateMetadata>().unwrap();
668-
if let Some((path, _)) = &metadata.source.dylib {
669-
files.push(escape_dep_filename(&FileName::Real(path.clone())));
666+
let source = compiler.cstore.crate_source_untracked(cnum);
667+
if let Some((path, _)) = source.dylib {
668+
files.push(escape_dep_filename(&FileName::Real(path)));
670669
}
671-
if let Some((path, _)) = &metadata.source.rlib {
672-
files.push(escape_dep_filename(&FileName::Real(path.clone())));
670+
if let Some((path, _)) = source.rlib {
671+
files.push(escape_dep_filename(&FileName::Real(path)));
673672
}
674-
if let Some((path, _)) = &metadata.source.rmeta {
675-
files.push(escape_dep_filename(&FileName::Real(path.clone())));
673+
if let Some((path, _)) = source.rmeta {
674+
files.push(escape_dep_filename(&FileName::Real(path)));
676675
}
677676
}
678677
}

src/librustc_metadata/creader.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Validates all used crates and extern libraries and loads their metadata
22
3-
use crate::cstore::{self, CStore, CrateSource, MetadataBlob};
3+
use crate::cstore::{self, CStore, MetadataBlob};
44
use crate::locator::{self, CratePaths};
55
use crate::schema::{CrateRoot, CrateDep};
66
use rustc_data_structures::sync::{Lrc, RwLock, Lock, AtomicCell};
@@ -14,7 +14,7 @@ use rustc::session::{Session, CrateDisambiguator};
1414
use rustc::session::config::{Sanitizer, self};
1515
use rustc_target::spec::{PanicStrategy, TargetTriple};
1616
use rustc::session::search_paths::PathKind;
17-
use rustc::middle::cstore::{ExternCrate, ExternCrateSource};
17+
use rustc::middle::cstore::{CrateSource, ExternCrate, ExternCrateSource};
1818
use rustc::util::common::record_time;
1919
use rustc::util::nodemap::FxHashSet;
2020
use rustc::hir::map::Definitions;
@@ -33,15 +33,15 @@ use syntax_pos::{Span, DUMMY_SP};
3333
use log::{debug, info, log_enabled};
3434
use proc_macro::bridge::client::ProcMacro;
3535

36-
pub struct Library {
36+
crate struct Library {
3737
pub dylib: Option<(PathBuf, PathKind)>,
3838
pub rlib: Option<(PathBuf, PathKind)>,
3939
pub rmeta: Option<(PathBuf, PathKind)>,
4040
pub metadata: MetadataBlob,
4141
}
4242

4343
pub struct CrateLoader<'a> {
44-
pub sess: &'a Session,
44+
sess: &'a Session,
4545
cstore: &'a CStore,
4646
local_crate_name: Symbol,
4747
}
@@ -268,13 +268,12 @@ impl<'a> CrateLoader<'a> {
268268
source_map_import_info: RwLock::new(vec![]),
269269
alloc_decoding_state: AllocDecodingState::new(interpret_alloc_index),
270270
dep_kind: Lock::new(dep_kind),
271-
source: cstore::CrateSource {
271+
source: CrateSource {
272272
dylib,
273273
rlib,
274274
rmeta,
275275
},
276276
private_dep,
277-
span,
278277
raw_proc_macros,
279278
dep_node_index: AtomicCell::new(DepNodeIndex::INVALID),
280279
};

src/librustc_metadata/cstore.rs

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,29 @@ use crate::schema;
55
use rustc::dep_graph::DepNodeIndex;
66
use rustc::hir::def_id::{CrateNum, DefIndex};
77
use rustc::hir::map::definitions::DefPathTable;
8-
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
8+
use rustc::middle::cstore::{CrateSource, DepKind, ExternCrate, MetadataLoader};
99
use rustc::mir::interpret::AllocDecodingState;
1010
use rustc_index::vec::IndexVec;
1111
use rustc::util::nodemap::{FxHashMap, NodeMap};
12-
13-
use rustc_data_structures::sync::{Lrc, RwLock, Lock, AtomicCell};
12+
use rustc_data_structures::sync::{Lrc, RwLock, Lock, MetadataRef, AtomicCell};
1413
use syntax::ast;
1514
use syntax::ext::base::SyntaxExtension;
1615
use syntax_pos;
17-
18-
pub use rustc::middle::cstore::{NativeLibrary, NativeLibraryKind, LinkagePreference};
19-
pub use rustc::middle::cstore::NativeLibraryKind::*;
20-
pub use rustc::middle::cstore::{CrateSource, LibSource, ForeignModule};
16+
use proc_macro::bridge::client::ProcMacro;
2117

2218
pub use crate::cstore_impl::{provide, provide_extern};
2319

2420
// A map from external crate numbers (as decoded from some crate file) to
2521
// local crate numbers (as generated during this session). Each external
2622
// crate may refer to types in other external crates, and each has their
2723
// own crate numbers.
28-
pub type CrateNumMap = IndexVec<CrateNum, CrateNum>;
24+
crate type CrateNumMap = IndexVec<CrateNum, CrateNum>;
2925

30-
pub use rustc_data_structures::sync::MetadataRef;
31-
use syntax_pos::Span;
32-
use proc_macro::bridge::client::ProcMacro;
33-
34-
pub struct MetadataBlob(pub MetadataRef);
26+
crate struct MetadataBlob(pub MetadataRef);
3527

3628
/// Holds information about a syntax_pos::SourceFile imported from another crate.
3729
/// See `imported_source_files()` for more information.
38-
pub struct ImportedSourceFile {
30+
crate struct ImportedSourceFile {
3931
/// This SourceFile's byte-offset within the source_map of its original crate
4032
pub original_start_pos: syntax_pos::BytePos,
4133
/// The end of this SourceFile within the source_map of its original crate
@@ -48,56 +40,54 @@ pub struct CrateMetadata {
4840
/// Information about the extern crate that caused this crate to
4941
/// be loaded. If this is `None`, then the crate was injected
5042
/// (e.g., by the allocator)
51-
pub extern_crate: Lock<Option<ExternCrate>>,
43+
crate extern_crate: Lock<Option<ExternCrate>>,
5244

53-
pub blob: MetadataBlob,
54-
pub cnum_map: CrateNumMap,
55-
pub cnum: CrateNum,
56-
pub dependencies: Lock<Vec<CrateNum>>,
57-
pub source_map_import_info: RwLock<Vec<ImportedSourceFile>>,
45+
crate blob: MetadataBlob,
46+
crate cnum_map: CrateNumMap,
47+
crate cnum: CrateNum,
48+
crate dependencies: Lock<Vec<CrateNum>>,
49+
crate source_map_import_info: RwLock<Vec<ImportedSourceFile>>,
5850

5951
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
60-
pub alloc_decoding_state: AllocDecodingState,
52+
crate alloc_decoding_state: AllocDecodingState,
6153

6254
// NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this
6355
// lifetime is only used behind `Lazy`, and therefore acts like an
6456
// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
6557
// is being used to decode those values.
66-
pub root: schema::CrateRoot<'static>,
58+
crate root: schema::CrateRoot<'static>,
6759

6860
/// For each definition in this crate, we encode a key. When the
6961
/// crate is loaded, we read all the keys and put them in this
7062
/// hashmap, which gives the reverse mapping. This allows us to
7163
/// quickly retrace a `DefPath`, which is needed for incremental
7264
/// compilation support.
73-
pub def_path_table: Lrc<DefPathTable>,
65+
crate def_path_table: Lrc<DefPathTable>,
7466

75-
pub trait_impls: FxHashMap<(u32, DefIndex), schema::Lazy<[DefIndex]>>,
67+
crate trait_impls: FxHashMap<(u32, DefIndex), schema::Lazy<[DefIndex]>>,
7668

77-
pub dep_kind: Lock<DepKind>,
78-
pub source: CrateSource,
69+
crate dep_kind: Lock<DepKind>,
70+
crate source: CrateSource,
7971

8072
/// Whether or not this crate should be consider a private dependency
8173
/// for purposes of the 'exported_private_dependencies' lint
82-
pub private_dep: bool,
83-
84-
pub span: Span,
74+
crate private_dep: bool,
8575

86-
pub raw_proc_macros: Option<&'static [ProcMacro]>,
76+
crate raw_proc_macros: Option<&'static [ProcMacro]>,
8777

8878
/// The `DepNodeIndex` of the `DepNode` representing this upstream crate.
8979
/// It is initialized on the first access in `get_crate_dep_node_index()`.
9080
/// Do not access the value directly, as it might not have been initialized
9181
/// yet.
9282
/// The field must always be initialized to `DepNodeIndex::INVALID`.
93-
pub(super) dep_node_index: AtomicCell<DepNodeIndex>,
83+
crate dep_node_index: AtomicCell<DepNodeIndex>,
9484
}
9585

9686
pub struct CStore {
9787
metas: RwLock<IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>>,
9888
/// Map from NodeId's of local extern crate statements to crate numbers
9989
extern_mod_crate_map: Lock<NodeMap<CrateNum>>,
100-
pub metadata_loader: Box<dyn MetadataLoader + Sync>,
90+
crate metadata_loader: Box<dyn MetadataLoader + Sync>,
10191
}
10292

10393
pub enum LoadedMacro {
@@ -118,25 +108,25 @@ impl CStore {
118108
}
119109
}
120110

121-
pub(super) fn alloc_new_crate_num(&self) -> CrateNum {
111+
crate fn alloc_new_crate_num(&self) -> CrateNum {
122112
let mut metas = self.metas.borrow_mut();
123113
let cnum = CrateNum::new(metas.len());
124114
metas.push(None);
125115
cnum
126116
}
127117

128-
pub(super) fn get_crate_data(&self, cnum: CrateNum) -> Lrc<CrateMetadata> {
118+
crate fn get_crate_data(&self, cnum: CrateNum) -> Lrc<CrateMetadata> {
129119
self.metas.borrow()[cnum].clone()
130120
.unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum))
131121
}
132122

133-
pub(super) fn set_crate_data(&self, cnum: CrateNum, data: Lrc<CrateMetadata>) {
123+
crate fn set_crate_data(&self, cnum: CrateNum, data: Lrc<CrateMetadata>) {
134124
let mut metas = self.metas.borrow_mut();
135125
assert!(metas[cnum].is_none(), "Overwriting crate metadata entry");
136126
metas[cnum] = Some(data);
137127
}
138128

139-
pub(super) fn iter_crate_data<I>(&self, mut i: I)
129+
crate fn iter_crate_data<I>(&self, mut i: I)
140130
where I: FnMut(CrateNum, &Lrc<CrateMetadata>)
141131
{
142132
for (k, v) in self.metas.borrow().iter_enumerated() {
@@ -146,16 +136,14 @@ impl CStore {
146136
}
147137
}
148138

149-
pub(super) fn crate_dependencies_in_rpo(&self, krate: CrateNum) -> Vec<CrateNum> {
139+
crate fn crate_dependencies_in_rpo(&self, krate: CrateNum) -> Vec<CrateNum> {
150140
let mut ordering = Vec::new();
151141
self.push_dependencies_in_postorder(&mut ordering, krate);
152142
ordering.reverse();
153143
ordering
154144
}
155145

156-
pub(super) fn push_dependencies_in_postorder(&self,
157-
ordering: &mut Vec<CrateNum>,
158-
krate: CrateNum) {
146+
crate fn push_dependencies_in_postorder(&self, ordering: &mut Vec<CrateNum>, krate: CrateNum) {
159147
if ordering.contains(&krate) {
160148
return;
161149
}
@@ -170,7 +158,7 @@ impl CStore {
170158
ordering.push(krate);
171159
}
172160

173-
pub(super) fn do_postorder_cnums_untracked(&self) -> Vec<CrateNum> {
161+
crate fn do_postorder_cnums_untracked(&self) -> Vec<CrateNum> {
174162
let mut ordering = Vec::new();
175163
for (num, v) in self.metas.borrow().iter_enumerated() {
176164
if let &Some(_) = v {
@@ -180,11 +168,11 @@ impl CStore {
180168
return ordering
181169
}
182170

183-
pub(super) fn add_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId, cnum: CrateNum) {
171+
crate fn add_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId, cnum: CrateNum) {
184172
self.extern_mod_crate_map.borrow_mut().insert(emod_id, cnum);
185173
}
186174

187-
pub(super) fn do_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> {
175+
crate fn do_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum> {
188176
self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
189177
}
190178
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use crate::foreign_modules;
66
use crate::schema;
77

88
use rustc::ty::query::QueryConfig;
9-
use rustc::middle::cstore::{CrateStore, DepKind,
10-
EncodedMetadata, NativeLibraryKind};
9+
use rustc::middle::cstore::{CrateSource, CrateStore, DepKind, EncodedMetadata, NativeLibraryKind};
1110
use rustc::middle::exported_symbols::ExportedSymbol;
1211
use rustc::middle::stability::DeprecationEntry;
1312
use rustc::middle::dependency_format::Linkage;
@@ -414,12 +413,6 @@ impl cstore::CStore {
414413
}
415414
}
416415

417-
pub fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind {
418-
let data = self.get_crate_data(cnum);
419-
let r = *data.dep_kind.lock();
420-
r
421-
}
422-
423416
pub fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition {
424417
self.get_crate_data(cnum).root.edition
425418
}
@@ -428,14 +421,6 @@ impl cstore::CStore {
428421
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
429422
}
430423

431-
pub fn ctor_kind_untracked(&self, def: DefId) -> def::CtorKind {
432-
self.get_crate_data(def.krate).get_ctor_kind(def.index)
433-
}
434-
435-
pub fn item_attrs_untracked(&self, def: DefId, sess: &Session) -> Lrc<[ast::Attribute]> {
436-
self.get_crate_data(def.krate).get_item_attrs(def.index, sess)
437-
}
438-
439424
pub fn item_children_untracked(
440425
&self,
441426
def_id: DefId,
@@ -493,6 +478,10 @@ impl cstore::CStore {
493478
pub fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssocItem {
494479
self.get_crate_data(def.krate).get_associated_item(def.index)
495480
}
481+
482+
pub fn crate_source_untracked(&self, cnum: CrateNum) -> CrateSource {
483+
self.get_crate_data(cnum).source.clone()
484+
}
496485
}
497486

498487
impl CrateStore for cstore::CStore {

0 commit comments

Comments
 (0)