Skip to content

Commit

Permalink
update_extern_crate belongs to CrateMetadata
Browse files Browse the repository at this point in the history
This also allows us to make extern_crate field private
  • Loading branch information
spastorino committed Nov 1, 2019
1 parent 69a021f commit 6f9bfda
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
53 changes: 8 additions & 45 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc::hir::map::Definitions;
use rustc::hir::def_id::LOCAL_CRATE;

use std::path::Path;
use std::{cmp, fs};
use std::fs;

use syntax::ast;
use syntax::attr;
Expand Down Expand Up @@ -412,47 +412,6 @@ impl<'a> CrateLoader<'a> {
}
}

fn update_extern_crate(&self,
cnum: CrateNum,
mut extern_crate: ExternCrate,
visited: &mut FxHashSet<(CrateNum, bool)>)
{
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.borrow().iter() {
self.update_extern_crate(dep_cnum, extern_crate, visited);
}
}

// Go through the crate metadata and load any crates that it references
fn resolve_crate_deps(&mut self,
root: &CratePaths,
Expand Down Expand Up @@ -889,7 +848,9 @@ impl<'a> CrateLoader<'a> {

let def_id = definitions.opt_local_def_id(item.id).unwrap();
let path_len = definitions.def_path(def_id.index).data.len();
self.update_extern_crate(

let cmeta = self.cstore.get_crate_data(cnum);
cmeta.update_extern_crate(
cnum,
ExternCrate {
src: ExternCrateSource::Extern(def_id),
Expand All @@ -908,7 +869,8 @@ impl<'a> CrateLoader<'a> {
pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> CrateNum {
let cnum = self.resolve_crate(name, span, DepKind::Explicit, None);

self.update_extern_crate(
let cmeta = self.cstore.get_crate_data(cnum);
cmeta.update_extern_crate(
cnum,
ExternCrate {
src: ExternCrateSource::Path,
Expand All @@ -926,7 +888,8 @@ impl<'a> CrateLoader<'a> {
pub fn maybe_process_path_extern(&mut self, name: Symbol, span: Span) -> Option<CrateNum> {
let cnum = self.maybe_resolve_crate(name, span, DepKind::Explicit, None).ok()?;

self.update_extern_crate(
let cmeta = self.cstore.get_crate_data(cnum);
cmeta.update_extern_crate(
cnum,
ExternCrate {
src: ExternCrateSource::Path,
Expand Down
59 changes: 57 additions & 2 deletions src/librustc_metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc::middle::cstore::{
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel};
use rustc::mir::{Body, Promoted};
use rustc::mir::interpret::AllocDecodingState;
use rustc::util::nodemap::FxHashMap;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc::session::Session;
use rustc::ty::{self, Ty, TyCtxt};
use rustc_data_structures::sync::{Lrc, Lock, MetadataRef, Once, AtomicCell};
Expand Down Expand Up @@ -110,7 +110,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>>,
}

impl<'a, 'tcx> CrateMetadata {
Expand Down Expand Up @@ -464,6 +464,61 @@ impl<'a, 'tcx> CrateMetadata {
});
}

crate fn get_extern_crate_arenas(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ExternCrate> {
let r = *self.extern_crate.lock();
r.map(|c| &*tcx.arena.alloc(c))
}

crate fn is_extern_crate_direct(&self) -> bool {
match *self.extern_crate.borrow() {
Some(extern_crate) if !extern_crate.is_direct() => true,
_ => false,
}
}

// let cmeta = self.cstore.get_crate_data(cnum);
crate fn update_extern_crate(
&self,
cnum: CrateNum,
mut extern_crate: ExternCrate,
visited: &mut FxHashSet<(CrateNum, bool)>,
) {
if !visited.insert((cnum, extern_crate.is_direct())) { return }

let mut old_extern_crate = self.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 self.dependencies.borrow().iter() {
self.update_extern_crate(dep_cnum, extern_crate, visited);
}
}


/// Iterates over the diagnostic items in the given crate.
crate fn get_diagnostic_items(
&self,
Expand Down
13 changes: 2 additions & 11 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
is_sanitizer_runtime => { cdata.root.sanitizer_runtime }
is_profiler_runtime => { cdata.root.profiler_runtime }
panic_strategy => { cdata.root.panic_strategy }
extern_crate => {
let r = *cdata.extern_crate.lock();
r.map(|c| &*tcx.arena.alloc(c))
}
extern_crate => { cdata.get_extern_crate_arenas(tcx) }
is_no_builtins => { cdata.root.no_builtins }
symbol_mangling_version => { cdata.root.symbol_mangling_version }
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
Expand Down Expand Up @@ -223,13 +220,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
diagnostic_items => { cdata.get_diagnostic_items(tcx) }
missing_lang_items => { cdata.get_missing_lang_items(tcx) }

missing_extern_crate_item => {
let r = match *cdata.extern_crate.borrow() {
Some(extern_crate) if !extern_crate.is_direct() => true,
_ => false,
};
r
}
missing_extern_crate_item => { cdata.is_extern_crate_direct() }

used_crate_source => { Lrc::new(cdata.source.clone()) }

Expand Down

0 comments on commit 6f9bfda

Please sign in to comment.