Skip to content

Commit 7bc6c75

Browse files
committed
Refactor away handle_external_def
1 parent 8f34053 commit 7bc6c75

File tree

1 file changed

+36
-67
lines changed

1 file changed

+36
-67
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use ParentLink::{ModuleParentLink, BlockParentLink};
2424
use Resolver;
2525
use {resolve_error, resolve_struct_error, ResolutionError};
2626

27-
use rustc::middle::cstore::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
27+
use rustc::middle::cstore::{CrateStore, ChildItem, DlDef};
2828
use rustc::middle::def::*;
2929
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
3030
use rustc::middle::ty::VariantKind;
@@ -42,7 +42,6 @@ use rustc_front::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaul
4242
use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
4343
use rustc_front::hir::{PathListIdent, PathListMod, StmtDecl};
4444
use rustc_front::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
45-
use rustc_front::hir::Visibility;
4645
use rustc_front::intravisit::{self, Visitor};
4746

4847
use std::mem::replace;
@@ -439,42 +438,48 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
439438
}
440439
}
441440

442-
fn handle_external_def(&mut self,
443-
def: Def,
444-
vis: Visibility,
445-
final_ident: &str,
446-
name: Name,
447-
new_parent: Module<'b>) {
448-
debug!("(building reduced graph for external crate) building external def {}, priv {:?}",
449-
final_ident,
450-
vis);
451-
let is_public = vis == hir::Public || new_parent.is_trait();
441+
/// Builds the reduced graph for a single item in an external crate.
442+
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>, xcdef: ChildItem) {
443+
let def = match xcdef.def {
444+
DlDef(def) => def,
445+
_ => return,
446+
};
447+
448+
if let Def::ForeignMod(def_id) = def {
449+
// Foreign modules have no names. Recur and populate eagerly.
450+
for child in self.session.cstore.item_children(def_id) {
451+
self.build_reduced_graph_for_external_crate_def(parent, child);
452+
}
453+
return;
454+
}
455+
456+
let name = xcdef.name;
457+
let is_public = xcdef.vis == hir::Public || parent.is_trait();
452458

453459
let mut modifiers = DefModifiers::empty();
454460
if is_public {
455461
modifiers = modifiers | DefModifiers::PUBLIC;
456462
}
457-
if new_parent.is_normal() {
463+
if parent.is_normal() {
458464
modifiers = modifiers | DefModifiers::IMPORTABLE;
459465
}
460466

461467
match def {
462468
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
463469
debug!("(building reduced graph for external crate) building module {} {}",
464-
final_ident,
470+
name,
465471
is_public);
466-
let parent_link = ModuleParentLink(new_parent, name);
472+
let parent_link = ModuleParentLink(parent, name);
467473
let module = self.new_module(parent_link, Some(def), true, is_public);
468-
self.try_define(new_parent, name, TypeNS, (module, DUMMY_SP));
474+
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
469475
}
470476
Def::Variant(_, variant_id) => {
471-
debug!("(building reduced graph for external crate) building variant {}",
472-
final_ident);
477+
debug!("(building reduced graph for external crate) building variant {}", name);
473478
// Variants are always treated as importable to allow them to be glob used.
474479
// All variants are defined in both type and value namespaces as future-proofing.
475480
let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
476-
self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers));
477-
self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers));
481+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
482+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
478483
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
479484
// Not adding fields for variants as they are not accessed with a self receiver
480485
self.structs.insert(variant_id, Vec::new());
@@ -486,12 +491,11 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
486491
Def::AssociatedConst(..) |
487492
Def::Method(..) => {
488493
debug!("(building reduced graph for external crate) building value (fn/static) {}",
489-
final_ident);
490-
self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers));
494+
name);
495+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
491496
}
492497
Def::Trait(def_id) => {
493-
debug!("(building reduced graph for external crate) building type {}",
494-
final_ident);
498+
debug!("(building reduced graph for external crate) building type {}", name);
495499

496500
// If this is a trait, add all the trait item names to the trait
497501
// info.
@@ -508,24 +512,22 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
508512
self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());
509513
}
510514

511-
let parent_link = ModuleParentLink(new_parent, name);
515+
let parent_link = ModuleParentLink(parent, name);
512516
let module = self.new_module(parent_link, Some(def), true, is_public);
513-
self.try_define(new_parent, name, TypeNS, (module, DUMMY_SP));
517+
self.try_define(parent, name, TypeNS, (module, DUMMY_SP));
514518
}
515519
Def::TyAlias(..) | Def::AssociatedTy(..) => {
516-
debug!("(building reduced graph for external crate) building type {}",
517-
final_ident);
518-
self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers));
520+
debug!("(building reduced graph for external crate) building type {}", name);
521+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
519522
}
520523
Def::Struct(def_id)
521524
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
522-
debug!("(building reduced graph for external crate) building type and value for \
523-
{}",
524-
final_ident);
525-
self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers));
525+
debug!("(building reduced graph for external crate) building type and value for {}",
526+
name);
527+
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, modifiers));
526528
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
527529
let def = Def::Struct(ctor_def_id);
528-
self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers));
530+
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, modifiers));
529531
}
530532

531533
// Record the def ID and fields of this struct.
@@ -545,39 +547,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
545547
}
546548
}
547549

548-
/// Builds the reduced graph for a single item in an external crate.
549-
fn build_reduced_graph_for_external_crate_def(&mut self,
550-
root: Module<'b>,
551-
xcdef: ChildItem) {
552-
match xcdef.def {
553-
DlDef(def) => {
554-
// Add the new child item, if necessary.
555-
match def {
556-
Def::ForeignMod(def_id) => {
557-
// Foreign modules have no names. Recur and populate
558-
// eagerly.
559-
for child in self.session.cstore.item_children(def_id) {
560-
self.build_reduced_graph_for_external_crate_def(root, child)
561-
}
562-
}
563-
_ => {
564-
self.handle_external_def(def,
565-
xcdef.vis,
566-
&xcdef.name.as_str(),
567-
xcdef.name,
568-
root);
569-
}
570-
}
571-
}
572-
DlImpl(_) => {
573-
debug!("(building reduced graph for external crate) ignoring impl");
574-
}
575-
DlField => {
576-
debug!("(building reduced graph for external crate) ignoring field");
577-
}
578-
}
579-
}
580-
581550
/// Builds the reduced graph rooted at the given external module.
582551
fn populate_external_module(&mut self, module: Module<'b>) {
583552
debug!("(populating external module) attempting to populate {}",

0 commit comments

Comments
 (0)