Extract generic_params method to a HasGenericParams trait

This commit is contained in:
Florian Diebold 2019-04-14 13:07:45 +02:00
parent 4497e1d3ea
commit 8bcbcc454c
7 changed files with 33 additions and 37 deletions

View file

@ -11,7 +11,7 @@ use crate::{
expr::{Body, BodySourceMap}, expr::{Body, BodySourceMap},
ty::InferenceResult, ty::InferenceResult,
adt::{EnumVariantId, StructFieldId, VariantDef}, adt::{EnumVariantId, StructFieldId, VariantDef},
generics::GenericParams, generics::HasGenericParams,
docs::{Documentation, Docs, docs_from_ast}, docs::{Documentation, Docs, docs_from_ast},
ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId}, ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId},
impl_block::ImplBlock, impl_block::ImplBlock,
@ -299,10 +299,6 @@ impl Struct {
.map(|(id, _)| StructField { parent: (*self).into(), id }) .map(|(id, _)| StructField { parent: (*self).into(), id })
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
pub fn ty(&self, db: &impl HirDatabase) -> Ty { pub fn ty(&self, db: &impl HirDatabase) -> Ty {
db.type_for_def((*self).into(), Namespace::Types) db.type_for_def((*self).into(), Namespace::Types)
} }
@ -363,10 +359,6 @@ impl Enum {
.map(|(id, _)| EnumVariant { parent: *self, id }) .map(|(id, _)| EnumVariant { parent: *self, id })
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
pub fn ty(&self, db: &impl HirDatabase) -> Ty { pub fn ty(&self, db: &impl HirDatabase) -> Ty {
db.type_for_def((*self).into(), Namespace::Types) db.type_for_def((*self).into(), Namespace::Types)
} }
@ -537,10 +529,6 @@ impl Function {
db.infer((*self).into()) db.infer((*self).into())
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
/// The containing impl block, if this is a method. /// The containing impl block, if this is a method.
pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> { pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
let module_impls = db.impls_in_module(self.module(db)); let module_impls = db.impls_in_module(self.module(db));
@ -696,10 +684,6 @@ impl Trait {
self.id.module(db) self.id.module(db)
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
pub fn name(self, db: &impl DefDatabase) -> Option<Name> { pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
self.trait_data(db).name().clone() self.trait_data(db).name().clone()
} }
@ -737,10 +721,6 @@ impl TypeAlias {
self.id.source(db) self.id.source(db)
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
pub fn module(&self, db: &impl DefDatabase) -> Module { pub fn module(&self, db: &impl DefDatabase) -> Module {
self.id.module(db) self.id.module(db)
} }

View file

@ -118,3 +118,16 @@ impl From<Container> for GenericDef {
} }
} }
} }
pub trait HasGenericParams {
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
}
impl<T> HasGenericParams for T
where
T: Into<GenericDef>,
{
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params(self.into())
}
}

View file

@ -9,12 +9,13 @@ use ra_syntax::{
use crate::{ use crate::{
Const, TypeAlias, Function, HirFileId, Const, TypeAlias, Function, HirFileId,
HirDatabase, DefDatabase, HirDatabase, DefDatabase, TraitRef,
type_ref::TypeRef, type_ref::TypeRef,
ids::LocationCtx, ids::LocationCtx,
resolve::Resolver, resolve::Resolver,
ty::Ty, generics::GenericParams, ty::Ty,
TraitRef, code_model_api::{Module, ModuleSource} generics::HasGenericParams,
code_model_api::{Module, ModuleSource}
}; };
#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq)]
@ -92,10 +93,6 @@ impl ImplBlock {
db.impls_in_module(self.module).impls[self.impl_id].items().to_vec() db.impls_in_module(self.module).impls[self.impl_id].items().to_vec()
} }
pub fn generic_params(&self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params((*self).into())
}
pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver { pub(crate) fn resolver(&self, db: &impl DefDatabase) -> Resolver {
let r = self.module().resolver(db); let r = self.module().resolver(db);
// add generic params, if present // add generic params, if present

View file

@ -67,6 +67,7 @@ pub use self::{
adt::AdtDef, adt::AdtDef,
expr::ExprScopes, expr::ExprScopes,
resolve::Resolution, resolve::Resolution,
generics::{GenericParams, GenericParam, HasGenericParams},
source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax}, source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax},
}; };

View file

@ -20,9 +20,9 @@ use std::sync::Arc;
use std::mem; use std::mem;
use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError}; use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError};
use ra_arena::map::ArenaMap;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use ra_arena::map::ArenaMap;
use test_utils::tested_by; use test_utils::tested_by;
use crate::{ use crate::{
@ -33,15 +33,18 @@ use crate::{
ImplItem, ImplItem,
type_ref::{TypeRef, Mutability}, type_ref::{TypeRef, Mutability},
expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat,Array, self}, expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat,Array, self},
generics::GenericParams, generics::{GenericParams, HasGenericParams},
path::{GenericArgs, GenericArg}, path::{GenericArgs, GenericArg},
adt::VariantDef, adt::VariantDef,
resolve::{Resolver, Resolution}, resolve::{Resolver, Resolution},
nameres::Namespace, nameres::Namespace,
ty::infer::diagnostics::InferenceDiagnostic,
diagnostics::DiagnosticSink, diagnostics::DiagnosticSink,
}; };
use super::{Ty, TypableDef, Substs, primitive, op, ApplicationTy, TypeCtor, traits::{ Solution, Obligation, Guidance}, CallableDef, TraitRef}; use super::{
Ty, TypableDef, Substs, primitive, op, ApplicationTy, TypeCtor, CallableDef, TraitRef,
traits::{ Solution, Obligation, Guidance},
};
use self::diagnostics::InferenceDiagnostic;
/// The entry point of type inference. /// The entry point of type inference.
pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> { pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {

View file

@ -16,8 +16,8 @@ use crate::{
name::KnownName, name::KnownName,
nameres::Namespace, nameres::Namespace,
resolve::{Resolver, Resolution}, resolve::{Resolver, Resolution},
path::{ PathSegment, GenericArg}, path::{PathSegment, GenericArg},
generics::GenericParams, generics::{GenericParams, HasGenericParams},
adt::VariantDef, Trait adt::VariantDef, Trait
}; };
use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef}; use super::{Ty, primitive, FnSig, Substs, TypeCtor, TraitRef};

View file

@ -10,10 +10,12 @@ use crate::{
HirDatabase, Module, Crate, Name, Function, Trait, HirDatabase, Module, Crate, Name, Function, Trait,
impl_block::{ImplId, ImplBlock, ImplItem}, impl_block::{ImplId, ImplBlock, ImplItem},
ty::{Ty, TypeCtor}, ty::{Ty, TypeCtor},
nameres::CrateModuleId, resolve::Resolver, traits::TraitItem nameres::CrateModuleId,
resolve::Resolver,
traits::TraitItem,
generics::HasGenericParams,
}; };
use super::{ TraitRef, Substs}; use super::{TraitRef, Substs};
/// This is used as a key for indexing impls. /// This is used as a key for indexing impls.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]