Skip to content
Permalink
Browse files

rustc_metadata: Privatize `CrateMetadata::extern_crate`

  • Loading branch information
petrochenkov committed Nov 23, 2019
1 parent 37304cd commit e2414852b1c65bc819869cf9cb5328158d3ff369
Showing with 23 additions and 31 deletions.
  1. +7 −0 src/librustc/middle/cstore.rs
  2. +6 −30 src/librustc_metadata/creader.rs
  3. +10 −1 src/librustc_metadata/rmeta/decoder.rs
@@ -146,6 +146,13 @@ impl ExternCrate {
pub fn is_direct(&self) -> bool {
self.dependency_of == LOCAL_CRATE
}

pub fn rank(&self) -> impl PartialOrd {
// Prefer:
// - direct extern crate to indirect
// - shorter paths to longer
(self.is_direct(), !self.path_len)
}
}

#[derive(Copy, Clone, Debug, HashStable)]
@@ -400,36 +400,12 @@ impl<'a> CrateLoader<'a> {
if !visited.insert((cnum, extern_crate.is_direct())) { return }

let cmeta = self.cstore.get_crate_data(cnum);
let mut old_extern_crate = cmeta.extern_crate.borrow_mut();

// 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.is_direct(),
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.is_direct(),
cmp::Reverse(c.path_len),
),
};
if old_rank >= new_rank {
return; // no change needed
}

*old_extern_crate = Some(extern_crate);
drop(old_extern_crate);

// Propagate the extern crate info to dependencies.
extern_crate.dependency_of = cnum;
for &dep_cnum in cmeta.dependencies().iter() {
self.update_extern_crate(dep_cnum, extern_crate, visited);
if cmeta.update_extern_crate(extern_crate) {
// Propagate the extern crate info to dependencies.
extern_crate.dependency_of = cnum;
for &dep_cnum in cmeta.dependencies().iter() {
self.update_extern_crate(dep_cnum, extern_crate, visited);
}
}
}

@@ -112,7 +112,7 @@ crate struct CrateMetadata {

/// Information about the `extern crate` item or path that caused this crate to be loaded.
/// If this is `None`, then the crate was injected (e.g., by the allocator).
crate extern_crate: Lock<Option<ExternCrate>>,
extern_crate: Lock<Option<ExternCrate>>,
}

/// Holds information about a syntax_pos::SourceFile imported from another crate.
@@ -1525,6 +1525,15 @@ impl<'a, 'tcx> CrateMetadata {
crate fn add_dependency(&self, cnum: CrateNum) {
self.dependencies.borrow_mut().push(cnum);
}

crate fn update_extern_crate(&self, new_extern_crate: ExternCrate) -> bool {
let mut extern_crate = self.extern_crate.borrow_mut();
let update = Some(new_extern_crate.rank()) > extern_crate.as_ref().map(ExternCrate::rank);
if update {
*extern_crate = Some(new_extern_crate);
}
update
}
}

// Cannot be implemented on 'ProcMacro', as libproc_macro

0 comments on commit e241485

Please sign in to comment.
You can’t perform that action at this time.