Skip to content

Commit 24aef24

Browse files
committed
rustc_metadata: split the Def description of a DefId from item_children.
1 parent adddfcc commit 24aef24

File tree

9 files changed

+95
-109
lines changed

9 files changed

+95
-109
lines changed

src/librustc/middle/cstore.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ pub enum InlinedItemRef<'a> {
9696
ImplItem(DefId, &'a hir::ImplItem)
9797
}
9898

99-
#[derive(Copy, Clone)]
100-
pub struct ChildItem {
101-
pub def: Def,
102-
pub name: ast::Name,
103-
pub vis: ty::Visibility,
104-
}
105-
10699
#[derive(Copy, Clone, Debug)]
107100
pub struct ExternCrate {
108101
/// def_id of an `extern crate` in the current crate that caused
@@ -128,6 +121,7 @@ pub struct ExternCrate {
128121
/// can be accessed.
129122
pub trait CrateStore<'tcx> {
130123
// item info
124+
fn describe_def(&self, def: DefId) -> Option<Def>;
131125
fn stability(&self, def: DefId) -> Option<attr::Stability>;
132126
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
133127
fn visibility(&self, def: DefId) -> ty::Visibility;
@@ -209,7 +203,7 @@ pub trait CrateStore<'tcx> {
209203
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
210204
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
211205
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
212-
fn item_children(&self, did: DefId) -> Vec<ChildItem>;
206+
fn item_children(&self, did: DefId) -> Vec<def::Export>;
213207

214208
// misc. metadata
215209
fn maybe_get_item_ast<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -286,6 +280,7 @@ pub struct DummyCrateStore;
286280
#[allow(unused_variables)]
287281
impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
288282
// item info
283+
fn describe_def(&self, def: DefId) -> Option<Def> { bug!("describe_def") }
289284
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
290285
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
291286
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
@@ -386,7 +381,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
386381
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
387382
{ bug!("struct_ctor_def_id") }
388383
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
389-
fn item_children(&self, did: DefId) -> Vec<ChildItem> { bug!("item_children") }
384+
fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
390385

391386
// misc. metadata
392387
fn maybe_get_item_ast<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)

src/librustc_metadata/csearch.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use common;
1313
use encoder;
1414
use loader;
1515

16-
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, ChildItem, ExternCrate};
16+
use rustc::middle::cstore::{InlinedItem, CrateStore, CrateSource, ExternCrate};
1717
use rustc::middle::cstore::{NativeLibraryKind, LinkMeta, LinkagePreference};
18-
use rustc::hir::def;
18+
use rustc::hir::def::{self, Def};
1919
use rustc::middle::lang_items;
2020
use rustc::ty::{self, Ty, TyCtxt};
2121
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
@@ -37,6 +37,11 @@ use rustc_back::target::Target;
3737
use rustc::hir;
3838

