diff options
Diffstat (limited to 'crates/hir/src/semantics')
-rw-r--r-- | crates/hir/src/semantics/source_to_def.rs | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 66fc11611..a333d7aea 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs | |||
@@ -7,7 +7,8 @@ use hir_def::{ | |||
7 | expr::PatId, | 7 | expr::PatId, |
8 | keys::{self, Key}, | 8 | keys::{self, Key}, |
9 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId, | 9 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FieldId, FunctionId, GenericDefId, ImplId, |
10 | ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, | 10 | LifetimeParamId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, |
11 | VariantId, | ||
11 | }; | 12 | }; |
12 | use hir_expand::{name::AsName, AstId, MacroDefKind}; | 13 | use hir_expand::{name::AsName, AstId, MacroDefKind}; |
13 | use rustc_hash::FxHashMap; | 14 | use rustc_hash::FxHashMap; |
@@ -128,15 +129,30 @@ impl SourceToDefCtx<'_, '_> { | |||
128 | 129 | ||
129 | pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> { | 130 | pub(super) fn type_param_to_def(&mut self, src: InFile<ast::TypeParam>) -> Option<TypeParamId> { |
130 | let container: ChildContainer = | 131 | let container: ChildContainer = |
131 | self.find_type_param_container(src.as_ref().map(|it| it.syntax()))?.into(); | 132 | self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into(); |
132 | let db = self.db; | 133 | let db = self.db; |
133 | let dyn_map = | 134 | let dyn_map = |
134 | &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); | 135 | &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); |
135 | dyn_map[keys::TYPE_PARAM].get(&src).copied() | 136 | dyn_map[keys::TYPE_PARAM].get(&src).copied() |
136 | } | 137 | } |
137 | 138 | ||
139 | pub(super) fn lifetime_param_to_def( | ||
140 | &mut self, | ||
141 | src: InFile<ast::LifetimeParam>, | ||
142 | ) -> Option<LifetimeParamId> { | ||
143 | let container: ChildContainer = | ||
144 | self.find_generic_param_container(src.as_ref().map(|it| it.syntax()))?.into(); | ||
145 | let db = self.db; | ||
146 | let dyn_map = | ||
147 | &*self.cache.entry(container).or_insert_with(|| container.child_by_source(db)); | ||
148 | dyn_map[keys::LIFETIME_PARAM].get(&src).copied() | ||
149 | } | ||
150 | |||
138 | // FIXME: use DynMap as well? | 151 | // FIXME: use DynMap as well? |
139 | pub(super) fn macro_call_to_def(&mut self, src: InFile<ast::MacroCall>) -> Option<MacroDefId> { | 152 | pub(super) fn macro_rules_to_def( |
153 | &mut self, | ||
154 | src: InFile<ast::MacroRules>, | ||
155 | ) -> Option<MacroDefId> { | ||
140 | let kind = MacroDefKind::Declarative; | 156 | let kind = MacroDefKind::Declarative; |
141 | let file_id = src.file_id.original_file(self.db.upcast()); | 157 | let file_id = src.file_id.original_file(self.db.upcast()); |
142 | let krate = self.file_to_def(file_id)?.krate; | 158 | let krate = self.file_to_def(file_id)?.krate; |
@@ -203,7 +219,7 @@ impl SourceToDefCtx<'_, '_> { | |||
203 | Some(def.into()) | 219 | Some(def.into()) |
204 | } | 220 | } |
205 | 221 | ||
206 | fn find_type_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { | 222 | fn find_generic_param_container(&mut self, src: InFile<&SyntaxNode>) -> Option<GenericDefId> { |
207 | for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { | 223 | for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { |
208 | let res: GenericDefId = match_ast! { | 224 | let res: GenericDefId = match_ast! { |
209 | match (container.value) { | 225 | match (container.value) { |
@@ -247,7 +263,7 @@ pub(crate) enum ChildContainer { | |||
247 | VariantId(VariantId), | 263 | VariantId(VariantId), |
248 | TypeAliasId(TypeAliasId), | 264 | TypeAliasId(TypeAliasId), |
249 | /// XXX: this might be the same def as, for example an `EnumId`. However, | 265 | /// XXX: this might be the same def as, for example an `EnumId`. However, |
250 | /// here the children generic parameters, and not, eg enum variants. | 266 | /// here the children are generic parameters, and not, eg enum variants. |
251 | GenericDefId(GenericDefId), | 267 | GenericDefId(GenericDefId), |
252 | } | 268 | } |
253 | impl_from! { | 269 | impl_from! { |