Skip to content

Commit a1d45d9

Browse files
committed
Refactor explicitly_linked: bool -> dep_kind: DepKind.
1 parent c102d7f commit a1d45d9

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

src/librustc/middle/cstore.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ pub struct CrateSource {
6363
pub rlib: Option<(PathBuf, PathKind)>,
6464
}
6565

66+
#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
67+
pub enum DepKind {
68+
/// A dependency that is always injected into the dependency list and so
69+
/// doesn't need to be linked to an rlib, e.g. the injected allocator.
70+
Implicit,
71+
/// A dependency that is required by an rlib version of this crate.
72+
/// Ordinary `extern crate`s result in `Explicit` dependencies.
73+
Explicit,
74+
}
75+
6676
#[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
6777
pub enum LinkagePreference {
6878
RequireDynamic,
@@ -169,10 +179,10 @@ pub trait CrateStore<'tcx> {
169179
// crate metadata
170180
fn dylib_dependency_formats(&self, cnum: CrateNum)
171181
-> Vec<(CrateNum, LinkagePreference)>;
182+
fn dep_kind(&self, cnum: CrateNum) -> DepKind;
172183
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
173184
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
174185
fn is_staged_api(&self, cnum: CrateNum) -> bool;
175-
fn is_explicitly_linked(&self, cnum: CrateNum) -> bool;
176186
fn is_allocator(&self, cnum: CrateNum) -> bool;
177187
fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
178188
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
@@ -341,7 +351,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
341351
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
342352
{ bug!("missing_lang_items") }
343353
fn is_staged_api(&self, cnum: CrateNum) -> bool { bug!("is_staged_api") }
344-
fn is_explicitly_linked(&self, cnum: CrateNum) -> bool { bug!("is_explicitly_linked") }
354+
fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
345355
fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
346356
fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
347357
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }

src/librustc/middle/dependency_format.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use hir::def_id::CrateNum;
6565

