//! from rustc::middle::cstore in no particular order.
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
-use ich::StableHashingContext;
use middle;
impl_stable_hash_for!(struct middle::cstore::ExternCrate {
src,
span,
+ path_len,
direct
});
-impl<'a> HashStable<StableHashingContext<'a>> for middle::cstore::ExternCrateSource {
- fn hash_stable<W: StableHasherResult>(
- &self,
- hcx: &mut StableHashingContext<'a>,
- hasher: &mut StableHasher<W>,
- ) {
- use middle::cstore::ExternCrateSource::*;
-
- ::std::mem::discriminant(self).hash_stable(hcx, hasher);
-
- match *self {
- Extern { def_id, path_len } => {
- def_id.hash_stable(hcx, hasher);
- path_len.hash_stable(hcx, hasher);
- }
- Use { path_len } => path_len.hash_stable(hcx, hasher),
- Path => {}
- }
- }
-}
+impl_stable_hash_for!(enum middle::cstore::ExternCrateSource {
+ Extern(def_id),
+ Use,
+ Path,
+});
impl_stable_hash_for!(struct middle::cstore::CrateSource {
dylib,
/// span of the extern crate that caused this to be loaded
pub span: Span,
+ /// Number of links to reach the extern;
+ /// used to select the extern with the shortest path
+ pub path_len: usize,
+
/// If true, then this crate is the crate named by the extern
/// crate referenced above. If false, then this crate is a dep
/// of the crate.
#[derive(Copy, Clone, Debug)]
pub enum ExternCrateSource {
/// Crate is loaded by `extern crate`.
- Extern {
+ Extern(
/// def_id of the item in the current crate that caused
/// this crate to be loaded; note that there could be multiple
/// such ids
- def_id: DefId,
-
- /// Number of links to reach the extern crate `def_id`
- /// declaration; used to select the extern crate with the shortest
- /// path
- path_len: usize,
- },
+ DefId,
+ ),
// Crate is loaded by `use`.
- Use {
- path_len: usize,
- },
+ Use,
/// Crate is implicitly loaded by an absolute or an `extern::` path.
Path,
}
if cnum != LOCAL_CRATE {
let opt_extern_crate = self.extern_crate(cnum.as_def_id());
if let Some(ExternCrate {
- src: ExternCrateSource::Extern { def_id, .. },
+ src: ExternCrateSource::Extern(def_id),
direct: true,
..
}) = *opt_extern_crate
if cur_def.index == CRATE_DEF_INDEX {
match *self.extern_crate(cur_def) {
Some(ExternCrate {
- src: ExternCrateSource::Extern { def_id, .. },
+ src: ExternCrateSource::Extern(def_id),
direct: true,
..
}) => {
let cmeta = self.cstore.get_crate_data(cnum);
let mut old_extern_crate = cmeta.extern_crate.borrow_mut();
- fn path_len_reverse(src: ExternCrateSource) -> cmp::Reverse<usize> {
- cmp::Reverse(match src {
- ExternCrateSource::Extern { path_len, .. } |
- ExternCrateSource::Use { path_len } => path_len,
- _ => usize::max_value(),
- })
- }
-
// Prefer:
// - something over nothing (tuple.0);
// - direct extern crate to indirect (tuple.1);
let new_rank = (
true,
extern_crate.direct,
- path_len_reverse(extern_crate.src),
+ cmp::Reverse(extern_crate.path_len),
);
let old_rank = match *old_extern_crate {
None => (false, false, cmp::Reverse(usize::max_value())),
Some(ref c) => (
true,
c.direct,
- path_len_reverse(c.src),
+ cmp::Reverse(c.path_len),
),
};
if old_rank >= new_rank {
self.update_extern_crate(
cnum,
ExternCrate {
- src: ExternCrateSource::Extern { def_id, path_len },
+ src: ExternCrateSource::Extern(def_id),
span: item.span,
+ path_len,
direct: true,
},
&mut FxHashSet(),
ExternCrate {
src: ExternCrateSource::Path,
span,
+ // to have the least priority in `update_extern_crate`
+ path_len: usize::max_value(),
direct: true,
},
&mut FxHashSet(),
self.update_extern_crate(
cnum,
ExternCrate {
- src: ExternCrateSource::Use { path_len },
+ src: ExternCrateSource::Use,
span,
+ path_len,
direct: true,
},
&mut FxHashSet(),