Skip to content

Commit 9a0e88a

Browse files
committed
Refactor away ParentLink.
1 parent 1cf592f commit 9a0e88a

File tree

3 files changed

+78
-101
lines changed

3 files changed

+78
-101
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
//! any imports resolved.
1515
1616
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
17-
use Module;
17+
use {Module, ModuleKind};
1818
use Namespace::{self, TypeNS, ValueNS};
1919
use {NameBinding, NameBindingKind, ToNameBinding};
20-
use ParentLink::{ModuleParentLink, BlockParentLink};
2120
use Resolver;
2221
use {resolve_error, resolve_struct_error, ResolutionError};
2322

@@ -196,19 +195,17 @@ impl<'b> Resolver<'b> {
196195
krate: crate_id,
197196
index: CRATE_DEF_INDEX,
198197
};
199-
let parent_link = ModuleParentLink(parent, name);
200198
let def = Def::Mod(def_id);
201-
let module = self.new_extern_crate_module(parent_link, def, item.id);
199+
let module = self.new_extern_crate_module(parent, name, def, item.id);
202200
self.define(parent, name, TypeNS, (module, sp, vis));
203201

204202
self.populate_module_if_necessary(module);
205203
}
206204
}
207205

208206
ItemKind::Mod(..) => {
209-
let parent_link = ModuleParentLink(parent, name);
210207
let def = Def::Mod(self.definitions.local_def_id(item.id));
211-
let module = self.new_module(parent_link, Some(def), Some(item.id));
208+
let module = self.new_module(parent, ModuleKind::Def(def, name), Some(item.id));
212209
module.no_implicit_prelude.set({
213210
parent.no_implicit_prelude.get() ||
214211
attr::contains_name(&item.attrs, "no_implicit_prelude")
@@ -244,9 +241,8 @@ impl<'b> Resolver<'b> {
244241
}
245242

246243
ItemKind::Enum(ref enum_definition, _) => {
247-
let parent_link = ModuleParentLink(parent, name);
248-
let def = Def::Enum(self.definitions.local_def_id(item.id));
249-
let module = self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
244+
let kind = ModuleKind::Def(Def::Enum(self.definitions.local_def_id(item.id)), name);
245+
let module = self.new_module(parent, kind, parent.normal_ancestor_id);
250246
self.define(parent, name, TypeNS, (module, sp, vis));
251247

252248
for variant in &(*enum_definition).variants {
@@ -297,10 +293,8 @@ impl<'b> Resolver<'b> {
297293
let def_id = self.definitions.local_def_id(item.id);
298294

299295
// Add all the items within to a new module.
300-
let parent_link = ModuleParentLink(parent, name);
301-
let def = Def::Trait(def_id);
302-
let module_parent =
303-
self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
296+
let kind = ModuleKind::Def(Def::Trait(def_id), name);
297+
let module_parent = self.new_module(parent, kind, parent.normal_ancestor_id);
304298
self.define(parent, name, TypeNS, (module_parent, sp, vis));
305299

306300
// Add the names of all the items to the trait info.
@@ -375,8 +369,8 @@ impl<'b> Resolver<'b> {
375369
{}",
376370
block_id);
377371

378-
let parent_link = BlockParentLink(parent, block_id);
379-
let new_module = self.new_module(parent_link, None, parent.normal_ancestor_id);
372+
let new_module =
373+
self.new_module(parent, ModuleKind::Block(block_id), parent.normal_ancestor_id);
380374
self.module_map.insert(block_id, new_module);
381375
self.current_module = new_module; // Descend into the block.
382376
}
@@ -407,8 +401,7 @@ impl<'b> Resolver<'b> {
407401
Def::Mod(_) | Def::Enum(..) => {
408402
debug!("(building reduced graph for external crate) building module {} {:?}",
409403
name, vis);
410-
let parent_link = ModuleParentLink(parent, name);
411-
let module = self.new_module(parent_link, Some(def), None);
404+
let module = self.new_module(parent, ModuleKind::Def(def, name), None);
412405
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
413406
}
414407
Def::Variant(variant_id) => {
@@ -451,8 +444,7 @@ impl<'b> Resolver<'b> {
451444
self.trait_item_map.insert((trait_item_name, def_id), false);
452445
}
453446

454-
let parent_link = ModuleParentLink(parent, name);
455-
let module = self.new_module(parent_link, Some(def), None);
447+
let module = self.new_module(parent, ModuleKind::Def(def, name), None);
456448
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
457449
}
458450
Def::TyAlias(..) | Def::AssociatedTy(..) => {

src/librustc_resolve/lib.rs

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use self::TypeParameters::*;
4141
use self::RibKind::*;
4242
use self::UseLexicalScopeFlag::*;
4343
use self::ModulePrefixResult::*;
44-
use self::ParentLink::*;
4544

4645
use rustc::hir::map::Definitions;
4746
use rustc::hir::{self, PrimTy, TyBool, TyChar, TyFloat, TyInt, TyUint, TyStr};
@@ -753,18 +752,15 @@ impl<'a> LexicalScopeBinding<'a> {
753752
}
754753
}
755754

756-
/// The link from a module up to its nearest parent node.
757-
#[derive(Clone,Debug)]
758-
enum ParentLink<'a> {
759-
NoParentLink,
760-
ModuleParentLink(Module<'a>, Name),
761-
BlockParentLink(Module<'a>, NodeId),
755+
enum ModuleKind {
756+
Block(NodeId),
757+
Def(Def, Name),
762758
}
763759

764760
/// One node in the tree of modules.
765761
pub struct ModuleS<'a> {
766-
parent_link: ParentLink<'a>,
767-
def: Option<Def>,
762+
parent: Option<Module<'a>>,
763+
kind: ModuleKind,
768764

769765
// The node id of the closest normal module (`mod`) ancestor (including this module).
770766
normal_ancestor_id: Option<NodeId>,
@@ -792,11 +788,11 @@ pub struct ModuleS<'a> {
792788
pub type Module<'a> = &'a ModuleS<'a>;
793789

794790
impl<'a> ModuleS<'a> {
795-
fn new(parent_link: ParentLink<'a>, def: Option<Def>, normal_ancestor_id: Option<NodeId>)
791+
fn new(parent: Option<Module<'a>>, kind: ModuleKind, normal_ancestor_id: Option<NodeId>)
796792
-> Self {
797793
ModuleS {
798-
parent_link: parent_link,
799-
def: def,
794+
parent: parent,
795+
kind: kind,
800796
normal_ancestor_id: normal_ancestor_id,
801797
extern_crate_id: None,
802798
resolutions: RefCell::new(FnvHashMap()),
@@ -814,36 +810,36 @@ impl<'a> ModuleS<'a> {
814810
}
815811
}
816812

813+
fn def(&self) -> Option<Def> {
814+
match self.kind {
815+
ModuleKind::Def(def, _) => Some(def),
816+
_ => None,
817+
}
818+
}
819+
817820
fn def_id(&self) -> Option<DefId> {
818-
self.def.as_ref().map(Def::def_id)
821+
self.def().as_ref().map(Def::def_id)
819822
}
820823

821824
// `self` resolves to the first module ancestor that `is_normal`.
822825
fn is_normal(&self) -> bool {
823-
match self.def {
824-
Some(Def::Mod(_)) => true,
826+
match self.kind {
827+
ModuleKind::Def(Def::Mod(_), _) => true,
825828
_ => false,
826829
}
827830
}
828831

829832
fn is_trait(&self) -> bool {
830-
match self.def {
831-
Some(Def::Trait(_)) => true,
833+
match self.kind {
834+
ModuleKind::Def(Def::Trait(_), _) => true,
832835
_ => false,
833836
}
834837
}
835-
836-
fn parent(&self) -> Option<&'a Self> {
837-
match self.parent_link {
838-
ModuleParentLink(parent, _) | BlockParentLink(parent, _) => Some(parent),
839-
NoParentLink => None,
840-
}
841-
}
842838
}
843839

844840
impl<'a> fmt::Debug for ModuleS<'a> {
845841
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
846-
write!(f, "{:?}", self.def)
842+
write!(f, "{:?}", self.def())
847843
}
848844
}
849845

@@ -903,7 +899,7 @@ impl<'a> NameBinding<'a> {
903899
fn def(&self) -> Def {
904900
match self.kind {
905901
NameBindingKind::Def(def) => def,
906-
NameBindingKind::Module(module) => module.def.unwrap(),
902+
NameBindingKind::Module(module) => module.def().unwrap(),
907903
NameBindingKind::Import { binding, .. } => binding.def(),
908904
NameBindingKind::Ambiguity { .. } => Def::Err,
909905
}
@@ -1111,7 +1107,7 @@ impl<'a> ResolverArenas<'a> {
11111107
impl<'a> ty::NodeIdTree for Resolver<'a> {
11121108
fn is_descendant_of(&self, mut node: NodeId, ancestor: NodeId) -> bool {
11131109
while node != ancestor {
1114-
node = match self.module_map[&node].parent() {
1110+
node = match self.module_map[&node].parent {
11151111
Some(parent) => parent.normal_ancestor_id.unwrap(),
11161112
None => return false,
11171113
}
@@ -1178,10 +1174,10 @@ impl<'a> Resolver<'a> {
11781174
macro_loader: &'a mut MacroLoader,
11791175
arenas: &'a ResolverArenas<'a>)
11801176
-> Resolver<'a> {
1181-
let root_def_id = DefId::local(CRATE_DEF_INDEX);
1177+
let graph_root_kind =
1178+
ModuleKind::Def(Def::Mod(DefId::local(CRATE_DEF_INDEX)), keywords::Invalid.name());
11821179
let graph_root =
1183-
ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), Some(CRATE_NODE_ID));
1184-
let graph_root = arenas.alloc_module(graph_root);
1180+
arenas.alloc_module(ModuleS::new(None, graph_root_kind, Some(CRATE_NODE_ID)));
11851181
let mut module_map = NodeMap();
11861182
module_map.insert(CRATE_NODE_ID, graph_root);
11871183

@@ -1263,18 +1259,15 @@ impl<'a> Resolver<'a> {
12631259
self.report_errors();
12641260
}
12651261

1266-
fn new_module(&self,
1267-
parent_link: ParentLink<'a>,
1268-
def: Option<Def>,
1269-
normal_ancestor_id: Option<NodeId>)
1262+
fn new_module(&self, parent: Module<'a>, kind: ModuleKind, normal_ancestor_id: Option<NodeId>)
12701263
-> Module<'a> {
1271-
self.arenas.alloc_module(ModuleS::new(parent_link, def, normal_ancestor_id))
1264+
self.arenas.alloc_module(ModuleS::new(Some(parent), kind, normal_ancestor_id))
12721265
}
12731266

1274-
fn new_extern_crate_module(&self, parent_link: ParentLink<'a>, def: Def, local_node_id: NodeId)
1267+
fn new_extern_crate_module(&self, parent: Module<'a>, name: Name, def: Def, node_id: NodeId)
12751268
-> Module<'a> {
1276-
let mut module = ModuleS::new(parent_link, Some(def), Some(local_node_id));
1277-
module.extern_crate_id = Some(local_node_id);
1269+
let mut module = ModuleS::new(Some(parent), ModuleKind::Def(def, name), Some(node_id));
1270+
module.extern_crate_id = Some(node_id);
12781271
module.populated.set(false);
12791272
self.arenas.modules.alloc(module)
12801273
}
@@ -1336,11 +1329,10 @@ impl<'a> Resolver<'a> {
13361329
-> Option<Module<'a>> {
13371330
match this.resolve_name_in_module(module, needle, TypeNS, false, None) {
13381331
Success(binding) if binding.is_extern_crate() => Some(module),
1339-
_ => match module.parent_link {
1340-
ModuleParentLink(ref parent, _) => {
1341-
search_parent_externals(this, needle, parent)
1342-
}
1343-
_ => None,
1332+
_ => if let (&ModuleKind::Def(..), Some(parent)) = (&module.kind, module.parent) {
1333+
search_parent_externals(this, needle, parent)
1334+
} else {
1335+
None
13441336
},
13451337
}
13461338
}
@@ -1516,15 +1508,13 @@ impl<'a> Resolver<'a> {
15161508
return Some(LexicalScopeBinding::Item(binding));
15171509
}
15181510

1519-
// We can only see through anonymous modules
1520-
if module.def.is_some() {
1521-
return match self.prelude {
1522-
Some(prelude) if !module.no_implicit_prelude.get() => {
1523-
self.resolve_name_in_module(prelude, name, ns, false, None).success()
1524-
.map(LexicalScopeBinding::Item)
1525-
}
1526-
_ => None,
1527-
};
1511+
if let ModuleKind::Block(..) = module.kind { // We can see through blocks
1512+
} else if !module.no_implicit_prelude.get() {
1513+
return self.prelude.and_then(|prelude| {
1514+
self.resolve_name_in_module(prelude, name, ns, false, None).success()
1515+
}).map(LexicalScopeBinding::Item)
1516+
} else {
1517+
return None;
15281518
}
15291519
}
15301520

@@ -1561,7 +1551,7 @@ impl<'a> Resolver<'a> {
15611551
while i < module_path.len() && "super" == module_path[i].as_str() {
15621552
debug!("(resolving module prefix) resolving `super` at {}",
15631553
module_to_string(&containing_module));
1564-
if let Some(parent) = containing_module.parent() {
1554+
if let Some(parent) = containing_module.parent {
15651555
containing_module = self.module_map[&parent.normal_ancestor_id.unwrap()];
15661556
i += 1;
15671557
} else {
@@ -2954,7 +2944,7 @@ impl<'a> Resolver<'a> {
29542944
UseLexicalScope,
29552945
Some(expr.span)) {
29562946
Success(e) => {
2957-
if let Some(def_type) = e.def {
2947+
if let Some(def_type) = e.def() {
29582948
def = def_type;
29592949
}
29602950
context = UnresolvedNameContext::PathIsMod(parent);
@@ -3163,16 +3153,13 @@ impl<'a> Resolver<'a> {
31633153
};
31643154
search_in_module(self, search_module);
31653155

3166-
match search_module.parent_link {
3167-
NoParentLink | ModuleParentLink(..) => {
3168-
if !search_module.no_implicit_prelude.get() {
3169-
self.prelude.map(|prelude| search_in_module(self, prelude));
3170-
}
3171-
break;
3172-
}
3173-
BlockParentLink(parent_module, _) => {
3174-
search_module = parent_module;
3156+
if let ModuleKind::Block(..) = search_module.kind {
3157+
search_module = search_module.parent.unwrap();
3158+
} else {
3159+
if !search_module.no_implicit_prelude.get() {
3160+
self.prelude.map(|prelude| search_in_module(self, prelude));
31753161
}
3162+
break;
31763163
}
31773164
}
31783165

@@ -3240,9 +3227,9 @@ impl<'a> Resolver<'a> {
32403227
// collect submodules to explore
32413228
if let Ok(module) = name_binding.module() {
32423229
// form the path
3243-
let path_segments = match module.parent_link {
3244-
NoParentLink => path_segments.clone(),
3245-
ModuleParentLink(_, name) => {
3230+
let path_segments = match module.kind {
3231+
_ if module.parent.is_none() => path_segments.clone(),
3232+
ModuleKind::Def(_, name) => {
32463233
let mut paths = path_segments.clone();
32473234
let ident = ast::Ident::with_empty_ctxt(name);
32483235
let params = PathParameters::none();
@@ -3259,7 +3246,7 @@ impl<'a> Resolver<'a> {
32593246
if !in_module_is_extern || name_binding.vis == ty::Visibility::Public {
32603247
// add the module to the lookup
32613248
let is_extern = in_module_is_extern || name_binding.is_extern_crate();
3262-
if !worklist.iter().any(|&(m, ..)| m.def == module.def) {
3249+
if !worklist.iter().any(|&(m, ..)| m.def() == module.def()) {
32633250
worklist.push((module, path_segments, is_extern));
32643251
}
32653252
}
@@ -3294,7 +3281,7 @@ impl<'a> Resolver<'a> {
32943281
let mut path_resolution = err_path_resolution();
32953282
let vis = match self.resolve_module_path(&segments, DontUseLexicalScope, Some(path.span)) {
32963283
Success(module) => {
3297-
path_resolution = PathResolution::new(module.def.unwrap());
3284+
path_resolution = PathResolution::new(module.def().unwrap());
32983285
ty::Visibility::Restricted(module.normal_ancestor_id.unwrap())
32993286
}
33003287
Indeterminate => unreachable!(),
@@ -3360,10 +3347,10 @@ impl<'a> Resolver<'a> {
33603347
return self.report_conflict(parent, name, ns, old_binding, binding);
33613348
}
33623349

3363-
let container = match parent.def {
3364-
Some(Def::Mod(_)) => "module",
3365-
Some(Def::Trait(_)) => "trait",
3366-
None => "block",
3350+
let container = match parent.kind {
3351+
ModuleKind::Def(Def::Mod(_), _) => "module",
3352+
ModuleKind::Def(Def::Trait(_), _) => "trait",
3353+
ModuleKind::Block(..) => "block",
33673354
_ => "enum",
33683355
};
33693356

@@ -3510,17 +3497,15 @@ fn module_to_string(module: Module) -> String {
35103497
let mut names = Vec::new();
35113498

35123499
fn collect_mod(names: &mut Vec<ast::Name>, module: Module) {
3513-
match module.parent_link {
3514-
NoParentLink => {}
3515-
ModuleParentLink(ref module, name) => {
3500+
if let ModuleKind::Def(_, name) = module.kind {
3501+
if let Some(parent) = module.parent {
35163502
names.push(name);
3517-
collect_mod(names, module);
3518-
}
3519-
BlockParentLink(ref module, _) => {
3520-
// danger, shouldn't be ident?
3521-
names.push(token::intern("<opaque>"));
3522-
collect_mod(names, module);
3503+
collect_mod(names, parent);
35233504
}
3505+
} else {
3506+
// danger, shouldn't be ident?
3507+
names.push(token::intern("<opaque>"));
3508+
collect_mod(names, module);
35243509
}
35253510
}
35263511
collect_mod(&mut names, module);

0 commit comments

Comments
 (0)