diff options
author | Florian Diebold <[email protected]> | 2020-01-31 15:52:43 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-02-07 17:28:10 +0000 |
commit | ed25cf70d5e0df9c7a33deb503ea14c2d97bd7a7 (patch) | |
tree | bc6e8620244d64beed8f1a9ce9059e5dfd2a0fbe /crates/ra_hir_ty/src/lib.rs | |
parent | f8b7b64bce772f21124b4790538ca97418cc23ca (diff) |
Change Ty::Param to contain param ID
Diffstat (limited to 'crates/ra_hir_ty/src/lib.rs')
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 1f0fd1128..1e162943c 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -45,10 +45,10 @@ use std::{fmt, iter, mem}; | |||
45 | 45 | ||
46 | use hir_def::{ | 46 | use hir_def::{ |
47 | expr::ExprId, type_ref::Mutability, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, | 47 | expr::ExprId, type_ref::Mutability, AdtId, AssocContainerId, DefWithBodyId, GenericDefId, |
48 | HasModule, Lookup, TraitId, TypeAliasId, | 48 | HasModule, Lookup, TraitId, TypeAliasId, TypeParamId, generics::TypeParamProvenance, |
49 | }; | 49 | }; |
50 | use hir_expand::name::Name; | ||
51 | use ra_db::{impl_intern_key, salsa, CrateId}; | 50 | use ra_db::{impl_intern_key, salsa, CrateId}; |
51 | use hir_expand::name::Name; | ||
52 | 52 | ||
53 | use crate::{ | 53 | use crate::{ |
54 | db::HirDatabase, | 54 | db::HirDatabase, |
@@ -288,14 +288,7 @@ pub enum Ty { | |||
288 | Projection(ProjectionTy), | 288 | Projection(ProjectionTy), |
289 | 289 | ||
290 | /// A type parameter; for example, `T` in `fn f<T>(x: T) {} | 290 | /// A type parameter; for example, `T` in `fn f<T>(x: T) {} |
291 | Param { | 291 | Param(TypeParamId), |
292 | /// The index of the parameter (starting with parameters from the | ||
293 | /// surrounding impl, then the current function). | ||
294 | idx: u32, | ||
295 | /// The name of the parameter, for displaying. | ||
296 | // FIXME get rid of this | ||
297 | name: Name, | ||
298 | }, | ||
299 | 292 | ||
300 | /// A bound type variable. Used during trait resolution to represent Chalk | 293 | /// A bound type variable. Used during trait resolution to represent Chalk |
301 | /// variables, and in `Dyn` and `Opaque` bounds to represent the `Self` type. | 294 | /// variables, and in `Dyn` and `Opaque` bounds to represent the `Self` type. |
@@ -366,15 +359,15 @@ impl Substs { | |||
366 | } | 359 | } |
367 | 360 | ||
368 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 361 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
369 | pub(crate) fn identity(generic_params: &Generics) -> Substs { | 362 | pub(crate) fn type_params(generic_params: &Generics) -> Substs { |
370 | Substs( | 363 | Substs( |
371 | generic_params.iter().map(|(idx, p)| Ty::Param { idx, name: p.name.clone().unwrap_or_else(Name::missing) }).collect(), | 364 | generic_params.iter().map(|(id, _)| Ty::Param(id)).collect(), |
372 | ) | 365 | ) |
373 | } | 366 | } |
374 | 367 | ||
375 | /// Return Substs that replace each parameter by a bound variable. | 368 | /// Return Substs that replace each parameter by a bound variable. |
376 | pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { | 369 | pub(crate) fn bound_vars(generic_params: &Generics) -> Substs { |
377 | Substs(generic_params.iter().map(|(idx, _p)| Ty::Bound(idx)).collect()) | 370 | Substs(generic_params.iter().enumerate().map(|(idx, _)| Ty::Bound(idx as u32)).collect()) |
378 | } | 371 | } |
379 | 372 | ||
380 | pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder { | 373 | pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder { |
@@ -422,11 +415,6 @@ impl SubstsBuilder { | |||
422 | self.fill((starting_from..).map(Ty::Bound)) | 415 | self.fill((starting_from..).map(Ty::Bound)) |
423 | } | 416 | } |
424 | 417 | ||
425 | pub fn fill_with_params(self) -> Self { | ||
426 | let start = self.vec.len() as u32; | ||
427 | self.fill((start..).map(|idx| Ty::Param { idx, name: Name::missing() })) | ||
428 | } | ||
429 | |||
430 | pub fn fill_with_unknown(self) -> Self { | 418 | pub fn fill_with_unknown(self) -> Self { |
431 | self.fill(iter::repeat(Ty::Unknown)) | 419 | self.fill(iter::repeat(Ty::Unknown)) |
432 | } | 420 | } |
@@ -762,13 +750,19 @@ pub trait TypeWalk { | |||
762 | /// Replaces type parameters in this type using the given `Substs`. (So e.g. | 750 | /// Replaces type parameters in this type using the given `Substs`. (So e.g. |
763 | /// if `self` is `&[T]`, where type parameter T has index 0, and the | 751 | /// if `self` is `&[T]`, where type parameter T has index 0, and the |
764 | /// `Substs` contain `u32` at index 0, we'll have `&[u32]` afterwards.) | 752 | /// `Substs` contain `u32` at index 0, we'll have `&[u32]` afterwards.) |
765 | fn subst(self, substs: &Substs) -> Self | 753 | // TODO: this should mostly not be used anymore |
754 | fn subst_type_params(self, db: &impl HirDatabase, def: GenericDefId, substs: &Substs) -> Self | ||
766 | where | 755 | where |
767 | Self: Sized, | 756 | Self: Sized, |
768 | { | 757 | { |
758 | let generics = generics(db, def); | ||
769 | self.fold(&mut |ty| match ty { | 759 | self.fold(&mut |ty| match ty { |
770 | Ty::Param { idx, name } => { | 760 | Ty::Param(id) => { |
771 | substs.get(idx as usize).cloned().unwrap_or(Ty::Param { idx, name }) | 761 | if let Some(idx) = generics.param_idx(id) { |
762 | substs.get(idx as usize).cloned().unwrap_or(Ty::Param(id)) | ||
763 | } else { | ||
764 | ty | ||
765 | } | ||
772 | } | 766 | } |
773 | ty => ty, | 767 | ty => ty, |
774 | }) | 768 | }) |
@@ -1042,7 +1036,18 @@ impl HirDisplay for Ty { | |||
1042 | match self { | 1036 | match self { |
1043 | Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, | 1037 | Ty::Apply(a_ty) => a_ty.hir_fmt(f)?, |
1044 | Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, | 1038 | Ty::Projection(p_ty) => p_ty.hir_fmt(f)?, |
1045 | Ty::Param { name, .. } => write!(f, "{}", name)?, | 1039 | Ty::Param(id) => { |
1040 | let generic_params = f.db.generic_params(id.parent); | ||
1041 | let param_data = &generic_params.types[id.local_id]; | ||
1042 | match param_data.provenance { | ||
1043 | TypeParamProvenance::TypeParamList | TypeParamProvenance::TraitSelf => { | ||
1044 | write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? | ||
1045 | } | ||
1046 | TypeParamProvenance::ArgumentImplTrait => { | ||
1047 | write!(f, "impl TODO")? | ||
1048 | } | ||
1049 | } | ||
1050 | }, | ||
1046 | Ty::Bound(idx) => write!(f, "?{}", idx)?, | 1051 | Ty::Bound(idx) => write!(f, "?{}", idx)?, |
1047 | Ty::Dyn(predicates) | Ty::Opaque(predicates) => { | 1052 | Ty::Dyn(predicates) | Ty::Opaque(predicates) => { |
1048 | match self { | 1053 | match self { |