]> git.lizzy.rs Git - rust.git/commitdiff
Move some code to scope
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 20 Dec 2019 15:26:49 +0000 (16:26 +0100)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 20 Dec 2019 15:52:02 +0000 (16:52 +0100)
crates/ra_hir_def/src/item_scope.rs
crates/ra_hir_def/src/nameres/collector.rs

index 93e579bb0c2e761bb77c5db4c6c629f921cb1024..f4fb768cd3a6803c7c6706d8e21aa50a76511d37 100644 (file)
@@ -47,6 +47,41 @@ pub(crate) enum BuiltinShadowMode {
 /// Legacy macros can only be accessed through special methods like `get_legacy_macros`.
 /// Other methods will only resolve values, types and module scoped macros only.
 impl ItemScope {
+    pub fn push_res(
+        &mut self,
+        name: Name,
+        res: &Resolution,
+        import: Option<LocalImportId>,
+    ) -> bool {
+        let mut changed = false;
+        let existing = self.items.entry(name.clone()).or_default();
+
+        if existing.def.types.is_none() && res.def.types.is_some() {
+            existing.def.types = res.def.types;
+            existing.import = import.or(res.import);
+            changed = true;
+        }
+        if existing.def.values.is_none() && res.def.values.is_some() {
+            existing.def.values = res.def.values;
+            existing.import = import.or(res.import);
+            changed = true;
+        }
+        if existing.def.macros.is_none() && res.def.macros.is_some() {
+            existing.def.macros = res.def.macros;
+            existing.import = import.or(res.import);
+            changed = true;
+        }
+
+        if existing.def.is_none()
+            && res.def.is_none()
+            && existing.import.is_none()
+            && res.import.is_some()
+        {
+            existing.import = res.import;
+        }
+        changed
+    }
+
     pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
         //FIXME: shadowing
         self.items.iter().chain(BUILTIN_SCOPE.iter())
index 17c7e691e28866832bd598cbec228e85a7042d1b..95c499ec9fcd86f0a3578213274f9cbb94f7835e 100644 (file)
@@ -467,34 +467,10 @@ fn update_recursive(
             // prevent stack overflows (but this shouldn't be possible)
             panic!("infinite recursion in glob imports!");
         }
-        let module_items = &mut self.def_map.modules[module_id].scope;
+        let scope = &mut self.def_map.modules[module_id].scope;
         let mut changed = false;
         for (name, res) in resolutions {
-            let existing = module_items.items.entry(name.clone()).or_default();
-
-            if existing.def.types.is_none() && res.def.types.is_some() {
-                existing.def.types = res.def.types;
-                existing.import = import.or(res.import);
-                changed = true;
-            }
-            if existing.def.values.is_none() && res.def.values.is_some() {
-                existing.def.values = res.def.values;
-                existing.import = import.or(res.import);
-                changed = true;
-            }
-            if existing.def.macros.is_none() && res.def.macros.is_some() {
-                existing.def.macros = res.def.macros;
-                existing.import = import.or(res.import);
-                changed = true;
-            }
-
-            if existing.def.is_none()
-                && res.def.is_none()
-                && existing.import.is_none()
-                && res.import.is_some()
-            {
-                existing.import = res.import;
-            }
+            changed |= scope.push_res(name.clone(), res, import);
         }
 
         if !changed {