Skip to content

Commit 8a6187f

Browse files
committed
Refactor fields def_id and kind of Module into a single field def.
Change build_reduced_graph.rs so the fields def and module of NsDef are never both Some unless the NsDef represents a duplicate definition (see issue 26421).
1 parent ceda838 commit 8a6187f

File tree

5 files changed

+126
-184
lines changed

5 files changed

+126
-184
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 33 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
1919
use resolve_imports::ImportResolution;
2020
use Module;
21-
use ModuleKind::*;
2221
use Namespace::{TypeNS, ValueNS};
2322
use NameBindings;
2423
use {names_to_string, module_to_string};
@@ -395,8 +394,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
395394
self.external_exports.insert(def_id);
396395
let parent_link = ModuleParentLink(Rc::downgrade(parent), name);
397396
let external_module = Rc::new(Module::new(parent_link,
398-
Some(def_id),
399-
NormalModuleKind,
397+
Some(DefMod(def_id)),
400398
false,
401399
true));
402400
debug!("(build reduced graph for item) found extern `{}`",
@@ -436,13 +434,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
436434
let name_bindings = self.add_child(name, parent, ForbidDuplicateModules, sp);
437435

438436
let parent_link = self.get_parent_link(parent, name);
439-
let def_id = self.ast_map.local_def_id(item.id);
440-
name_bindings.define_module(parent_link,
441-
Some(def_id),
442-
NormalModuleKind,
443-
false,
444-
is_public,
445-
sp);
437+
let def = DefMod(self.ast_map.local_def_id(item.id));
438+
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
446439

447440
name_bindings.get_module()
448441
}
@@ -479,17 +472,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
479472
ForbidDuplicateTypesAndModules,
480473
sp);
481474

482-
name_bindings.define_type(DefTy(self.ast_map.local_def_id(item.id), false),
483-
sp,
484-
modifiers);
485-
486475
let parent_link = self.get_parent_link(parent, name);
487-
name_bindings.set_module_kind(parent_link,
488-
Some(self.ast_map.local_def_id(item.id)),
489-
TypeModuleKind,
490-
false,
491-
is_public,
492-
sp);
476+
let def = DefTy(self.ast_map.local_def_id(item.id), false);
477+
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
493478
parent.clone()
494479
}
495480

@@ -499,17 +484,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
499484
ForbidDuplicateTypesAndModules,
500485
sp);
501486

502-
name_bindings.define_type(DefTy(self.ast_map.local_def_id(item.id), true),
503-
sp,
504-
modifiers);
505-
506487
let parent_link = self.get_parent_link(parent, name);
507-
name_bindings.set_module_kind(parent_link,
508-
Some(self.ast_map.local_def_id(item.id)),
509-
EnumModuleKind,
510-
false,
511-
is_public,
512-
sp);
488+
let def = DefTy(self.ast_map.local_def_id(item.id), true);
489+
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
513490

514491
let module = name_bindings.get_module();
515492

@@ -592,18 +569,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
592569
ForbidDuplicateTypesAndModules,
593570
sp);
594571

572+
let def_id = self.ast_map.local_def_id(item.id);
573+
595574
// Add all the items within to a new module.
596575
let parent_link = self.get_parent_link(parent, name);
597-
name_bindings.define_module(parent_link,
598-
Some(self.ast_map.local_def_id(item.id)),
599-
TraitModuleKind,
600-
false,
601-
is_public,
602-
sp);
576+
let def = DefTrait(def_id);
577+
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
603578
let module_parent = name_bindings.get_module();
604579

605-
let def_id = self.ast_map.local_def_id(item.id);
606-
607580
// Add the names of all the items to the trait info.
608581
for trait_item in items {
609582
let name_bindings = self.add_child(trait_item.name,
@@ -634,7 +607,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
634607
self.trait_item_map.insert((trait_item.name, def_id), trait_item_def_id);
635608
}
636609

637-
name_bindings.define_type(DefTrait(def_id), sp, modifiers);
638610
parent.clone()
639611
}
640612
}
@@ -705,7 +677,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
705677

