Skip to content

Commit

Permalink
Move path_len to ExternCrate
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed Apr 13, 2018
1 parent e986510 commit 2c7e83f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
27 changes: 6 additions & 21 deletions src/librustc/ich/impls_cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
19 changes: 8 additions & 11 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/item_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
..
}) => {
Expand Down
20 changes: 8 additions & 12 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,29 +367,21 @@ 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);
// - shorter paths to longer (tuple.2).
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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit 2c7e83f

Please sign in to comment.