From 8bcbcc454cbb48b897083c122566c0b4c2b780aa Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 14 Apr 2019 13:07:45 +0200 Subject: Extract generic_params method to a HasGenericParams trait --- crates/ra_hir/src/code_model_api.rs | 22 +--------------------- crates/ra_hir/src/generics.rs | 13 +++++++++++++ crates/ra_hir/src/impl_block.rs | 11 ++++------- crates/ra_hir/src/lib.rs | 1 + crates/ra_hir/src/ty/infer.rs | 11 +++++++---- crates/ra_hir/src/ty/lower.rs | 4 ++-- crates/ra_hir/src/ty/method_resolution.rs | 8 +++++--- 7 files changed, 33 insertions(+), 37 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 28de9e76a..8f1ed1086 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -11,7 +11,7 @@ use crate::{ 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 @@ impl Struct { .map(|(id, _)| StructField { parent: (*self).into(), id }) } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - 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 @@ impl Enum { .map(|(id, _)| EnumVariant { parent: *self, id }) } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - 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 @@ impl Function { db.infer((*self).into()) } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - db.generic_params((*self).into()) - } - /// The containing impl block, if this is a method. pub fn impl_block(&self, db: &impl DefDatabase) -> Option { let module_impls = db.impls_in_module(self.module(db)); @@ -696,10 +684,6 @@ impl Trait { self.id.module(db) } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - db.generic_params((*self).into()) - } - pub fn name(self, db: &impl DefDatabase) -> Option { self.trait_data(db).name().clone() } @@ -737,10 +721,6 @@ impl TypeAlias { self.id.source(db) } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - db.generic_params((*self).into()) - } - pub fn module(&self, db: &impl DefDatabase) -> Module { self.id.module(db) } diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index f92b146ef..5625c2459 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -118,3 +118,16 @@ impl From for GenericDef { } } } + +pub trait HasGenericParams { + fn generic_params(self, db: &impl DefDatabase) -> Arc; +} + +impl HasGenericParams for T +where + T: Into, +{ + fn generic_params(self, db: &impl DefDatabase) -> Arc { + db.generic_params(self.into()) + } +} diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 71486aa2d..a8a466e43 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -9,12 +9,13 @@ use ra_syntax::{ 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 @@ impl ImplBlock { db.impls_in_module(self.module).impls[self.impl_id].items().to_vec() } - pub fn generic_params(&self, db: &impl DefDatabase) -> Arc { - 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 diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 24e08f8cc..9292de1b5 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -67,6 +67,7 @@ pub use self::{ adt::AdtDef, expr::ExprScopes, resolve::Resolution, + generics::{GenericParams, GenericParam, HasGenericParams}, source_binder::{SourceAnalyzer, PathResolution, ScopeEntryWithSyntax}, }; diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 28459d750..651a78fe5 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -20,9 +20,9 @@ use std::sync::Arc; 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 @@ use crate::{ 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 { diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index bb8fdd8c7..7fac084ce 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -16,8 +16,8 @@ use crate::{ 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}; diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 126edeaff..6b7918187 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -10,10 +10,12 @@ use crate::{ 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)] -- cgit v1.2.3