From d75f768c13752dfa5ea9189a0b4dfb9b460993e6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 17:34:28 +0100 Subject: Minor --- crates/ra_hir_def/src/generics.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 94ce83564..b3e345082 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -58,11 +58,12 @@ impl GenericParams { GenericDefId::TraitId(it) => { // traits get the Self type as an implicit first type parameter generics.params.alloc(GenericParamData { name: name::SELF_TYPE, default: None }); - generics.fill(&it.source(db).value); // add super traits as bounds on Self // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar let self_param = TypeRef::Path(name::SELF_TYPE.into()); generics.fill_bounds(&it.source(db).value, self_param); + + generics.fill(&it.source(db).value); } GenericDefId::TypeAliasId(it) => generics.fill(&it.lookup(db).source(db).value), // Note that we don't add `Self` here: in `impl`s, `Self` is not a @@ -75,7 +76,7 @@ impl GenericParams { generics } - fn fill(&mut self, node: &impl TypeParamsOwner) { + fn fill(&mut self, node: &dyn TypeParamsOwner) { if let Some(params) = node.type_param_list() { self.fill_params(params) } @@ -84,7 +85,7 @@ impl GenericParams { } } - fn fill_bounds(&mut self, node: &impl ast::TypeBoundsOwner, type_ref: TypeRef) { + fn fill_bounds(&mut self, node: &dyn ast::TypeBoundsOwner, type_ref: TypeRef) { for bound in node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) { -- cgit v1.2.3 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_def/src/generics.rs | 93 ++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index b3e345082..0df5a20f5 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -4,12 +4,18 @@ //! in rustc. use std::sync::Arc; -use hir_expand::name::{self, AsName, Name}; -use ra_arena::Arena; +use either::Either; +use hir_expand::{ + name::{self, AsName, Name}, + InFile, +}; +use ra_arena::{map::ArenaMap, Arena}; +use ra_db::FileId; use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; use crate::{ db::DefDatabase, + src::HasChildSource, src::HasSource, type_ref::{TypeBound, TypeRef}, AdtId, AstItemDef, GenericDefId, LocalGenericParamId, Lookup, @@ -39,46 +45,81 @@ pub struct WherePredicate { pub bound: TypeBound, } +type SourceMap = ArenaMap>; + impl GenericParams { pub(crate) fn generic_params_query( db: &impl DefDatabase, def: GenericDefId, ) -> Arc { - Arc::new(GenericParams::new(db, def.into())) + let (params, _source_map) = GenericParams::new(db, def.into()); + Arc::new(params) } - fn new(db: &impl DefDatabase, def: GenericDefId) -> GenericParams { + fn new(db: &impl DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { let mut generics = GenericParams { params: Arena::default(), where_predicates: Vec::new() }; + let mut sm = ArenaMap::default(); // FIXME: add `: Sized` bound for everything except for `Self` in traits - match def { - GenericDefId::FunctionId(it) => generics.fill(&it.lookup(db).source(db).value), - GenericDefId::AdtId(AdtId::StructId(it)) => generics.fill(&it.source(db).value), - GenericDefId::AdtId(AdtId::UnionId(it)) => generics.fill(&it.source(db).value), - GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value), + let file_id = match def { + GenericDefId::FunctionId(it) => { + let src = it.lookup(db).source(db); + generics.fill(&mut sm, &src.value); + src.file_id + } + GenericDefId::AdtId(AdtId::StructId(it)) => { + let src = it.source(db); + generics.fill(&mut sm, &src.value); + src.file_id + } + GenericDefId::AdtId(AdtId::UnionId(it)) => { + let src = it.source(db); + generics.fill(&mut sm, &src.value); + src.file_id + } + GenericDefId::AdtId(AdtId::EnumId(it)) => { + let src = it.source(db); + generics.fill(&mut sm, &src.value); + src.file_id + } GenericDefId::TraitId(it) => { + let src = it.source(db); + // traits get the Self type as an implicit first type parameter - generics.params.alloc(GenericParamData { name: name::SELF_TYPE, default: None }); + let self_param_id = generics + .params + .alloc(GenericParamData { name: name::SELF_TYPE, default: None }); + sm.insert(self_param_id, Either::Left(src.value.clone())); // add super traits as bounds on Self // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar let self_param = TypeRef::Path(name::SELF_TYPE.into()); - generics.fill_bounds(&it.source(db).value, self_param); + generics.fill_bounds(&src.value, self_param); - generics.fill(&it.source(db).value); + generics.fill(&mut sm, &src.value); + src.file_id + } + GenericDefId::TypeAliasId(it) => { + let src = it.lookup(db).source(db); + generics.fill(&mut sm, &src.value); + src.file_id } - GenericDefId::TypeAliasId(it) => generics.fill(&it.lookup(db).source(db).value), // Note that we don't add `Self` here: in `impl`s, `Self` is not a // type-parameter, but rather is a type-alias for impl's target // type, so this is handled by the resolver. - GenericDefId::ImplId(it) => generics.fill(&it.source(db).value), - GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {} - } + GenericDefId::ImplId(it) => { + let src = it.source(db); + generics.fill(&mut sm, &src.value); + src.file_id + } + // We won't be using this ID anyway + GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => FileId(!0).into(), + }; - generics + (generics, InFile::new(file_id, sm)) } - fn fill(&mut self, node: &dyn TypeParamsOwner) { + fn fill(&mut self, sm: &mut SourceMap, node: &dyn TypeParamsOwner) { if let Some(params) = node.type_param_list() { - self.fill_params(params) + self.fill_params(sm, params) } if let Some(where_clause) = node.where_clause() { self.fill_where_predicates(where_clause); @@ -93,13 +134,14 @@ impl GenericParams { } } - fn fill_params(&mut self, params: ast::TypeParamList) { + fn fill_params(&mut self, sm: &mut SourceMap, params: ast::TypeParamList) { for type_param in params.type_params() { let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); // FIXME: Use `Path::from_src` let default = type_param.default_type().map(TypeRef::from_ast); let param = GenericParamData { name: name.clone(), default }; - self.params.alloc(param); + let param_id = self.params.alloc(param); + sm.insert(param_id, Either::Right(type_param.clone())); let type_ref = TypeRef::Path(name.into()); self.fill_bounds(&type_param, type_ref); @@ -132,3 +174,12 @@ impl GenericParams { self.params.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) } } + +impl HasChildSource for GenericDefId { + type ChildId = LocalGenericParamId; + type Value = Either; + fn child_source(&self, db: &impl DefDatabase) -> InFile { + let (_, sm) = GenericParams::new(db, *self); + sm + } +} -- 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_def/src/generics.rs | 20 +++++++++++++++++++- crates/ra_hir_def/src/keys.rs | 3 ++- crates/ra_hir_def/src/lib.rs | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 0df5a20f5..159f9034b 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -14,11 +14,14 @@ use ra_db::FileId; use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; use crate::{ + child_by_source::ChildBySource, db::DefDatabase, + dyn_map::DynMap, + keys, src::HasChildSource, src::HasSource, type_ref::{TypeBound, TypeRef}, - AdtId, AstItemDef, GenericDefId, LocalGenericParamId, Lookup, + AdtId, AstItemDef, GenericDefId, GenericParamId, LocalGenericParamId, Lookup, }; /// Data about a generic parameter (to a function, struct, impl, ...). @@ -183,3 +186,18 @@ impl HasChildSource for GenericDefId { sm } } + +impl ChildBySource for GenericDefId { + fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { + let mut res = DynMap::default(); + let arena_map = self.child_source(db); + let arena_map = arena_map.as_ref(); + for (local_id, src) in arena_map.value.iter() { + let id = GenericParamId { parent: *self, local_id }; + if let Either::Right(type_param) = src { + res[keys::TYPE_PARAM].insert(arena_map.with_value(type_param.clone()), id) + } + } + res + } +} diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index 447b7e3ba..ca5630c80 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; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, + ConstId, EnumVariantId, FunctionId, GenericParamId, StaticId, StructFieldId, TypeAliasId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -20,6 +20,7 @@ pub const ENUM_VARIANT: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); pub const RECORD_FIELD: Key = Key::new(); +pub const TYPE_PARAM: Key = Key::new(); /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are /// 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..6dfb3c03d 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -525,6 +525,20 @@ impl HasModule for DefWithBodyId { } } +impl HasModule for GenericDefId { + fn module(&self, db: &impl db::DefDatabase) -> ModuleId { + match self { + GenericDefId::FunctionId(it) => it.lookup(db).module(db), + GenericDefId::AdtId(it) => it.module(db), + GenericDefId::TraitId(it) => it.module(db), + GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), + GenericDefId::ImplId(it) => it.module(db), + GenericDefId::EnumVariantId(it) => it.parent.module(db), + GenericDefId::ConstId(it) => it.lookup(db).module(db), + } + } +} + impl HasModule for StaticLoc { fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { self.container -- 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_def/src/generics.rs | 28 ++++++++++++++-------------- crates/ra_hir_def/src/keys.rs | 4 ++-- crates/ra_hir_def/src/lib.rs | 8 ++++---- crates/ra_hir_def/src/resolver.rs | 18 +++++++++--------- 4 files changed, 29 insertions(+), 29 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 159f9034b..976cf72d0 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -21,12 +21,12 @@ use crate::{ src::HasChildSource, src::HasSource, type_ref::{TypeBound, TypeRef}, - AdtId, AstItemDef, GenericDefId, GenericParamId, LocalGenericParamId, Lookup, + AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, }; /// Data about a generic parameter (to a function, struct, impl, ...). #[derive(Clone, PartialEq, Eq, Debug)] -pub struct GenericParamData { +pub struct TypeParamData { pub name: Name, pub default: Option, } @@ -34,7 +34,8 @@ pub struct GenericParamData { /// Data about the generic parameters of a function, struct, impl, etc. #[derive(Clone, PartialEq, Eq, Debug)] pub struct GenericParams { - pub params: Arena, + pub types: Arena, + // lifetimes: Arena, pub where_predicates: Vec, } @@ -48,7 +49,7 @@ pub struct WherePredicate { pub bound: TypeBound, } -type SourceMap = ArenaMap>; +type SourceMap = ArenaMap>; impl GenericParams { pub(crate) fn generic_params_query( @@ -60,7 +61,7 @@ impl GenericParams { } fn new(db: &impl DefDatabase, def: GenericDefId) -> (GenericParams, InFile) { - let mut generics = GenericParams { params: Arena::default(), where_predicates: Vec::new() }; + let mut generics = GenericParams { types: Arena::default(), where_predicates: Vec::new() }; let mut sm = ArenaMap::default(); // FIXME: add `: Sized` bound for everything except for `Self` in traits let file_id = match def { @@ -88,9 +89,8 @@ impl GenericParams { let src = it.source(db); // traits get the Self type as an implicit first type parameter - let self_param_id = generics - .params - .alloc(GenericParamData { name: name::SELF_TYPE, default: None }); + let self_param_id = + generics.types.alloc(TypeParamData { name: name::SELF_TYPE, default: None }); sm.insert(self_param_id, Either::Left(src.value.clone())); // add super traits as bounds on Self // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar @@ -142,8 +142,8 @@ impl GenericParams { let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); // FIXME: Use `Path::from_src` let default = type_param.default_type().map(TypeRef::from_ast); - let param = GenericParamData { name: name.clone(), default }; - let param_id = self.params.alloc(param); + let param = TypeParamData { name: name.clone(), default }; + let param_id = self.types.alloc(param); sm.insert(param_id, Either::Right(type_param.clone())); let type_ref = TypeRef::Path(name.into()); @@ -173,13 +173,13 @@ impl GenericParams { self.where_predicates.push(WherePredicate { type_ref, bound }); } - pub fn find_by_name(&self, name: &Name) -> Option { - self.params.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) + pub fn find_by_name(&self, name: &Name) -> Option { + self.types.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) } } impl HasChildSource for GenericDefId { - type ChildId = LocalGenericParamId; + type ChildId = LocalTypeParamId; type Value = Either; fn child_source(&self, db: &impl DefDatabase) -> InFile { let (_, sm) = GenericParams::new(db, *self); @@ -193,7 +193,7 @@ impl ChildBySource for GenericDefId { let arena_map = self.child_source(db); let arena_map = arena_map.as_ref(); for (local_id, src) in arena_map.value.iter() { - let id = GenericParamId { parent: *self, local_id }; + let id = TypeParamId { parent: *self, local_id }; if let Either::Right(type_param) = src { res[keys::TYPE_PARAM].insert(arena_map.with_value(type_param.clone()), id) } diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index ca5630c80..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; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, GenericParamId, StaticId, StructFieldId, TypeAliasId, + ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -20,7 +20,7 @@ pub const ENUM_VARIANT: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); pub const RECORD_FIELD: Key = Key::new(); -pub const TYPE_PARAM: Key = Key::new(); +pub const TYPE_PARAM: Key = Key::new(); /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are /// 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 6dfb3c03d..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 { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct GenericParamId { +pub struct TypeParamId { pub parent: GenericDefId, - pub local_id: LocalGenericParamId, + pub local_id: LocalTypeParamId, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LocalGenericParamId(RawId); -impl_arena_id!(LocalGenericParamId); +pub struct LocalTypeParamId(RawId); +impl_arena_id!(LocalTypeParamId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ContainerId { 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::{ path::{Path, PathKind}, per_ns::PerNs, AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, - GenericDefId, GenericParamId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, - StaticId, StructId, TraitId, TypeAliasId, + GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, + StructId, TraitId, TypeAliasId, TypeParamId, }; #[derive(Debug, Clone, Default)] @@ -59,7 +59,7 @@ enum Scope { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TypeNs { SelfType(ImplId), - GenericParam(GenericParamId), + GenericParam(TypeParamId), AdtId(AdtId), AdtSelfType(AdtId), // Yup, enum variants are added to the types ns, but any usage of variant as @@ -157,7 +157,7 @@ impl Resolver { if let Some(local_id) = params.find_by_name(first_name) { let idx = if path.segments.len() == 1 { None } else { Some(1) }; return Some(( - TypeNs::GenericParam(GenericParamId { local_id, parent: *def }), + TypeNs::GenericParam(TypeParamId { local_id, parent: *def }), idx, )); } @@ -252,7 +252,7 @@ impl Resolver { Scope::GenericParams { params, def } if n_segments > 1 => { if let Some(local_id) = params.find_by_name(first_name) { - let ty = TypeNs::GenericParam(GenericParamId { local_id, parent: *def }); + let ty = TypeNs::GenericParam(TypeParamId { local_id, parent: *def }); return Some(ResolveValueResult::Partial(ty, 1)); } } @@ -399,7 +399,7 @@ pub enum ScopeDef { PerNs(PerNs), ImplSelfType(ImplId), AdtSelfType(AdtId), - GenericParam(GenericParamId), + GenericParam(TypeParamId), Local(PatId), } @@ -431,10 +431,10 @@ impl Scope { } } Scope::GenericParams { params, def } => { - for (local_id, param) in params.params.iter() { + for (local_id, param) in params.types.iter() { f( param.name.clone(), - ScopeDef::GenericParam(GenericParamId { local_id, parent: *def }), + ScopeDef::GenericParam(TypeParamId { local_id, parent: *def }), ) } } @@ -481,7 +481,7 @@ impl Resolver { fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver { let params = db.generic_params(def); - if params.params.is_empty() { + if params.types.is_empty() { self } else { self.push_scope(Scope::GenericParams { def, params }) -- cgit v1.2.3