From 5f39c5794ec2521e9bb4c108771a1644269859ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 Nov 2019 15:41:53 +0300 Subject: Use GenericDefIdMore --- crates/ra_hir/src/code_model.rs | 18 +++---------- crates/ra_hir/src/db.rs | 6 ++--- crates/ra_hir/src/from_id.rs | 24 ++++++++---------- crates/ra_hir/src/source_binder.rs | 4 +-- crates/ra_hir/src/ty.rs | 8 +++--- crates/ra_hir/src/ty/infer/path.rs | 2 +- crates/ra_hir/src/ty/lower.rs | 32 +++++++++++++----------- crates/ra_hir/src/ty/method_resolution.rs | 2 +- crates/ra_hir/src/ty/traits/chalk.rs | 2 +- crates/ra_ide_api/src/completion/presentation.rs | 2 +- 10 files changed, 44 insertions(+), 56 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 3f44a50c4..534f1f8e9 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,9 +11,9 @@ use hir_def::{ per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - AstItemDef, ConstId, ContainerId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, - LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, - TraitId, TypeAliasId, UnionId, + AstItemDef, ConstId, ContainerId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, + LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, + StaticId, StructId, TraitId, TypeAliasId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -897,16 +897,6 @@ impl_froms!( Const ); -impl From for GenericDef { - fn from(item: AssocItem) -> Self { - match item { - AssocItem::Function(f) => f.into(), - AssocItem::Const(c) => c.into(), - AssocItem::TypeAlias(t) => t.into(), - } - } -} - #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct Local { pub(crate) parent: DefWithBody, @@ -960,7 +950,7 @@ impl Local { #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct GenericParam { - pub(crate) parent: GenericDef, + pub(crate) parent: GenericDefId, pub(crate) idx: u32, } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 7ec04ad73..b034d4e44 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -12,7 +12,7 @@ use crate::{ CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef, TypeCtor, }, - Crate, DefWithBody, GenericDef, ImplBlock, Trait, + Crate, DefWithBody, ImplBlock, Trait, }; pub use hir_def::{ @@ -49,7 +49,7 @@ pub trait HirDatabase: DefDatabase { #[salsa::invoke(crate::ty::generic_predicates_for_param_query)] fn generic_predicates_for_param( &self, - def: GenericDef, + def: GenericDefId, param_idx: u32, ) -> Arc<[GenericPredicate]>; @@ -57,7 +57,7 @@ pub trait HirDatabase: DefDatabase { fn generic_predicates(&self, def: GenericDefId) -> Arc<[GenericPredicate]>; #[salsa::invoke(crate::ty::generic_defaults_query)] - fn generic_defaults(&self, def: GenericDef) -> Substs; + fn generic_defaults(&self, def: GenericDefId) -> Substs; #[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)] fn impls_in_crate(&self, krate: Crate) -> Arc; diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index 3a27d6f0c..619f6055e 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -137,20 +137,6 @@ impl From for GenericDefId { } } -impl From for GenericDef { - fn from(def: GenericDefId) -> Self { - match def { - GenericDefId::FunctionId(it) => GenericDef::Function(it.into()), - GenericDefId::AdtId(it) => GenericDef::Adt(it.into()), - GenericDefId::TraitId(it) => GenericDef::Trait(it.into()), - GenericDefId::TypeAliasId(it) => GenericDef::TypeAlias(it.into()), - GenericDefId::ImplId(it) => GenericDef::ImplBlock(it.into()), - GenericDefId::EnumVariantId(it) => GenericDef::EnumVariant(it.into()), - GenericDefId::ConstId(it) => GenericDef::Const(it.into()), - } - } -} - impl From for TypableDef { fn from(id: AdtId) -> Self { Adt::from(id).into() @@ -244,3 +230,13 @@ impl From for AttrDefId { } } } + +impl From for GenericDefId { + fn from(item: AssocItem) -> Self { + match item { + AssocItem::Function(f) => f.id.into(), + AssocItem::Const(c) => c.id.into(), + AssocItem::TypeAlias(t) => t.id.into(), + } + } +} diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index b4f0e81d3..cbfeca3ab 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -251,7 +251,7 @@ impl SourceAnalyzer { let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty { TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam { - parent: self.resolver.generic_def().unwrap().into(), + parent: self.resolver.generic_def().unwrap(), idx, }), TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { @@ -326,7 +326,7 @@ impl SourceAnalyzer { resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), resolver::ScopeDef::GenericParam(idx) => { - let parent = self.resolver.generic_def().unwrap().into(); + let parent = self.resolver.generic_def().unwrap(); ScopeDef::GenericParam(GenericParam { parent, idx }) } resolver::ScopeDef::Local(pat_id) => { diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 2473ac574..1e05ac3f2 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -21,8 +21,8 @@ use hir_def::{generics::GenericParams, AdtId, GenericDefId}; use ra_db::{impl_intern_key, salsa}; use crate::{ - db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy, - GenericDef, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain, + db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy, IntTy, + Mutability, Name, Trait, TypeAlias, Uncertain, }; use display::{HirDisplay, HirFormatter}; @@ -356,9 +356,9 @@ impl Substs { ) } - pub fn build_for_def(db: &impl HirDatabase, def: impl Into) -> SubstsBuilder { + pub fn build_for_def(db: &impl HirDatabase, def: impl Into) -> SubstsBuilder { let def = def.into(); - let params = db.generic_params(def.into()); + let params = db.generic_params(def); let param_count = params.count_params_including_parent(); Substs::builder(param_count) } diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index ee54d8217..6165eba4f 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -203,7 +203,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()), Container::Trait(t) => { // we're picking this method - let trait_substs = Substs::build_for_def(self.db, t) + let trait_substs = Substs::build_for_def(self.db, t.id) .push(ty.clone()) .fill(std::iter::repeat_with(|| self.new_type_var())) .build(); diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index da3c8e94a..1ceafd9b1 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -29,8 +29,8 @@ use crate::{ Adt, }, util::make_mut_slice, - Const, Enum, EnumVariant, Function, GenericDef, ImplBlock, ModuleDef, Path, Static, Struct, - Trait, TypeAlias, Union, + Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, Trait, + TypeAlias, Union, }; // FIXME: this is only really used in `type_for_def`, which contains a bunch of @@ -261,8 +261,10 @@ impl Ty { let traits = traits_from_env.flat_map(|t| t.all_super_traits(db)); for t in traits { if let Some(associated_ty) = t.associated_type_by_name(db, &segment.name) { - let substs = - Substs::build_for_def(db, t).push(self_ty.clone()).fill_with_unknown().build(); + let substs = Substs::build_for_def(db, t.id) + .push(self_ty.clone()) + .fill_with_unknown() + .build(); // FIXME handle type parameters on the segment return Ty::Projection(ProjectionTy { associated_ty, parameters: substs }); } @@ -287,11 +289,11 @@ impl Ty { segment: &PathSegment, resolved: TypableDef, ) -> Substs { - let def_generic: Option = match resolved { - TypableDef::Function(func) => Some(func.into()), + let def_generic: Option = match resolved { + TypableDef::Function(func) => Some(func.id.into()), TypableDef::Adt(adt) => Some(adt.into()), - TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), - TypableDef::TypeAlias(t) => Some(t.into()), + TypableDef::EnumVariant(var) => Some(var.parent_enum(db).id.into()), + TypableDef::TypeAlias(t) => Some(t.id.into()), TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None, }; substs_from_path_segment(db, resolver, segment, def_generic, false) @@ -338,7 +340,7 @@ pub(super) fn substs_from_path_segment( db: &impl HirDatabase, resolver: &Resolver, segment: &PathSegment, - def_generic: Option, + def_generic: Option, add_self_param: bool, ) -> Substs { let mut substs = Vec::new(); @@ -376,7 +378,7 @@ pub(super) fn substs_from_path_segment( // handle defaults if let Some(def_generic) = def_generic { - let default_substs = db.generic_defaults(def_generic); + let default_substs = db.generic_defaults(def_generic.into()); assert_eq!(substs.len(), default_substs.len()); for (i, default_ty) in default_substs.iter().enumerate() { @@ -439,7 +441,7 @@ impl TraitRef { ) -> Substs { let has_self_param = segment.args_and_bindings.as_ref().map(|a| a.has_self_type).unwrap_or(false); - substs_from_path_segment(db, resolver, segment, Some(resolved.into()), !has_self_param) + substs_from_path_segment(db, resolver, segment, Some(resolved.id.into()), !has_self_param) } pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef { @@ -579,10 +581,10 @@ pub(crate) fn field_types_query( /// these are fine: `T: Foo, U: Foo<()>`. pub(crate) fn generic_predicates_for_param_query( db: &impl HirDatabase, - def: GenericDef, + def: GenericDefId, param_idx: u32, ) -> Arc<[GenericPredicate]> { - let resolver = GenericDefId::from(def).resolver(db); + let resolver = def.resolver(db); resolver .where_predicates_in_scope() // we have to filter out all other predicates *first*, before attempting to lower them @@ -615,8 +617,8 @@ pub(crate) fn generic_predicates_query( } /// Resolve the default type params from generics -pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs { - let resolver = GenericDefId::from(def).resolver(db); +pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs { + let resolver = def.resolver(db); let generic_params = db.generic_params(def.into()); let defaults = generic_params diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index f7905b5ff..c5ab690eb 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -365,7 +365,7 @@ fn generic_implements_goal( self_ty: Canonical, ) -> Canonical> { let num_vars = self_ty.num_vars; - let substs = super::Substs::build_for_def(db, trait_) + let substs = super::Substs::build_for_def(db, trait_.id) .push(self_ty.value) .fill_with_bound_vars(num_vars as u32) .build(); diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index a0dbf6305..0272dd9ae 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -736,7 +736,7 @@ fn closure_fn_trait_impl_datum( let trait_ref = TraitRef { trait_, - substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(), + substs: Substs::build_for_def(db, trait_.id).push(self_ty).push(arg_ty).build(), }; let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db); diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index bac3f7582..85b053a6e 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -292,7 +292,7 @@ fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool { } fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool { - let subst = db.generic_defaults(def); + let subst = db.generic_defaults(def.into()); subst.iter().any(|ty| ty == &Ty::Unknown) } -- cgit v1.2.3