6666
use session;
6767
use session::config;
68+
use middle::cstore::DepKind;
6869
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
6970
use util::nodemap::FxHashMap;
7071
use rustc_back::PanicStrategy;
@@ -188,7 +189,7 @@ fn calculate_type(sess: &session::Session,
188189
let src = sess.cstore.used_crate_source(cnum);
189190
if src.dylib.is_none() &&
190191
!formats.contains_key(&cnum) &&
191-
sess.cstore.is_explicitly_linked(cnum) {
192+
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
192193
assert!(src.rlib.is_some());
193194
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
194195
add_library(sess, cnum, RequireStatic, &mut formats);
@@ -272,7 +273,7 @@ fn attempt_static(sess: &session::Session) -> Option<DependencyList> {
272273
// everything in explicitly so long as it's actually required.
273274
let last_crate = sess.cstore.crates().len();
274275
let mut ret = (1..last_crate+1).map(|cnum| {
275-
if sess.cstore.is_explicitly_linked(CrateNum::new(cnum)) {
276+
if sess.cstore.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
276277
Linkage::Static
277278
} else {
278279
Linkage::NotLinked

src/librustc_metadata/creader.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use schema::CrateRoot;
1616

1717
use rustc::hir::def_id::{CrateNum, DefIndex};
1818
use rustc::hir::svh::Svh;
19-
use rustc::middle::cstore::LoadedMacros;
19+
use rustc::middle::cstore::{DepKind, LoadedMacros};
2020
use rustc::session::{config, Session};
2121
use rustc_back::PanicStrategy;
2222
use rustc::session::search_paths::PathKind;
@@ -29,7 +29,7 @@ use std::cell::{RefCell, Cell};
2929
use std::ops::Deref;
3030
use std::path::PathBuf;
3131
use std::rc::Rc;
32-
use std::fs;
32+
use std::{cmp, fs};
3333

3434
use syntax::ast;
3535
use syntax::abi::Abi;
@@ -60,7 +60,7 @@ fn dump_crates(cstore: &CStore) {
6060
info!(" name: {}", data.name());
6161
info!(" cnum: {}", data.cnum);
6262
info!(" hash: {}", data.hash());
63-
info!(" reqd: {}", data.explicitly_linked.get());
63+
info!(" reqd: {:?}", data.dep_kind.get());
6464
let CrateSource { dylib, rlib } = data.source.clone();
6565
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
6666
rlib.map(|rl| info!(" rlib: {}", rl.0.display()));
@@ -258,7 +258,7 @@ impl<'a> CrateLoader<'a> {
258258
name: &str,
259259
span: Span,
260260
lib: Library,
261-
explicitly_linked: bool)
261+
dep_kind: DepKind)
262262
-> (CrateNum, Rc<cstore::CrateMetadata>) {
263263
info!("register crate `extern crate {} as {}`", name, ident);
264264
let crate_root = lib.metadata.get_root();
@@ -299,7 +299,7 @@ impl<'a> CrateLoader<'a> {
299299
cnum_map: RefCell::new(cnum_map),
300300
cnum: cnum,
301301
codemap_import_info: RefCell::new(vec![]),
302-
explicitly_linked: Cell::new(explicitly_linked),
302+
dep_kind: Cell::new(dep_kind),
303303
source: cstore::CrateSource {
304304
dylib: dylib,
305305
rlib: rlib,
@@ -317,7 +317,7 @@ impl<'a> CrateLoader<'a> {
317317
hash: Option<&Svh>,
318318
span: Span,
319319
kind: PathKind,
320-
explicitly_linked: bool)
320+
dep_kind: DepKind)
321321
-> (CrateNum, Rc<cstore::CrateMetadata>) {
322322
info!("resolving crate `extern crate {} as {}`", name, ident);
323323
let result = match self.existing_match(name, hash, kind) {
@@ -350,12 +350,11 @@ impl<'a> CrateLoader<'a> {
350350
match result {
351351
LoadResult::Previous(cnum) => {
352352
let data = self.cstore.get_crate_data(cnum);
353-
data.explicitly_linked.set(explicitly_linked || data.explicitly_linked.get());
353+
data.dep_kind.set(cmp::max(data.dep_kind.get(), dep_kind));
354354
(cnum, data)
355355
}
356356
LoadResult::Loaded(library) => {
357-
self.register_crate(root, ident, name, span, library,
358-
explicitly_linked)
357+
self.register_crate(root, ident, name, span, library, dep_kind)
359358
}
360359
}
361360
}
@@ -442,7 +441,7 @@ impl<'a> CrateLoader<'a> {
442441
Some(&dep.hash),
443442
span,
444443
PathKind::Dependency,
445-
dep.explicitly_linked);
444+
dep.kind);
446445
(CrateNum::new(crate_num + 1), local_cnum)
447446
}).collect();
448447

@@ -716,7 +715,7 @@ impl<'a> CrateLoader<'a> {
716715
// #![panic_runtime] crate.
717716
self.inject_dependency_if(cnum, "a panic runtime",
718717
&|data| data.needs_panic_runtime());
719-
runtime_found = runtime_found || data.explicitly_linked.get();
718+
runtime_found = runtime_found || data.dep_kind.get() == DepKind::Explicit;
720719
}
721720
});
722721

@@ -745,8 +744,9 @@ impl<'a> CrateLoader<'a> {
745744
};
746745
info!("panic runtime not found -- loading {}", name);
747746

747+
let dep_kind = DepKind::Implicit;
748748
let (cnum, data) =
749-
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, false);
749+
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, dep_kind);
750750

751751
// Sanity check the loaded crate to ensure it is indeed a panic runtime
752752
// and the panic strategy is indeed what we thought it was.
@@ -780,7 +780,7 @@ impl<'a> CrateLoader<'a> {
780780
self.inject_dependency_if(cnum, "an allocator",
781781
&|data| data.needs_allocator());
782782
found_required_allocator = found_required_allocator ||
783-
data.explicitly_linked.get();
783+
data.dep_kind.get() == DepKind::Explicit;
784784
}
785785
});
786786
if !needs_allocator || found_required_allocator { return }
@@ -826,8 +826,9 @@ impl<'a> CrateLoader<'a> {
826826
} else {
827827
&self.sess.target.target.options.exe_allocation_crate
828828
};
829+
let dep_kind = DepKind::Implicit;
829830
let (cnum, data) =
830-
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, false);
831+
self.resolve_crate(&None, name, name, None, DUMMY_SP, PathKind::Crate, dep_kind);
831832

