Auto merge of #42507 - ibabushkin:external-span-trans, r=eddyb

Fix translation of external spans

Previously, I noticed that spans from external crates don't generate any output. This limitation is problematic if analysis is performed on one or more external crates, as is the case with [rust-semverver](https://github.com/ibabushkin/rust-semverver). This change should address this behaviour, with the potential drawback that a minor performance hit is to be expected, as spans from potentially large crates have to be translated now.
This commit is contained in:
bors 2017-06-09 12:49:49 +00:00
commit 5fe923d434
9 changed files with 28 additions and 24 deletions

View File

@ -230,7 +230,7 @@ pub trait CrateStore {
// item info
fn visibility(&self, def: DefId) -> ty::Visibility;
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
// trait info
@ -283,7 +283,7 @@ pub trait CrateStore {
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<def::Export>;
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
// misc. metadata
@ -345,7 +345,9 @@ impl CrateStore for DummyCrateStore {
{ bug!("crate_data_as_rc_any") }
// item info
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
fn visible_parent_map<'a>(&'a self, session: &Session)
-> ::std::cell::Ref<'a, DefIdMap<DefId>>
{
bug!("visible_parent_map")
}
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
@ -419,7 +421,9 @@ impl CrateStore for DummyCrateStore {
bug!("def_path_table")
}
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
bug!("item_children")
}
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
// misc. metadata

View File

@ -129,7 +129,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
pub fn try_push_visible_item_path<T>(self, buffer: &mut T, external_def_id: DefId) -> bool
where T: ItemPathBuffer
{
let visible_parent_map = self.sess.cstore.visible_parent_map();
let visible_parent_map = self.sess.cstore.visible_parent_map(self.sess);
let (mut cur_def, mut cur_path) = (external_def_id, Vec::<ast::Name>::new());
loop {

View File

@ -80,7 +80,7 @@ provide! { <'tcx> tcx, def_id, cdata
variances_of => { Rc::new(cdata.get_item_variances(def_id.index)) }
associated_item_def_ids => {
let mut result = vec![];
cdata.each_child_of_item(def_id.index, |child| result.push(child.def.def_id()));
cdata.each_child_of_item(def_id.index, |child| result.push(child.def.def_id()), tcx.sess);
Rc::new(result)
}
associated_item => { cdata.get_associated_item(def_id.index) }
@ -348,12 +348,12 @@ impl CrateStore for cstore::CStore {
self.get_crate_data(def.krate).get_struct_field_names(def.index)
}
fn item_children(&self, def_id: DefId) -> Vec<def::Export>
fn item_children(&self, def_id: DefId, sess: &Session) -> Vec<def::Export>
{
self.dep_graph.read(DepNode::MetaData(def_id));
let mut result = vec![];
self.get_crate_data(def_id.krate)
.each_child_of_item(def_id.index, |child| result.push(child));
.each_child_of_item(def_id.index, |child| result.push(child), sess);
result
}
@ -456,7 +456,7 @@ impl CrateStore for cstore::CStore {
/// Returns a map from a sufficiently visible external item (i.e. an external item that is
/// visible from at least one local module) to a sufficiently visible parent (considering
/// modules that re-export the external item to be parents).
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
{
let visible_parent_map = self.visible_parent_map.borrow();
if !visible_parent_map.is_empty() {
@ -506,7 +506,7 @@ impl CrateStore for cstore::CStore {
index: CRATE_DEF_INDEX
});
while let Some(def) = bfs_queue.pop_front() {
for child in self.item_children(def) {
for child in self.item_children(def, sess) {
add_child(bfs_queue, child, def);
}
}

View File

@ -654,7 +654,7 @@ impl<'a, 'tcx> CrateMetadata {
}
/// Iterates over each child of the given item.
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
where F: FnMut(def::Export)
{
if let Some(ref proc_macros) = self.proc_macros {
@ -677,19 +677,19 @@ impl<'a, 'tcx> CrateMetadata {
// Find the item.
let item = match self.maybe_entry(id) {
None => return,
Some(item) => item.decode(self),
Some(item) => item.decode((self, sess)),
};
// Iterate over all children.
let macros_only = self.dep_kind.get().macros_only();
for child_index in item.children.decode(self) {
for child_index in item.children.decode((self, sess)) {
if macros_only {
continue
}
// Get the item.
if let Some(child) = self.maybe_entry(child_index) {
let child = child.decode(self);
let child = child.decode((self, sess));
match child.kind {
EntryKind::MacroDef(..) => {}
_ if macros_only => continue,
@ -700,12 +700,12 @@ impl<'a, 'tcx> CrateMetadata {
match child.kind {
// FIXME(eddyb) Don't encode these in children.
EntryKind::ForeignMod => {
for child_index in child.children.decode(self) {
for child_index in child.children.decode((self, sess)) {
if let Some(def) = self.get_def(child_index) {
callback(def::Export {
def: def,
ident: Ident::with_empty_ctxt(self.item_name(child_index)),
span: self.entry(child_index).span.decode(self),
span: self.entry(child_index).span.decode((self, sess)),
});
}
}
@ -718,7 +718,7 @@ impl<'a, 'tcx> CrateMetadata {
}
let def_key = self.def_key(child_index);
let span = child.span.decode(self);
let span = child.span.decode((self, sess));
if let (Some(def), Some(name)) =
(self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) {
let ident = Ident::with_empty_ctxt(name);
@ -747,7 +747,7 @@ impl<'a, 'tcx> CrateMetadata {
}
if let EntryKind::Mod(data) = item.kind {
for exp in data.decode(self).reexports.decode(self) {
for exp in data.decode((self, sess)).reexports.decode((self, sess)) {
match exp.def {
Def::Macro(..) => {}
_ if macros_only => continue,

View File

@ -478,7 +478,7 @@ impl<'a> Resolver<'a> {
span);
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
for child in self.session.cstore.item_children(def_id) {
for child in self.session.cstore.item_children(def_id, self.session) {
let ns = if let Def::AssociatedTy(..) = child.def { TypeNS } else { ValueNS };
self.define(module, child.ident, ns,
(child.def, ty::Visibility::Public, DUMMY_SP, expansion));
@ -564,7 +564,7 @@ impl<'a> Resolver<'a> {
/// is built, building it if it is not.
pub fn populate_module_if_necessary(&mut self, module: Module<'a>) {
if module.populated.get() { return }
for child in self.session.cstore.item_children(module.def_id().unwrap()) {
for child in self.session.cstore.item_children(module.def_id().unwrap(), self.session) {
self.build_reduced_graph_for_external_crate_def(module, child);
}
module.populated.set(true)

View File

@ -526,7 +526,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
if !external_mods.insert(def_id) {
return;
}
for child in tcx.sess.cstore.item_children(def_id) {
for child in tcx.sess.cstore.item_children(def_id, tcx.sess) {
handle_external_def(tcx, traits, external_mods, child.def)
}
}

View File

@ -443,7 +443,7 @@ fn build_module(cx: &DocContext, did: DefId) -> clean::Module {
// two namespaces, so the target may be listed twice. Make sure we only
// visit each node at most once.
let mut visited = FxHashSet();
for item in cx.tcx.sess.cstore.item_children(did) {
for item in cx.tcx.sess.cstore.item_children(did, cx.tcx.sess) {
let def_id = item.def.def_id();
if cx.tcx.sess.cstore.visibility(def_id) == ty::Visibility::Public {
if !visited.insert(def_id) { continue }

View File

@ -241,7 +241,7 @@ impl Clean<ExternalCrate> for CrateNum {
}
}).collect()
} else {
cx.tcx.sess.cstore.item_children(root).iter().map(|item| item.def)
cx.tcx.sess.cstore.item_children(root, cx.tcx.sess).iter().map(|item| item.def)
.filter_map(as_primitive).collect()
};

View File

@ -70,7 +70,7 @@ impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> {
return;
}
for item in self.cstore.item_children(def_id) {
for item in self.cstore.item_children(def_id, self.cx.tcx.sess) {
self.visit_item(item.def);
}
}