Skip to content

Commit 5fe923d

Browse files
committed
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.
2 parents a7ac71b + 9a054f2 commit 5fe923d

File tree

9 files changed

+28
-24
lines changed

9 files changed

+28
-24
lines changed

src/librustc/middle/cstore.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub trait CrateStore {
230230

231231
// item info
232232
fn visibility(&self, def: DefId) -> ty::Visibility;
233-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
233+
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
234234
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
235235

236236
// trait info
@@ -283,7 +283,7 @@ pub trait CrateStore {
283283
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
284284
fn def_path_table(&self, cnum: CrateNum) -> Rc<DefPathTable>;
285285
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
286-
fn item_children(&self, did: DefId) -> Vec<def::Export>;
286+
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
287287
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro;
288288

289289
// misc. metadata
@@ -345,7 +345,9 @@ impl CrateStore for DummyCrateStore {
345345
{ bug!("crate_data_as_rc_any") }
346346
// item info
347347
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
348-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
348+
fn visible_parent_map<'a>(&'a self, session: &Session)
349+
-> ::std::cell::Ref<'a, DefIdMap<DefId>>
350+
{
349351
bug!("visible_parent_map")
350352
}
351353
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
@@ -419,7 +421,9 @@ impl CrateStore for DummyCrateStore {
419421
bug!("def_path_table")
420422
}
421423
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
422-
fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
424+
fn item_children(&self, did: DefId, sess: &Session) -> Vec<def::Export> {
425+
bug!("item_children")
426+
}
423427
fn load_macro(&self, did: DefId, sess: &Session) -> LoadedMacro { bug!("load_macro") }
424428

425429
// misc. metadata

src/librustc/ty/item_path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
129129
pub fn try_push_visible_item_path<T>(self, buffer: &mut T, external_def_id: DefId) -> bool
130130
where T: ItemPathBuffer
131131
{
132-
let visible_parent_map = self.sess.cstore.visible_parent_map();
132+
let visible_parent_map = self.sess.cstore.visible_parent_map(self.sess);
133133

134134
let (mut cur_def, mut cur_path) = (external_def_id, Vec::<ast::Name>::new());
135135
loop {

src/librustc_metadata/cstore_impl.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ provide! { <'tcx> tcx, def_id, cdata
8080
variances_of => { Rc::new(cdata.get_item_variances(def_id.index)) }
8181
associated_item_def_ids => {
8282
let mut result = vec![];
83-
cdata.each_child_of_item(def_id.index, |child| result.push(child.def.def_id()));
83+
cdata.each_child_of_item(def_id.index, |child| result.push(child.def.def_id()), tcx.sess);
8484
Rc::new(result)
8585
}
8686
associated_item => { cdata.get_associated_item(def_id.index) }
@@ -348,12 +348,12 @@ impl CrateStore for cstore::CStore {
348348
self.get_crate_data(def.krate).get_struct_field_names(def.index)
349349
}
350350

351-
fn item_children(&self, def_id: DefId) -> Vec<def::Export>
351+
fn item_children(&self, def_id: DefId, sess: &Session) -> Vec<def::Export>
352352
{
353353
self.dep_graph.read(DepNode::MetaData(def_id));
354354
let mut result = vec![];
355355
self.get_crate_data(def_id.krate)
356-
.each_child_of_item(def_id.index, |child| result.push(child));
356+
.each_child_of_item(def_id.index, |child| result.push(child), sess);
357357
result
358358
}
359359

@@ -456,7 +456,7 @@ impl CrateStore for cstore::CStore {
456456
/// Returns a map from a sufficiently visible external item (i.e. an external item that is
457457
/// visible from at least one local module) to a sufficiently visible parent (considering
458458
/// modules that re-export the external item to be parents).
459-
fn visible_parent_map<'a>(&'a self) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
459+
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>> {
460460
{
461461
let visible_parent_map = self.visible_parent_map.borrow();
462462
if !visible_parent_map.is_empty() {
@@ -506,7 +506,7 @@ impl CrateStore for cstore::CStore {
506506
index: CRATE_DEF_INDEX
507507
});
508508
while let Some(def) = bfs_queue.pop_front() {
509-
for child in self.item_children(def) {
509+
for child in self.item_children(def, sess) {
510510
add_child(bfs_queue, child, def);
511511
}
512512
}

src/librustc_metadata/decoder.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'a, 'tcx> CrateMetadata {
654654
}
655655

656656
/// Iterates over each child of the given item.
657-
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
657+
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F, sess: &Session)
658658
where F: FnMut(def::Export)
659659
{
660660
if let Some(ref proc_macros) = self.proc_macros {
@@ -677,19 +677,19 @@ impl<'a, 'tcx> CrateMetadata {
677677
// Find the item.
678678
let item = match self.maybe_entry(id) {
679679
None => return,
680-
Some(item) => item.decode(self),
680+
Some(item) => item.decode((self, sess)),
681681
};
682682

683683
// Iterate over all children.
684684
let macros_only = self.dep_kind.get().macros_only();
685-
for child_index in item.children.decode(self) {
685+
for child_index in item.children.decode((self, sess)) {
686686
if macros_only {
687687
continue
688688
}
689689

690690
// Get the item.
691691
if let Some(child) = self.maybe_entry(child_index) {
692-
let child = child.decode(self);
692+
let child = child.decode((self, sess));
693693
match child.kind {
694694
EntryKind::MacroDef(..) => {}
695695
_ if macros_only => continue,
@@ -700,12 +700,12 @@ impl<'a, 'tcx> CrateMetadata {
700700
match child.kind {
701701
// FIXME(eddyb) Don't encode these in children.
702702
EntryKind::ForeignMod => {
703-
for child_index in child.children.decode(self) {
703+
for child_index in child.children.decode((self, sess)) {
704704
if let Some(def) = self.get_def(child_index) {
705705
callback(def::Export {
706706
def: def,
707707
ident: Ident::with_empty_ctxt(self.item_name(child_index)),
708-
span: self.entry(child_index).span.decode(self),
708+
span: self.entry(child_index).span.decode((self, sess)),
709709
});
710710
}
711711
}
@@ -718,7 +718,7 @@ impl<'a, 'tcx> CrateMetadata {
718718
}
719719

720720
let def_key = self.def_key(child_index);
721-
let span = child.span.decode(self);
721+
let span = child.span.decode((self, sess));
722722
if let (Some(def), Some(name)) =
723723
(self.get_def(child_index), def_key.disambiguated_data.data.get_opt_name()) {
724724
let ident = Ident::with_empty_ctxt(name);
@@ -747,7 +747,7 @@ impl<'a, 'tcx> CrateMetadata {
747747
}
748748

749749
if let EntryKind::Mod(data) = item.kind {
750-
for exp in data.decode(self).reexports.decode(self) {
750+
for exp in data.decode((self, sess)).reexports.decode((self, sess)) {
751751
match exp.def {
752752
Def::Macro(..) => {}
753753
_ if macros_only => continue,

src/librustc_resolve/build_reduced_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<'a> Resolver<'a> {
478478
span);
479479
self.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
480480

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

src/librustc_typeck/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
526526
if !external_mods.insert(def_id) {
527527
return;
528528
}
529-
for child in tcx.sess.cstore.item_children(def_id) {
529+
for child in tcx.sess.cstore.item_children(def_id, tcx.sess) {
530530
handle_external_def(tcx, traits, external_mods, child.def)
531531
}
532532
}

src/librustdoc/clean/inline.rs

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

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl Clean<ExternalCrate> for CrateNum {
241241
}
242242
}).collect()
243243
} else {
244-
cx.tcx.sess.cstore.item_children(root).iter().map(|item| item.def)
244+
cx.tcx.sess.cstore.item_children(root, cx.tcx.sess).iter().map(|item| item.def)
245245
.filter_map(as_primitive).collect()
246246
};
247247

src/librustdoc/visit_lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<'a, 'b, 'tcx> LibEmbargoVisitor<'a, 'b, 'tcx> {
7070
return;
7171
}
7272

73-
for item in self.cstore.item_children(def_id) {
73+
for item in self.cstore.item_children(def_id, self.cx.tcx.sess) {
7474
self.visit_item(item.def);
7575
}
7676
}

0 commit comments

Comments
 (0)