832833
// Sanity check the crate we loaded to ensure that it is indeed an
833834
// allocator.
@@ -993,7 +994,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
993994
if let PMDSource::Owned(lib) = ekrate.metadata {
994995
if ekrate.target_only || config::host_triple() == self.sess.opts.target_triple {
995996
let ExternCrateInfo { ref ident, ref name, .. } = info;
996-
self.register_crate(&None, ident, name, item.span, lib, true);
997+
self.register_crate(&None, ident, name, item.span, lib, DepKind::Explicit);
997998
}
998999
}
9991000

@@ -1006,7 +1007,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
10061007
};
10071008

10081009
let (cnum, ..) = self.resolve_crate(
1009-
&None, &info.ident, &info.name, None, item.span, PathKind::Crate, true,
1010+
&None, &info.ident, &info.name, None, item.span, PathKind::Crate, DepKind::Explicit,
10101011
);
10111012

10121013
let def_id = definitions.opt_local_def_id(item.id).unwrap();

src/librustc_metadata/cstore.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::dep_graph::DepGraph;
1818
use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex, DefId};
1919
use rustc::hir::map::DefKey;
2020
use rustc::hir::svh::Svh;
21-
use rustc::middle::cstore::ExternCrate;
21+
use rustc::middle::cstore::{DepKind, ExternCrate};
2222
use rustc_back::PanicStrategy;
2323
use rustc_data_structures::indexed_vec::IndexVec;
2424
use rustc::util::nodemap::{FxHashMap, NodeMap, NodeSet, DefIdMap};
@@ -78,12 +78,7 @@ pub struct CrateMetadata {
7878
/// compilation support.
7979
pub key_map: FxHashMap<DefKey, DefIndex>,
8080

81-
/// Flag if this crate is required by an rlib version of this crate, or in
82-
/// other words whether it was explicitly linked to. An example of a crate
83-
/// where this is false is when an allocator crate is injected into the
84-
/// dependency list, and therefore isn't actually needed to link an rlib.
85-
pub explicitly_linked: Cell<bool>,
86-
81+
pub dep_kind: Cell<DepKind>,
8782
pub source: CrateSource,
8883
}
8984

src/librustc_metadata/cstore_impl.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use encoder;
1313
use locator;
1414
use schema;
1515

16-
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, ExternCrate};
16+
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, DepKind, ExternCrate};
1717
use rustc::middle::cstore::{NativeLibraryKind, LinkMeta, LinkagePreference};
1818
use rustc::hir::def::{self, Def};
1919
use rustc::middle::lang_items;
@@ -221,6 +221,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
221221
self.get_crate_data(cnum).get_dylib_dependency_formats()
222222
}
223223

224+
fn dep_kind(&self, cnum: CrateNum) -> DepKind
225+
{
226+
self.get_crate_data(cnum).dep_kind.get()
227+
}
228+
224229
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>
225230
{
226231
self.get_crate_data(cnum).get_lang_items()
@@ -237,11 +242,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
237242
self.get_crate_data(cnum).is_staged_api()
238243
}
239244

240-
fn is_explicitly_linked(&self, cnum: CrateNum) -> bool
241-
{
242-
self.get_crate_data(cnum).explicitly_linked.get()
243-
}
244-
245245
fn is_allocator(&self, cnum: CrateNum) -> bool
246246
{
247247
self.get_crate_data(cnum).is_allocator()

src/librustc_metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10801080
CrateDep {
10811081
name: syntax::parse::token::intern(dep.name()),
10821082
hash: dep.hash(),
1083-
explicitly_linked: dep.explicitly_linked.get(),
1083+
kind: dep.dep_kind.get(),
10841084
}
10851085
}))
10861086
}

src/librustc_metadata/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use index;
1414
use rustc::hir;
1515
use rustc::hir::def::{self, CtorKind};
1616
use rustc::hir::def_id::{DefIndex, DefId};
17-
use rustc::middle::cstore::{LinkagePreference, NativeLibraryKind};
17+
use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibraryKind};
1818
use rustc::middle::lang_items;
1919
use rustc::mir;
2020
use rustc::ty::{self, Ty};
@@ -187,7 +187,7 @@ pub struct CrateRoot {
187187
pub struct CrateDep {
188188
pub name: ast::Name,
189189
pub hash: hir::svh::Svh,
190-
pub explicitly_linked: bool,
190+
pub kind: DepKind,
191191
}
192192

193193
#[derive(RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)