From dda9587e75f4fd1740d16531038023ff582ef43f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 18:24:52 +0100 Subject: Track source of type parameters --- crates/ra_hir/src/code_model/src.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 78a454082..8f04ebd23 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -9,8 +9,8 @@ use hir_def::{ use ra_syntax::ast; use crate::{ - db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, - Module, Static, Struct, StructField, Trait, TypeAlias, Union, + db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, GenericParam, ImplBlock, + Import, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::InFile; @@ -129,3 +129,11 @@ impl HasSource for Import { src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root))) } } + +impl HasSource for GenericParam { + type Ast = Either; + fn source(self, db: &impl DefDatabase) -> InFile { + let child_source = self.id.parent.child_source(db); + child_source.map(|it| it[self.id.local_id].clone()) + } +} -- cgit v1.2.3 From 1692f07393dba4f5c122df1a609d5b18751bf406 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 18:48:35 +0100 Subject: ToNav for GenericParam --- crates/ra_hir/src/code_model.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 29ace8479..8833750c8 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -860,6 +860,13 @@ pub struct GenericParam { pub(crate) id: GenericParamId, } +impl GenericParam { + pub fn name(self, db: &impl HirDatabase) -> Name { + let params = db.generic_params(self.id.parent); + params.params[self.id.local_id].name.clone() + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplBlock { pub(crate) id: ImplId, -- cgit v1.2.3 From 7d2080a0311cab62388f416beeb360695dbc5ded Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 19:52:09 +0100 Subject: Classify name works for TypeParams --- crates/ra_hir/src/code_model.rs | 4 ++++ crates/ra_hir/src/from_source.rs | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8833750c8..0295eb948 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -865,6 +865,10 @@ impl GenericParam { let params = db.generic_params(self.id.parent); params.params[self.id.local_id].name.clone() } + + pub fn module(self, db: &impl HirDatabase) -> Module { + self.id.parent.module(db).into() + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 437f800c1..686ab1d79 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir_def::{ child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, - EnumVariantId, LocationCtx, ModuleId, VariantId, + EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId, }; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ @@ -11,8 +11,8 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, - MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, GenericParam, ImplBlock, InFile, + Local, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub trait FromSource: Sized { @@ -177,6 +177,23 @@ impl Local { } } +impl GenericParam { + pub fn from_source(db: &impl HirDatabase, src: InFile) -> Option { + let file_id = src.file_id; + let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { + let res = match_ast! { + match it { + ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.id.into() }, + _ => return None, + } + }; + Some(res) + })?; + let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?; + Some(GenericParam { id }) + } +} + impl Module { pub fn from_declaration(db: &impl DefDatabase, src: InFile) -> Option { let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); -- cgit v1.2.3 From f4f8b8147426b0096d4b5126e487caaa13d13c27 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 20:05:08 +0100 Subject: Get the right analyzer for impls --- crates/ra_hir/src/source_binder.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 8c4b635d2..2957e496c 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -37,7 +37,7 @@ use crate::{ InEnvironment, TraitEnvironment, Ty, }, Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam, - Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, + ImplBlock, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option { @@ -59,6 +59,10 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) - let src = node.with_value(it); Some(Enum::from_source(db, src)?.id.resolver(db)) }, + ast::ImplBlock(it) => { + let src = node.with_value(it); + Some(ImplBlock::from_source(db, src)?.id.resolver(db)) + }, _ => match node.value.kind() { FN_DEF | CONST_DEF | STATIC_DEF => { let def = def_with_body_from_child_node(db, node)?; -- cgit v1.2.3 From 88c5b1282a5770097c6c768b24bedfc3a6944e08 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 20:09:53 +0100 Subject: Rename GenericParam -> TypeParam We don't have LifetimeParam yet, but they are planned! --- crates/ra_hir/src/code_model.rs | 16 ++++++++-------- crates/ra_hir/src/code_model/src.rs | 6 +++--- crates/ra_hir/src/from_source.rs | 8 ++++---- crates/ra_hir/src/lib.rs | 6 +++--- crates/ra_hir/src/source_binder.rs | 10 +++++----- 5 files changed, 23 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 0295eb948..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::{ per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, - AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, GenericParamId, - HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, - Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, + AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, + LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, + StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -856,14 +856,14 @@ impl Local { } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct GenericParam { - pub(crate) id: GenericParamId, +pub struct TypeParam { + pub(crate) id: TypeParamId, } -impl GenericParam { +impl TypeParam { pub fn name(self, db: &impl HirDatabase) -> Name { let params = db.generic_params(self.id.parent); - params.params[self.id.local_id].name.clone() + params.types[self.id.local_id].name.clone() } pub fn module(self, db: &impl HirDatabase) -> Module { @@ -1111,7 +1111,7 @@ impl HirDisplay for Type { pub enum ScopeDef { ModuleDef(ModuleDef), MacroDef(MacroDef), - GenericParam(GenericParam), + GenericParam(TypeParam), ImplSelfType(ImplBlock), AdtSelfType(Adt), Local(Local), diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 8f04ebd23..b09582f93 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -9,8 +9,8 @@ use hir_def::{ use ra_syntax::ast; use crate::{ - db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, GenericParam, ImplBlock, - Import, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, + db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, + Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, }; pub use hir_expand::InFile; @@ -130,7 +130,7 @@ impl HasSource for Import { } } -impl HasSource for GenericParam { +impl HasSource for TypeParam { type Ast = Either; fn source(self, db: &impl DefDatabase) -> InFile { let child_source = self.id.parent.child_source(db); diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 686ab1d79..68e59fc1e 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -11,8 +11,8 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, - Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, GenericParam, ImplBlock, InFile, - Local, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, + MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, }; pub trait FromSource: Sized { @@ -177,7 +177,7 @@ impl Local { } } -impl GenericParam { +impl TypeParam { pub fn from_source(db: &impl HirDatabase, src: InFile) -> Option { let file_id = src.file_id; let parent: GenericDefId = src.value.syntax().ancestors().find_map(|it| { @@ -190,7 +190,7 @@ impl GenericParam { Some(res) })?; let &id = parent.child_by_source(db)[keys::TYPE_PARAM].get(&src)?; - Some(GenericParam { id }) + Some(TypeParam { id }) } } 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; pub use crate::{ code_model::{ src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, - DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, - HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, - StructField, Trait, Type, TypeAlias, Union, VariantDef, + DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs, + ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, + StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, }, from_source::FromSource, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 2957e496c..b80aaeb90 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -36,8 +36,8 @@ use crate::{ method_resolution::{self, implements_trait}, InEnvironment, TraitEnvironment, Ty, }, - Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam, - ImplBlock, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, + Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, Local, + MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, }; fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option { @@ -112,7 +112,7 @@ pub enum PathResolution { /// A local binding (only value namespace) Local(Local), /// A generic parameter - GenericParam(GenericParam), + TypeParam(TypeParam), SelfType(crate::ImplBlock), Macro(MacroDef), AssocItem(crate::AssocItem), @@ -266,7 +266,7 @@ impl SourceAnalyzer { ) -> Option { let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), - TypeNs::GenericParam(id) => PathResolution::GenericParam(GenericParam { id }), + TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { PathResolution::Def(Adt::from(it).into()) } @@ -338,7 +338,7 @@ impl SourceAnalyzer { resolver::ScopeDef::PerNs(it) => it.into(), resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), - resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(GenericParam { id }), + resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }), resolver::ScopeDef::Local(pat_id) => { let parent = self.resolver.body_owner().unwrap().into(); ScopeDef::Local(Local { parent, pat_id }) -- cgit v1.2.3