Skip to content

Commit c102d7f

Browse files
committed
Clean up CrateSource.
1 parent 624a9b7 commit c102d7f

File tree

4 files changed

+27
-68
lines changed

4 files changed

+27
-68
lines changed

src/librustc/middle/cstore.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub struct LinkMeta {
6161
pub struct CrateSource {
6262
pub dylib: Option<(PathBuf, PathKind)>,
6363
pub rlib: Option<(PathBuf, PathKind)>,
64-
pub cnum: CrateNum,
6564
}
6665

6766
#[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]

src/librustc_metadata/creader.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use syntax::parse;
3737
use syntax::attr;
3838
use syntax::ext::base::SyntaxExtension;
3939
use syntax::parse::token::{InternedString, intern};
40-
use syntax_pos::{self, Span, mk_sp};
40+
use syntax_pos::{Span, DUMMY_SP, mk_sp};
4141
use log;
4242

4343
pub struct Library {
@@ -56,16 +56,14 @@ pub struct CrateLoader<'a> {
5656

5757
fn dump_crates(cstore: &CStore) {
5858
info!("resolved crates:");
59-
cstore.iter_crate_data_origins(|_, data, opt_source| {
59+
cstore.iter_crate_data(|_, data| {
6060
info!(" name: {}", data.name());
6161
info!(" cnum: {}", data.cnum);
6262
info!(" hash: {}", data.hash());
6363
info!(" reqd: {}", data.explicitly_linked.get());
64-
opt_source.map(|cs| {
65-
let CrateSource { dylib, rlib, cnum: _ } = cs;
66-
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
67-
rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
68-
});
64+
let CrateSource { dylib, rlib } = data.source.clone();
65+
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
66+
rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
6967
})
7068
}
7169

@@ -261,8 +259,7 @@ impl<'a> CrateLoader<'a> {
261259
span: Span,
262260
lib: Library,
263261
explicitly_linked: bool)
264-
-> (CrateNum, Rc<cstore::CrateMetadata>,
265-
cstore::CrateSource) {
262+
-> (CrateNum, Rc<cstore::CrateMetadata>) {
266263
info!("register crate `extern crate {} as {}`", name, ident);
267264
let crate_root = lib.metadata.get_root();
268265
self.verify_no_symbol_conflicts(span, &crate_root);
@@ -303,17 +300,14 @@ impl<'a> CrateLoader<'a> {
303300
cnum: cnum,
304301
codemap_import_info: RefCell::new(vec![]),
305302
explicitly_linked: Cell::new(explicitly_linked),
303+
source: cstore::CrateSource {
304+
dylib: dylib,
305+
rlib: rlib,
306+
},
306307
});
307308

308-
let source = cstore::CrateSource {
309-
dylib: dylib,
310-
rlib: rlib,
311-
cnum: cnum,
312-
};
313-
314309
self.cstore.set_crate_data(cnum, cmeta.clone());
315-
self.cstore.add_used_crate_source(source.clone());
316-
(cnum, cmeta, source)
310+
(cnum, cmeta)
317311
}
318312

319313
fn resolve_crate(&mut self,
@@ -324,7 +318,7 @@ impl<'a> CrateLoader<'a> {
324318
span: Span,
325319
kind: PathKind,
326320
explicitly_linked: bool)
327-
-> (CrateNum, Rc<cstore::CrateMetadata>, cstore::CrateSource) {
321+
-> (CrateNum, Rc<cstore::CrateMetadata>) {
328322
info!("resolving crate `extern crate {} as {}`", name, ident);
329323
let result = match self.existing_match(name, hash, kind) {
330324
Some(cnum) => LoadResult::Previous(cnum),
@@ -356,10 +350,8 @@ impl<'a> CrateLoader<'a> {
356350
match result {
357351
LoadResult::Previous(cnum) => {
358352
let data = self.cstore.get_crate_data(cnum);
359-
if explicitly_linked && !data.explicitly_linked.get() {
360-
data.explicitly_linked.set(explicitly_linked);
361-
}
362-
(cnum, data, self.cstore.used_crate_source(cnum))
353+
data.explicitly_linked.set(explicitly_linked || data.explicitly_linked.get());
354+
(cnum, data)
363355
}
364356
LoadResult::Loaded(library) => {
365357
self.register_crate(root, ident, name, span, library,
@@ -508,9 +500,8 @@ impl<'a> CrateLoader<'a> {
508500

509501
let (dylib, metadata) = match library {
510502
LoadResult::Previous(cnum) => {
511-
let dylib = self.cstore.opt_used_crate_source(cnum).unwrap().dylib;
512503
let data = self.cstore.get_crate_data(cnum);
513-
(dylib, PMDSource::Registered(data))
504+
(data.source.dylib.clone(), PMDSource::Registered(data))
514505
}
515506
LoadResult::Loaded(library) => {
516507
let dylib = library.dylib.clone();
@@ -754,9 +745,8 @@ impl<'a> CrateLoader<'a> {
754745
};
755746
info!("panic runtime not found -- loading {}", name);
756747

757-
let (cnum, data, _) = self.resolve_crate(&None, name, name, None,
758-
syntax_pos::DUMMY_SP,
759-
PathKind::Crate, false);
748+
let (cnum, data) =
749+
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, false);
760750

761751
// Sanity check the loaded crate to ensure it is indeed a panic runtime
762752
// and the panic strategy is indeed what we thought it was.
@@ -836,9 +826,8 @@ impl<'a> CrateLoader<'a> {
836826
} else {
837827
&self.sess.target.target.options.exe_allocation_crate
838828
};
839-
let (cnum, data, _) = self.resolve_crate(&None, name, name, None,
840-
syntax_pos::DUMMY_SP,
841-
PathKind::Crate, false);
829+
let (cnum, data) =
830+
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, false);
842831

843832
// Sanity check the crate we loaded to ensure that it is indeed an
844833
// allocator.

src/librustc_metadata/cstore.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub struct CrateMetadata {
8383
/// where this is false is when an allocator crate is injected into the
8484
/// dependency list, and therefore isn't actually needed to link an rlib.
8585
pub explicitly_linked: Cell<bool>,
86+
87+
pub source: CrateSource,
8688
}
8789

8890
pub struct CachedInlinedItem {
@@ -97,7 +99,6 @@ pub struct CStore {
9799
metas: RefCell<FxHashMap<CrateNum, Rc<CrateMetadata>>>,
98100
/// Map from NodeId's of local extern crate statements to crate numbers
99101
extern_mod_crate_map: RefCell<NodeMap<CrateNum>>,
100-
used_crate_sources: RefCell<Vec<CrateSource>>,
101102
used_libraries: RefCell<Vec<(String, NativeLibraryKind)>>,
102103
used_link_args: RefCell<Vec<String>>,
103104
statically_included_foreign_items: RefCell<NodeSet>,
@@ -112,7 +113,6 @@ impl CStore {
112113
dep_graph: dep_graph.clone(),
113114
metas: RefCell::new(FxHashMap()),
114115
extern_mod_crate_map: RefCell::new(FxHashMap()),
115-
used_crate_sources: RefCell::new(Vec::new()),
116116
used_libraries: RefCell::new(Vec::new()),
117117
used_link_args: RefCell::new(Vec::new()),
118118
statically_included_foreign_items: RefCell::new(NodeSet()),
@@ -146,38 +146,9 @@ impl CStore {
146146
}
147147
}
148148

149-
/// Like `iter_crate_data`, but passes source paths (if available) as well.
150-
pub fn iter_crate_data_origins<I>(&self, mut i: I)
151-
where I: FnMut(CrateNum, &CrateMetadata, Option<CrateSource>)
152-
{
153-
for (&k, v) in self.metas.borrow().iter() {
154-
let origin = self.opt_used_crate_source(k);
155-
origin.as_ref().map(|cs| {
156-
assert!(k == cs.cnum);
157-
});
158-
i(k, &v, origin);
159-
}
160-
}
161-
162-
pub fn add_used_crate_source(&self, src: CrateSource) {
163-
let mut used_crate_sources = self.used_crate_sources.borrow_mut();
164-
if !used_crate_sources.contains(&src) {
165-
used_crate_sources.push(src);
166-
}
167-
}
168-
169-
pub fn opt_used_crate_source(&self, cnum: CrateNum) -> Option<CrateSource> {
170-
self.used_crate_sources
171-
.borrow_mut()
172-
.iter()
173-
.find(|source| source.cnum == cnum)
174-
.cloned()
175-
}
176-
177149
pub fn reset(&self) {
178150
self.metas.borrow_mut().clear();
179151
self.extern_mod_crate_map.borrow_mut().clear();
180-
self.used_crate_sources.borrow_mut().clear();
181152
self.used_libraries.borrow_mut().clear();
182153
self.used_link_args.borrow_mut().clear();
183154
self.statically_included_foreign_items.borrow_mut().clear();
@@ -223,14 +194,14 @@ impl CStore {
223194
}
224195
info!("topological ordering: {:?}", ordering);
225196
ordering.reverse();
226-
let mut libs = self.used_crate_sources
197+
let mut libs = self.metas
227198
.borrow()
228199
.iter()
229-
.map(|src| {
230-
(src.cnum,
200+
.map(|(&cnum, data)| {
201+
(cnum,
231202
match prefer {
232-
LinkagePreference::RequireDynamic => src.dylib.clone().map(|p| p.0),
233-
LinkagePreference::RequireStatic => src.rlib.clone().map(|p| p.0),
203+
LinkagePreference::RequireDynamic => data.source.dylib.clone().map(|p| p.0),
204+
LinkagePreference::RequireStatic => data.source.rlib.clone().map(|p| p.0),
234205
})
235206
})
236207
.collect::<Vec<_>>();

src/librustc_metadata/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
507507

508508
fn used_crate_source(&self, cnum: CrateNum) -> CrateSource
509509
{
510-
self.opt_used_crate_source(cnum).unwrap()
510+
self.get_crate_data(cnum).source.clone()
511511
}
512512

513513
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<CrateNum>

0 commit comments

Comments
 (0)