706678
let new_module = Rc::new(Module::new(BlockParentLink(Rc::downgrade(parent), block_id),
707679
None,
708-
AnonymousModuleKind,
709680
false,
710681
false));
711682
parent.anonymous_children.borrow_mut().insert(block_id, new_module.clone());
@@ -732,37 +703,30 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
732703
DefModifiers::empty()
733704
} | DefModifiers::IMPORTABLE;
734705
let is_exported = is_public &&
735-
match new_parent.def_id.get() {
706+
match new_parent.def_id() {
736707
None => true,
737708
Some(did) => self.external_exports.contains(&did),
738709
};
739710
if is_exported {
740711
self.external_exports.insert(def.def_id());
741712
}
742713

743-
let kind = match def {
744-
DefTy(_, true) => EnumModuleKind,
745-
DefTy(_, false) | DefStruct(..) => TypeModuleKind,
746-
_ => NormalModuleKind,
747-
};
748-
749714
match def {
750-
DefMod(def_id) |
751-
DefForeignMod(def_id) |
752-
DefStruct(def_id) |
753-
DefTy(def_id, _) => {
715+
DefMod(_) |
716+
DefForeignMod(_) |
717+
DefStruct(_) |
718+
DefTy(..) => {
754719
if let Some(module_def) = child_name_bindings.type_ns.module() {
755720
debug!("(building reduced graph for external crate) already created module");
756-
module_def.def_id.set(Some(def_id));
721+
module_def.def.set(Some(def));
757722
} else {
758723
debug!("(building reduced graph for external crate) building module {} {}",
759724
final_ident,
760725
is_public);
761726
let parent_link = self.get_parent_link(new_parent, name);
762727

763728
child_name_bindings.define_module(parent_link,
764-
Some(def_id),
765-
kind,
729+
Some(def),
766730
true,
767731
is_public,
768732
DUMMY_SP);
@@ -806,7 +770,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
806770
(def.modifiers & DefModifiers::IMPORTABLE),
807771
None => modifiers,
808772
};
809-
if new_parent.kind.get() != NormalModuleKind {
773+
if !new_parent.is_normal() {
810774
modifiers = modifiers & !DefModifiers::IMPORTABLE;
811775
}
812776
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
@@ -835,33 +799,33 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
835799
}
836800
}
837801

838-
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
839-
840802
// Define a module if necessary.
841803
let parent_link = self.get_parent_link(new_parent, name);
842-
child_name_bindings.set_module_kind(parent_link,
843-
Some(def_id),
844-
TraitModuleKind,
845-
true,
846-
is_public,
847-
DUMMY_SP)
804+
child_name_bindings.define_module(parent_link,
805+
Some(def),
806+
true,
807+
is_public,
808+
DUMMY_SP)
848809
}
849810
DefTy(..) | DefAssociatedTy(..) => {
850811
debug!("(building reduced graph for external crate) building type {}",
851812
final_ident);
852813

853-
let modifiers = match new_parent.kind.get() {
854-
NormalModuleKind => modifiers,
814+
let modifiers = match new_parent.is_normal() {
815+
true => modifiers,
855816
_ => modifiers & !DefModifiers::IMPORTABLE,
856817
};
857818

858-
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
819+
if let DefTy(..) = def {
820+
child_name_bindings.type_ns.set_modifiers(modifiers);
821+
} else {
822+
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
823+
}
859824
}
860825
DefStruct(def_id) => {
861826
debug!("(building reduced graph for external crate) building type and value for \
862827
{}",
863828
final_ident);
864-
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
865829
let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);
866830

867831
if fields.is_empty() {
@@ -937,7 +901,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
937901
debug!("(populating external module) attempting to populate {}",
938902
module_to_string(&**module));
939903

940-
let def_id = match module.def_id.get() {
904+
let def_id = match module.def_id() {
941905
None => {
942906
debug!("(populating external module) ... no def ID!");
943907
return;
@@ -971,8 +935,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
971935
/// crate.
972936
fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
973937
csearch::each_top_level_item_of_crate(&self.session.cstore,
974-
root.def_id
975-
.get()
938+
root.def_id()
976939
.unwrap()
977940
.krate,
978941
|def_like, name, visibility| {

0 commit comments

Comments
 (0)