Move path_len
to ExternCrate
This commit is contained in:
parent
e986510bde
commit
2c7e83f746
@ -12,7 +12,6 @@
|
||||
//! from rustc::middle::cstore in no particular order.
|
||||
|
||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
|
||||
use ich::StableHashingContext;
|
||||
|
||||
use middle;
|
||||
|
||||
@ -50,29 +49,15 @@ impl_stable_hash_for!(enum middle::cstore::LinkagePreference {
|
||||
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,
|
||||
|
@ -153,6 +153,10 @@ pub struct ExternCrate {
|
||||
/// 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.
|
||||
@ -162,21 +166,14 @@ pub struct ExternCrate {
|
||||
#[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,
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
||||
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
|
||||
@ -138,7 +138,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
|
||||
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,
|
||||
..
|
||||
}) => {
|
||||
|
@ -367,14 +367,6 @@ impl<'a> CrateLoader<'a> {
|
||||
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);
|
||||
@ -382,14 +374,14 @@ impl<'a> CrateLoader<'a> {
|
||||
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 {
|
||||
@ -1089,8 +1081,9 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
|
||||
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(),
|
||||
@ -1116,6 +1109,8 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
|
||||
ExternCrate {
|
||||
src: ExternCrateSource::Path,
|
||||
span,
|
||||
// to have the least priority in `update_extern_crate`
|
||||
path_len: usize::max_value(),
|
||||
direct: true,
|
||||
},
|
||||
&mut FxHashSet(),
|
||||
@ -1141,8 +1136,9 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
|
||||
self.update_extern_crate(
|
||||
cnum,
|
||||
ExternCrate {
|
||||
src: ExternCrateSource::Use { path_len },
|
||||
src: ExternCrateSource::Use,
|
||||
span,
|
||||
path_len,
|
||||
direct: true,
|
||||
},
|
||||
&mut FxHashSet(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user