3939
impl<'tcx> CrateStore<'tcx> for cstore::CStore {
40+
fn describe_def(&self, def: DefId) -> Option<Def> {
41+
self.dep_graph.read(DepNode::MetaData(def));
42+
self.get_crate_data(def.krate).get_def(def.index)
43+
}
44+
4045
fn stability(&self, def: DefId) -> Option<attr::Stability> {
4146
self.dep_graph.read(DepNode::MetaData(def));
4247
self.get_crate_data(def.krate).get_stability(def.index)
@@ -158,10 +163,8 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
158163
fn impl_or_trait_items(&self, def_id: DefId) -> Vec<DefId> {
159164
self.dep_graph.read(DepNode::MetaData(def_id));
160165
let mut result = vec![];
161-
let get_crate_data = &mut |cnum| self.get_crate_data(cnum);
162166
self.get_crate_data(def_id.krate)
163-
.each_child_of_item(def_id.index, get_crate_data,
164-
&mut |def, _, _| result.push(def.def_id()));
167+
.each_child_of_item(def_id.index, |child| result.push(child.def_id));
165168
result
166169
}
167170

@@ -366,20 +369,12 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
366369
self.get_crate_data(def.krate).get_struct_field_names(def.index)
367370
}
368371

369-
fn item_children(&self, def_id: DefId) -> Vec<ChildItem>
372+
fn item_children(&self, def_id: DefId) -> Vec<def::Export>
370373
{
371374
self.dep_graph.read(DepNode::MetaData(def_id));
372375
let mut result = vec![];
373-
let get_crate_data = &mut |cnum| self.get_crate_data(cnum);
374376
self.get_crate_data(def_id.krate)
375-
.each_child_of_item(def_id.index, get_crate_data,
376-
&mut |def, name, vis| {
377-
result.push(ChildItem {
378-
def: def,
379-
name: name,
380-
vis: vis
381-
});
382-
});
377+
.each_child_of_item(def_id.index, |child| result.push(child));
383378
result
384379
}
385380

@@ -567,7 +562,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
567562
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
568563
if !visible_parent_map.is_empty() { return visible_parent_map; }
569564

570-
use rustc::middle::cstore::ChildItem;
571565
use std::collections::vec_deque::VecDeque;
572566
use std::collections::hash_map::Entry;
573567
for cnum in (1 .. self.next_crate_num().as_usize()).map(CrateNum::new) {
@@ -580,12 +574,12 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
580574
}
581575

582576
let mut bfs_queue = &mut VecDeque::new();
583-
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: ChildItem, parent: DefId| {
584-
let child = if child.vis == ty::Visibility::Public {
585-
child.def.def_id()
586-
} else {
577+
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: def::Export, parent: DefId| {
578+
let child = child.def_id;
579+
580+
if self.visibility(child) != ty::Visibility::Public {
587581
return;
588-
};
582+
}
589583

590584
match visible_parent_map.entry(child) {
591585
Entry::Occupied(mut entry) => {

src/librustc_metadata/decoder.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc::hir;
2323
use rustc::hir::intravisit::IdRange;
2424

2525
use rustc::middle::cstore::{InlinedItem, LinkagePreference};
26-
use rustc::hir::def::Def;
26+
use rustc::hir::def::{self, Def};
2727
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2828
use rustc::middle::lang_items;
2929
use rustc::ty::{ImplContainer, TraitContainer};
@@ -505,6 +505,10 @@ impl<'a, 'tcx> CrateMetadata {
505505
self.maybe_get(doc, item_tag::ty).map(|dcx| dcx.typed(tcx).decode())
506506
}
507507

508+
pub fn get_def(&self, index: DefIndex) -> Option<Def> {
509+
self.item_family(self.entry(index)).to_def(self.local_def_id(index))
510+
}
511+
508512
pub fn get_trait_def(&self,
509513
item_id: DefIndex,
510514
tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::TraitDef<'tcx> {
@@ -664,11 +668,8 @@ impl<'a, 'tcx> CrateMetadata {
664668
}
665669

666670
/// Iterates over each child of the given item.
667-
pub fn each_child_of_item<F, G>(&self, id: DefIndex,
668-
mut get_crate_data: &mut G,
669-
mut callback: &mut F)
670-
where F: FnMut(Def, ast::Name, ty::Visibility),
671-
G: FnMut(CrateNum) -> Rc<CrateMetadata>,
671+
pub fn each_child_of_item<F>(&self, id: DefIndex, mut callback: F)
672+
where F: FnMut(def::Export)
672673
{
673674
// Find the item.
674675
let item_doc = match self.maybe_entry(id) {
@@ -682,15 +683,31 @@ impl<'a, 'tcx> CrateMetadata {
682683
};
683684

684685
// Iterate over all children.
685-
for child_index in dcx.seq::<DefIndex>() {
686+
for child_index in dcx.seq() {
686687
// Get the item.
687688
if let Some(child) = self.maybe_entry(child_index) {
688689
// Hand off the item to the callback.
689-
let family = self.item_family(child);
690-
if let Family::ForeignMod = family {
691-
self.each_child_of_item(child_index, get_crate_data, callback);
692-
} else if let Some(def) = family.to_def(self.local_def_id(child_index)) {
693-
callback(def, self.item_name(child), self.item_visibility(child));
690+
match self.item_family(child) {
691+
// FIXME(eddyb) Don't encode these in children.
692+
Family::ForeignMod => {
693+
for child_index in self.get(child, item_tag::children).seq() {
694+
callback(def::Export {
695+
def_id: self.local_def_id(child_index),
696+
name: self.item_name(self.entry(child_index))
697+
});
698+
}
699+
continue;
700+
}
701+
Family::Impl | Family::DefaultImpl => continue,
702+
703+
_ => {}
704+
}
705+
706+
if let Some(name) = self.maybe_item_name(child) {
707+
callback(def::Export {
708+
def_id: self.local_def_id(child_index),
709+
name: name
710+
});
694711
}
695712
}
696713
}
@@ -700,26 +717,7 @@ impl<'a, 'tcx> CrateMetadata {
700717
_ => return
701718
};
702719
for exp in reexports {
703-
// This reexport may be in yet another crate.
704-
let crate_data = if exp.def_id.krate == self.cnum {
705-
None
706-
} else {
707-
Some(get_crate_data(exp.def_id.krate))
708-
};
709-
let crate_data = match crate_data {
710-
Some(ref cdata) => &**cdata,
711-
None => self
712-
};
713-
714-
// Get the item.
715-
if let Some(child) = crate_data.maybe_entry(exp.def_id.index) {
716-
// Hand off the item to the callback.
717-
if let Some(def) = self.item_family(child).to_def(exp.def_id) {
718-
// These items have a public visibility because they're part of
719-
// a public re-export.
720-
callback(def, exp.name, ty::Visibility::Public);
721-
}
722-
}
720+
callback(exp);
723721
}
724722
}
725723

src/librustc_metadata/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
373373
fn encode_struct_ctor(&mut self, ctor_def_id: DefId) {
374374
self.encode_def_key(ctor_def_id);
375375
self.encode_family(Family::Struct);
376+
self.encode_visibility(ty::Visibility::Public);
376377
self.encode_bounds_and_type_for_item(ctor_def_id);
377378

378379
self.encode_stability(ctor_def_id);

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use ParentLink::{ModuleParentLink, BlockParentLink};
2121
use Resolver;
2222
use {resolve_error, resolve_struct_error, ResolutionError};
2323

24-
use rustc::middle::cstore::ChildItem;
2524
use rustc::hir::def::*;
2625
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
2726
use rustc::hir::map::DefPathData;
@@ -387,10 +386,22 @@ impl<'b> Resolver<'b> {
387386
}
388387

389388
/// Builds the reduced graph for a single item in an external crate.
390-
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>, child: ChildItem) {
391-
let def = child.def;
389+
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
390+
child: Export) {
391+
let def_id = child.def_id;
392392
let name = child.name;
393-
let vis = if parent.is_trait() { ty::Visibility::Public } else { child.vis };
393+
394+
let def = if let Some(def) = self.session.cstore.describe_def(def_id) {
395+
def
396+
} else {
397+
return;
398+
};
399+
400+
let vis = if parent.is_trait() {
401+
ty::Visibility::Public
402+
} else {
403+
self.session.cstore.visibility(def_id)
404+
};
394405

395406
match def {
396407
Def::Mod(_) | Def::Enum(..) => {
@@ -416,7 +427,7 @@ impl<'b> Resolver<'b> {
416427
name);
417428
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
418429
}
419-
Def::Trait(def_id) => {
430+
Def::Trait(_) => {
420431
debug!("(building reduced graph for external crate) building type {}", name);
421432

422433
// If this is a trait, add all the trait item names to the trait
@@ -443,7 +454,7 @@ impl<'b> Resolver<'b> {
443454
debug!("(building reduced graph for external crate) building type {}", name);
444455
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
445456
}
446-
Def::Struct(def_id)
457+
Def::Struct(_)
447458
if self.session.cstore.def_key(def_id).disambiguated_data.data !=
448459
DefPathData::StructCtor
449460
=> {
@@ -459,7 +470,7 @@ impl<'b> Resolver<'b> {
459470
let fields = self.session.cstore.struct_field_names(def_id);
460471
self.structs.insert(def_id, fields);
461472
}
462-
Def::Union(def_id) => {
473+
Def::Union(_) => {
463474
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
464475

465476
// Record the def ID and fields of this union.

src/librustc_typeck/check/method/suggest.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,27 +451,27 @@ pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
451451
fn handle_external_def(ccx: &CrateCtxt,
452452
traits: &mut AllTraitsVec,
453453
external_mods: &mut FnvHashSet<DefId>,
454-
def: Def) {
455-
match def {
456-
Def::Trait(did) => {
457-
traits.push(TraitInfo::new(did));
454+
def_id: DefId) {
455+
match ccx.tcx.sess.cstore.describe_def(def_id) {
456+
Some(Def::Trait(_)) => {
457+
traits.push(TraitInfo::new(def_id));
458458
}
459-
Def::Mod(did) => {
460-
if !external_mods.insert(did) {
459+
Some(Def::Mod(_)) => {
460+
if !external_mods.insert(def_id) {
461461
return;
462462
}
463-
for child in ccx.tcx.sess.cstore.item_children(did) {
464-
handle_external_def(ccx, traits, external_mods, child.def)
463+
for child in ccx.tcx.sess.cstore.item_children(def_id) {
464+
handle_external_def(ccx, traits, external_mods, child.def_id)
465465
}
466466
}
467467
_ => {}
468468
}
469469
}
470470
for cnum in ccx.tcx.sess.cstore.crates() {
471-
handle_external_def(ccx, &mut traits, &mut external_mods, Def::Mod(DefId {
471+
handle_external_def(ccx, &mut traits, &mut external_mods, DefId {
472472
krate: cnum,
473473
index: CRATE_DEF_INDEX
474-
}));
474+
});
475475
}
476476

477477
*ccx.all_traits.borrow_mut() = Some(traits);

src/librustdoc/clean/inline.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,12 @@ fn build_module<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
498498
// visit each node at most once.
499499
let mut visited = FnvHashSet();
500500
for item in tcx.sess.cstore.item_children(did) {
501-
if item.vis == ty::Visibility::Public {
502-
if !visited.insert(item.def) { continue }
503-
if let Some(i) = try_inline_def(cx, tcx, item.def) {
504-
items.extend(i)
501+
if tcx.sess.cstore.visibility(item.def_id) == ty::Visibility::Public {
502+
if !visited.insert(item.def_id) { continue }
503+
if let Some(def) = tcx.sess.cstore.describe_def(item.def_id) {
504+
if let Some(i) = try_inline_def(cx, tcx, def) {
505+
items.extend(i)
506+
}
505507
}
506508
}
507509
}

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,7 @@ impl Clean<ExternalCrate> for CrateNum {
240240
let root = DefId { krate: self.0, index: CRATE_DEF_INDEX };
241241
cx.tcx_opt().map(|tcx| {
242242
for item in tcx.sess.cstore.item_children(root) {
243-
let did = match item.def {
244-
Def::Mod(did) => did,
245-
_ => continue
246-
};
247-
let attrs = inline::load_attrs(cx, tcx, did);
243+
let attrs = inline::load_attrs(cx, tcx, item.def_id);
248244
PrimitiveType::find(&attrs).map(|prim| primitives.push(prim));
249245
}
250246
});

0 commit comments

Comments
 (0)