use DefModifiers;
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
-use resolve_imports::NameResolution;
use Module;
use Namespace::{self, TypeNS, ValueNS};
use {NameBinding, NameBindingKind};
debug!("(building import directive) building import directive: {}::{}",
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
target);
-
- let mut import_resolutions = module_.import_resolutions.borrow_mut();
- for &ns in [TypeNS, ValueNS].iter() {
- let mut resolution = import_resolutions.entry((target, ns)).or_insert(
- NameResolution::default()
- );
-
- resolution.outstanding_references += 1;
- }
+ module_.increment_outstanding_references_for(target, ValueNS);
+ module_.increment_outstanding_references_for(target, TypeNS);
}
GlobImport => {
// Set the glob flag. This tells us that we don't know the
Success(t) => f(t),
}
}
+
+ fn success(self) -> Option<T> {
+ match self {
+ Success(t) => Some(t),
+ _ => None,
+ }
+ }
}
enum FallbackSuggestion {
}
}
+ fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
+ let mut resolutions = self.import_resolutions.borrow_mut();
+ resolutions.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
+ }
+
+ fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace) {
+ match self.import_resolutions.borrow_mut().get_mut(&(name, ns)).unwrap()
+ .outstanding_references {
+ 0 => panic!("No more outstanding references!"),
+ ref mut outstanding_references => { *outstanding_references -= 1; }
+ }
+ }
+
fn for_each_local_child<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&self, mut f: F) {
for (&(name, ns), name_binding) in self.children.borrow().iter() {
if !name_binding.is_extern_crate() {
}
// We've successfully resolved the import. Write the results in.
- let mut import_resolutions = module_.import_resolutions.borrow_mut();
{
+ let mut import_resolutions = module_.import_resolutions.borrow_mut();
let mut check_and_write_import = |namespace, result| {
let result: &ResolveResult<&NameBinding> = result;
}
let value_def_and_priv = {
- let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
- assert!(import_resolution_value.outstanding_references >= 1);
- import_resolution_value.outstanding_references -= 1;
+ module_.decrement_outstanding_references_for(target, ValueNS);
// Record what this import resolves to for later uses in documentation,
// this may resolve to either a value or a type, but for documentation
// purposes it's good enough to just favor one over the other.
- import_resolution_value.binding.as_ref().map(|binding| {
+ value_result.success().map(|binding| {
let def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)
};
let type_def_and_priv = {
- let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap();
- assert!(import_resolution_type.outstanding_references >= 1);
- import_resolution_type.outstanding_references -= 1;
+ module_.decrement_outstanding_references_for(target, TypeNS);
- import_resolution_type.binding.as_ref().map(|binding| {
+ type_result.success().map(|binding| {
let def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)