Skip to content

Commit

Permalink
Refactor away separate tracking of used_public and used_reexport.
Browse files Browse the repository at this point in the history
NameBinding now encodes these directly with binding.is_public() and (binding.is_public() && binding.is_import()) (respectively)
  • Loading branch information
jseyfried committed Feb 8, 2016
1 parent 2e24c74 commit 4428b1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 48 deletions.
39 changes: 18 additions & 21 deletions src/librustc_resolve/lib.rs
Expand Up @@ -1311,15 +1311,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
name);
return Indeterminate;
}
Success((binding, used_proxy)) => {
Success(binding) => {
// Check to see whether there are type bindings, and, if
// so, whether there is a module within.
if let Some(module_def) = binding.module() {
search_module = module_def;

// Keep track of the closest private module used
// when resolving this import chain.
if !used_proxy && !search_module.is_public {
if !binding.is_public() {
if let Some(did) = search_module.def_id() {
closest_private = LastMod(DependsOn(did));
}
Expand Down Expand Up @@ -1410,7 +1410,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
debug!("(resolving module path for import) indeterminate; bailing");
return Indeterminate;
}
Success((binding, _)) => match binding.module() {
Success(binding) => match binding.module() {
Some(containing_module) => {
search_module = containing_module;
start_index = 1;
Expand Down Expand Up @@ -1444,7 +1444,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
name: Name,
namespace: Namespace,
record_used: bool)
-> ResolveResult<(&'a NameBinding<'a>, bool)> {
-> ResolveResult<&'a NameBinding<'a>> {
debug!("(resolving item in lexical scope) resolving `{}` in namespace {:?} in `{}`",
name,
namespace,
Expand All @@ -1466,10 +1466,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
debug!("(resolving item in lexical scope) indeterminate higher scope; bailing");
return Indeterminate;
}
Success((binding, used_reexport)) => {
Success(binding) => {
// We found the module.
debug!("(resolving item in lexical scope) found name in module, done");
return Success((binding, used_reexport));
return Success(binding);
}
}

Expand Down Expand Up @@ -1565,16 +1565,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
/// Attempts to resolve the supplied name in the given module for the
/// given namespace. If successful, returns the binding corresponding to
/// the name.
///
/// The boolean returned on success is an indicator of whether this lookup
/// passed through a public re-export proxy.
fn resolve_name_in_module(&mut self,
module_: Module<'a>,
name: Name,
namespace: Namespace,
allow_private_imports: bool,
record_used: bool)
-> ResolveResult<(&'a NameBinding<'a>, bool)> {
-> ResolveResult<&'a NameBinding<'a>> {
debug!("(resolving name in module) resolving `{}` in `{}`",
name,
module_to_string(&*module_));
Expand All @@ -1590,7 +1587,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.used_crates.insert(krate);
}
}
return Success((binding, false));
return Success(binding);
}

// Check the list of resolved imports.
Expand All @@ -1605,7 +1602,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if record_used {
self.record_import_use(name, namespace, &import_resolution);
}
return Success((binding, true));
return Success(binding);
}
}
Some(..) | None => {} // Continue.
Expand Down Expand Up @@ -2636,7 +2633,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
-> BareIdentifierPatternResolution {
let module = self.current_module;
match self.resolve_item_in_lexical_scope(module, name, ValueNS, true) {
Success((binding, _)) => {
Success(binding) => {
debug!("(resolve bare identifier pattern) succeeded in finding {} at {:?}",
name,
binding);
Expand Down Expand Up @@ -2796,7 +2793,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let module = self.current_module;
let name = identifier.unhygienic_name;
match self.resolve_item_in_lexical_scope(module, name, namespace, record_used) {
Success((binding, _)) => binding.def().map(LocalDef::from_def),
Success(binding) => binding.def().map(LocalDef::from_def),
Failed(Some((span, msg))) => {
resolve_error(self, span, ResolutionError::FailedToResolve(&*msg));
None
Expand Down Expand Up @@ -2934,7 +2931,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let name = segments.last().unwrap().identifier.name;
let result = self.resolve_name_in_module(containing_module, name, namespace, false, true);
let def = match result {
Success((binding, _)) => {
Success(binding) => {
let (def, lp) = binding.def_and_lp();
(def, last_private.or(lp))
}
Expand Down Expand Up @@ -2990,7 +2987,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {

let name = segments.last().unwrap().identifier.name;
match self.resolve_name_in_module(containing_module, name, namespace, false, true) {
Success((binding, _)) => {
Success(binding) => {
let (def, lp) = binding.def_and_lp();
Some((def, last_private.or(lp)))
}
Expand Down Expand Up @@ -3028,11 +3025,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

if let AnonymousModuleRibKind(module) = self.get_ribs(namespace)[i].kind {
if let Success((binding, _)) = self.resolve_name_in_module(module,
ident.unhygienic_name,
namespace,
true,
true) {
if let Success(binding) = self.resolve_name_in_module(module,
ident.unhygienic_name,
namespace,
true,
true) {
if let Some(def) = binding.def() {
return Some(LocalDef::from_def(def));
}
Expand Down
46 changes: 19 additions & 27 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -371,14 +371,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
}

/// Resolves the name in the namespace of the module because it is being imported by
/// importing_module. Returns the name bindings defining the name
/// and whether or not the name was imported.
/// importing_module. Returns the name bindings defining the name.
fn resolve_name_in_module(&mut self,
module: Module<'b>, // Module containing the name
name: Name,
ns: Namespace,
importing_module: Module<'b>) // Module importing the name
-> (ResolveResult<&'b NameBinding<'b>>, bool) {
-> ResolveResult<&'b NameBinding<'b>> {
build_reduced_graph::populate_module_if_necessary(self.resolver, module);
if let Some(name_binding) = module.get_child(name, ns) {
if name_binding.is_extern_crate() {
Expand All @@ -387,32 +386,32 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
self.resolver.used_crates.insert(krate);
}
}
return (Success(name_binding), false)
return Success(name_binding);
}

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

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

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

if let Some(binding) = resolution.binding {
self.resolver.record_import_use(name, ns, &resolution);
(Success(binding), true)
Success(binding)
} else {
(Failed(None), false)
Failed(None)
}
}

Expand All @@ -427,8 +426,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
// In this case we continue as if we resolved the import and let
// check_for_conflicts_between_imports_and_items handle the conflict
Some(_) => match (importing_module.def_id(), module.def_id()) {
(Some(id1), Some(id2)) if id1 == id2 => (Failed(None), false),
_ => (Indeterminate, false)
(Some(id1), Some(id2)) if id1 == id2 => Failed(None),
_ => Indeterminate
},
}
}
Expand Down Expand Up @@ -460,13 +459,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
};

// We need to resolve both namespaces for this to succeed.
let (value_result, value_used_reexport) =
let value_result =
self.resolve_name_in_module(target_module, source, ValueNS, module_);
let (type_result, type_used_reexport) =
let type_result =
self.resolve_name_in_module(target_module, source, TypeNS, module_);

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

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

let mut value_used_public = false;
let mut type_used_public = false;

// We've successfully resolved the import. Write the results in.
let mut import_resolutions = module_.import_resolutions.borrow_mut();

{
let mut check_and_write_import = |namespace, result, used_public: &mut bool| {
let mut check_and_write_import = |namespace, result| {
let result: &ResolveResult<&NameBinding> = result;

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

self.add_export(module_, target, &import_resolution);
*used_public = name_binding.is_public();
}
Failed(_) => {
// Continue.
Expand All @@ -572,8 +567,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
directive.span,
(target, namespace));
};
check_and_write_import(ValueNS, &value_result, &mut value_used_public);
check_and_write_import(TypeNS, &type_result, &mut type_used_public);
check_and_write_import(ValueNS, &value_result);
check_and_write_import(TypeNS, &type_result);
}

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

let value_used_public = value_used_reexport || value_used_public;
let type_used_public = type_used_reexport || type_used_public;

let value_def_and_priv = {
let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
assert!(import_resolution_value.outstanding_references >= 1);
Expand All @@ -596,7 +588,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
// purposes it's good enough to just favor one over the other.
import_resolution_value.binding.as_ref().map(|binding| {
let def = binding.def().unwrap();
let last_private = if value_used_public { lp } else { DependsOn(def.def_id()) };
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)
})
};
Expand All @@ -608,7 +600,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {

import_resolution_type.binding.as_ref().map(|binding| {
let def = binding.def().unwrap();
let last_private = if type_used_public { lp } else { DependsOn(def.def_id()) };
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)
})
};
Expand Down

0 comments on commit 4428b1c

Please sign in to comment.