From 30fefcc08cc0c670ce541476491238d258ca55c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 6 Dec 2019 17:35:05 +0100 Subject: Store GenericParams in arena --- crates/ra_hir_ty/src/infer/expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_ty') diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index b8df27706..0c3428999 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -662,7 +662,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { let mut substs = Vec::with_capacity(parent_param_count + param_count); // Parent arguments are unknown, except for the receiver type if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) { - for param in &parent_generics.params { + for (_id, param) in parent_generics.params.iter() { if param.name == name::SELF_TYPE { substs.push(receiver_ty.clone()); } else { -- cgit v1.2.3 From 8e9837df21942ca12a5aece0a868ea46eb405742 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 11:50:36 +0100 Subject: Remove idx and parent generics from generics This makes `hir_def::GenericParams` flatter. The logic for re-numbering the params is moved to hir instead. --- crates/ra_hir_ty/src/autoderef.rs | 8 ++-- crates/ra_hir_ty/src/infer/expr.rs | 24 ++++++----- crates/ra_hir_ty/src/lib.rs | 32 +++++--------- crates/ra_hir_ty/src/lower.rs | 35 ++++++++------- crates/ra_hir_ty/src/traits/chalk.rs | 14 +++--- crates/ra_hir_ty/src/utils.rs | 82 +++++++++++++++++++++++++++++++++++- 6 files changed, 135 insertions(+), 60 deletions(-) (limited to 'crates/ra_hir_ty') diff --git a/crates/ra_hir_ty/src/autoderef.rs b/crates/ra_hir_ty/src/autoderef.rs index 9d1d4e48c..a6db7f623 100644 --- a/crates/ra_hir_ty/src/autoderef.rs +++ b/crates/ra_hir_ty/src/autoderef.rs @@ -10,10 +10,10 @@ use hir_expand::name; use log::{info, warn}; use ra_db::CrateId; -use crate::db::HirDatabase; - -use super::{ +use crate::{ + db::HirDatabase, traits::{InEnvironment, Solution}, + utils::generics, Canonical, Substs, Ty, TypeWalk, }; @@ -54,7 +54,7 @@ fn deref_by_trait( }; let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?; - let generic_params = db.generic_params(target.into()); + let generic_params = generics(db, target.into()); if generic_params.count_params_including_parent() != 1 { // the Target type + Deref trait should only have one generic parameter, // namely Deref's Self type diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index 0c3428999..e52040eb5 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -6,7 +6,6 @@ use std::sync::Arc; use hir_def::{ builtin_type::Signedness, expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, - generics::GenericParams, path::{GenericArg, GenericArgs}, resolver::resolver_for_expr, AdtId, ContainerId, Lookup, StructFieldId, @@ -15,7 +14,11 @@ use hir_expand::name::{self, Name}; use ra_syntax::ast::RangeOp; use crate::{ - autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data, + autoderef, + db::HirDatabase, + method_resolution, op, + traits::InEnvironment, + utils::{generics, variant_data, Generics}, CallableDef, InferTy, IntTy, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, Uncertain, }; @@ -596,7 +599,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { Some((ty, func)) => { let ty = canonicalized_receiver.decanonicalize_ty(ty); self.write_method_resolution(tgt_expr, func); - (ty, self.db.value_ty(func.into()), Some(self.db.generic_params(func.into()))) + (ty, self.db.value_ty(func.into()), Some(generics(self.db, func.into()))) } None => (receiver_ty, Ty::Unknown, None), }; @@ -653,16 +656,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { fn substs_for_method_call( &mut self, - def_generics: Option>, + def_generics: Option, generic_args: Option<&GenericArgs>, receiver_ty: &Ty, ) -> Substs { - let (parent_param_count, param_count) = - def_generics.as_ref().map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); + let (parent_param_count, param_count) = def_generics + .as_ref() + .map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); let mut substs = Vec::with_capacity(parent_param_count + param_count); // Parent arguments are unknown, except for the receiver type - if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) { - for (_id, param) in parent_generics.params.iter() { + if let Some(parent_generics) = def_generics.as_ref().map(|p| p.iter_parent()) { + for (_id, param) in parent_generics { if param.name == name::SELF_TYPE { substs.push(receiver_ty.clone()); } else { @@ -706,9 +710,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { // construct a TraitDef let substs = a_ty.parameters.prefix( - self.db - .generic_params(trait_.into()) - .count_params_including_parent(), + generics(self.db, trait_.into()).count_params_including_parent(), ); self.obligations.push(Obligation::Trait(TraitRef { trait_: trait_.into(), diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 3c1f738df..99fd7158e 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -44,7 +44,7 @@ use std::sync::Arc; use std::{fmt, iter, mem}; use hir_def::{ - expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, + expr::ExprId, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, }; use hir_expand::name::Name; @@ -53,7 +53,7 @@ use ra_db::{impl_intern_key, salsa, CrateId}; use crate::{ db::HirDatabase, primitive::{FloatTy, IntTy, Uncertain}, - utils::make_mut_slice, + utils::{generics, make_mut_slice, Generics}, }; use display::{HirDisplay, HirFormatter}; @@ -166,15 +166,15 @@ impl TypeCtor { | TypeCtor::Closure { .. } // 1 param representing the signature of the closure => 1, TypeCtor::Adt(adt) => { - let generic_params = db.generic_params(AdtId::from(adt).into()); + let generic_params = generics(db, AdtId::from(adt).into()); generic_params.count_params_including_parent() } TypeCtor::FnDef(callable) => { - let generic_params = db.generic_params(callable.into()); + let generic_params = generics(db, callable.into()); generic_params.count_params_including_parent() } TypeCtor::AssociatedType(type_alias) => { - let generic_params = db.generic_params(type_alias.into()); + let generic_params = generics(db, type_alias.into()); generic_params.count_params_including_parent() } TypeCtor::FnPtr { num_args } => num_args as usize + 1, @@ -364,35 +364,25 @@ impl Substs { } /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). - pub fn identity(generic_params: &GenericParams) -> Substs { + pub(crate) fn identity(generic_params: &Generics) -> Substs { Substs( - generic_params - .params_including_parent() - .into_iter() - .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() }) - .collect(), + generic_params.iter().map(|(idx, p)| Ty::Param { idx, name: p.name.clone() }).collect(), ) } /// Return Substs that replace each parameter by a bound variable. - pub fn bound_vars(generic_params: &GenericParams) -> Substs { - Substs( - generic_params - .params_including_parent() - .into_iter() - .map(|p| Ty::Bound(p.idx)) - .collect(), - ) + pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { + Substs(generic_params.iter().map(|(idx, _p)| Ty::Bound(idx)).collect()) } pub fn build_for_def(db: &impl HirDatabase, def: impl Into) -> SubstsBuilder { let def = def.into(); - let params = db.generic_params(def); + let params = generics(db, def); let param_count = params.count_params_including_parent(); Substs::builder(param_count) } - pub fn build_for_generics(generic_params: &GenericParams) -> SubstsBuilder { + pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { Substs::builder(generic_params.count_params_including_parent()) } diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 32569ac66..d31f6a2d2 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -24,7 +24,7 @@ use crate::{ db::HirDatabase, primitive::{FloatTy, IntTy}, utils::{ - all_super_traits, associated_type_by_name_including_super_traits, make_mut_slice, + all_super_traits, associated_type_by_name_including_super_traits, generics, make_mut_slice, variant_data, }, FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, @@ -111,7 +111,9 @@ impl Ty { Some((it, None)) => it, _ => return None, }; - if let TypeNs::GenericParam(idx) = resolution { + if let TypeNs::GenericParam(param_id) = resolution { + let generics = generics(db, resolver.generic_def().expect("generics in scope")); + let idx = generics.param_idx(param_id); Some(idx) } else { None @@ -174,9 +176,11 @@ impl Ty { Ty::Dyn(Arc::new([GenericPredicate::Implemented(trait_ref)])) }; } - TypeNs::GenericParam(idx) => { + TypeNs::GenericParam(param_id) => { + let generics = generics(db, resolver.generic_def().expect("generics in scope")); + let idx = generics.param_idx(param_id); // FIXME: maybe return name in resolution? - let name = resolved_segment.name.clone(); + let name = generics.param_name(param_id); Ty::Param { idx, name } } TypeNs::SelfType(impl_id) => db.impl_self_ty(impl_id).clone(), @@ -315,10 +319,10 @@ pub(super) fn substs_from_path_segment( add_self_param: bool, ) -> Substs { let mut substs = Vec::new(); - let def_generics = def_generic.map(|def| db.generic_params(def.into())); + let def_generics = def_generic.map(|def| generics(db, def.into())); let (parent_param_count, param_count) = - def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); + def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count)); if add_self_param { // FIXME this add_self_param argument is kind of a hack: Traits have the @@ -567,12 +571,11 @@ pub(crate) fn generic_predicates_query( /// Resolve the default type params from generics 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 generic_params = generics(db, def.into()); let defaults = generic_params - .params_including_parent() - .into_iter() - .map(|p| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t))) + .iter() + .map(|(_idx, p)| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t))) .collect(); Substs(defaults) @@ -589,7 +592,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig { /// Build the declared type of a function. This should not need to look at the /// function body. fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Ty { - let generics = db.generic_params(def.into()); + let generics = generics(db, def.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(def.into()), substs) } @@ -639,7 +642,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> Ty { if struct_data.variant_data.is_unit() { return type_for_adt(db, def.into()); // Unit struct } - let generics = db.generic_params(def.into()); + let generics = generics(db, def.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(def.into()), substs) } @@ -653,7 +656,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId .iter() .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) .collect::>(); - let generics = db.generic_params(def.parent.into()); + let generics = generics(db, def.parent.into()); let substs = Substs::identity(&generics); let ret = type_for_adt(db, def.parent.into()).subst(&substs); FnSig::from_params_and_return(params, ret) @@ -666,18 +669,18 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) if var_data.is_unit() { return type_for_adt(db, def.parent.into()); // Unit variant } - let generics = db.generic_params(def.parent.into()); + let generics = generics(db, def.parent.into()); let substs = Substs::identity(&generics); Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs) } fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Ty { - let generics = db.generic_params(adt.into()); + let generics = generics(db, adt.into()); Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics)) } fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Ty { - let generics = db.generic_params(t.into()); + let generics = generics(db, t.into()); let resolver = t.resolver(db); let type_ref = &db.type_alias_data(t).type_ref; let substs = Substs::identity(&generics); diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index e3f02fa15..1d44320b9 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -19,8 +19,8 @@ use ra_db::{ use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; use crate::{ - db::HirDatabase, display::HirDisplay, ApplicationTy, GenericPredicate, ProjectionTy, Substs, - TraitRef, Ty, TypeCtor, TypeWalk, + db::HirDatabase, display::HirDisplay, utils::generics, ApplicationTy, GenericPredicate, + ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, }; /// This represents a trait whose name we could not resolve. @@ -547,7 +547,7 @@ pub(crate) fn associated_ty_data_query( ContainerId::TraitId(t) => t, _ => panic!("associated type not in trait"), }; - let generic_params = db.generic_params(type_alias.into()); + let generic_params = generics(db, type_alias.into()); let bound_data = chalk_rust_ir::AssociatedTyDatumBound { // FIXME add bounds and where clauses bounds: vec![], @@ -589,7 +589,7 @@ pub(crate) fn trait_datum_query( let trait_: TraitId = from_chalk(db, trait_id); let trait_data = db.trait_data(trait_); debug!("trait {:?} = {:?}", trait_id, trait_data.name); - let generic_params = db.generic_params(trait_.into()); + let generic_params = generics(db, trait_.into()); let bound_vars = Substs::bound_vars(&generic_params); let flags = chalk_rust_ir::TraitFlags { auto: trait_data.auto, @@ -626,7 +626,7 @@ pub(crate) fn struct_datum_query( let where_clauses = type_ctor .as_generic_def() .map(|generic_def| { - let generic_params = db.generic_params(generic_def.into()); + let generic_params = generics(db, generic_def.into()); let bound_vars = Substs::bound_vars(&generic_params); convert_where_clauses(db, generic_def, &bound_vars) }) @@ -669,7 +669,7 @@ fn impl_block_datum( let trait_ref = db.impl_trait(impl_id)?; let impl_data = db.impl_data(impl_id); - let generic_params = db.generic_params(impl_id.into()); + let generic_params = generics(db, impl_id.into()); let bound_vars = Substs::bound_vars(&generic_params); let trait_ref = trait_ref.subst(&bound_vars); let trait_ = trait_ref.trait_; @@ -767,7 +767,7 @@ fn type_alias_associated_ty_value( .trait_data(trait_ref.trait_) .associated_type_by_name(&type_alias_data.name) .expect("assoc ty value should not exist"); // validated when building the impl data as well - let generic_params = db.generic_params(impl_id.into()); + let generic_params = generics(db, impl_id.into()); let bound_vars = Substs::bound_vars(&generic_params); let ty = db.ty(type_alias.into()).subst(&bound_vars); let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index e4ba890ef..2c458867f 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs @@ -5,9 +5,10 @@ use std::sync::Arc; use hir_def::{ adt::VariantData, db::DefDatabase, + generics::{GenericParamData, GenericParams}, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - TraitId, TypeAliasId, VariantId, + ContainerId, GenericDefId, GenericParamId, Lookup, TraitId, TypeAliasId, VariantId, }; use hir_expand::name::{self, Name}; @@ -82,3 +83,82 @@ pub(crate) fn make_mut_slice(a: &mut Arc<[T]>) -> &mut [T] { } Arc::get_mut(a).unwrap() } + +pub(crate) fn generics(db: &impl DefDatabase, def: GenericDefId) -> Generics { + let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def))); + Generics { def, params: db.generic_params(def), parent_generics } +} + +pub(crate) struct Generics { + def: GenericDefId, + pub(crate) params: Arc, + parent_generics: Option>, +} + +impl Generics { + pub(crate) fn iter<'a>(&'a self) -> impl Iterator + 'a { + self.parent_generics + .as_ref() + .into_iter() + .flat_map(|it| it.params.params.iter()) + .chain(self.params.params.iter()) + .enumerate() + .map(|(i, (_local_id, p))| (i as u32, p)) + } + + pub(crate) fn iter_parent<'a>( + &'a self, + ) -> impl Iterator + 'a { + self.parent_generics + .as_ref() + .into_iter() + .flat_map(|it| it.params.params.iter()) + .enumerate() + .map(|(i, (_local_id, p))| (i as u32, p)) + } + + pub(crate) fn count_parent_params(&self) -> usize { + self.parent_generics.as_ref().map_or(0, |p| p.count_params_including_parent()) + } + + pub(crate) fn count_params_including_parent(&self) -> usize { + let parent_count = self.count_parent_params(); + parent_count + self.params.params.len() + } + pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { + self.find_param(param).0 + } + pub(crate) fn param_name(&self, param: GenericParamId) -> Name { + self.find_param(param).1.name.clone() + } + fn find_param(&self, param: GenericParamId) -> (u32, &GenericParamData) { + if param.parent == self.def { + let (idx, (_local_id, data)) = self + .params + .params + .iter() + .enumerate() + .find(|(_, (idx, _))| *idx == param.local_id) + .unwrap(); + + return ((self.count_parent_params() + idx) as u32, data); + } + self.parent_generics.as_ref().unwrap().find_param(param) + } +} + +fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option { + let container = match def { + GenericDefId::FunctionId(it) => it.lookup(db).container, + GenericDefId::TypeAliasId(it) => it.lookup(db).container, + GenericDefId::ConstId(it) => it.lookup(db).container, + GenericDefId::EnumVariantId(it) => return Some(it.parent.into()), + GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None, + }; + + match container { + ContainerId::ImplId(it) => Some(it.into()), + ContainerId::TraitId(it) => Some(it.into()), + ContainerId::ModuleId(_) => None, + } +} -- cgit v1.2.3 From d6c2b92409902d9ceca8cd064026cfcc1f357cf6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 13:05:05 +0100 Subject: Refactor parameter count tracking --- crates/ra_hir_ty/src/autoderef.rs | 2 +- crates/ra_hir_ty/src/infer/expr.rs | 18 ++++++++---------- crates/ra_hir_ty/src/lib.rs | 10 +++++----- crates/ra_hir_ty/src/lower.rs | 13 ++++++------- crates/ra_hir_ty/src/traits/chalk.rs | 2 +- crates/ra_hir_ty/src/utils.rs | 17 +++++++++-------- 6 files changed, 30 insertions(+), 32 deletions(-) (limited to 'crates/ra_hir_ty') diff --git a/crates/ra_hir_ty/src/autoderef.rs b/crates/ra_hir_ty/src/autoderef.rs index a6db7f623..d557962b4 100644 --- a/crates/ra_hir_ty/src/autoderef.rs +++ b/crates/ra_hir_ty/src/autoderef.rs @@ -55,7 +55,7 @@ fn deref_by_trait( let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?; let generic_params = generics(db, target.into()); - if generic_params.count_params_including_parent() != 1 { + if generic_params.len() != 1 { // the Target type + Deref trait should only have one generic parameter, // namely Deref's Self type return None; diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index e52040eb5..2c296987c 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -660,10 +660,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { generic_args: Option<&GenericArgs>, receiver_ty: &Ty, ) -> Substs { - let (parent_param_count, param_count) = def_generics - .as_ref() - .map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); - let mut substs = Vec::with_capacity(parent_param_count + param_count); + let (total_len, _parent_len, child_len) = + def_generics.as_ref().map_or((0, 0, 0), |g| g.len_split()); + let mut substs = Vec::with_capacity(total_len); // Parent arguments are unknown, except for the receiver type if let Some(parent_generics) = def_generics.as_ref().map(|p| p.iter_parent()) { for (_id, param) in parent_generics { @@ -677,7 +676,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // handle provided type arguments if let Some(generic_args) = generic_args { // if args are provided, it should be all of them, but we can't rely on that - for arg in generic_args.args.iter().take(param_count) { + for arg in generic_args.args.iter().take(child_len) { match arg { GenericArg::Type(type_ref) => { let ty = self.make_ty(type_ref); @@ -687,10 +686,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } }; let supplied_params = substs.len(); - for _ in supplied_params..parent_param_count + param_count { + for _ in supplied_params..total_len { substs.push(Ty::Unknown); } - assert_eq!(substs.len(), parent_param_count + param_count); + assert_eq!(substs.len(), total_len); Substs(substs.into()) } @@ -709,9 +708,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { CallableDef::FunctionId(f) => { if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { // construct a TraitDef - let substs = a_ty.parameters.prefix( - generics(self.db, trait_.into()).count_params_including_parent(), - ); + let substs = + a_ty.parameters.prefix(generics(self.db, trait_.into()).len()); self.obligations.push(Obligation::Trait(TraitRef { trait_: trait_.into(), substs, diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 99fd7158e..036d3a589 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -167,15 +167,15 @@ impl TypeCtor { => 1, TypeCtor::Adt(adt) => { let generic_params = generics(db, AdtId::from(adt).into()); - generic_params.count_params_including_parent() + generic_params.len() } TypeCtor::FnDef(callable) => { let generic_params = generics(db, callable.into()); - generic_params.count_params_including_parent() + generic_params.len() } TypeCtor::AssociatedType(type_alias) => { let generic_params = generics(db, type_alias.into()); - generic_params.count_params_including_parent() + generic_params.len() } TypeCtor::FnPtr { num_args } => num_args as usize + 1, TypeCtor::Tuple { cardinality } => cardinality as usize, @@ -378,12 +378,12 @@ impl Substs { pub fn build_for_def(db: &impl HirDatabase, def: impl Into) -> SubstsBuilder { let def = def.into(); let params = generics(db, def); - let param_count = params.count_params_including_parent(); + let param_count = params.len(); Substs::builder(param_count) } pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { - Substs::builder(generic_params.count_params_including_parent()) + Substs::builder(generic_params.len()) } pub fn build_for_type_ctor(db: &impl HirDatabase, type_ctor: TypeCtor) -> SubstsBuilder { diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index d31f6a2d2..eab91229e 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -321,9 +321,8 @@ pub(super) fn substs_from_path_segment( let mut substs = Vec::new(); let def_generics = def_generic.map(|def| generics(db, def.into())); - let (parent_param_count, param_count) = - def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); - substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count)); + let (total_len, parent_len, child_len) = def_generics.map_or((0, 0, 0), |g| g.len_split()); + substs.extend(iter::repeat(Ty::Unknown).take(parent_len)); if add_self_param { // FIXME this add_self_param argument is kind of a hack: Traits have the // Self type as an implicit first type parameter, but it can't be @@ -334,8 +333,8 @@ pub(super) fn substs_from_path_segment( if let Some(generic_args) = &segment.args_and_bindings { // if args are provided, it should be all of them, but we can't rely on that let self_param_correction = if add_self_param { 1 } else { 0 }; - let param_count = param_count - self_param_correction; - for arg in generic_args.args.iter().take(param_count) { + let child_len = child_len + self_param_correction; + for arg in generic_args.args.iter().take(child_len) { match arg { GenericArg::Type(type_ref) => { let ty = Ty::from_hir(db, resolver, type_ref); @@ -346,10 +345,10 @@ pub(super) fn substs_from_path_segment( } // add placeholders for args that were not provided let supplied_params = substs.len(); - for _ in supplied_params..parent_param_count + param_count { + for _ in supplied_params..total_len { substs.push(Ty::Unknown); } - assert_eq!(substs.len(), parent_param_count + param_count); + assert_eq!(substs.len(), total_len); // handle defaults if let Some(def_generic) = def_generic { diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index 1d44320b9..1e7ff93d5 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -557,7 +557,7 @@ pub(crate) fn associated_ty_data_query( trait_id: trait_.to_chalk(db), id, name: lalrpop_intern::intern(&db.type_alias_data(type_alias).name.to_string()), - binders: make_binders(bound_data, generic_params.count_params_including_parent()), + binders: make_binders(bound_data, generic_params.len()), }; Arc::new(datum) } diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index 2c458867f..936cfe25e 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs @@ -117,13 +117,14 @@ impl Generics { .map(|(i, (_local_id, p))| (i as u32, p)) } - pub(crate) fn count_parent_params(&self) -> usize { - self.parent_generics.as_ref().map_or(0, |p| p.count_params_including_parent()) + pub(crate) fn len(&self) -> usize { + self.len_split().0 } - - pub(crate) fn count_params_including_parent(&self) -> usize { - let parent_count = self.count_parent_params(); - parent_count + self.params.params.len() + /// (total, parents, child) + pub(crate) fn len_split(&self) -> (usize, usize, usize) { + let parent = self.parent_generics.as_ref().map_or(0, |p| p.len()); + let child = self.params.params.len(); + (parent + child, parent, child) } pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { self.find_param(param).0 @@ -140,8 +141,8 @@ impl Generics { .enumerate() .find(|(_, (idx, _))| *idx == param.local_id) .unwrap(); - - return ((self.count_parent_params() + idx) as u32, data); + let (_total, parent_len, _child) = self.len_split(); + return ((parent_len + idx) as u32, data); } self.parent_generics.as_ref().unwrap().find_param(param) } -- cgit v1.2.3 From 29b5e1ec2a4bc25daddfe5137503b156b3cd283f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 7 Dec 2019 13:19:21 +0100 Subject: Reformat --- crates/ra_hir_ty/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_ty') diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 036d3a589..3ad913e55 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -44,8 +44,8 @@ use std::sync::Arc; use std::{fmt, iter, mem}; use hir_def::{ - expr::ExprId, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, - GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, + expr::ExprId, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, + Lookup, TraitId, TypeAliasId, }; use hir_expand::name::Name; use ra_db::{impl_intern_key, salsa, CrateId}; -- cgit v1.2.3