]> git.lizzy.rs Git - rust.git/commitdiff
Write and use increment_outstanding_references_for and decrement_outstanding_referenc...
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Sun, 7 Feb 2016 22:40:23 +0000 (22:40 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Mon, 8 Feb 2016 02:25:05 +0000 (02:25 +0000)
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/lib.rs
src/librustc_resolve/resolve_imports.rs

index 0c7563f9864676dd9178e49228680da905a790e3..d311afb23976dbccb7ad9ebe8e9e27077090d38c 100644 (file)
@@ -16,7 +16,6 @@
 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};
@@ -699,15 +698,8 @@ fn build_import_directive(&mut self,
                 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
index 5a78315d79cf11203933910f2d85548d192ce108..0325bcc24c49930583bf11868fafa3919d1e1021 100644 (file)
@@ -670,6 +670,13 @@ fn and_then<U, F: FnOnce(T) -> ResolveResult<U>>(self, f: F) -> ResolveResult<U>
             Success(t) => f(t),
         }
     }
+
+    fn success(self) -> Option<T> {
+        match self {
+            Success(t) => Some(t),
+            _ => None,
+        }
+    }
 }
 
 enum FallbackSuggestion {
@@ -870,6 +877,19 @@ fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'
         }
     }
 
+    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() {
index b67de2d170dbb0b2de0f6b86598cdd50857ea752..18404a49a36908c9ec83fdb07a721bde2b7345ca 100644 (file)
@@ -511,9 +511,9 @@ fn resolve_single_import(&mut self,
         }
 
         // 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;
 
@@ -567,14 +567,12 @@ fn resolve_single_import(&mut self,
         }
 
         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)
@@ -582,11 +580,9 @@ fn resolve_single_import(&mut self,
         };
 
         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)