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 @@
expr::{Body, BodySourceMap},
ty::InferenceResult,
adt::{EnumVariantId, StructFieldId, VariantDef},
generics::GenericParams,
generics::HasGenericParams,
docs::{Documentation, Docs, docs_from_ast},
ids::{FunctionId, StructId, EnumId, AstItemDef, ConstId, StaticId, TraitId, TypeAliasId},
impl_block::ImplBlock,
@ -299,10 +299,6 @@ pub fn field(&self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
.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 {
db.type_for_def((*self).into(), Namespace::Types)
}
@ -363,10 +359,6 @@ pub fn variant(&self, db: &impl DefDatabase, name: &Name) -> Option<EnumVariant>
.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 {
db.type_for_def((*self).into(), Namespace::Types)
}
@ -537,10 +529,6 @@ pub fn infer(&self, db: &impl HirDatabase) -> Arc<InferenceResult> {
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.
pub fn impl_block(&self, db: &impl DefDatabase) -> Option<ImplBlock> {
let module_impls = db.impls_in_module(self.module(db));
@ -696,10 +684,6 @@ pub fn module(&self, db: &impl DefDatabase) -> Module {
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> {
self.trait_data(db).name().clone()
}
@ -737,10 +721,6 @@ pub fn source(&self, db: &impl DefDatabase) -> (HirFileId, TreeArc<ast::TypeAlia
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 {
self.id.module(db)
}

View File

@ -118,3 +118,16 @@ fn from(c: Container) -> Self {
}
}
}
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 crate::{
Const, TypeAlias, Function, HirFileId,
HirDatabase, DefDatabase,
HirDatabase, DefDatabase, TraitRef,
type_ref::TypeRef,
ids::LocationCtx,
resolve::Resolver,
ty::Ty, generics::GenericParams,
TraitRef, code_model_api::{Module, ModuleSource}
ty::Ty,
generics::HasGenericParams,
code_model_api::{Module, ModuleSource}
};
#[derive(Debug, Default, PartialEq, Eq)]
@ -92,10 +93,6 @@ pub fn items(&self, db: &impl DefDatabase) -> Vec<ImplItem> {
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 {
let r = self.module().resolver(db);
// add generic params, if present

View File

@ -67,6 +67,7 @@ fn from(it: $v) -> $e {
adt::AdtDef,
expr::ExprScopes,
resolve::Resolution,
generics::{GenericParams, GenericParam, HasGenericParams},
source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax},
};

View File

@ -20,9 +20,9 @@
use std::mem;
use ena::unify::{InPlaceUnificationTable, UnifyKey, UnifyValue, NoError};
use ra_arena::map::ArenaMap;
use rustc_hash::FxHashMap;
use ra_arena::map::ArenaMap;
use test_utils::tested_by;
use crate::{
@ -33,15 +33,18 @@
ImplItem,
type_ref::{TypeRef, Mutability},
expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat,Array, self},
generics::GenericParams,
generics::{GenericParams, HasGenericParams},
path::{GenericArgs, GenericArg},
adt::VariantDef,
resolve::{Resolver, Resolution},
nameres::Namespace,
ty::infer::diagnostics::InferenceDiagnostic,
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.
pub fn infer(db: &impl HirDatabase, def: DefWithBody) -> Arc<InferenceResult> {

View File

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

View File

@ -10,10 +10,12 @@
HirDatabase, Module, Crate, Name, Function, Trait,
impl_block::{ImplId, ImplBlock, ImplItem},
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.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]