From da7056245d9b59a4b3af7266dd271bab58cb6527 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 16 Feb 2019 21:19:24 +0100 Subject: Add generic params to impl blocks --- crates/ra_hir/src/generics.rs | 6 ++++-- crates/ra_hir/src/impl_block.rs | 16 ++++++++++------ crates/ra_syntax/src/ast/generated.rs | 1 + crates/ra_syntax/src/grammar.ron | 2 +- 4 files changed, 16 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index c72360f44..6ae0ead1b 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use ra_syntax::ast::{self, NameOwner, TypeParamsOwner}; -use crate::{db::PersistentHirDatabase, Name, AsName, Function, Struct, Enum, Trait, Type}; +use crate::{db::PersistentHirDatabase, Name, AsName, Function, Struct, Enum, Trait, Type, ImplBlock}; /// Data about a generic parameter (to a function, struct, impl, ...). #[derive(Clone, PartialEq, Eq, Debug)] @@ -30,8 +30,9 @@ pub enum GenericDef { Enum(Enum), Trait(Trait), Type(Type), + ImplBlock(ImplBlock), } -impl_froms!(GenericDef: Function, Struct, Enum, Trait, Type); +impl_froms!(GenericDef: Function, Struct, Enum, Trait, Type, ImplBlock); impl GenericParams { pub(crate) fn generic_params_query( @@ -45,6 +46,7 @@ impl GenericParams { GenericDef::Enum(it) => generics.fill(&*it.source(db).1), GenericDef::Trait(it) => generics.fill(&*it.source(db).1), GenericDef::Type(it) => generics.fill(&*it.source(db).1), + GenericDef::ImplBlock(it) => generics.fill(&*it.source(db).1), } Arc::new(generics) diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 4d8bdf33a..7d862882d 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -13,7 +13,7 @@ use crate::{ type_ref::TypeRef, ids::LocationCtx, resolve::Resolver, - ty::Ty, + ty::Ty, generics::GenericParams }; use crate::code_model_api::{Module, ModuleSource}; @@ -38,7 +38,7 @@ impl ImplSourceMap { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplBlock { module: Module, impl_id: ImplId, @@ -58,7 +58,7 @@ impl ImplBlock { } /// Returns the syntax of the impl block - pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc) { + pub fn source(&self, db: &impl PersistentHirDatabase) -> (HirFileId, TreeArc) { let source_map = db.impls_in_module_source_map(self.module); let (file_id, source) = self.module.definition_source(db); (file_id, source_map.get(&source, self.impl_id)) @@ -72,11 +72,11 @@ impl ImplBlock { self.module } - pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option { + pub fn target_trait_ref(&self, db: &impl PersistentHirDatabase) -> Option { db.impls_in_module(self.module).impls[self.impl_id].target_trait().cloned() } - pub fn target_type(&self, db: &impl HirDatabase) -> TypeRef { + pub fn target_type(&self, db: &impl PersistentHirDatabase) -> TypeRef { db.impls_in_module(self.module).impls[self.impl_id].target_type().clone() } @@ -96,10 +96,14 @@ impl ImplBlock { None } - pub fn items(&self, db: &impl HirDatabase) -> Vec { + pub fn items(&self, db: &impl PersistentHirDatabase) -> Vec { db.impls_in_module(self.module).impls[self.impl_id].items().to_vec() } + pub fn generic_params(&self, db: &impl PersistentHirDatabase) -> Arc { + db.generic_params((*self).into()) + } + pub fn resolver(&self, db: &impl HirDatabase) -> Resolver { let r = self.module().resolver(db); // TODO: add generics diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7c5e8ce5e..aa9da92da 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1352,6 +1352,7 @@ impl ToOwned for ImplBlock { } +impl ast::TypeParamsOwner for ImplBlock {} impl ImplBlock { pub fn item_list(&self) -> Option<&ItemList> { super::child_opt(self) diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 304bc5909..5ec68014b 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -322,7 +322,7 @@ Grammar( ], options: ["TypeRef"] ), - "ImplBlock": (options: ["ItemList"]), + "ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner"]), "ParenType": (options: ["TypeRef"]), "TupleType": ( collections: [["fields", "TypeRef"]] ), -- cgit v1.2.3