2494: Get the right analyzer for impls r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-12-07 19:26:33 +00:00 committed by GitHub
commit 971df306ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 272 additions and 92 deletions

View file

@ -15,9 +15,9 @@ use hir_def::{
per_ns::PerNs, per_ns::PerNs,
resolver::HasResolver, resolver::HasResolver,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, GenericParamId, AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId,
HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId,
Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
}; };
use hir_expand::{ use hir_expand::{
diagnostics::DiagnosticSink, diagnostics::DiagnosticSink,
@ -856,8 +856,19 @@ impl Local {
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GenericParam { pub struct TypeParam {
pub(crate) id: GenericParamId, pub(crate) id: TypeParamId,
}
impl TypeParam {
pub fn name(self, db: &impl HirDatabase) -> Name {
let params = db.generic_params(self.id.parent);
params.types[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)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -1100,7 +1111,7 @@ impl HirDisplay for Type {
pub enum ScopeDef { pub enum ScopeDef {
ModuleDef(ModuleDef), ModuleDef(ModuleDef),
MacroDef(MacroDef), MacroDef(MacroDef),
GenericParam(GenericParam), GenericParam(TypeParam),
ImplSelfType(ImplBlock), ImplSelfType(ImplBlock),
AdtSelfType(Adt), AdtSelfType(Adt),
Local(Local), Local(Local),

View file

@ -10,7 +10,7 @@ use ra_syntax::ast;
use crate::{ use crate::{
db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef,
Module, Static, Struct, StructField, Trait, TypeAlias, Union, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
}; };
pub use hir_expand::InFile; 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))) src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
} }
} }
impl HasSource for TypeParam {
type Ast = Either<ast::TraitDef, ast::TypeParam>;
fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
let child_source = self.id.parent.child_source(db);
child_source.map(|it| it[self.id.local_id].clone())
}
}

View file

@ -1,7 +1,7 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use hir_def::{ use hir_def::{
child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, 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 hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
use ra_syntax::{ use ra_syntax::{
@ -12,7 +12,7 @@ use ra_syntax::{
use crate::{ use crate::{
db::{AstDatabase, DefDatabase, HirDatabase}, db::{AstDatabase, DefDatabase, HirDatabase},
Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, ImplBlock, InFile, Local,
MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, Union, MacroDef, Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
}; };
pub trait FromSource: Sized { pub trait FromSource: Sized {
@ -177,6 +177,23 @@ impl Local {
} }
} }
impl TypeParam {
pub fn from_source(db: &impl HirDatabase, src: InFile<ast::TypeParam>) -> Option<Self> {
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(TypeParam { id })
}
}
impl Module { impl Module {
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);

View file

@ -42,9 +42,9 @@ pub mod from_source;
pub use crate::{ pub use crate::{
code_model::{ code_model::{
src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, src::HasSource, Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency,
DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, GenericParam, DefWithBody, Docs, Enum, EnumVariant, FieldSource, Function, GenericDef, HasAttrs,
HasAttrs, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, ImplBlock, Import, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct,
StructField, Trait, Type, TypeAlias, Union, VariantDef, StructField, Trait, Type, TypeAlias, TypeParam, Union, VariantDef,
}, },
from_source::FromSource, from_source::FromSource,
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},

View file

@ -36,8 +36,8 @@ use crate::{
method_resolution::{self, implements_trait}, method_resolution::{self, implements_trait},
InEnvironment, TraitEnvironment, Ty, InEnvironment, TraitEnvironment, Ty,
}, },
Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam, Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, ImplBlock, Local,
Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
}; };
fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { 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>) -
let src = node.with_value(it); let src = node.with_value(it);
Some(Enum::from_source(db, src)?.id.resolver(db)) 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() { _ => match node.value.kind() {
FN_DEF | CONST_DEF | STATIC_DEF => { FN_DEF | CONST_DEF | STATIC_DEF => {
let def = def_with_body_from_child_node(db, node)?; let def = def_with_body_from_child_node(db, node)?;
@ -108,7 +112,7 @@ pub enum PathResolution {
/// A local binding (only value namespace) /// A local binding (only value namespace)
Local(Local), Local(Local),
/// A generic parameter /// A generic parameter
GenericParam(GenericParam), TypeParam(TypeParam),
SelfType(crate::ImplBlock), SelfType(crate::ImplBlock),
Macro(MacroDef), Macro(MacroDef),
AssocItem(crate::AssocItem), AssocItem(crate::AssocItem),
@ -262,7 +266,7 @@ impl SourceAnalyzer {
) -> Option<PathResolution> { ) -> Option<PathResolution> {
let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), 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) => { TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
PathResolution::Def(Adt::from(it).into()) PathResolution::Def(Adt::from(it).into())
} }
@ -334,7 +338,7 @@ impl SourceAnalyzer {
resolver::ScopeDef::PerNs(it) => it.into(), resolver::ScopeDef::PerNs(it) => it.into(),
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(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) => { resolver::ScopeDef::Local(pat_id) => {
let parent = self.resolver.body_owner().unwrap().into(); let parent = self.resolver.body_owner().unwrap().into();
ScopeDef::Local(Local { parent, pat_id }) ScopeDef::Local(Local { parent, pat_id })

View file

@ -4,20 +4,29 @@
//! in rustc. //! in rustc.
use std::sync::Arc; use std::sync::Arc;
use hir_expand::name::{self, AsName, Name}; use either::Either;
use ra_arena::Arena; 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 ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner};
use crate::{ use crate::{
child_by_source::ChildBySource,
db::DefDatabase, db::DefDatabase,
dyn_map::DynMap,
keys,
src::HasChildSource,
src::HasSource, src::HasSource,
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
AdtId, AstItemDef, GenericDefId, LocalGenericParamId, Lookup, AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
}; };
/// Data about a generic parameter (to a function, struct, impl, ...). /// Data about a generic parameter (to a function, struct, impl, ...).
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct GenericParamData { pub struct TypeParamData {
pub name: Name, pub name: Name,
pub default: Option<TypeRef>, pub default: Option<TypeRef>,
} }
@ -25,7 +34,8 @@ pub struct GenericParamData {
/// Data about the generic parameters of a function, struct, impl, etc. /// Data about the generic parameters of a function, struct, impl, etc.
#[derive(Clone, PartialEq, Eq, Debug)] #[derive(Clone, PartialEq, Eq, Debug)]
pub struct GenericParams { pub struct GenericParams {
pub params: Arena<LocalGenericParamId, GenericParamData>, pub types: Arena<LocalTypeParamId, TypeParamData>,
// lifetimes: Arena<LocalLifetimeParamId, LifetimeParamData>,
pub where_predicates: Vec<WherePredicate>, pub where_predicates: Vec<WherePredicate>,
} }
@ -39,52 +49,87 @@ pub struct WherePredicate {
pub bound: TypeBound, pub bound: TypeBound,
} }
type SourceMap = ArenaMap<LocalTypeParamId, Either<ast::TraitDef, ast::TypeParam>>;
impl GenericParams { impl GenericParams {
pub(crate) fn generic_params_query( pub(crate) fn generic_params_query(
db: &impl DefDatabase, db: &impl DefDatabase,
def: GenericDefId, def: GenericDefId,
) -> Arc<GenericParams> { ) -> Arc<GenericParams> {
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<SourceMap>) {
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 // FIXME: add `: Sized` bound for everything except for `Self` in traits
match def { let file_id = match def {
GenericDefId::FunctionId(it) => generics.fill(&it.lookup(db).source(db).value), GenericDefId::FunctionId(it) => {
GenericDefId::AdtId(AdtId::StructId(it)) => generics.fill(&it.source(db).value), let src = it.lookup(db).source(db);
GenericDefId::AdtId(AdtId::UnionId(it)) => generics.fill(&it.source(db).value), generics.fill(&mut sm, &src.value);
GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).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) => { GenericDefId::TraitId(it) => {
let src = it.source(db);
// traits get the Self type as an implicit first type parameter // 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.fill(&it.source(db).value); 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 // add super traits as bounds on Self
// i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
let self_param = TypeRef::Path(name::SELF_TYPE.into()); 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(&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 // 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-parameter, but rather is a type-alias for impl's target
// type, so this is handled by the resolver. // type, so this is handled by the resolver.
GenericDefId::ImplId(it) => generics.fill(&it.source(db).value), GenericDefId::ImplId(it) => {
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {} 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: &impl TypeParamsOwner) { fn fill(&mut self, sm: &mut SourceMap, node: &dyn TypeParamsOwner) {
if let Some(params) = node.type_param_list() { 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() { if let Some(where_clause) = node.where_clause() {
self.fill_where_predicates(where_clause); self.fill_where_predicates(where_clause);
} }
} }
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 for bound in
node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds()) node.type_bound_list().iter().flat_map(|type_bound_list| type_bound_list.bounds())
{ {
@ -92,13 +137,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() { for type_param in params.type_params() {
let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
// FIXME: Use `Path::from_src` // FIXME: Use `Path::from_src`
let default = type_param.default_type().map(TypeRef::from_ast); let default = type_param.default_type().map(TypeRef::from_ast);
let param = GenericParamData { name: name.clone(), default }; let param = TypeParamData { name: name.clone(), default };
self.params.alloc(param); let param_id = self.types.alloc(param);
sm.insert(param_id, Either::Right(type_param.clone()));
let type_ref = TypeRef::Path(name.into()); let type_ref = TypeRef::Path(name.into());
self.fill_bounds(&type_param, type_ref); self.fill_bounds(&type_param, type_ref);
@ -127,7 +173,31 @@ impl GenericParams {
self.where_predicates.push(WherePredicate { type_ref, bound }); self.where_predicates.push(WherePredicate { type_ref, bound });
} }
pub fn find_by_name(&self, name: &Name) -> Option<LocalGenericParamId> { pub fn find_by_name(&self, name: &Name) -> Option<LocalTypeParamId> {
self.params.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None }) self.types.iter().find_map(|(id, p)| if &p.name == name { Some(id) } else { None })
}
}
impl HasChildSource for GenericDefId {
type ChildId = LocalTypeParamId;
type Value = Either<ast::TraitDef, ast::TypeParam>;
fn child_source(&self, db: &impl DefDatabase) -> InFile<SourceMap> {
let (_, sm) = GenericParams::new(db, *self);
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 = 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)
}
}
res
} }
} }

View file

@ -8,7 +8,7 @@ use rustc_hash::FxHashMap;
use crate::{ use crate::{
dyn_map::{DynMap, Policy}, dyn_map::{DynMap, Policy},
ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId,
}; };
type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; 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();
pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new();
pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
/// equal if they point to exactly the same object. /// equal if they point to exactly the same object.

View file

@ -318,14 +318,14 @@ macro_rules! impl_froms {
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GenericParamId { pub struct TypeParamId {
pub parent: GenericDefId, pub parent: GenericDefId,
pub local_id: LocalGenericParamId, pub local_id: LocalTypeParamId,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LocalGenericParamId(RawId); pub struct LocalTypeParamId(RawId);
impl_arena_id!(LocalGenericParamId); impl_arena_id!(LocalTypeParamId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ContainerId { pub enum ContainerId {
@ -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 { impl HasModule for StaticLoc {
fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { fn module(&self, _db: &impl db::DefDatabase) -> ModuleId {
self.container self.container

View file

@ -18,8 +18,8 @@ use crate::{
path::{Path, PathKind}, path::{Path, PathKind},
per_ns::PerNs, per_ns::PerNs,
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
GenericDefId, GenericParamId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId,
StaticId, StructId, TraitId, TypeAliasId, StructId, TraitId, TypeAliasId, TypeParamId,
}; };
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -59,7 +59,7 @@ enum Scope {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TypeNs { pub enum TypeNs {
SelfType(ImplId), SelfType(ImplId),
GenericParam(GenericParamId), GenericParam(TypeParamId),
AdtId(AdtId), AdtId(AdtId),
AdtSelfType(AdtId), AdtSelfType(AdtId),
// Yup, enum variants are added to the types ns, but any usage of variant as // 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) { if let Some(local_id) = params.find_by_name(first_name) {
let idx = if path.segments.len() == 1 { None } else { Some(1) }; let idx = if path.segments.len() == 1 { None } else { Some(1) };
return Some(( return Some((
TypeNs::GenericParam(GenericParamId { local_id, parent: *def }), TypeNs::GenericParam(TypeParamId { local_id, parent: *def }),
idx, idx,
)); ));
} }
@ -252,7 +252,7 @@ impl Resolver {
Scope::GenericParams { params, def } if n_segments > 1 => { Scope::GenericParams { params, def } if n_segments > 1 => {
if let Some(local_id) = params.find_by_name(first_name) { 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)); return Some(ResolveValueResult::Partial(ty, 1));
} }
} }
@ -399,7 +399,7 @@ pub enum ScopeDef {
PerNs(PerNs), PerNs(PerNs),
ImplSelfType(ImplId), ImplSelfType(ImplId),
AdtSelfType(AdtId), AdtSelfType(AdtId),
GenericParam(GenericParamId), GenericParam(TypeParamId),
Local(PatId), Local(PatId),
} }
@ -431,10 +431,10 @@ impl Scope {
} }
} }
Scope::GenericParams { params, def } => { Scope::GenericParams { params, def } => {
for (local_id, param) in params.params.iter() { for (local_id, param) in params.types.iter() {
f( f(
param.name.clone(), 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 { fn push_generic_params_scope(self, db: &impl DefDatabase, def: GenericDefId) -> Resolver {
let params = db.generic_params(def); let params = db.generic_params(def);
if params.params.is_empty() { if params.types.is_empty() {
self self
} else { } else {
self.push_scope(Scope::GenericParams { def, params }) self.push_scope(Scope::GenericParams { def, params })

View file

@ -5,10 +5,10 @@ use std::sync::Arc;
use hir_def::{ use hir_def::{
adt::VariantData, adt::VariantData,
db::DefDatabase, db::DefDatabase,
generics::{GenericParamData, GenericParams}, generics::{GenericParams, TypeParamData},
resolver::{HasResolver, TypeNs}, resolver::{HasResolver, TypeNs},
type_ref::TypeRef, type_ref::TypeRef,
ContainerId, GenericDefId, GenericParamId, Lookup, TraitId, TypeAliasId, VariantId, ContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId,
}; };
use hir_expand::name::{self, Name}; use hir_expand::name::{self, Name};
@ -96,23 +96,21 @@ pub(crate) struct Generics {
} }
impl Generics { impl Generics {
pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a {
self.parent_generics self.parent_generics
.as_ref() .as_ref()
.into_iter() .into_iter()
.flat_map(|it| it.params.params.iter()) .flat_map(|it| it.params.types.iter())
.chain(self.params.params.iter()) .chain(self.params.types.iter())
.enumerate() .enumerate()
.map(|(i, (_local_id, p))| (i as u32, p)) .map(|(i, (_local_id, p))| (i as u32, p))
} }
pub(crate) fn iter_parent<'a>( pub(crate) fn iter_parent<'a>(&'a self) -> impl Iterator<Item = (u32, &'a TypeParamData)> + 'a {
&'a self,
) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a {
self.parent_generics self.parent_generics
.as_ref() .as_ref()
.into_iter() .into_iter()
.flat_map(|it| it.params.params.iter()) .flat_map(|it| it.params.types.iter())
.enumerate() .enumerate()
.map(|(i, (_local_id, p))| (i as u32, p)) .map(|(i, (_local_id, p))| (i as u32, p))
} }
@ -123,20 +121,20 @@ impl Generics {
/// (total, parents, child) /// (total, parents, child)
pub(crate) fn len_split(&self) -> (usize, usize, usize) { pub(crate) fn len_split(&self) -> (usize, usize, usize) {
let parent = self.parent_generics.as_ref().map_or(0, |p| p.len()); let parent = self.parent_generics.as_ref().map_or(0, |p| p.len());
let child = self.params.params.len(); let child = self.params.types.len();
(parent + child, parent, child) (parent + child, parent, child)
} }
pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { pub(crate) fn param_idx(&self, param: TypeParamId) -> u32 {
self.find_param(param).0 self.find_param(param).0
} }
pub(crate) fn param_name(&self, param: GenericParamId) -> Name { pub(crate) fn param_name(&self, param: TypeParamId) -> Name {
self.find_param(param).1.name.clone() self.find_param(param).1.name.clone()
} }
fn find_param(&self, param: GenericParamId) -> (u32, &GenericParamData) { fn find_param(&self, param: TypeParamId) -> (u32, &TypeParamData) {
if param.parent == self.def { if param.parent == self.def {
let (idx, (_local_id, data)) = self let (idx, (_local_id, data)) = self
.params .params
.params .types
.iter() .iter()
.enumerate() .enumerate()
.find(|(_, (idx, _))| *idx == param.local_id) .find(|(_, (idx, _))| *idx == param.local_id)

View file

@ -6,7 +6,7 @@ use ra_db::{FileId, SourceDatabase};
use ra_syntax::{ use ra_syntax::{
ast::{self, DocCommentsOwner, NameOwner}, ast::{self, DocCommentsOwner, NameOwner},
match_ast, AstNode, SmolStr, match_ast, AstNode, SmolStr,
SyntaxKind::{self, BIND_PAT}, SyntaxKind::{self, BIND_PAT, TYPE_PARAM},
TextRange, TextRange,
}; };
@ -351,6 +351,26 @@ impl ToNav for hir::Local {
} }
} }
impl ToNav for hir::TypeParam {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let range = match src.value {
Either::Left(it) => it.syntax().text_range(),
Either::Right(it) => it.syntax().text_range(),
};
NavigationTarget {
file_id: src.file_id.original_file(db),
name: self.name(db).to_string().into(),
kind: TYPE_PARAM,
full_range: range,
focus_range: None,
container_name: None,
description: None,
docs: None,
}
}
}
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
let parse = db.parse(symbol.file_id); let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(parse.tree().syntax()); let node = symbol.ptr.to_node(parse.tree().syntax());

View file

@ -64,9 +64,11 @@ pub(crate) fn reference_definition(
let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind); let name_kind = classify_name_ref(db, name_ref).map(|d| d.kind);
match name_kind { match name_kind {
Some(Macro(mac)) => return Exact(mac.to_nav(db)), Some(Macro(it)) => return Exact(it.to_nav(db)),
Some(Field(field)) => return Exact(field.to_nav(db)), Some(Field(it)) => return Exact(it.to_nav(db)),
Some(AssocItem(assoc)) => return Exact(assoc.to_nav(db)), Some(TypeParam(it)) => return Exact(it.to_nav(db)),
Some(AssocItem(it)) => return Exact(it.to_nav(db)),
Some(Local(it)) => return Exact(it.to_nav(db)),
Some(Def(def)) => match NavigationTarget::from_def(db, def) { Some(Def(def)) => match NavigationTarget::from_def(db, def) {
Some(nav) => return Exact(nav), Some(nav) => return Exact(nav),
None => return Approximate(vec![]), None => return Approximate(vec![]),
@ -77,10 +79,6 @@ pub(crate) fn reference_definition(
// us to the actual type // us to the actual type
return Exact(imp.to_nav(db)); return Exact(imp.to_nav(db));
} }
Some(Local(local)) => return Exact(local.to_nav(db)),
Some(GenericParam(_)) => {
// FIXME: go to the generic param def
}
None => {} None => {}
}; };
@ -723,4 +721,17 @@ mod tests {
"foo FN_DEF FileId(1) [359; 376) [362; 365)", "foo FN_DEF FileId(1) [359; 376) [362; 365)",
); );
} }
#[test]
fn goto_for_type_param() {
check_goto(
"
//- /lib.rs
struct Foo<T> {
t: <|>T,
}
",
"T TYPE_PARAM FileId(1) [11; 12)",
);
}
} }

View file

@ -138,7 +138,7 @@ fn hover_text_from_name_kind(
*no_fallback = true; *no_fallback = true;
None None
} }
GenericParam(_) | SelfType(_) => { TypeParam(_) | SelfType(_) => {
// FIXME: Hover for generic param // FIXME: Hover for generic param
None None
} }

View file

@ -85,7 +85,7 @@ pub(crate) fn find_all_refs(
NameKind::Def(def) => NavigationTarget::from_def(db, def)?, NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
NameKind::SelfType(imp) => imp.to_nav(db), NameKind::SelfType(imp) => imp.to_nav(db),
NameKind::Local(local) => local.to_nav(db), NameKind::Local(local) => local.to_nav(db),
NameKind::GenericParam(_) => return None, NameKind::TypeParam(_) => return None,
}; };
let search_scope = { let search_scope = {

View file

@ -110,6 +110,15 @@ pub(crate) fn classify_name(db: &RootDatabase, name: InFile<&ast::Name>) -> Opti
kind: NameKind::Macro(def), kind: NameKind::Macro(def),
}) })
}, },
ast::TypeParam(it) => {
let src = name.with_value(it);
let def = hir::TypeParam::from_source(db, src)?;
Some(NameDefinition {
visibility: None,
container: def.module(db),
kind: NameKind::TypeParam(def),
})
},
_ => None, _ => None,
} }
} }
@ -168,9 +177,8 @@ pub(crate) fn classify_name_ref(
let kind = NameKind::Local(local); let kind = NameKind::Local(local);
Some(NameDefinition { kind, container, visibility: None }) Some(NameDefinition { kind, container, visibility: None })
} }
PathResolution::GenericParam(par) => { PathResolution::TypeParam(par) => {
// FIXME: get generic param def let kind = NameKind::TypeParam(par);
let kind = NameKind::GenericParam(par);
Some(NameDefinition { kind, container, visibility }) Some(NameDefinition { kind, container, visibility })
} }
PathResolution::Macro(def) => { PathResolution::Macro(def) => {

View file

@ -4,8 +4,8 @@
//! Note that the reference search is possible for not all of the classified items. //! Note that the reference search is possible for not all of the classified items.
use hir::{ use hir::{
Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, Adt, AssocItem, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, StructField,
StructField, VariantDef, TypeParam, VariantDef,
}; };
use ra_syntax::{ast, ast::VisibilityOwner}; use ra_syntax::{ast, ast::VisibilityOwner};
@ -19,7 +19,7 @@ pub enum NameKind {
Def(ModuleDef), Def(ModuleDef),
SelfType(ImplBlock), SelfType(ImplBlock),
Local(Local), Local(Local),
GenericParam(GenericParam), TypeParam(TypeParam),
} }
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]

View file

@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.parameter { color: #94BFF3; } .parameter { color: #94BFF3; }
.builtin { color: #DD6718; } .builtin { color: #DD6718; }
.text { color: #DCDCCC; } .text { color: #DCDCCC; }
.type { color: #7CB8BB; }
.attribute { color: #94BFF3; } .attribute { color: #94BFF3; }
.literal { color: #BFEBBF; } .literal { color: #BFEBBF; }
.macro { color: #94BFF3; } .macro { color: #94BFF3; }
@ -45,4 +46,12 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="keyword">let</span> <span class="variable">z</span> = &<span class="variable.mut">y</span>; <span class="keyword">let</span> <span class="variable">z</span> = &<span class="variable.mut">y</span>;
<span class="variable.mut">y</span>; <span class="variable.mut">y</span>;
}
<span class="keyword">enum</span> <span class="type">E</span>&lt;<span class="type">X</span>&gt; {
<span class="constant">V</span>(<span class="type">X</span>)
}
<span class="keyword">impl</span>&lt;<span class="type">X</span>&gt; <span class="type">E</span>&lt;<span class="type">X</span>&gt; {
<span class="keyword">fn</span> <span class="function">new</span>&lt;<span class="type">T</span>&gt;() -&gt; <span class="type">E</span>&lt;<span class="type">T</span>&gt; {}
}</code></pre> }</code></pre>

View file

@ -9,6 +9,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.parameter { color: #94BFF3; } .parameter { color: #94BFF3; }
.builtin { color: #DD6718; } .builtin { color: #DD6718; }
.text { color: #DCDCCC; } .text { color: #DCDCCC; }
.type { color: #7CB8BB; }
.attribute { color: #94BFF3; } .attribute { color: #94BFF3; }
.literal { color: #BFEBBF; } .literal { color: #BFEBBF; }
.macro { color: #94BFF3; } .macro { color: #94BFF3; }

View file

@ -225,8 +225,7 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
Def(hir::ModuleDef::Trait(_)) => "type", Def(hir::ModuleDef::Trait(_)) => "type",
Def(hir::ModuleDef::TypeAlias(_)) => "type", Def(hir::ModuleDef::TypeAlias(_)) => "type",
Def(hir::ModuleDef::BuiltinType(_)) => "type", Def(hir::ModuleDef::BuiltinType(_)) => "type",
SelfType(_) => "type", SelfType(_) | TypeParam(_) => "type",
GenericParam(_) => "type",
Local(local) => { Local(local) => {
if local.is_mut(db) { if local.is_mut(db) {
"variable.mut" "variable.mut"
@ -255,6 +254,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
.parameter { color: #94BFF3; } .parameter { color: #94BFF3; }
.builtin { color: #DD6718; } .builtin { color: #DD6718; }
.text { color: #DCDCCC; } .text { color: #DCDCCC; }
.type { color: #7CB8BB; }
.attribute { color: #94BFF3; } .attribute { color: #94BFF3; }
.literal { color: #BFEBBF; } .literal { color: #BFEBBF; }
.macro { color: #94BFF3; } .macro { color: #94BFF3; }
@ -303,6 +303,14 @@ fn main() {
y; y;
} }
enum E<X> {
V(X)
}
impl<X> E<X> {
fn new<T>() -> E<T> {}
}
"# "#
.trim(), .trim(),
); );