Skip to content

Commit 6a6e1db

Browse files
committed
Refactor away get_module_if_available and get_module and reformat one-liners
1 parent 572c2f3 commit 6a6e1db

File tree

4 files changed

+56
-88
lines changed

4 files changed

+56
-88
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,9 @@ impl<'a, 'b:'a, 'tcx:'b> DerefMut for GraphBuilder<'a, 'b, 'tcx> {
8686
impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
8787
/// Constructs the reduced graph for the entire crate.
8888
fn build_reduced_graph(self, krate: &hir::Crate) {
89-
let parent = self.graph_root.get_module();
9089
let mut visitor = BuildReducedGraphVisitor {
90+
parent: self.graph_root.clone(),
9191
builder: self,
92-
parent: parent,
9392
};
9493
visit::walk_crate(&mut visitor, krate);
9594
}
@@ -318,10 +317,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
318317
};
319318
self.external_exports.insert(def_id);
320319
let parent_link = ModuleParentLink(Rc::downgrade(parent), name);
321-
let external_module = Rc::new(Module::new(parent_link,
322-
Some(DefMod(def_id)),
323-
false,
324-
true));
320+
let def = DefMod(def_id);
321+
let external_module = Module::new(parent_link, Some(def), false, true);
322+
325323
debug!("(build reduced graph for item) found extern `{}`",
326324
module_to_string(&*external_module));
327325
self.check_for_conflicts_between_external_crates(&**parent, name, sp);
@@ -338,9 +336,9 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
338336

339337
let parent_link = self.get_parent_link(parent, name);
340338
let def = DefMod(self.ast_map.local_def_id(item.id));
341-
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
342-
343-
name_bindings.get_module()
339+
let module = Module::new(parent_link, Some(def), false, is_public);
340+
name_bindings.define_module(module.clone(), sp);
341+
module
344342
}
345343

346344
ItemForeignMod(..) => parent.clone(),
@@ -377,7 +375,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
377375

378376
let parent_link = self.get_parent_link(parent, name);
379377
let def = DefTy(self.ast_map.local_def_id(item.id), false);
380-
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
378+
let module = Module::new(parent_link, Some(def), false, is_public);
379+
name_bindings.define_module(module, sp);
381380
parent.clone()
382381
}
383382

@@ -389,9 +388,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
389388

390389
let parent_link = self.get_parent_link(parent, name);
391390
let def = DefTy(self.ast_map.local_def_id(item.id), true);
392-
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
393-
394-
let module = name_bindings.get_module();
391+
let module = Module::new(parent_link, Some(def), false, is_public);
392+
name_bindings.define_module(module.clone(), sp);
395393

396394
for variant in &(*enum_definition).variants {
397395
let item_def_id = self.ast_map.local_def_id(item.id);
@@ -454,8 +452,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
454452
// Add all the items within to a new module.
455453
let parent_link = self.get_parent_link(parent, name);
456454
let def = DefTrait(def_id);
457-
name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
458-
let module_parent = name_bindings.get_module();
455+
let module_parent = Module::new(parent_link, Some(def), false, is_public);
456+
name_bindings.define_module(module_parent.clone(), sp);
459457

460458
// Add the names of all the items to the trait info.
461459
for trait_item in items {
@@ -555,10 +553,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
555553
{}",
556554
block_id);
557555

558-
let new_module = Rc::new(Module::new(BlockParentLink(Rc::downgrade(parent), block_id),
559-
None,
560-
false,
561-
false));
556+
let parent_link = BlockParentLink(Rc::downgrade(parent), block_id);
557+
let new_module = Module::new(parent_link, None, false, false);
562558
parent.anonymous_children.borrow_mut().insert(block_id, new_module.clone());
563559
new_module
564560
} else {
@@ -604,12 +600,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
604600
final_ident,
605601
is_public);
606602
let parent_link = self.get_parent_link(new_parent, name);
607-
608-
child_name_bindings.define_module(parent_link,
609-
Some(def),
610-
true,
611-
is_public,
612-
DUMMY_SP);
603+
let module = Module::new(parent_link, Some(def), true, is_public);
604+
child_name_bindings.define_module(module, DUMMY_SP);
613605
}
614606
}
615607
_ => {}
@@ -681,11 +673,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
681673

