]> git.lizzy.rs Git - rust.git/commitdiff
Introduce `ContainerId`
authorAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 20 Dec 2019 11:07:23 +0000 (12:07 +0100)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Fri, 20 Dec 2019 11:07:23 +0000 (12:07 +0100)
crates/ra_hir_def/src/body/lower.rs
crates/ra_hir_def/src/lib.rs
crates/ra_hir_def/src/nameres/collector.rs
crates/ra_hir_def/src/resolver.rs
crates/ra_hir_ty/src/infer/path.rs
crates/ra_hir_ty/src/method_resolution.rs
crates/ra_hir_ty/src/utils.rs

index afd5231cc1903abea676b3cb05b93db4fc7fa2a9..0103a1aabe3e1aa7b28a48547acc51fa4b727e10 100644 (file)
@@ -25,7 +25,7 @@
     path::GenericArgs,
     path::Path,
     type_ref::{Mutability, TypeRef},
-    AssocContainerId, DefWithBodyId, FunctionLoc, Intern,
+    ContainerId, DefWithBodyId, FunctionLoc, Intern,
 };
 
 pub(super) fn lower(
@@ -490,7 +490,7 @@ fn collect_block(&mut self, expr: ast::BlockExpr) -> ExprId {
     }
 
     fn collect_block_items(&mut self, block: &ast::Block) {
-        let container = AssocContainerId::DefWithBodyId(self.def);
+        let container = ContainerId::DefWithBodyId(self.def).into();
         for item in block.items() {
             match item {
                 ast::ModuleItem::FnDef(def) => {
index 3d42762aeff350b38038594ed706873dabb6ec89..5e46db1aabd3facd556c3f6412f2c4a6dbb83e1a 100644 (file)
@@ -331,12 +331,18 @@ pub struct TypeParamId {
 impl_arena_id!(LocalTypeParamId);
 
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-pub enum AssocContainerId {
+pub enum ContainerId {
     ModuleId(ModuleId),
+    DefWithBodyId(DefWithBodyId),
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub enum AssocContainerId {
+    ContainerId(ContainerId),
     ImplId(ImplId),
     TraitId(TraitId),
-    DefWithBodyId(DefWithBodyId),
 }
+impl_froms!(AssocContainerId: ContainerId);
 
 /// A Data Type
 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -479,13 +485,21 @@ pub trait HasModule {
     fn module(&self, db: &impl db::DefDatabase) -> ModuleId;
 }
 
+impl HasModule for ContainerId {
+    fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
+        match *self {
+            ContainerId::ModuleId(it) => it,
+            ContainerId::DefWithBodyId(it) => it.module(db),
+        }
+    }
+}
+
 impl HasModule for AssocContainerId {
     fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
         match *self {
-            AssocContainerId::ModuleId(it) => it,
+            AssocContainerId::ContainerId(it) => it.module(db),
             AssocContainerId::ImplId(it) => it.lookup(db).container,
             AssocContainerId::TraitId(it) => it.lookup(db).container,
-            AssocContainerId::DefWithBodyId(it) => it.module(db),
         }
     }
 }
index 848959f7c4a6b98ebfa79ecb4c968ca3e9f58244..0f3319f30208733e55f8a106a5d83d2bb8620bbb 100644 (file)
@@ -24,7 +24,7 @@
     },
     path::{ModPath, PathKind},
     per_ns::PerNs,
-    AdtId, AssocContainerId, AstId, ConstLoc, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
+    AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
     LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc,
     TypeAliasLoc, UnionLoc,
 };
@@ -760,10 +760,11 @@ fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
         self.collect_derives(attrs, def);
 
         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: AssocContainerId::ModuleId(module),
+                    container: container.into(),
                     ast_id: AstId::new(self.file_id, ast_id),
                 }
                 .intern(self.def_collector.db);
@@ -787,7 +788,7 @@ fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
             }
             raw::DefKind::Const(ast_id) => {
                 let def = ConstLoc {
-                    container: AssocContainerId::ModuleId(module),
+                    container: container.into(),
                     ast_id: AstId::new(self.file_id, ast_id),
                 }
                 .intern(self.def_collector.db);
@@ -808,7 +809,7 @@ fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
             }
             raw::DefKind::TypeAlias(ast_id) => {
                 let def = TypeAliasLoc {
-                    container: AssocContainerId::ModuleId(module),
+                    container: container.into(),
                     ast_id: AstId::new(self.file_id, ast_id),
                 }
                 .intern(self.def_collector.db);
index d79c9813b7dc728b9077ea530262373fb22ea01c..af9d194f8a5c9ec42249a9e042616f1450a97f3d 100644 (file)
@@ -17,9 +17,9 @@
     nameres::{BuiltinShadowMode, CrateDefMap},
     path::{ModPath, PathKind},
     per_ns::PerNs,
-    AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
-    GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
-    StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
+    AdtId, AssocContainerId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId,
+    FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
+    StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
 };
 
 #[derive(Debug, Clone, Default)]
@@ -580,13 +580,21 @@ fn resolver(self, db: &impl DefDatabase) -> Resolver {
     }
 }
 
+impl HasResolver for ContainerId {
+    fn resolver(self, db: &impl DefDatabase) -> Resolver {
+        match self {
+            ContainerId::ModuleId(it) => it.resolver(db),
+            ContainerId::DefWithBodyId(it) => it.resolver(db),
+        }
+    }
+}
+
 impl HasResolver for AssocContainerId {
     fn resolver(self, db: &impl DefDatabase) -> Resolver {
         match self {
+            AssocContainerId::ContainerId(it) => it.resolver(db),
             AssocContainerId::TraitId(it) => it.resolver(db),
             AssocContainerId::ImplId(it) => it.resolver(db),
-            AssocContainerId::ModuleId(it) => it.resolver(db),
-            AssocContainerId::DefWithBodyId(it) => it.resolver(db),
         }
     }
 }
index a96ab75d1f49e98a2e197c6b0b20e7b80916ab31..ffd3583675f842f2f59241d1a00cc36a2f57e784 100644 (file)
@@ -237,7 +237,7 @@ fn resolve_ty_assoc_item(
                         }));
                         Some(substs)
                     }
-                    AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => None,
+                    AssocContainerId::ContainerId(_) => None,
                 };
 
                 self.write_assoc_resolution(id, item.into());
index 1c2e7b934db60348babc2fb594211bd6aad01a1c..1b2f4014c62c640bffceeeda2e565336d2db7d1c 100644 (file)
@@ -456,7 +456,7 @@ fn transform_receiver_ty(
             .fill_with_unknown()
             .build(),
         AssocContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?,
-        AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => unreachable!(),
+        AssocContainerId::ContainerId(_) => unreachable!(),
     };
     let sig = db.callable_item_signature(function_id.into());
     Some(sig.params()[0].clone().subst(&substs))
index 8b5b611ecc93bb88e2bf83de0567099c2fb79d1d..0b1806a84fd40faa15e896910d2b60c5443e22b3 100644 (file)
@@ -157,6 +157,6 @@ fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<Generi
     match container {
         AssocContainerId::ImplId(it) => Some(it.into()),
         AssocContainerId::TraitId(it) => Some(it.into()),
-        AssocContainerId::ModuleId(_) | AssocContainerId::DefWithBodyId(_) => None,
+        AssocContainerId::ContainerId(_) => None,
     }
 }