aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-07 19:26:33 +0000
committerGitHub <[email protected]>2019-12-07 19:26:33 +0000
commit971df306ada43f6150e12a143873f680e104a866 (patch)
tree913c24e889f3db8044b4b9f11bc3969e7eb02e34 /crates/ra_hir
parenta3eb8787452a04400784ba8fed38303232595695 (diff)
parent88c5b1282a5770097c6c768b24bedfc3a6944e08 (diff)
Merge #2494
2494: Get the right analyzer for impls r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs23
-rw-r--r--crates/ra_hir/src/code_model/src.rs10
-rw-r--r--crates/ra_hir/src/from_source.rs21
-rw-r--r--crates/ra_hir/src/lib.rs6
-rw-r--r--crates/ra_hir/src/source_binder.rs14
5 files changed, 57 insertions, 17 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 29ace8479..4578a0ba8 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -15,9 +15,9 @@ use hir_def::{
15 per_ns::PerNs, 15 per_ns::PerNs,
16 resolver::HasResolver, 16 resolver::HasResolver,
17 type_ref::{Mutability, TypeRef}, 17 type_ref::{Mutability, TypeRef},
18 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, GenericParamId, 18 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId,
19 HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, 19 LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId,
20 Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, 20 StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
21}; 21};
22use hir_expand::{ 22use hir_expand::{
23 diagnostics::DiagnosticSink, 23 diagnostics::DiagnosticSink,
@@ -856,8 +856,19 @@ impl Local {
856} 856}
857 857
858#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 858#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
859pub struct GenericParam { 859pub struct TypeParam {
860 pub(crate) id: GenericParamId, 860 pub(crate) id: TypeParamId,
861}
862
863impl TypeParam {
864 pub fn name(self, db: &impl HirDatabase) -> Name {
865 let params = db.generic_params(self.id.parent);
866 params.types[self.id.local_id].name.clone()
867 }
868
869 pub fn module(self, db: &impl HirDatabase) -> Module {
870 self.id.parent.module(db).into()
871 }
861} 872}
862 873
863#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 874#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1100,7 +1111,7 @@ impl HirDisplay for Type {
1100pub enum ScopeDef { 1111pub enum ScopeDef {
1101 ModuleDef(ModuleDef), 1112 ModuleDef(ModuleDef),
1102 MacroDef(MacroDef), 1113 MacroDef(MacroDef),
1103 GenericParam(GenericParam), 1114 GenericParam(TypeParam),
1104 ImplSelfType(ImplBlock), 1115 ImplSelfType(ImplBlock),
1105 AdtSelfType(Adt), 1116 AdtSelfType(Adt),
1106 Local(Local), 1117 Local(Local),
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 78a454082..b09582f93 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -10,7 +10,7 @@ use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, 12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
13 Module, Static, Struct, StructField, Trait, TypeAlias, Union, 13 Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
14}; 14};
15 15
16pub use hir_expand::InFile; 16pub use hir_expand::InFile;
@@ -129,3 +129,11 @@ impl HasSource for Import {
129 src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root))) 129 src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
130 } 130 }
131} 131}
132
133impl HasSource for TypeParam {
134 type Ast = Either<ast::TraitDef, ast::TypeParam>;
135 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
136 let child_source = self.id.parent.child_source(db);
137 child_source.map(|it| it[self.id.local_id].clone())
138 }
139}
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 437f800c1..68e59fc1e 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use hir_def::{ 2use hir_def::{
3 child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, 3 child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef,
4 EnumVariantId, LocationCtx, ModuleId, VariantId, 4 EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId,
5}; 5};
6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
7use ra_syntax::{ 7use ra_syntax::{
@@ -12,7 +12,7 @@ use ra_syntax::{
12use crate::{ 12use crate::{
13 db::{AstDatabase, DefDatabase, HirDatabase}, 13 db::{AstDatabase, DefDatabase, HirDatabase},
14 Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, 14 Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local,
15 MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, 15 MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
16}; 16};
17 17
18pub trait FromSource: Sized { 18pub trait FromSource: Sized {
@@ -177,6 +177,23 @@ impl Local {
177 } 177 }
178} 178}
179 179
180impl TypeParam {
181 pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
182 let file_id = src.file_id;
183 let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| {
184 let res = match_ast! {
185 match it {
186 ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.id.into() },
187 _ => return None,
188 }
189 };
190 Some(res)
191 })?;
192 let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?;
193 Some(TypeParam { id })
194 }
195}
196
180impl Module { 197impl Module {
181 pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { 198 pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
182 let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); 199 let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index f12e4ca3f..9eb34b5dc 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -42,9 +42,9 @@ pub mod from_source;
42pub use crate::{ 42pub use crate::{
43 code_model::{ 43 code_model::{
44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, 44 src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, 45 DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs,
46 HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, 46 ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
47 StructField, Trait, Type, TypeAlias, Union, VariantDef, 47 StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
48 }, 48 },
49 from_source::FromSource, 49 from_source::FromSource,
50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 50 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 8c4b635d2..b80aaeb90 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -36,8 +36,8 @@ use crate::{
36 method_resolution::{self, implements_trait}, 36 method_resolution::{self, implements_trait},
37 InEnvironment, TraitEnvironment, Ty, 37 InEnvironment, TraitEnvironment, Ty,
38 }, 38 },
39 Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam, 39 Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, Local,
40 Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, 40 MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
41}; 41};
42 42
43fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { 43fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
@@ -59,6 +59,10 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -
59 let src = node.with_value(it); 59 let src = node.with_value(it);
60 Some(Enum::from_source(db, src)?.id.resolver(db)) 60 Some(Enum::from_source(db, src)?.id.resolver(db))
61 }, 61 },
62 ast::ImplBlock(it) => {
63 let src = node.with_value(it);
64 Some(ImplBlock::from_source(db, src)?.id.resolver(db))
65 },
62 _ => match node.value.kind() { 66 _ => match node.value.kind() {
63 FN_DEF | CONST_DEF | STATIC_DEF => { 67 FN_DEF | CONST_DEF | STATIC_DEF => {
64 let def = def_with_body_from_child_node(db, node)?; 68 let def = def_with_body_from_child_node(db, node)?;
@@ -108,7 +112,7 @@ pub enum PathResolution {
108 /// A local binding (only value namespace) 112 /// A local binding (only value namespace)
109 Local(Local), 113 Local(Local),
110 /// A generic parameter 114 /// A generic parameter
111 GenericParam(GenericParam), 115 TypeParam(TypeParam),
112 SelfType(crate::ImplBlock), 116 SelfType(crate::ImplBlock),
113 Macro(MacroDef), 117 Macro(MacroDef),
114 AssocItem(crate::AssocItem), 118 AssocItem(crate::AssocItem),
@@ -262,7 +266,7 @@ impl SourceAnalyzer {
262 ) -> Option<PathResolution> { 266 ) -> Option<PathResolution> {
263 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { 267 let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
264 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 268 TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
265 TypeNs::GenericParam(id) => PathResolution::GenericParam(GenericParam { id }), 269 TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
266 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { 270 TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
267 PathResolution::Def(Adt::from(it).into()) 271 PathResolution::Def(Adt::from(it).into())
268 } 272 }
@@ -334,7 +338,7 @@ impl SourceAnalyzer {
334 resolver::ScopeDef::PerNs(it) => it.into(), 338 resolver::ScopeDef::PerNs(it) => it.into(),
335 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), 339 resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
336 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), 340 resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
337 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(GenericParam { id }), 341 resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }),
338 resolver::ScopeDef::Local(pat_id) => { 342 resolver::ScopeDef::Local(pat_id) => {
339 let parent = self.resolver.body_owner().unwrap().into(); 343 let parent = self.resolver.body_owner().unwrap().into();
340 ScopeDef::Local(Local { parent, pat_id }) 344 ScopeDef::Local(Local { parent, pat_id })