682674
// Define a module if necessary.
683675
let parent_link = self.get_parent_link(new_parent, name);
684-
child_name_bindings.define_module(parent_link,
685-
Some(def),
686-
true,
687-
is_public,
688-
DUMMY_SP)
676+
let module = Module::new(parent_link, Some(def), true, is_public);
677+
child_name_bindings.define_module(module, DUMMY_SP);
689678
}
690679
DefTy(..) | DefAssociatedTy(..) => {
691680
debug!("(building reduced graph for external crate) building type {}",

src/librustc_resolve/lib.rs

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -813,8 +813,8 @@ impl Module {
813813
def: Option<Def>,
814814
external: bool,
815815
is_public: bool)
816-
-> Module {
817-
Module {
816+
-> Rc<Module> {
817+
Rc::new(Module {
818818
parent_link: parent_link,
819819
def: Cell::new(def),
820820
is_public: is_public,
@@ -828,7 +828,7 @@ impl Module {
828828
pub_glob_count: Cell::new(0),
829829
resolved_import_count: Cell::new(0),
830830
populated: Cell::new(!external),
831-
}
831+
})
832832
}
833833

834834
fn def_id(&self) -> Option<DefId> {
@@ -971,19 +971,27 @@ impl NameBinding {
971971
}
972972
}
973973

974-
fn and_then<T, F: Fn(&NsDef) -> Option<T>>(&self, f: F) -> Option<T> {
975-
self.borrow().as_ref().and_then(f)
974+
fn borrow(&self) -> ::std::cell::Ref<Option<NsDef>> {
975+
self.0.borrow()
976976
}
977977

978-
fn borrow(&self) -> ::std::cell::Ref<Option<NsDef>> { self.0.borrow() }
979-
980978
// Lifted versions of the NsDef methods and fields
981-
fn def(&self) -> Option<Def> { self.and_then(NsDef::def) }
982-
fn module(&self) -> Option<Rc<Module>> { self.and_then(NsDef::module) }
983-
fn span(&self) -> Option<Span> { self.and_then(|def| def.span) }
984-
fn modifiers(&self) -> Option<DefModifiers> { self.and_then(|def| Some(def.modifiers)) }
979+
fn def(&self) -> Option<Def> {
980+
self.borrow().as_ref().and_then(NsDef::def)
981+
}
982+
fn module(&self) -> Option<Rc<Module>> {
983+
self.borrow().as_ref().and_then(NsDef::module)
984+
}
985+
fn span(&self) -> Option<Span> {
986+
self.borrow().as_ref().and_then(|def| def.span)
987+
}
988+
fn modifiers(&self) -> Option<DefModifiers> {
989+
self.borrow().as_ref().and_then(|def| Some(def.modifiers))
990+
}
985991

986-
fn defined(&self) -> bool { self.borrow().is_some() }
992+
fn defined(&self) -> bool {
993+
self.borrow().is_some()
994+
}
987995

988996
fn defined_with(&self, modifiers: DefModifiers) -> bool {
989997
self.modifiers().map(|m| m.contains(modifiers)).unwrap_or(false)
@@ -1030,14 +1038,8 @@ impl NameBindings {
10301038
}
10311039

10321040
/// Creates a new module in this set of name bindings.
1033-
fn define_module(&self,
1034-
parent_link: ParentLink,
1035-
def: Option<Def>,
1036-
external: bool,
1037-
is_public: bool,
1038-
sp: Span) {
1039-
let module = Module::new(parent_link, def, external, is_public);
1040-
self.type_ns.set(NsDef::create_from_module(Rc::new(module), Some(sp)));
1041+
fn define_module(&self, module: Rc<Module>, sp: Span) {
1042+
self.type_ns.set(NsDef::create_from_module(module, Some(sp)));
10411043
}
10421044

10431045
/// Records a type definition.
@@ -1051,20 +1053,6 @@ impl NameBindings {
10511053
debug!("defining value for def {:?} with modifiers {:?}", def, modifiers);
10521054
self.value_ns.set(NsDef::create_from_def(def, modifiers, Some(sp)));
10531055
}
1054-
1055-
/// Returns the module node if applicable.
1056-
fn get_module_if_available(&self) -> Option<Rc<Module>> { self.type_ns.module() }
1057-
1058-
/// Returns the module node. Panics if this node does not have a module
1059-
/// definition.
1060-
fn get_module(&self) -> Rc<Module> {
1061-
match self.get_module_if_available() {
1062-
None => {
1063-
panic!("get_module called on a node with no module definition!")
1064-
}
1065-
Some(module_def) => module_def,
1066-
}
1067-
}
10681056
}
10691057

10701058
/// Interns the names of the primitive types.
@@ -1106,7 +1094,7 @@ pub struct Resolver<'a, 'tcx: 'a> {
11061094

11071095
ast_map: &'a hir_map::Map<'tcx>,
11081096

1109-
graph_root: NameBindings,
1097+
graph_root: Rc<Module>,
11101098

11111099
trait_item_map: FnvHashMap<(Name, DefId), DefId>,
11121100

@@ -1173,19 +1161,10 @@ enum FallbackChecks {
11731161
impl<'a, 'tcx> Resolver<'a, 'tcx> {
11741162
fn new(session: &'a Session,
11751163
ast_map: &'a hir_map::Map<'tcx>,
1176-
crate_span: Span,
11771164
make_glob_map: MakeGlobMap)
11781165
-> Resolver<'a, 'tcx> {
1179-
let graph_root = NameBindings::new();
1180-
11811166
let root_def_id = ast_map.local_def_id(CRATE_NODE_ID);
1182-
graph_root.define_module(NoParentLink,
1183-
Some(DefMod(root_def_id)),
1184-
false,
1185-
true,
1186-
crate_span);
1187-
1188-
let current_module = graph_root.get_module();
1167+
let graph_root = Module::new(NoParentLink, Some(DefMod(root_def_id)), false, true);
11891168

11901169
Resolver {
11911170
session: session,
@@ -1194,14 +1173,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11941173

11951174
// The outermost module has def ID 0; this is not reflected in the
11961175
// AST.
1197-
graph_root: graph_root,
1176+
graph_root: graph_root.clone(),
11981177

11991178
trait_item_map: FnvHashMap(),
12001179
structs: FnvHashMap(),
12011180

12021181
unresolved_imports: 0,
12031182

1204-
current_module: current_module,
1183+
current_module: graph_root,
12051184
value_ribs: Vec::new(),
12061185
type_ribs: Vec::new(),
12071186
label_ribs: Vec::new(),
@@ -1441,7 +1420,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14411420
DontUseLexicalScope => {
14421421
// This is a crate-relative path. We will start the
14431422
// resolution process at index zero.
1444-
search_module = self.graph_root.get_module();
1423+
search_module = self.graph_root.clone();
14451424
start_index = 0;
14461425
last_private = LastMod(AllPublic);
14471426
}
@@ -1792,7 +1771,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17921771
build_reduced_graph::populate_module_if_necessary(self, &module_);
17931772

17941773
for (_, child_node) in module_.children.borrow().iter() {
1795-
match child_node.get_module_if_available() {
1774+
match child_node.type_ns.module() {
17961775
None => {
17971776
// Continue.
17981777
}
@@ -1845,7 +1824,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
18451824
module_to_string(&*orig_module));
18461825
}
18471826
Some(name_bindings) => {
1848-
match (*name_bindings).get_module_if_available() {
1827+
match name_bindings.type_ns.module() {
18491828
None => {
18501829
debug!("!!! (with scope) didn't find module for `{}` in `{}`",
18511830
name,
@@ -3115,7 +3094,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
31153094
.map(|ps| ps.identifier.name)
31163095
.collect::<Vec<_>>();
31173096

3118-
let root_module = self.graph_root.get_module();
3097+
let root_module = self.graph_root.clone();
31193098

31203099
let containing_module;
31213100
let last_private;
@@ -3278,7 +3257,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32783257
Some(_) => None,
32793258
None => {
32803259
match this.current_module.children.borrow().get(last_name) {
3281-
Some(child) => child.get_module_if_available(),
3260+
Some(child) => child.type_ns.module(),
32823261
None => None,
32833262
}
32843263
}
@@ -3883,7 +3862,7 @@ pub fn create_resolver<'a, 'tcx>(session: &'a Session,
38833862
make_glob_map: MakeGlobMap,
38843863
callback: Option<Box<Fn(hir_map::Node, &mut bool) -> bool>>)
38853864
-> Resolver<'a, 'tcx> {
3886-
let mut resolver = Resolver::new(session, ast_map, krate.span, make_glob_map);
3865+
let mut resolver = Resolver::new(session, ast_map, make_glob_map);
38873866

38883867
resolver.callback = callback;
38893868

src/librustc_resolve/record_exports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'a, 'b, 'tcx> ExportRecorder<'a, 'b, 'tcx> {
7979
build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
8080

8181
for (_, child_name_bindings) in module_.children.borrow().iter() {
82-
match child_name_bindings.get_module_if_available() {
82+
match child_name_bindings.type_ns.module() {
8383
None => {
8484
// Nothing to do.
8585
}
@@ -149,6 +149,6 @@ impl<'a, 'b, 'tcx> ExportRecorder<'a, 'b, 'tcx> {
149149

150150
pub fn record(resolver: &mut Resolver) {
151151
let mut recorder = ExportRecorder { resolver: resolver };
152-
let root_module = recorder.graph_root.get_module();
152+
let root_module = recorder.graph_root.clone();
153153
recorder.record_exports_for_module_subtree(root_module);
154154
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
209209
i,
210210
self.resolver.unresolved_imports);
211211

212-
let module_root = self.resolver.graph_root.get_module();
212+
let module_root = self.resolver.graph_root.clone();
213213
let errors = self.resolve_imports_for_module_subtree(module_root.clone());
214214

215215
if self.resolver.unresolved_imports == 0 {
@@ -254,7 +254,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
254254

255255
build_reduced_graph::populate_module_if_necessary(self.resolver, &module_);
256256
for (_, child_node) in module_.children.borrow().iter() {
257-
match child_node.get_module_if_available() {
257+
match child_node.type_ns.module() {
258258
None => {
259259
// Nothing to do.
260260
}
@@ -337,7 +337,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
337337
// First, resolve the module path for the directive, if necessary.
338338
let container = if module_path.is_empty() {
339339
// Use the crate root.
340-
Some((self.resolver.graph_root.get_module(), LastMod(AllPublic)))
340+
Some((self.resolver.graph_root.clone(), LastMod(AllPublic)))
341341
} else {
342342
match self.resolver.resolve_module_path(module_.clone(),
343343
&module_path[..],

0 commit comments

Comments
 (0)