Skip to content

Commit 4428b1c

Browse files
committed
Refactor away separate tracking of used_public and used_reexport.
NameBinding now encodes these directly with binding.is_public() and (binding.is_public() && binding.is_import()) (respectively)
1 parent 2e24c74 commit 4428b1c

File tree

2 files changed

+37
-48
lines changed

2 files changed

+37
-48
lines changed

src/librustc_resolve/lib.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,15 +1311,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13111311
name);
13121312
return Indeterminate;
13131313
}
1314-
Success((binding, used_proxy)) => {
1314+
Success(binding) => {
13151315
// Check to see whether there are type bindings, and, if
13161316
// so, whether there is a module within.
13171317
if let Some(module_def) = binding.module() {
13181318
search_module = module_def;
13191319

13201320
// Keep track of the closest private module used
13211321
// when resolving this import chain.
1322-
if !used_proxy && !search_module.is_public {
1322+
if !binding.is_public() {
13231323
if let Some(did) = search_module.def_id() {
13241324
closest_private = LastMod(DependsOn(did));
13251325
}
@@ -1410,7 +1410,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14101410
debug!("(resolving module path for import) indeterminate; bailing");
14111411
return Indeterminate;
14121412
}
1413-
Success((binding, _)) => match binding.module() {
1413+
Success(binding) => match binding.module() {
14141414
Some(containing_module) => {
14151415
search_module = containing_module;
14161416
start_index = 1;
@@ -1444,7 +1444,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14441444
name: Name,
14451445
namespace: Namespace,
14461446
record_used: bool)
1447-
-> ResolveResult<(&'a NameBinding<'a>, bool)> {
1447+
-> ResolveResult<&'a NameBinding<'a>> {
14481448
debug!("(resolving item in lexical scope) resolving `{}` in namespace {:?} in `{}`",
14491449
name,
14501450
namespace,
@@ -1466,10 +1466,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14661466
debug!("(resolving item in lexical scope) indeterminate higher scope; bailing");
14671467
return Indeterminate;
14681468
}
1469-
Success((binding, used_reexport)) => {
1469+
Success(binding) => {
14701470
// We found the module.
14711471
debug!("(resolving item in lexical scope) found name in module, done");
1472-
return Success((binding, used_reexport));
1472+
return Success(binding);
14731473
}
14741474
}
14751475

@@ -1565,16 +1565,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15651565
/// Attempts to resolve the supplied name in the given module for the
15661566
/// given namespace. If successful, returns the binding corresponding to
15671567
/// the name.
1568-
///
1569-
/// The boolean returned on success is an indicator of whether this lookup
1570-
/// passed through a public re-export proxy.
15711568
fn resolve_name_in_module(&mut self,
15721569
module_: Module<'a>,
15731570
name: Name,
15741571
namespace: Namespace,
15751572
allow_private_imports: bool,
15761573
record_used: bool)
1577-
-> ResolveResult<(&'a NameBinding<'a>, bool)> {
1574+
-> ResolveResult<&'a NameBinding<'a>> {
15781575
debug!("(resolving name in module) resolving `{}` in `{}`",
15791576
name,
15801577
module_to_string(&*module_));
@@ -1590,7 +1587,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15901587
self.used_crates.insert(krate);
15911588
}
15921589
}
1593-
return Success((binding, false));
1590+
return Success(binding);
15941591
}
15951592

15961593
// Check the list of resolved imports.
@@ -1605,7 +1602,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16051602
if record_used {
16061603
self.record_import_use(name, namespace, &import_resolution);
16071604
}
1608-
return Success((binding, true));
1605+
return Success(binding);
16091606
}
16101607
}
16111608
Some(..) | None => {} // Continue.
@@ -2636,7 +2633,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
26362633
-> BareIdentifierPatternResolution {
26372634
let module = self.current_module;
26382635
match self.resolve_item_in_lexical_scope(module, name, ValueNS, true) {
2639-
Success((binding, _)) => {
2636+
Success(binding) => {
26402637
debug!("(resolve bare identifier pattern) succeeded in finding {} at {:?}",
26412638
name,
26422639
binding);
@@ -2796,7 +2793,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27962793
let module = self.current_module;
27972794
let name = identifier.unhygienic_name;
27982795
match self.resolve_item_in_lexical_scope(module, name, namespace, record_used) {
2799-
Success((binding, _)) => binding.def().map(LocalDef::from_def),
2796+
Success(binding) => binding.def().map(LocalDef::from_def),
28002797
Failed(Some((span, msg))) => {
28012798
resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
28022799
None
@@ -2934,7 +2931,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29342931
let name = segments.last().unwrap().identifier.name;
29352932
let result = self.resolve_name_in_module(containing_module, name, namespace, false, true);
29362933
let def = match result {
2937-
Success((binding, _)) => {
2934+
Success(binding) => {
29382935
let (def, lp) = binding.def_and_lp();
29392936
(def, last_private.or(lp))
29402937
}
@@ -2990,7 +2987,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29902987

29912988
let name = segments.last().unwrap().identifier.name;
29922989
match self.resolve_name_in_module(containing_module, name, namespace, false, true) {
2993-
Success((binding, _)) => {
2990+
Success(binding) => {
29942991
let (def, lp) = binding.def_and_lp();
29952992
Some((def, last_private.or(lp)))
29962993
}
@@ -3028,11 +3025,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30283025
}
30293026

30303027
if let AnonymousModuleRibKind(module) = self.get_ribs(namespace)[i].kind {
3031-
if let Success((binding, _)) = self.resolve_name_in_module(module,
3032-
ident.unhygienic_name,
3033-
namespace,
3034-
true,
3035-
true) {
3028+
if let Success(binding) = self.resolve_name_in_module(module,
3029+
ident.unhygienic_name,
3030+
namespace,
3031+
true,
3032+
true) {
30363033
if let Some(def) = binding.def() {
30373034
return Some(LocalDef::from_def(def));
30383035
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
371371
}
372372

373373
/// Resolves the name in the namespace of the module because it is being imported by
374-
/// importing_module. Returns the name bindings defining the name
375-
/// and whether or not the name was imported.
374+
/// importing_module. Returns the name bindings defining the name.
376375
fn resolve_name_in_module(&mut self,
377376
module: Module<'b>, // Module containing the name
378377
name: Name,
379378
ns: Namespace,
380379
importing_module: Module<'b>) // Module importing the name
381-
-> (ResolveResult<&'b NameBinding<'b>>, bool) {
380+
-> ResolveResult<&'b NameBinding<'b>> {
382381
build_reduced_graph::populate_module_if_necessary(self.resolver, module);
383382
if let Some(name_binding) = module.get_child(name, ns) {
384383
if name_binding.is_extern_crate() {
@@ -387,32 +386,32 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
387386
self.resolver.used_crates.insert(krate);
388387
}
389388
}
390-
return (Success(name_binding), false)
389+
return Success(name_binding);
391390
}
392391

393392
// If there is an unresolved glob at this point in the containing module, bail out.
394393
// We don't know enough to be able to resolve the name.
395394
if module.pub_glob_count.get() > 0 {
396-
return (Indeterminate, false);
395+
return Indeterminate;
397396
}
398397

399398
match module.import_resolutions.borrow().get(&(name, ns)) {
400399
// The containing module definitely doesn't have an exported import with the
401400
// name in question. We can therefore accurately report that names are unbound.
402-
None => (Failed(None), false),
401+
None => Failed(None),
403402

404403
// The name is an import which has been fully resolved, so we just follow it.
405404
Some(resolution) if resolution.outstanding_references == 0 => {
406405
// Import resolutions must be declared with "pub" in order to be exported.
407406
if !resolution.is_public {
408-
return (Failed(None), false);
407+
return Failed(None);
409408
}
410409

411410
if let Some(binding) = resolution.binding {
412411
self.resolver.record_import_use(name, ns, &resolution);
413-
(Success(binding), true)
412+
Success(binding)
414413
} else {
415-
(Failed(None), false)
414+
Failed(None)
416415
}
417416
}
418417

@@ -427,8 +426,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
427426
// In this case we continue as if we resolved the import and let
428427
// check_for_conflicts_between_imports_and_items handle the conflict
429428
Some(_) => match (importing_module.def_id(), module.def_id()) {
430-
(Some(id1), Some(id2)) if id1 == id2 => (Failed(None), false),
431-
_ => (Indeterminate, false)
429+
(Some(id1), Some(id2)) if id1 == id2 => Failed(None),
430+
_ => Indeterminate
432431
},
433432
}
434433
}
@@ -460,13 +459,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
460459
};
461460

462461
// We need to resolve both namespaces for this to succeed.
463-
let (value_result, value_used_reexport) =
462+
let value_result =
464463
self.resolve_name_in_module(target_module, source, ValueNS, module_);
465-
let (type_result, type_used_reexport) =
464+
let type_result =
466465
self.resolve_name_in_module(target_module, source, TypeNS, module_);
467466

468467
match (&value_result, &type_result) {
469-
(&Success(name_binding), _) if !value_used_reexport &&
468+
(&Success(name_binding), _) if !name_binding.is_import() &&
470469
directive.is_public &&
471470
!name_binding.is_public() => {
472471
let msg = format!("`{}` is private, and cannot be reexported", source);
@@ -477,7 +476,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
477476
.emit();
478477
}
479478

480-
(_, &Success(name_binding)) if !type_used_reexport && directive.is_public => {
479+
(_, &Success(name_binding)) if !name_binding.is_import() && directive.is_public => {
481480
if !name_binding.is_public() {
482481
let msg = format!("`{}` is private, and cannot be reexported", source);
483482
let note_msg =
@@ -521,14 +520,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
521520
_ => (),
522521
}
523522

524-
let mut value_used_public = false;
525-
let mut type_used_public = false;
526-
527523
// We've successfully resolved the import. Write the results in.
528524
let mut import_resolutions = module_.import_resolutions.borrow_mut();
529525

530526
{
531-
let mut check_and_write_import = |namespace, result, used_public: &mut bool| {
527+
let mut check_and_write_import = |namespace, result| {
532528
let result: &ResolveResult<&NameBinding> = result;
533529

534530
let import_resolution = import_resolutions.get_mut(&(target, namespace)).unwrap();
@@ -557,7 +553,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
557553
import_resolution.is_public = directive.is_public;
558554

559555
self.add_export(module_, target, &import_resolution);
560-
*used_public = name_binding.is_public();
561556
}
562557
Failed(_) => {
563558
// Continue.
@@ -572,8 +567,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
572567
directive.span,
573568
(target, namespace));
574569
};
575-
check_and_write_import(ValueNS, &value_result, &mut value_used_public);
576-
check_and_write_import(TypeNS, &type_result, &mut type_used_public);
570+
check_and_write_import(ValueNS, &value_result);
571+
check_and_write_import(TypeNS, &type_result);
577572
}
578573

579574
if let (&Failed(_), &Failed(_)) = (&value_result, &type_result) {
@@ -583,9 +578,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
583578
return Failed(Some((directive.span, msg)));
584579
}
585580

586-
let value_used_public = value_used_reexport || value_used_public;
587-
let type_used_public = type_used_reexport || type_used_public;
588-
589581
let value_def_and_priv = {
590582
let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
591583
assert!(import_resolution_value.outstanding_references >= 1);
@@ -596,7 +588,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
596588
// purposes it's good enough to just favor one over the other.
597589
import_resolution_value.binding.as_ref().map(|binding| {
598590
let def = binding.def().unwrap();
599-
let last_private = if value_used_public { lp } else { DependsOn(def.def_id()) };
591+
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
600592
(def, last_private)
601593
})
602594
};
@@ -608,7 +600,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
608600

609601
import_resolution_type.binding.as_ref().map(|binding| {
610602
let def = binding.def().unwrap();
611-
let last_private = if type_used_public { lp } else { DependsOn(def.def_id()) };
603+
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
612604
(def, last_private)
613605
})
614606
};

0 commit comments

Comments
 (0)