diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-07 12:33:08 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-07 12:33:08 +0000 |
commit | 7aac5f2b427247120025f63c4864a1b937ed20c7 (patch) | |
tree | 51a841eaac9c7416e379b23887ee97ddc541ab5c /crates/ra_hir_ty/src/lib.rs | |
parent | 35fc983dd9e904ad4961b9c10be3397bad33da0c (diff) | |
parent | 29b5e1ec2a4bc25daddfe5137503b156b3cd283f (diff) |
Merge #2492
2492: Refactor generic parameteres lowering r=flodiebold a=matklad
indices and parent params seem to be concerns, specific to `hir_ty`, so move them there.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src/lib.rs')
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 44 |
1 files changed, 17 insertions, 27 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 3c1f738df..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; | |||
44 | use std::{fmt, iter, mem}; | 44 | use std::{fmt, iter, mem}; |
45 | 45 | ||
46 | use hir_def::{ | 46 | use hir_def::{ |
47 | expr::ExprId, generics::GenericParams, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, | 47 | expr::ExprId, type_ref::Mutability, AdtId, ContainerId, DefWithBodyId, GenericDefId, HasModule, |
48 | GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, | 48 | Lookup, TraitId, TypeAliasId, |
49 | }; | 49 | }; |
50 | use hir_expand::name::Name; | 50 | use hir_expand::name::Name; |
51 | use ra_db::{impl_intern_key, salsa, CrateId}; | 51 | use ra_db::{impl_intern_key, salsa, CrateId}; |
@@ -53,7 +53,7 @@ use ra_db::{impl_intern_key, salsa, CrateId}; | |||
53 | use crate::{ | 53 | use crate::{ |
54 | db::HirDatabase, | 54 | db::HirDatabase, |
55 | primitive::{FloatTy, IntTy, Uncertain}, | 55 | primitive::{FloatTy, IntTy, Uncertain}, |
56 | utils::make_mut_slice, | 56 | utils::{generics, make_mut_slice, Generics}, |
57 | }; | 57 | }; |
58 | use display::{HirDisplay, HirFormatter}; | 58 | use display::{HirDisplay, HirFormatter}; |
59 | 59 | ||
@@ -166,16 +166,16 @@ impl TypeCtor { | |||
166 | | TypeCtor::Closure { .. } // 1 param representing the signature of the closure | 166 | | TypeCtor::Closure { .. } // 1 param representing the signature of the closure |
167 | => 1, | 167 | => 1, |
168 | TypeCtor::Adt(adt) => { | 168 | TypeCtor::Adt(adt) => { |
169 | let generic_params = db.generic_params(AdtId::from(adt).into()); | 169 | let generic_params = generics(db, AdtId::from(adt).into()); |
170 | generic_params.count_params_including_parent() | 170 | generic_params.len() |
171 | } | 171 | } |
172 | TypeCtor::FnDef(callable) => { | 172 | TypeCtor::FnDef(callable) => { |
173 | let generic_params = db.generic_params(callable.into()); | 173 | let generic_params = generics(db, callable.into()); |
174 | generic_params.count_params_including_parent() | 174 | generic_params.len() |
175 | } | 175 | } |
176 | TypeCtor::AssociatedType(type_alias) => { | 176 | TypeCtor::AssociatedType(type_alias) => { |
177 | let generic_params = db.generic_params(type_alias.into()); | 177 | let generic_params = generics(db, type_alias.into()); |
178 | generic_params.count_params_including_parent() | 178 | generic_params.len() |
179 | } | 179 | } |
180 | TypeCtor::FnPtr { num_args } => num_args as usize + 1, | 180 | TypeCtor::FnPtr { num_args } => num_args as usize + 1, |
181 | TypeCtor::Tuple { cardinality } => cardinality as usize, | 181 | TypeCtor::Tuple { cardinality } => cardinality as usize, |
@@ -364,36 +364,26 @@ impl Substs { | |||
364 | } | 364 | } |
365 | 365 | ||
366 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 366 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
367 | pub fn identity(generic_params: &GenericParams) -> Substs { | 367 | pub(crate) fn identity(generic_params: &Generics) -> Substs { |
368 | Substs( | 368 | Substs( |
369 | generic_params | 369 | generic_params.iter().map(|(idx, p)| Ty::Param { idx, name: p.name.clone() }).collect(), |
370 | .params_including_parent() | ||
371 | .into_iter() | ||
372 | .map(|p| Ty::Param { idx: p.idx, name: p.name.clone() }) | ||
373 | .collect(), | ||
374 | ) | 370 | ) |
375 | } | 371 | } |
376 | 372 | ||
377 | /// Return Substs that replace each parameter by a bound variable. | 373 | /// Return Substs that replace each parameter by a bound variable. |
378 | pub fn bound_vars(generic_params: &GenericParams) -> Substs { | 374 | pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { |
379 | Substs( | 375 | Substs(generic_params.iter().map(|(idx, _p)| Ty::Bound(idx)).collect()) |
380 | generic_params | ||
381 | .params_including_parent() | ||
382 | .into_iter() | ||
383 | .map(|p| Ty::Bound(p.idx)) | ||
384 | .collect(), | ||
385 | ) | ||
386 | } | 376 | } |
387 | 377 | ||
388 | pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder { | 378 | pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder { |
389 | let def = def.into(); | 379 | let def = def.into(); |
390 | let params = db.generic_params(def); | 380 | let params = generics(db, def); |
391 | let param_count = params.count_params_including_parent(); | 381 | let param_count = params.len(); |
392 | Substs::builder(param_count) | 382 | Substs::builder(param_count) |
393 | } | 383 | } |
394 | 384 | ||
395 | pub fn build_for_generics(generic_params: &GenericParams) -> SubstsBuilder { | 385 | pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { |
396 | Substs::builder(generic_params.count_params_including_parent()) | 386 | Substs::builder(generic_params.len()) |
397 | } | 387 | } |
398 | 388 | ||
399 | pub fn build_for_type_ctor(db: &impl HirDatabase, type_ctor: TypeCtor) -> SubstsBuilder { | 389 | pub fn build_for_type_ctor(db: &impl HirDatabase, type_ctor: TypeCtor) -> SubstsBuilder { |