diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-07 19:26:33 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-07 19:26:33 +0000 |
commit | 971df306ada43f6150e12a143873f680e104a866 (patch) | |
tree | 913c24e889f3db8044b4b9f11bc3969e7eb02e34 /crates | |
parent | a3eb8787452a04400784ba8fed38303232595695 (diff) | |
parent | 88c5b1282a5770097c6c768b24bedfc3a6944e08 (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')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_def/src/generics.rs | 128 | ||||
-rw-r--r-- | crates/ra_hir_def/src/keys.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 18 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 26 | ||||
-rw-r--r-- | crates/ra_ide/src/display/navigation_target.rs | 22 | ||||
-rw-r--r-- | crates/ra_ide/src/goto_definition.rs | 25 | ||||
-rw-r--r-- | crates/ra_ide/src/hover.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/references.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/references/classify.rs | 14 | ||||
-rw-r--r-- | crates/ra_ide/src/references/name_definition.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide/src/snapshots/highlighting.html | 9 | ||||
-rw-r--r-- | crates/ra_ide/src/snapshots/rainbow_highlighting.html | 1 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 12 |
19 files changed, 272 insertions, 92 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 | }; |
22 | use hir_expand::{ | 22 | use 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)] |
859 | pub struct GenericParam { | 859 | pub struct TypeParam { |
860 | pub(crate) id: GenericParamId, | 860 | pub(crate) id: TypeParamId, |
861 | } | ||
862 | |||
863 | impl 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 { | |||
1100 | pub enum ScopeDef { | 1111 | pub 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 | ||
11 | use crate::{ | 11 | use 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 | ||
16 | pub use hir_expand::InFile; | 16 | pub 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 | |||
133 | impl 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 |
2 | use hir_def::{ | 2 | use 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 | }; |
6 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; | 6 | use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; |
7 | use ra_syntax::{ | 7 | use ra_syntax::{ |
@@ -12,7 +12,7 @@ use ra_syntax::{ | |||
12 | use crate::{ | 12 | use 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 | ||
18 | pub trait FromSource: Sized { | 18 | pub trait FromSource: Sized { |
@@ -177,6 +177,23 @@ impl Local { | |||
177 | } | 177 | } |
178 | } | 178 | } |
179 | 179 | ||
180 | impl 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 | |||
180 | impl Module { | 197 | impl 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; | |||
42 | pub use crate::{ | 42 | pub 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 | ||
43 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { | 43 | fn 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 }) |
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 94ce83564..976cf72d0 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs | |||
@@ -4,20 +4,29 @@ | |||
4 | //! in rustc. | 4 | //! in rustc. |
5 | use std::sync::Arc; | 5 | use std::sync::Arc; |
6 | 6 | ||
7 | use hir_expand::name::{self, AsName, Name}; | 7 | use either::Either; |
8 | use ra_arena::Arena; | 8 | use hir_expand::{ |
9 | name::{self, AsName, Name}, | ||
10 | InFile, | ||
11 | }; | ||
12 | use ra_arena::{map::ArenaMap, Arena}; | ||
13 | use ra_db::FileId; | ||
9 | use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; | 14 | use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; |
10 | 15 | ||
11 | use crate::{ | 16 | use crate::{ |
17 | child_by_source::ChildBySource, | ||
12 | db::DefDatabase, | 18 | db::DefDatabase, |
19 | dyn_map::DynMap, | ||
20 | keys, | ||
21 | src::HasChildSource, | ||
13 | src::HasSource, | 22 | src::HasSource, |
14 | type_ref::{TypeBound, TypeRef}, | 23 | type_ref::{TypeBound, TypeRef}, |
15 | AdtId, AstItemDef, GenericDefId, LocalGenericParamId, Lookup, | 24 | AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, |
16 | }; | 25 | }; |
17 | 26 | ||
18 | /// Data about a generic parameter (to a function, struct, impl, ...). | 27 | /// Data about a generic parameter (to a function, struct, impl, ...). |
19 | #[derive(Clone, PartialEq, Eq, Debug)] | 28 | #[derive(Clone, PartialEq, Eq, Debug)] |
20 | pub struct GenericParamData { | 29 | pub struct TypeParamData { |
21 | pub name: Name, | 30 | pub name: Name, |
22 | pub default: Option<TypeRef>, | 31 | pub default: Option<TypeRef>, |
23 | } | 32 | } |
@@ -25,7 +34,8 @@ pub struct GenericParamData { | |||
25 | /// Data about the generic parameters of a function, struct, impl, etc. | 34 | /// Data about the generic parameters of a function, struct, impl, etc. |
26 | #[derive(Clone, PartialEq, Eq, Debug)] | 35 | #[derive(Clone, PartialEq, Eq, Debug)] |
27 | pub struct GenericParams { | 36 | pub struct GenericParams { |
28 | pub params: Arena<LocalGenericParamId, GenericParamData>, | 37 | pub types: Arena<LocalTypeParamId, TypeParamData>, |
38 | // lifetimes: Arena<LocalLifetimeParamId, LifetimeParamData>, | ||
29 | pub where_predicates: Vec<WherePredicate>, | 39 | pub where_predicates: Vec<WherePredicate>, |
30 | } | 40 | } |
31 | 41 | ||
@@ -39,52 +49,87 @@ pub struct WherePredicate { | |||
39 | pub bound: TypeBound, | 49 | pub bound: TypeBound, |
40 | } | 50 | } |
41 | 51 | ||
52 | type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>; | ||
53 | |||
42 | impl GenericParams { | 54 | impl GenericParams { |
43 | pub(crate) fn generic_params_query( | 55 | pub(crate) fn generic_params_query( |
44 | db: &impl DefDatabase, | 56 | db: &impl DefDatabase, |
45 | def: GenericDefId, | 57 | def: GenericDefId, |
46 | ) -> Arc<GenericParams> { | 58 | ) -> Arc<GenericParams> { |
47 | Arc::new(GenericParams::new(db, def.into())) | 59 | let (params, _source_map) = GenericParams::new(db, def.into()); |
60 | Arc::new(params) | ||
48 | } | 61 | } |
49 | 62 | ||
50 | fn new(db: &impl DefDatabase, def: GenericDefId) -> GenericParams { | 63 | fn new(db: &impl DefDatabase, def: GenericDefId) -> (GenericParams, InFile<SourceMap>) { |
51 | let mut generics = GenericParams { params: Arena::default(), where_predicates: Vec::new() }; | 64 | let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() }; |
65 | let mut sm = ArenaMap::default(); | ||
52 | // FIXME: add `: Sized` bound for everything except for `Self` in traits | 66 | // FIXME: add `: Sized` bound for everything except for `Self` in traits |
53 | match def { | 67 | let file_id = match def { |
54 | GenericDefId::FunctionId(it) => generics.fill(&it.lookup(db).source(db).value), | 68 | GenericDefId::FunctionId(it) => { |
55 | GenericDefId::AdtId(AdtId::StructId(it)) => generics.fill(&it.source(db).value), | 69 | let src = it.lookup(db).source(db); |
56 | GenericDefId::AdtId(AdtId::UnionId(it)) => generics.fill(&it.source(db).value), | 70 | generics.fill(&mut sm, &src.value); |
57 | GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value), | 71 | src.file_id |
72 | } | ||
73 | GenericDefId::AdtId(AdtId::StructId(it)) => { | ||
74 | let src = it.source(db); | ||
75 | generics.fill(&mut sm, &src.value); | ||
76 | src.file_id | ||
77 | } | ||
78 | GenericDefId::AdtId(AdtId::UnionId(it)) => { | ||
79 | let src = it.source(db); | ||
80 | generics.fill(&mut sm, &src.value); | ||
81 | src.file_id | ||
82 | } | ||
83 | GenericDefId::AdtId(AdtId::EnumId(it)) => { | ||
84 | let src = it.source(db); | ||
85 | generics.fill(&mut sm, &src.value); | ||
86 | src.file_id | ||
87 | } | ||
58 | GenericDefId::TraitId(it) => { | 88 | GenericDefId::TraitId(it) => { |
89 | let src = it.source(db); | ||
90 | |||
59 | // traits get the Self type as an implicit first type parameter | 91 | // traits get the Self type as an implicit first type parameter |
60 | generics.params.alloc(GenericParamData { name: name::SELF_TYPE, default: None }); | 92 | let self_param_id = |
61 | generics.fill(&it.source(db).value); | 93 | generics.types.alloc(TypeParamData { name: name::SELF_TYPE, default: None }); |
94 | sm.insert(self_param_id, Either::Left(src.value.clone())); | ||
62 | // add super traits as bounds on Self | 95 | // add super traits as bounds on Self |
63 | // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar | 96 | // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar |
64 | let self_param = TypeRef::Path(name::SELF_TYPE.into()); | 97 | let self_param = TypeRef::Path(name::SELF_TYPE.into()); |
65 | generics.fill_bounds(&it.source(db).value, self_param); | 98 | generics.fill_bounds(&src.value, self_param); |
99 | |||
100 | generics.fill(&mut sm, &src.value); | ||
101 | src.file_id | ||
102 | } | ||
103 | GenericDefId::TypeAliasId(it) => { | ||
104 | let src = it.lookup(db).source(db); | ||
105 | generics.fill(&mut sm, &src.value); | ||
106 | src.file_id | ||
66 | } | 107 | } |
67 | GenericDefId::TypeAliasId(it) => generics.fill(&it.lookup(db).source(db).value), | ||
68 | // Note that we don't add `Self` here: in `impl`s, `Self` is not a | 108 | // Note that we don't add `Self` here: in `impl`s, `Self` is not a |
69 | // type-parameter, but rather is a type-alias for impl's target | 109 | // type-parameter, but rather is a type-alias for impl's target |
70 | // type, so this is handled by the resolver. | 110 | // type, so this is handled by the resolver. |
71 | GenericDefId::ImplId(it) => generics.fill(&it.source(db).value), | 111 | GenericDefId::ImplId(it) => { |
72 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {} | 112 | let src = it.source(db); |
73 | } | 113 | generics.fill(&mut sm, &src.value); |
114 | src.file_id | ||
115 | } | ||
116 | // We won't be using this ID anyway | ||
117 | GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => FileId(!0).into(), | ||
118 | }; | ||
74 | 119 | ||
75 | generics | 120 | (generics, InFile::new(file_id, sm)) |
76 | } | 121 | } |
77 | 122 | ||
78 | fn fill(&mut self, node: &impl TypeParamsOwner) { | 123 | fn fill(&mut self, sm: &mut SourceMap, node: &dyn TypeParamsOwner) { |
79 | if let Some(params) = node.type_param_list() { | 124 | if let Some(params) = node.type_param_list() { |
80 | self.fill_params(params) | 125 | self.fill_params(sm, params) |
81 | } | 126 | } |
82 | if let Some(where_clause) = node.where_clause() { | 127 | if let Some(where_clause) = node.where_clause() { |
83 | self.fill_where_predicates(where_clause); | 128 | self.fill_where_predicates(where_clause); |
84 | } | 129 | } |
85 | } | 130 | } |
86 | 131 | ||
87 | fn fill_bounds(&mut self, node: &impl ast::TypeBoundsOwner, type_ref: TypeRef) { | 132 | fn fill_bounds(&mut self, node: &dyn ast::TypeBoundsOwner, type_ref: TypeRef) { |
88 | for bound in | 133 | for bound in |
89 | node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) | 134 | node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) |
90 | { | 135 | { |
@@ -92,13 +137,14 @@ impl GenericParams { | |||
92 | } | 137 | } |
93 | } | 138 | } |
94 | 139 | ||
95 | fn fill_params(&mut self, params: ast::TypeParamList) { | 140 | fn fill_params(&mut self, sm: &mut SourceMap, params: ast::TypeParamList) { |
96 | for type_param in params.type_params() { | 141 | for type_param in params.type_params() { |
97 | let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); | 142 | let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); |
98 | // FIXME: Use `Path::from_src` | 143 | // FIXME: Use `Path::from_src` |
99 | let default = type_param.default_type().map(TypeRef::from_ast); | 144 | let default = type_param.default_type().map(TypeRef::from_ast); |
100 | let param = GenericParamData { name: name.clone(), default }; | 145 | let param = TypeParamData { name: name.clone(), default }; |
101 | self.params.alloc(param); | 146 | let param_id = self.types.alloc(param); |
147 | sm.insert(param_id, Either::Right(type_param.clone())); | ||
102 | 148 | ||
103 | let type_ref = TypeRef::Path(name.into()); | 149 | let type_ref = TypeRef::Path(name.into()); |
104 | self.fill_bounds(&type_param, type_ref); | 150 | self.fill_bounds(&type_param, type_ref); |
@@ -127,7 +173,31 @@ impl GenericParams { | |||
127 | self.where_predicates.push(WherePredicate { type_ref, bound }); | 173 | self.where_predicates.push(WherePredicate { type_ref, bound }); |
128 | } | 174 | } |
129 | 175 | ||
130 | pub fn find_by_name(&self, name: &Name) -> Option<LocalGenericParamId> { | 176 | pub fn find_by_name(&self, name: &Name) -> Option<LocalTypeParamId> { |
131 | self.params.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) | 177 | self.types.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) |
178 | } | ||
179 | } | ||
180 | |||
181 | impl HasChildSource for GenericDefId { | ||
182 | type ChildId = LocalTypeParamId; | ||
183 | type Value = Either<ast::TraitDef, ast::TypeParam>; | ||
184 | fn child_source(&self, db: &impl DefDatabase) -> InFile<SourceMap> { | ||
185 | let (_, sm) = GenericParams::new(db, *self); | ||
186 | sm | ||
187 | } | ||
188 | } | ||
189 | |||
190 | impl ChildBySource for GenericDefId { | ||
191 | fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { | ||
192 | let mut res = DynMap::default(); | ||
193 | let arena_map = self.child_source(db); | ||
194 | let arena_map = arena_map.as_ref(); | ||
195 | for (local_id, src) in arena_map.value.iter() { | ||
196 | let id = TypeParamId { parent: *self, local_id }; | ||
197 | if let Either::Right(type_param) = src { | ||
198 | res[keys::TYPE_PARAM].insert(arena_map.with_value(type_param.clone()), id) | ||
199 | } | ||
200 | } | ||
201 | res | ||
132 | } | 202 | } |
133 | } | 203 | } |
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index 447b7e3ba..be702a4f8 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs | |||
@@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; | |||
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | dyn_map::{DynMap, Policy}, | 10 | dyn_map::{DynMap, Policy}, |
11 | ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, | 11 | ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; | 14 | type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; |
@@ -20,6 +20,7 @@ pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new(); | |||
20 | pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); | 20 | pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); |
21 | pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); | 21 | pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); |
22 | pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); | 22 | pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); |
23 | pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new(); | ||
23 | 24 | ||
24 | /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are | 25 | /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are |
25 | /// equal if they point to exactly the same object. | 26 | /// equal if they point to exactly the same object. |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index b8dfc0ab1..569da4f28 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -318,14 +318,14 @@ macro_rules! impl_froms { | |||
318 | } | 318 | } |
319 | 319 | ||
320 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 320 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
321 | pub struct GenericParamId { | 321 | pub struct TypeParamId { |
322 | pub parent: GenericDefId, | 322 | pub parent: GenericDefId, |
323 | pub local_id: LocalGenericParamId, | 323 | pub local_id: LocalTypeParamId, |
324 | } | 324 | } |
325 | 325 | ||
326 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 326 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
327 | pub struct LocalGenericParamId(RawId); | 327 | pub struct LocalTypeParamId(RawId); |
328 | impl_arena_id!(LocalGenericParamId); | 328 | impl_arena_id!(LocalTypeParamId); |
329 | 329 | ||
330 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 330 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
331 | pub enum ContainerId { | 331 | pub enum ContainerId { |
@@ -525,6 +525,20 @@ impl HasModule for DefWithBodyId { | |||
525 | } | 525 | } |
526 | } | 526 | } |
527 | 527 | ||
528 | impl HasModule for GenericDefId { | ||
529 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | ||
530 | match self { | ||
531 | GenericDefId::FunctionId(it) => it.lookup(db).module(db), | ||
532 | GenericDefId::AdtId(it) => it.module(db), | ||
533 | GenericDefId::TraitId(it) => it.module(db), | ||
534 | GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), | ||
535 | GenericDefId::ImplId(it) => it.module(db), | ||
536 | GenericDefId::EnumVariantId(it) => it.parent.module(db), | ||
537 | GenericDefId::ConstId(it) => it.lookup(db).module(db), | ||
538 | } | ||
539 | } | ||
540 | } | ||
541 | |||
528 | impl HasModule for StaticLoc { | 542 | impl HasModule for StaticLoc { |
529 | fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { | 543 | fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { |
530 | self.container | 544 | self.container |
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index e00bd03d5..4c859e497 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -18,8 +18,8 @@ use crate::{ | |||
18 | path::{Path, PathKind}, | 18 | path::{Path, PathKind}, |
19 | per_ns::PerNs, | 19 | per_ns::PerNs, |
20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, | 20 | AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, |
21 | GenericDefId, GenericParamId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, | 21 | GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, |
22 | StaticId, StructId, TraitId, TypeAliasId, | 22 | StructId, TraitId, TypeAliasId, TypeParamId, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | #[derive(Debug, Clone, Default)] | 25 | #[derive(Debug, Clone, Default)] |
@@ -59,7 +59,7 @@ enum Scope { | |||
59 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 59 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
60 | pub enum TypeNs { | 60 | pub enum TypeNs { |
61 | SelfType(ImplId), | 61 | SelfType(ImplId), |
62 | GenericParam(GenericParamId), | 62 | GenericParam(TypeParamId), |
63 | AdtId(AdtId), | 63 | AdtId(AdtId), |
64 | AdtSelfType(AdtId), | 64 | AdtSelfType(AdtId), |
65 | // Yup, enum variants are added to the types ns, but any usage of variant as | 65 | // Yup, enum variants are added to the types ns, but any usage of variant as |
@@ -157,7 +157,7 @@ impl Resolver { | |||
157 | if let Some(local_id) = params.find_by_name(first_name) { | 157 | if let Some(local_id) = params.find_by_name(first_name) { |
158 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 158 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; |
159 | return Some(( | 159 | return Some(( |
160 | TypeNs::GenericParam(GenericParamId { local_id, parent: *def }), | 160 | TypeNs::GenericParam(TypeParamId { local_id, parent: *def }), |
161 | idx, | 161 | idx, |
162 | )); | 162 | )); |
163 | } | 163 | } |
@@ -252,7 +252,7 @@ impl Resolver { | |||
252 | 252 | ||
253 | Scope::GenericParams { params, def } if n_segments > 1 => { | 253 | Scope::GenericParams { params, def } if n_segments > 1 => { |
254 | if let Some(local_id) = params.find_by_name(first_name) { | 254 | if let Some(local_id) = params.find_by_name(first_name) { |
255 | let ty = TypeNs::GenericParam(GenericParamId { local_id, parent: *def }); | 255 | let ty = TypeNs::GenericParam(TypeParamId { local_id, parent: *def }); |
256 | return Some(ResolveValueResult::Partial(ty, 1)); | 256 | return Some(ResolveValueResult::Partial(ty, 1)); |
257 | } | 257 | } |
258 | } | 258 | } |
@@ -399,7 +399,7 @@ pub enum ScopeDef { | |||
399 | PerNs(PerNs), | 399 | PerNs(PerNs), |
400 | ImplSelfType(ImplId), | 400 | ImplSelfType(ImplId), |
401 | AdtSelfType(AdtId), | 401 | AdtSelfType(AdtId), |
402 | GenericParam(GenericParamId), | 402 | GenericParam(TypeParamId), |
403 | Local(PatId), | 403 | Local(PatId), |
404 | } | 404 | } |
405 | 405 | ||
@@ -431,10 +431,10 @@ impl Scope { | |||
431 | } | 431 | } |
432 | } | 432 | } |
433 | Scope::GenericParams { params, def } => { | 433 | Scope::GenericParams { params, def } => { |
434 | for (local_id, param) in params.params.iter() { | 434 | for (local_id, param) in params.types.iter() { |
435 | f( | 435 | f( |
436 | param.name.clone(), | 436 | param.name.clone(), |
437 | ScopeDef::GenericParam(GenericParamId { local_id, parent: *def }), | 437 | ScopeDef::GenericParam(TypeParamId { local_id, parent: *def }), |
438 | ) | 438 | ) |
439 | } | 439 | } |
440 | } | 440 | } |
@@ -481,7 +481,7 @@ impl Resolver { | |||
481 | 481 | ||
482 | fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver { | 482 | fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver { |
483 | let params = db.generic_params(def); | 483 | let params = db.generic_params(def); |
484 | if params.params.is_empty() { | 484 | if params.types.is_empty() { |
485 | self | 485 | self |
486 | } else { | 486 | } else { |
487 | self.push_scope(Scope::GenericParams { def, params }) | 487 | self.push_scope(Scope::GenericParams { def, params }) |
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 936cfe25e..aeb211a91 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs | |||
@@ -5,10 +5,10 @@ use std::sync::Arc; | |||
5 | use hir_def::{ | 5 | use hir_def::{ |
6 | adt::VariantData, | 6 | adt::VariantData, |
7 | db::DefDatabase, | 7 | db::DefDatabase, |
8 | generics::{GenericParamData, GenericParams}, | 8 | generics::{GenericParams, TypeParamData}, |
9 | resolver::{HasResolver, TypeNs}, | 9 | resolver::{HasResolver, TypeNs}, |
10 | type_ref::TypeRef, | 10 | type_ref::TypeRef, |
11 | ContainerId, GenericDefId, GenericParamId, Lookup, TraitId, TypeAliasId, VariantId, | 11 | ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, |
12 | }; | 12 | }; |
13 | use hir_expand::name::{self, Name}; | 13 | use hir_expand::name::{self, Name}; |
14 | 14 | ||
@@ -96,23 +96,21 @@ pub(crate) struct Generics { | |||
96 | } | 96 | } |
97 | 97 | ||
98 | impl Generics { | 98 | impl Generics { |
99 | pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | 99 | pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a { |
100 | self.parent_generics | 100 | self.parent_generics |
101 | .as_ref() | 101 | .as_ref() |
102 | .into_iter() | 102 | .into_iter() |
103 | .flat_map(|it| it.params.params.iter()) | 103 | .flat_map(|it| it.params.types.iter()) |
104 | .chain(self.params.params.iter()) | 104 | .chain(self.params.types.iter()) |
105 | .enumerate() | 105 | .enumerate() |
106 | .map(|(i, (_local_id, p))| (i as u32, p)) | 106 | .map(|(i, (_local_id, p))| (i as u32, p)) |
107 | } | 107 | } |
108 | 108 | ||
109 | pub(crate) fn iter_parent<'a>( | 109 | pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a { |
110 | &'a self, | ||
111 | ) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | ||
112 | self.parent_generics | 110 | self.parent_generics |
113 | .as_ref() | 111 | .as_ref() |
114 | .into_iter() | 112 | .into_iter() |
115 | .flat_map(|it| it.params.params.iter()) | 113 | .flat_map(|it| it.params.types.iter()) |
116 | .enumerate() | 114 | .enumerate() |
117 | .map(|(i, (_local_id, p))| (i as u32, p)) | 115 | .map(|(i, (_local_id, p))| (i as u32, p)) |
118 | } | 116 | } |
@@ -123,20 +121,20 @@ impl Generics { | |||
123 | /// (total, parents, child) | 121 | /// (total, parents, child) |
124 | pub(crate) fn len_split(&self) -> (usize, usize, usize) { | 122 | pub(crate) fn len_split(&self) -> (usize, usize, usize) { |
125 | let parent = self.parent_generics.as_ref().map_or(0, |p| p.len()); | 123 | let parent = self.parent_generics.as_ref().map_or(0, |p| p.len()); |
126 | let child = self.params.params.len(); | 124 | let child = self.params.types.len(); |
127 | (parent + child, parent, child) | 125 | (parent + child, parent, child) |
128 | } | 126 | } |
129 | pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { | 127 | pub(crate) fn param_idx(&self, param: TypeParamId) -> u32 { |
130 | self.find_param(param).0 | 128 | self.find_param(param).0 |
131 | } | 129 | } |
132 | pub(crate) fn param_name(&self, param: GenericParamId) -> Name { | 130 | pub(crate) fn param_name(&self, param: TypeParamId) -> Name { |
133 | self.find_param(param).1.name.clone() | 131 | self.find_param(param).1.name.clone() |
134 | } | 132 | } |
135 | fn find_param(&self, param: GenericParamId) -> (u32, &GenericParamData) { | 133 | fn find_param(&self, param: TypeParamId) -> (u32, &TypeParamData) { |
136 | if param.parent == self.def { | 134 | if param.parent == self.def { |
137 | let (idx, (_local_id, data)) = self | 135 | let (idx, (_local_id, data)) = self |
138 | .params | 136 | .params |
139 | .params | 137 | .types |
140 | .iter() | 138 | .iter() |
141 | .enumerate() | 139 | .enumerate() |
142 | .find(|(_, (idx, _))| *idx == param.local_id) | 140 | .find(|(_, (idx, _))| *idx == param.local_id) |
diff --git a/crates/ra_ide/src/display/navigation_target.rs b/crates/ra_ide/src/display/navigation_target.rs index add11fbc3..6a6b49afd 100644 --- a/crates/ra_ide/src/display/navigation_target.rs +++ b/crates/ra_ide/src/display/navigation_target.rs | |||
@@ -6,7 +6,7 @@ use ra_db::{FileId, SourceDatabase}; | |||
6 | use ra_syntax::{ | 6 | use ra_syntax::{ |
7 | ast::{self, DocCommentsOwner, NameOwner}, | 7 | ast::{self, DocCommentsOwner, NameOwner}, |
8 | match_ast, AstNode, SmolStr, | 8 | match_ast, AstNode, SmolStr, |
9 | SyntaxKind::{self, BIND_PAT}, | 9 | SyntaxKind::{self, BIND_PAT, TYPE_PARAM}, |
10 | TextRange, | 10 | TextRange, |
11 | }; | 11 | }; |
12 | 12 | ||
@@ -351,6 +351,26 @@ impl ToNav for hir::Local { | |||
351 | } | 351 | } |
352 | } | 352 | } |
353 | 353 | ||
354 | impl ToNav for hir::TypeParam { | ||
355 | fn to_nav(&self, db: &RootDatabase) -> NavigationTarget { | ||
356 | let src = self.source(db); | ||
357 | let range = match src.value { | ||
358 | Either::Left(it) => it.syntax().text_range(), | ||
359 | Either::Right(it) => it.syntax().text_range(), | ||
360 | }; | ||
361 | NavigationTarget { | ||
362 | file_id: src.file_id.original_file(db), | ||
363 | name: self.name(db).to_string().into(), | ||
364 | kind: TYPE_PARAM, | ||
365 | full_range: range, | ||
366 | focus_range: None, | ||
367 | container_name: None, | ||
368 | description: None, | ||
369 | docs: None, | ||
370 | } | ||
371 | } | ||
372 | } | ||
373 | |||
354 | pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { | 374 | pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { |
355 | let parse = db.parse(symbol.file_id); | 375 | let parse = db.parse(symbol.file_id); |
356 | let node = symbol.ptr.to_node(parse.tree().syntax()); | 376 | let node = symbol.ptr.to_node(parse.tree().syntax()); |
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index d3c198813..1b968134d 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs | |||
@@ -64,9 +64,11 @@ pub(crate) fn reference_definition( | |||
64 | 64 | ||
65 | let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind); | 65 | let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind); |
66 | match name_kind { | 66 | match name_kind { |
67 | Some(Macro(mac)) => return Exact(mac.to_nav(db)), | 67 | Some(Macro(it)) => return Exact(it.to_nav(db)), |
68 | Some(Field(field)) => return Exact(field.to_nav(db)), | 68 | Some(Field(it)) => return Exact(it.to_nav(db)), |
69 | Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)), | 69 | Some(TypeParam(it)) => return Exact(it.to_nav(db)), |
70 | Some(AssocItem(it)) => return Exact(it.to_nav(db)), | ||
71 | Some(Local(it)) => return Exact(it.to_nav(db)), | ||
70 | Some(Def(def)) => match NavigationTarget::from_def(db, def) { | 72 | Some(Def(def)) => match NavigationTarget::from_def(db, def) { |
71 | Some(nav) => return Exact(nav), | 73 | Some(nav) => return Exact(nav), |
72 | None => return Approximate(vec![]), | 74 | None => return Approximate(vec![]), |
@@ -77,10 +79,6 @@ pub(crate) fn reference_definition( | |||
77 | // us to the actual type | 79 | // us to the actual type |
78 | return Exact(imp.to_nav(db)); | 80 | return Exact(imp.to_nav(db)); |
79 | } | 81 | } |
80 | Some(Local(local)) => return Exact(local.to_nav(db)), | ||
81 | Some(GenericParam(_)) => { | ||
82 | // FIXME: go to the generic param def | ||
83 | } | ||
84 | None => {} | 82 | None => {} |
85 | }; | 83 | }; |
86 | 84 | ||
@@ -723,4 +721,17 @@ mod tests { | |||
723 | "foo FN_DEF FileId(1) [359; 376) [362; 365)", | 721 | "foo FN_DEF FileId(1) [359; 376) [362; 365)", |
724 | ); | 722 | ); |
725 | } | 723 | } |
724 | |||
725 | #[test] | ||
726 | fn goto_for_type_param() { | ||
727 | check_goto( | ||
728 | " | ||
729 | //- /lib.rs | ||
730 | struct Foo<T> { | ||
731 | t: <|>T, | ||
732 | } | ||
733 | ", | ||
734 | "T TYPE_PARAM FileId(1) [11; 12)", | ||
735 | ); | ||
736 | } | ||
726 | } | 737 | } |
diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index d8185c688..d372ca758 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs | |||
@@ -138,7 +138,7 @@ fn hover_text_from_name_kind( | |||
138 | *no_fallback = true; | 138 | *no_fallback = true; |
139 | None | 139 | None |
140 | } | 140 | } |
141 | GenericParam(_) | SelfType(_) => { | 141 | TypeParam(_) | SelfType(_) => { |
142 | // FIXME: Hover for generic param | 142 | // FIXME: Hover for generic param |
143 | None | 143 | None |
144 | } | 144 | } |
diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 3e7bfd872..e3ecde50d 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs | |||
@@ -85,7 +85,7 @@ pub(crate) fn find_all_refs( | |||
85 | NameKind::Def(def) => NavigationTarget::from_def(db, def)?, | 85 | NameKind::Def(def) => NavigationTarget::from_def(db, def)?, |
86 | NameKind::SelfType(imp) => imp.to_nav(db), | 86 | NameKind::SelfType(imp) => imp.to_nav(db), |
87 | NameKind::Local(local) => local.to_nav(db), | 87 | NameKind::Local(local) => local.to_nav(db), |
88 | NameKind::GenericParam(_) => return None, | 88 | NameKind::TypeParam(_) => return None, |
89 | }; | 89 | }; |
90 | 90 | ||
91 | let search_scope = { | 91 | let search_scope = { |
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index b716d32e5..c1f091ec0 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs | |||
@@ -110,6 +110,15 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti | |||
110 | kind: NameKind::Macro(def), | 110 | kind: NameKind::Macro(def), |
111 | }) | 111 | }) |
112 | }, | 112 | }, |
113 | ast::TypeParam(it) => { | ||
114 | let src = name.with_value(it); | ||
115 | let def = hir::TypeParam::from_source(db, src)?; | ||
116 | Some(NameDefinition { | ||
117 | visibility: None, | ||
118 | container: def.module(db), | ||
119 | kind: NameKind::TypeParam(def), | ||
120 | }) | ||
121 | }, | ||
113 | _ => None, | 122 | _ => None, |
114 | } | 123 | } |
115 | } | 124 | } |
@@ -168,9 +177,8 @@ pub(crate) fn classify_name_ref( | |||
168 | let kind = NameKind::Local(local); | 177 | let kind = NameKind::Local(local); |
169 | Some(NameDefinition { kind, container, visibility: None }) | 178 | Some(NameDefinition { kind, container, visibility: None }) |
170 | } | 179 | } |
171 | PathResolution::GenericParam(par) => { | 180 | PathResolution::TypeParam(par) => { |
172 | // FIXME: get generic param def | 181 | let kind = NameKind::TypeParam(par); |
173 | let kind = NameKind::GenericParam(par); | ||
174 | Some(NameDefinition { kind, container, visibility }) | 182 | Some(NameDefinition { kind, container, visibility }) |
175 | } | 183 | } |
176 | PathResolution::Macro(def) => { | 184 | PathResolution::Macro(def) => { |
diff --git a/crates/ra_ide/src/references/name_definition.rs b/crates/ra_ide/src/references/name_definition.rs index 10d3a2364..8c67c8863 100644 --- a/crates/ra_ide/src/references/name_definition.rs +++ b/crates/ra_ide/src/references/name_definition.rs | |||
@@ -4,8 +4,8 @@ | |||
4 | //! Note that the reference search is possible for not all of the classified items. | 4 | //! Note that the reference search is possible for not all of the classified items. |
5 | 5 | ||
6 | use hir::{ | 6 | use hir::{ |
7 | Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, | 7 | Adt, AssocItem, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, StructField, |
8 | StructField, VariantDef, | 8 | TypeParam, VariantDef, |
9 | }; | 9 | }; |
10 | use ra_syntax::{ast, ast::VisibilityOwner}; | 10 | use ra_syntax::{ast, ast::VisibilityOwner}; |
11 | 11 | ||
@@ -19,7 +19,7 @@ pub enum NameKind { | |||
19 | Def(ModuleDef), | 19 | Def(ModuleDef), |
20 | SelfType(ImplBlock), | 20 | SelfType(ImplBlock), |
21 | Local(Local), | 21 | Local(Local), |
22 | GenericParam(GenericParam), | 22 | TypeParam(TypeParam), |
23 | } | 23 | } |
24 | 24 | ||
25 | #[derive(PartialEq, Eq)] | 25 | #[derive(PartialEq, Eq)] |
diff --git a/crates/ra_ide/src/snapshots/highlighting.html b/crates/ra_ide/src/snapshots/highlighting.html index b39c4d371..4166a8f90 100644 --- a/crates/ra_ide/src/snapshots/highlighting.html +++ b/crates/ra_ide/src/snapshots/highlighting.html | |||
@@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
9 | .parameter { color: #94BFF3; } | 9 | .parameter { color: #94BFF3; } |
10 | .builtin { color: #DD6718; } | 10 | .builtin { color: #DD6718; } |
11 | .text { color: #DCDCCC; } | 11 | .text { color: #DCDCCC; } |
12 | .type { color: #7CB8BB; } | ||
12 | .attribute { color: #94BFF3; } | 13 | .attribute { color: #94BFF3; } |
13 | .literal { color: #BFEBBF; } | 14 | .literal { color: #BFEBBF; } |
14 | .macro { color: #94BFF3; } | 15 | .macro { color: #94BFF3; } |
@@ -45,4 +46,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
45 | <span class="keyword">let</span> <span class="variable">z</span> = &<span class="variable.mut">y</span>; | 46 | <span class="keyword">let</span> <span class="variable">z</span> = &<span class="variable.mut">y</span>; |
46 | 47 | ||
47 | <span class="variable.mut">y</span>; | 48 | <span class="variable.mut">y</span>; |
49 | } | ||
50 | |||
51 | <span class="keyword">enum</span> <span class="type">E</span><<span class="type">X</span>> { | ||
52 | <span class="constant">V</span>(<span class="type">X</span>) | ||
53 | } | ||
54 | |||
55 | <span class="keyword">impl</span><<span class="type">X</span>> <span class="type">E</span><<span class="type">X</span>> { | ||
56 | <span class="keyword">fn</span> <span class="function">new</span><<span class="type">T</span>>() -> <span class="type">E</span><<span class="type">T</span>> {} | ||
48 | }</code></pre> \ No newline at end of file | 57 | }</code></pre> \ No newline at end of file |
diff --git a/crates/ra_ide/src/snapshots/rainbow_highlighting.html b/crates/ra_ide/src/snapshots/rainbow_highlighting.html index 79f11ea80..9dfbc8047 100644 --- a/crates/ra_ide/src/snapshots/rainbow_highlighting.html +++ b/crates/ra_ide/src/snapshots/rainbow_highlighting.html | |||
@@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
9 | .parameter { color: #94BFF3; } | 9 | .parameter { color: #94BFF3; } |
10 | .builtin { color: #DD6718; } | 10 | .builtin { color: #DD6718; } |
11 | .text { color: #DCDCCC; } | 11 | .text { color: #DCDCCC; } |
12 | .type { color: #7CB8BB; } | ||
12 | .attribute { color: #94BFF3; } | 13 | .attribute { color: #94BFF3; } |
13 | .literal { color: #BFEBBF; } | 14 | .literal { color: #BFEBBF; } |
14 | .macro { color: #94BFF3; } | 15 | .macro { color: #94BFF3; } |
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs index e6a79541f..7ecb1a027 100644 --- a/crates/ra_ide/src/syntax_highlighting.rs +++ b/crates/ra_ide/src/syntax_highlighting.rs | |||
@@ -225,8 +225,7 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str { | |||
225 | Def(hir::ModuleDef::Trait(_)) => "type", | 225 | Def(hir::ModuleDef::Trait(_)) => "type", |
226 | Def(hir::ModuleDef::TypeAlias(_)) => "type", | 226 | Def(hir::ModuleDef::TypeAlias(_)) => "type", |
227 | Def(hir::ModuleDef::BuiltinType(_)) => "type", | 227 | Def(hir::ModuleDef::BuiltinType(_)) => "type", |
228 | SelfType(_) => "type", | 228 | SelfType(_) | TypeParam(_) => "type", |
229 | GenericParam(_) => "type", | ||
230 | Local(local) => { | 229 | Local(local) => { |
231 | if local.is_mut(db) { | 230 | if local.is_mut(db) { |
232 | "variable.mut" | 231 | "variable.mut" |
@@ -255,6 +254,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
255 | .parameter { color: #94BFF3; } | 254 | .parameter { color: #94BFF3; } |
256 | .builtin { color: #DD6718; } | 255 | .builtin { color: #DD6718; } |
257 | .text { color: #DCDCCC; } | 256 | .text { color: #DCDCCC; } |
257 | .type { color: #7CB8BB; } | ||
258 | .attribute { color: #94BFF3; } | 258 | .attribute { color: #94BFF3; } |
259 | .literal { color: #BFEBBF; } | 259 | .literal { color: #BFEBBF; } |
260 | .macro { color: #94BFF3; } | 260 | .macro { color: #94BFF3; } |
@@ -303,6 +303,14 @@ fn main() { | |||
303 | 303 | ||
304 | y; | 304 | y; |
305 | } | 305 | } |
306 | |||
307 | enum E<X> { | ||
308 | V(X) | ||
309 | } | ||
310 | |||
311 | impl<X> E<X> { | ||
312 | fn new<T>() -> E<T> {} | ||
313 | } | ||
306 | "# | 314 | "# |
307 | .trim(), | 315 | .trim(), |
308 | ); | 316 | ); |