Skip to content

Commit 73afbef

Browse files
committed
Misc tidy ups in resolve
1 parent 542e2bb commit 73afbef

File tree

3 files changed

+56
-63
lines changed

3 files changed

+56
-63
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
371371

372372
ItemExternCrate(_) => {
373373
// n.b. we don't need to look at the path option here, because cstore already did
374-
for &crate_id in self.session.cstore
375-
.find_extern_mod_stmt_cnum(item.id).iter() {
374+
for &crate_id in self.session.cstore.find_extern_mod_stmt_cnum(item.id).iter() {
376375
let def_id = DefId { krate: crate_id, node: 0 };
377376
self.external_exports.insert(def_id);
378377
let parent_link = ModuleParentLink(parent.downgrade(), name);
@@ -400,7 +399,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
400399
Some(def_id),
401400
NormalModuleKind,
402401
false,
403-
item.vis == ast::Public,
402+
is_public,
404403
sp);
405404

406405
name_bindings.get_module()
@@ -432,8 +431,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
432431
// These items live in the type namespace.
433432
ItemTy(..) => {
434433
let name_bindings =
435-
self.add_child(name, parent, ForbidDuplicateTypesAndModules,
436-
sp);
434+
self.add_child(name, parent, ForbidDuplicateTypesAndModules, sp);
437435

438436
name_bindings.define_type(DefTy(local_def(item.id), false), sp,
439437
modifiers);
@@ -517,7 +515,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
517515
Some(local_def(item.id)),
518516
TraitModuleKind,
519517
false,
520-
item.vis == ast::Public,
518+
is_public,
521519
sp);
522520
let module_parent = name_bindings.get_module();
523521

@@ -636,8 +634,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
636634
name: Name,
637635
new_parent: &Rc<Module>) {
638636
debug!("(building reduced graph for \
639-
external crate) building external def, priv {:?}",
640-
vis);
637+
external crate) building external def {}, priv {:?}",
638+
final_ident, vis);
641639
let is_public = vis == ast::Public;
642640
let modifiers = if is_public { PUBLIC } else { DefModifiers::empty() } | IMPORTABLE;
643641
let is_exported = is_public && match new_parent.def_id.get() {
@@ -667,7 +665,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
667665
Some(_) | None => {
668666
debug!("(building reduced graph for \
669667
external crate) building module \
670-
{}", final_ident);
668+
{} {}", final_ident, is_public);
671669
let parent_link = self.get_parent_link(new_parent, name);
672670

673671
child_name_bindings.define_module(parent_link,
@@ -904,18 +902,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
904902

905903
match subclass {
906904
SingleImport(target, _) => {
907-
debug!("(building import directive) building import \
908-
directive: {}::{}",
909-
self.names_to_string(&module_.imports.borrow().last().unwrap().
910-
module_path),
905+
debug!("(building import directive) building import directive: {}::{}",
906+
self.names_to_string(&module_.imports.borrow().last().unwrap().module_path),
911907
token::get_name(target));
912908

913-
let mut import_resolutions = module_.import_resolutions
914-
.borrow_mut();
909+
let mut import_resolutions = module_.import_resolutions.borrow_mut();
915910
match import_resolutions.get_mut(&target) {
916911
Some(resolution) => {
917-
debug!("(building import directive) bumping \
918-
reference");
912+
debug!("(building import directive) bumping reference");
919913
resolution.outstanding_references += 1;
920914

921915
// the source of this name is different now

src/librustc_resolve/lib.rs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,16 @@ impl Target {
428428
#[derive(Debug)]
429429
struct ImportResolution {
430430
/// Whether this resolution came from a `use` or a `pub use`. Note that this
431-
/// should *not* be used whenever resolution is being performed, this is
432-
/// only looked at for glob imports statements currently. Privacy testing
433-
/// occurs during a later phase of compilation.
431+
/// should *not* be used whenever resolution is being performed. Privacy
432+
/// testing occurs during a later phase of compilation.
434433
is_public: bool,
435434

436435
// The number of outstanding references to this name. When this reaches
437436
// zero, outside modules can count on the targets being correct. Before
438437
// then, all bets are off; future imports could override this name.
438+
// Note that this is usually either 0 or 1 - shadowing is forbidden the only
439+
// way outstanding_references is > 1 in a legal program is if the name is
440+
// used in both namespaces.
439441
outstanding_references: uint,
440442

441443
/// The value that this `use` directive names, if there is one.
@@ -1144,8 +1146,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11441146
}
11451147

11461148
fn import_directive_subclass_to_string(&mut self,
1147-
subclass: ImportDirectiveSubclass)
1148-
-> String {
1149+
subclass: ImportDirectiveSubclass)
1150+
-> String {
11491151
match subclass {
11501152
SingleImport(_, source) => {
11511153
token::get_name(source).to_string()
@@ -1155,9 +1157,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11551157
}
11561158

11571159
fn import_path_to_string(&mut self,
1158-
names: &[Name],
1159-
subclass: ImportDirectiveSubclass)
1160-
-> String {
1160+
names: &[Name],
1161+
subclass: ImportDirectiveSubclass)
1162+
-> String {
11611163
if names.is_empty() {
11621164
self.import_directive_subclass_to_string(subclass)
11631165
} else {
@@ -1238,7 +1240,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12381240
match import_directive.subclass {
12391241
SingleImport(target, source) => {
12401242
resolution_result =
1241-
self.resolve_single_import(&*module_,
1243+
self.resolve_single_import(&module_,
12421244
containing_module,
12431245
target,
12441246
source,
@@ -1247,7 +1249,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12471249
}
12481250
GlobImport => {
12491251
resolution_result =
1250-
self.resolve_glob_import(&*module_,
1252+
self.resolve_glob_import(&module_,
12511253
containing_module,
12521254
import_directive,
12531255
lp);
@@ -1270,7 +1272,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12701272
// Decrement the count of unresolved globs if necessary. But only if
12711273
// the resolution result is indeterminate -- otherwise we'll stop
12721274
// processing imports here. (See the loop in
1273-
// resolve_imports_for_module.)
1275+
// resolve_imports_for_module).
12741276

12751277
if !resolution_result.indeterminate() {
12761278
match import_directive.subclass {
@@ -1301,16 +1303,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13011303

13021304
fn resolve_single_import(&mut self,
13031305
module_: &Module,
1304-
containing_module: Rc<Module>,
1306+
target_module: Rc<Module>,
13051307
target: Name,
13061308
source: Name,
13071309
directive: &ImportDirective,
13081310
lp: LastPrivate)
1309-
-> ResolveResult<()> {
1311+
-> ResolveResult<()> {
13101312
debug!("(resolving single import) resolving `{}` = `{}::{}` from \
13111313
`{}` id {}, last private {:?}",
13121314
token::get_name(target),
1313-
self.module_to_string(&*containing_module),
1315+
self.module_to_string(&*target_module),
13141316
token::get_name(source),
13151317
self.module_to_string(module_),
13161318
directive.id,
@@ -1363,14 +1365,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13631365
// containing module, bail out. We don't know enough to be
13641366
// able to resolve this import.
13651367

1366-
if containing_module.glob_count.get() > 0 {
1368+
if target_module.glob_count.get() > 0 {
13671369
debug!("(resolving single import) unresolved glob; \
13681370
bailing out");
13691371
return Indeterminate;
13701372
}
13711373

13721374
// Now search the exported imports within the containing module.
1373-
match containing_module.import_resolutions.borrow().get(&source) {
1375+
match target_module.import_resolutions.borrow().get(&source) {
13741376
None => {
13751377
debug!("(resolving single import) no import");
13761378
// The containing module definitely doesn't have an
@@ -1400,8 +1402,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14001402
return UnboundResult;
14011403
}
14021404

1403-
match import_resolution.
1404-
target_for_namespace(namespace) {
1405+
match import_resolution.target_for_namespace(namespace) {
14051406
None => {
14061407
return UnboundResult;
14071408
}
@@ -1446,7 +1447,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14461447

14471448
}
14481449
Some(_) => {
1449-
// If containing_module is the same module whose import we are resolving
1450+
// If target_module is the same module whose import we are resolving
14501451
// and there it has an unresolved import with the same name as `source`,
14511452
// then the user is actually trying to import an item that is declared
14521453
// in the same scope
@@ -1458,7 +1459,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14581459
// In this case we continue as if we resolved the import and let the
14591460
// check_for_conflicts_between_imports_and_items call below handle
14601461
// the conflict
1461-
match (module_.def_id.get(), containing_module.def_id.get()) {
1462+
match (module_.def_id.get(), target_module.def_id.get()) {
14621463
(Some(id1), Some(id2)) if id1 == id2 => {
14631464
if value_result.is_unknown() {
14641465
value_result = UnboundResult;
@@ -1479,29 +1480,26 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14791480
}
14801481
}
14811482

1482-
// If we didn't find a result in the type namespace, search the
1483-
// external modules.
14841483
let mut value_used_public = false;
14851484
let mut type_used_public = false;
1485+
1486+
// If we didn't find a result in the type namespace, search the
1487+
// external modules.
14861488
match type_result {
14871489
BoundResult(..) => {}
14881490
_ => {
1489-
match containing_module.external_module_children.borrow_mut()
1490-
.get(&source).cloned() {
1491+
match target_module.external_module_children.borrow_mut().get(&source).cloned() {
14911492
None => {} // Continue.
14921493
Some(module) => {
1493-
debug!("(resolving single import) found external \
1494-
module");
1494+
debug!("(resolving single import) found external module");
14951495
// track the module as used.
14961496
match module.def_id.get() {
14971497
Some(DefId{krate: kid, ..}) => { self.used_crates.insert(kid); },
14981498
_ => {}
14991499
}
15001500
let name_bindings =
1501-
Rc::new(Resolver::create_name_bindings_from_module(
1502-
module));
1503-
type_result = BoundResult(containing_module.clone(),
1504-
name_bindings);
1501+
Rc::new(Resolver::create_name_bindings_from_module(module));
1502+
type_result = BoundResult(target_module.clone(), name_bindings);
15051503
type_used_public = true;
15061504
}
15071505
}
@@ -1511,6 +1509,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15111509
// We've successfully resolved the import. Write the results in.
15121510
let mut import_resolutions = module_.import_resolutions.borrow_mut();
15131511
let import_resolution = &mut (*import_resolutions)[target];
1512+
15141513
{
15151514
let mut check_and_write_import = |namespace, result: &_, used_public: &mut bool| {
15161515
let namespace_name = match namespace {
@@ -1561,7 +1560,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15611560
if value_result.is_unbound() && type_result.is_unbound() {
15621561
let msg = format!("There is no `{}` in `{}`",
15631562
token::get_name(source),
1564-
self.module_to_string(&*containing_module));
1563+
self.module_to_string(&target_module));
15651564
return Failed(Some((directive.span, msg)));
15661565
}
15671566
let value_used_public = value_used_reexport || value_used_public;
@@ -1570,7 +1569,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15701569
assert!(import_resolution.outstanding_references >= 1);
15711570
import_resolution.outstanding_references -= 1;
15721571

1573-
// record what this import resolves to for later uses in documentation,
1572+
// Record what this import resolves to for later uses in documentation,
15741573
// this may resolve to either a value or a type, but for documentation
15751574
// purposes it's good enough to just favor one over the other.
15761575
let value_def_and_priv = import_resolution.value_target.as_ref().map(|target| {
@@ -1610,11 +1609,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16101609

16111610
// Resolves a glob import. Note that this function cannot fail; it either
16121611
// succeeds or bails out (as importing * from an empty module or a module
1613-
// that exports nothing is valid). containing_module is the module we are
1612+
// that exports nothing is valid). target_module is the module we are
16141613
// actually importing, i.e., `foo` in `use foo::*`.
16151614
fn resolve_glob_import(&mut self,
16161615
module_: &Module,
1617-
containing_module: Rc<Module>,
1616+
target_module: Rc<Module>,
16181617
import_directive: &ImportDirective,
16191618
lp: LastPrivate)
16201619
-> ResolveResult<()> {
@@ -1628,16 +1627,16 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16281627

16291628
// We must bail out if the node has unresolved imports of any kind
16301629
// (including globs).
1631-
if !(*containing_module).all_imports_resolved() {
1630+
if !(*target_module).all_imports_resolved() {
16321631
debug!("(resolving glob import) target module has unresolved \
16331632
imports; bailing out");
16341633
return Indeterminate;
16351634
}
16361635

1637-
assert_eq!(containing_module.glob_count.get(), 0);
1636+
assert_eq!(target_module.glob_count.get(), 0);
16381637

16391638
// Add all resolved imports from the containing module.
1640-
let import_resolutions = containing_module.import_resolutions.borrow();
1639+
let import_resolutions = target_module.import_resolutions.borrow();
16411640
for (ident, target_import_resolution) in &*import_resolutions {
16421641
debug!("(resolving glob import) writing module resolution \
16431642
{} into `{}`",
@@ -1697,30 +1696,30 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16971696
}
16981697

16991698
// Add all children from the containing module.
1700-
build_reduced_graph::populate_module_if_necessary(self, &containing_module);
1699+
build_reduced_graph::populate_module_if_necessary(self, &target_module);
17011700

1702-
for (&name, name_bindings) in &*containing_module.children.borrow() {
1701+
for (&name, name_bindings) in &*target_module.children.borrow() {
17031702
self.merge_import_resolution(module_,
1704-
containing_module.clone(),
1703+
target_module.clone(),
17051704
import_directive,
17061705
name,
17071706
name_bindings.clone());
17081707

17091708
}
17101709

17111710
// Add external module children from the containing module.
1712-
for (&name, module) in &*containing_module.external_module_children.borrow() {
1711+
for (&name, module) in &*target_module.external_module_children.borrow() {
17131712
let name_bindings =
17141713
Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
17151714
self.merge_import_resolution(module_,
1716-
containing_module.clone(),
1715+
target_module.clone(),
17171716
import_directive,
17181717
name,
17191718
name_bindings);
17201719
}
17211720

17221721
// Record the destination of this import
1723-
if let Some(did) = containing_module.def_id.get() {
1722+
if let Some(did) = target_module.def_id.get() {
17241723
self.def_map.borrow_mut().insert(id, PathResolution {
17251724
base_def: DefMod(did),
17261725
last_private: lp,

src/librustc_resolve/record_exports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ impl<'a, 'b, 'tcx> ExportRecorder<'a, 'b, 'tcx> {
133133
fn add_exports_for_module(&mut self,
134134
exports: &mut Vec<Export>,
135135
module_: &Module) {
136-
for (name, importresolution) in &*module_.import_resolutions.borrow() {
137-
if !importresolution.is_public {
136+
for (name, import_resolution) in &*module_.import_resolutions.borrow() {
137+
if !import_resolution.is_public {
138138
continue
139139
}
140140
let xs = [TypeNS, ValueNS];
141141
for &ns in &xs {
142-
match importresolution.target_for_namespace(ns) {
142+
match import_resolution.target_for_namespace(ns) {
143143
Some(target) => {
144144
debug!("(computing exports) maybe export '{}'",
145145
token::get_name(*name));

0 commit comments

Comments
 (0)