]> git.lizzy.rs Git - rust.git/commitdiff
Refactor PerNs construction
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 22 Dec 2019 14:08:57 +0000 (15:08 +0100)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sun, 22 Dec 2019 14:10:19 +0000 (15:10 +0100)
crates/ra_hir_def/src/item_scope.rs
crates/ra_hir_def/src/nameres/collector.rs

index eab3e2fff4b1475c9de16a5ab042ddb546b95dfe..a96b5cfd2614176325ac04dd4223bb18a8dec0b6 100644 (file)
@@ -5,7 +5,7 @@
 use once_cell::sync::Lazy;
 use rustc_hash::FxHashMap;
 
-use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
+use crate::{per_ns::PerNs, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId};
 
 #[derive(Debug, Default, PartialEq, Eq)]
 pub struct ItemScope {
@@ -153,3 +153,21 @@ pub struct Resolution {
     pub def: PerNs,
     pub(crate) import: bool,
 }
+
+impl From<ModuleDefId> for PerNs {
+    fn from(def: ModuleDefId) -> PerNs {
+        match def {
+            ModuleDefId::ModuleId(_) => PerNs::types(def),
+            ModuleDefId::FunctionId(_) => PerNs::values(def),
+            ModuleDefId::AdtId(adt) => match adt {
+                AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def),
+                AdtId::EnumId(_) => PerNs::types(def),
+            },
+            ModuleDefId::EnumVariantId(_) => PerNs::both(def, def),
+            ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def),
+            ModuleDefId::TraitId(_) => PerNs::types(def),
+            ModuleDefId::TypeAliasId(_) => PerNs::types(def),
+            ModuleDefId::BuiltinType(_) => PerNs::types(def),
+        }
+    }
+}
index 2b194f4881b594968cac11662eb920939d1b2437..b4e438257aea66454273c1267b8d8ecc366d9c22 100644 (file)
@@ -714,12 +714,9 @@ fn push_child_module(
             modules[res].scope.define_legacy_macro(name, mac)
         }
         modules[self.module_id].children.insert(name.clone(), res);
-        let resolution = Resolution {
-            def: PerNs::types(
-                ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(),
-            ),
-            import: false,
-        };
+        let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res };
+        let def: ModuleDefId = module.into();
+        let resolution = Resolution { def: def.into(), import: false };
         self.def_collector.update(self.module_id, None, &[(name, resolution)]);
         res
     }
@@ -734,63 +731,51 @@ fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
 
         let name = def.name.clone();
         let container = ContainerId::ModuleId(module);
-        let def: PerNs = match def.kind {
-            raw::DefKind::Function(ast_id) => {
-                let def = FunctionLoc {
-                    container: container.into(),
-                    ast_id: AstId::new(self.file_id, ast_id),
-                }
-                .intern(self.def_collector.db);
-
-                PerNs::values(def.into())
+        let def: ModuleDefId = match def.kind {
+            raw::DefKind::Function(ast_id) => FunctionLoc {
+                container: container.into(),
+                ast_id: AstId::new(self.file_id, ast_id),
             }
+            .intern(self.def_collector.db)
+            .into(),
             raw::DefKind::Struct(ast_id) => {
-                let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
-                    .intern(self.def_collector.db);
-                PerNs::both(def.into(), def.into())
+                StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
             raw::DefKind::Union(ast_id) => {
-                let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
-                    .intern(self.def_collector.db);
-                PerNs::both(def.into(), def.into())
+                UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
             raw::DefKind::Enum(ast_id) => {
-                let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
-                    .intern(self.def_collector.db);
-                PerNs::types(def.into())
+                EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
             raw::DefKind::Const(ast_id) => {
-                let def = ConstLoc {
-                    container: container.into(),
-                    ast_id: AstId::new(self.file_id, ast_id),
-                }
-                .intern(self.def_collector.db);
-
-                PerNs::values(def.into())
+                ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
             raw::DefKind::Static(ast_id) => {
-                let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
-                    .intern(self.def_collector.db);
-
-                PerNs::values(def.into())
+                StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
             raw::DefKind::Trait(ast_id) => {
-                let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
-                    .intern(self.def_collector.db);
-
-                PerNs::types(def.into())
+                TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
+                    .intern(self.def_collector.db)
+                    .into()
             }
-            raw::DefKind::TypeAlias(ast_id) => {
-                let def = TypeAliasLoc {
-                    container: container.into(),
-                    ast_id: AstId::new(self.file_id, ast_id),
-                }
-                .intern(self.def_collector.db);
-
-                PerNs::types(def.into())
+            raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc {
+                container: container.into(),
+                ast_id: AstId::new(self.file_id, ast_id),
             }
+            .intern(self.def_collector.db)
+            .into(),
         };
-        let resolution = Resolution { def, import: false };
+        let resolution = Resolution { def: def.into(), import: false };
         self.def_collector.update(self.module_id, None, &[(name, resolution)])
     }