diff options
author | Aleksey Kladov <[email protected]> | 2019-12-07 10:50:36 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-07 12:05:56 +0000 |
commit | 8e9837df21942ca12a5aece0a868ea46eb405742 (patch) | |
tree | 664cfafc63c0895d76df5c85f6f10c0739f82275 /crates/ra_hir_ty | |
parent | 30fefcc08cc0c670ce541476491238d258ca55c1 (diff) |
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.
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/src/autoderef.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer/expr.rs | 24 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 32 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lower.rs | 35 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/traits/chalk.rs | 14 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 82 |
6 files changed, 135 insertions, 60 deletions
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; | |||
10 | use log::{info, warn}; | 10 | use log::{info, warn}; |
11 | use ra_db::CrateId; | 11 | use ra_db::CrateId; |
12 | 12 | ||
13 | use crate::db::HirDatabase; | 13 | use crate::{ |
14 | 14 | db::HirDatabase, | |
15 | use super::{ | ||
16 | traits::{InEnvironment, Solution}, | 15 | traits::{InEnvironment, Solution}, |
16 | utils::generics, | ||
17 | Canonical, Substs, Ty, TypeWalk, | 17 | Canonical, Substs, Ty, TypeWalk, |
18 | }; | 18 | }; |
19 | 19 | ||
@@ -54,7 +54,7 @@ fn deref_by_trait( | |||
54 | }; | 54 | }; |
55 | let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?; | 55 | let target = db.trait_data(deref_trait).associated_type_by_name(&name::TARGET_TYPE)?; |
56 | 56 | ||
57 | let generic_params = db.generic_params(target.into()); | 57 | let generic_params = generics(db, target.into()); |
58 | if generic_params.count_params_including_parent() != 1 { | 58 | if generic_params.count_params_including_parent() != 1 { |
59 | // the Target type + Deref trait should only have one generic parameter, | 59 | // the Target type + Deref trait should only have one generic parameter, |
60 | // namely Deref's Self type | 60 | // 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; | |||
6 | use hir_def::{ | 6 | use hir_def::{ |
7 | builtin_type::Signedness, | 7 | builtin_type::Signedness, |
8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
9 | generics::GenericParams, | ||
10 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
11 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
12 | AdtId, ContainerId, Lookup, StructFieldId, | 11 | AdtId, ContainerId, Lookup, StructFieldId, |
@@ -15,7 +14,11 @@ use hir_expand::name::{self, Name}; | |||
15 | use ra_syntax::ast::RangeOp; | 14 | use ra_syntax::ast::RangeOp; |
16 | 15 | ||
17 | use crate::{ | 16 | use crate::{ |
18 | autoderef, db::HirDatabase, method_resolution, op, traits::InEnvironment, utils::variant_data, | 17 | autoderef, |
18 | db::HirDatabase, | ||
19 | method_resolution, op, | ||
20 | traits::InEnvironment, | ||
21 | utils::{generics, variant_data, Generics}, | ||
19 | CallableDef, InferTy, IntTy, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, | 22 | CallableDef, InferTy, IntTy, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, |
20 | TraitRef, Ty, TypeCtor, TypeWalk, Uncertain, | 23 | TraitRef, Ty, TypeCtor, TypeWalk, Uncertain, |
21 | }; | 24 | }; |
@@ -596,7 +599,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
596 | Some((ty, func)) => { | 599 | Some((ty, func)) => { |
597 | let ty = canonicalized_receiver.decanonicalize_ty(ty); | 600 | let ty = canonicalized_receiver.decanonicalize_ty(ty); |
598 | self.write_method_resolution(tgt_expr, func); | 601 | self.write_method_resolution(tgt_expr, func); |
599 | (ty, self.db.value_ty(func.into()), Some(self.db.generic_params(func.into()))) | 602 | (ty, self.db.value_ty(func.into()), Some(generics(self.db, func.into()))) |
600 | } | 603 | } |
601 | None => (receiver_ty, Ty::Unknown, None), | 604 | None => (receiver_ty, Ty::Unknown, None), |
602 | }; | 605 | }; |
@@ -653,16 +656,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
653 | 656 | ||
654 | fn substs_for_method_call( | 657 | fn substs_for_method_call( |
655 | &mut self, | 658 | &mut self, |
656 | def_generics: Option<Arc<GenericParams>>, | 659 | def_generics: Option<Generics>, |
657 | generic_args: Option<&GenericArgs>, | 660 | generic_args: Option<&GenericArgs>, |
658 | receiver_ty: &Ty, | 661 | receiver_ty: &Ty, |
659 | ) -> Substs { | 662 | ) -> Substs { |
660 | let (parent_param_count, param_count) = | 663 | let (parent_param_count, param_count) = def_generics |
661 | def_generics.as_ref().map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); | 664 | .as_ref() |
665 | .map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); | ||
662 | let mut substs = Vec::with_capacity(parent_param_count + param_count); | 666 | let mut substs = Vec::with_capacity(parent_param_count + param_count); |
663 | // Parent arguments are unknown, except for the receiver type | 667 | // Parent arguments are unknown, except for the receiver type |
664 | if let Some(parent_generics) = def_generics.and_then(|p| p.parent_params.clone()) { | 668 | if let Some(parent_generics) = def_generics.as_ref().map(|p| p.iter_parent()) { |
665 | for (_id, param) in parent_generics.params.iter() { | 669 | for (_id, param) in parent_generics { |
666 | if param.name == name::SELF_TYPE { | 670 | if param.name == name::SELF_TYPE { |
667 | substs.push(receiver_ty.clone()); | 671 | substs.push(receiver_ty.clone()); |
668 | } else { | 672 | } else { |
@@ -706,9 +710,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
706 | if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { | 710 | if let ContainerId::TraitId(trait_) = f.lookup(self.db).container { |
707 | // construct a TraitDef | 711 | // construct a TraitDef |
708 | let substs = a_ty.parameters.prefix( | 712 | let substs = a_ty.parameters.prefix( |
709 | self.db | 713 | generics(self.db, trait_.into()).count_params_including_parent(), |
710 | .generic_params(trait_.into()) | ||
711 | .count_params_including_parent(), | ||
712 | ); | 714 | ); |
713 | self.obligations.push(Obligation::Trait(TraitRef { | 715 | self.obligations.push(Obligation::Trait(TraitRef { |
714 | trait_: trait_.into(), | 716 | 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; | |||
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, |
48 | GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, | 48 | GenericDefId, HasModule, Lookup, TraitId, TypeAliasId, |
49 | }; | 49 | }; |
50 | use hir_expand::name::Name; | 50 | use hir_expand::name::Name; |
@@ -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,15 +166,15 @@ 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.count_params_including_parent() |
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.count_params_including_parent() |
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.count_params_including_parent() |
179 | } | 179 | } |
180 | TypeCtor::FnPtr { num_args } => num_args as usize + 1, | 180 | TypeCtor::FnPtr { num_args } => num_args as usize + 1, |
@@ -364,35 +364,25 @@ 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.count_params_including_parent(); |
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.count_params_including_parent()) |
397 | } | 387 | } |
398 | 388 | ||
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::{ | |||
24 | db::HirDatabase, | 24 | db::HirDatabase, |
25 | primitive::{FloatTy, IntTy}, | 25 | primitive::{FloatTy, IntTy}, |
26 | utils::{ | 26 | utils::{ |
27 | all_super_traits, associated_type_by_name_including_super_traits, make_mut_slice, | 27 | all_super_traits, associated_type_by_name_including_super_traits, generics, make_mut_slice, |
28 | variant_data, | 28 | variant_data, |
29 | }, | 29 | }, |
30 | FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, | 30 | FnSig, GenericPredicate, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, |
@@ -111,7 +111,9 @@ impl Ty { | |||
111 | Some((it, None)) => it, | 111 | Some((it, None)) => it, |
112 | _ => return None, | 112 | _ => return None, |
113 | }; | 113 | }; |
114 | if let TypeNs::GenericParam(idx) = resolution { | 114 | if let TypeNs::GenericParam(param_id) = resolution { |
115 | let generics = generics(db, resolver.generic_def().expect("generics in scope")); | ||
116 | let idx = generics.param_idx(param_id); | ||
115 | Some(idx) | 117 | Some(idx) |
116 | } else { | 118 | } else { |
117 | None | 119 | None |
@@ -174,9 +176,11 @@ impl Ty { | |||
174 | Ty::Dyn(Arc::new([GenericPredicate::Implemented(trait_ref)])) | 176 | Ty::Dyn(Arc::new([GenericPredicate::Implemented(trait_ref)])) |
175 | }; | 177 | }; |
176 | } | 178 | } |
177 | TypeNs::GenericParam(idx) => { | 179 | TypeNs::GenericParam(param_id) => { |
180 | let generics = generics(db, resolver.generic_def().expect("generics in scope")); | ||
181 | let idx = generics.param_idx(param_id); | ||
178 | // FIXME: maybe return name in resolution? | 182 | // FIXME: maybe return name in resolution? |
179 | let name = resolved_segment.name.clone(); | 183 | let name = generics.param_name(param_id); |
180 | Ty::Param { idx, name } | 184 | Ty::Param { idx, name } |
181 | } | 185 | } |
182 | TypeNs::SelfType(impl_id) => db.impl_self_ty(impl_id).clone(), | 186 | TypeNs::SelfType(impl_id) => db.impl_self_ty(impl_id).clone(), |
@@ -315,10 +319,10 @@ pub(super) fn substs_from_path_segment( | |||
315 | add_self_param: bool, | 319 | add_self_param: bool, |
316 | ) -> Substs { | 320 | ) -> Substs { |
317 | let mut substs = Vec::new(); | 321 | let mut substs = Vec::new(); |
318 | let def_generics = def_generic.map(|def| db.generic_params(def.into())); | 322 | let def_generics = def_generic.map(|def| generics(db, def.into())); |
319 | 323 | ||
320 | let (parent_param_count, param_count) = | 324 | let (parent_param_count, param_count) = |
321 | def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len())); | 325 | def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.params.len())); |
322 | substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count)); | 326 | substs.extend(iter::repeat(Ty::Unknown).take(parent_param_count)); |
323 | if add_self_param { | 327 | if add_self_param { |
324 | // FIXME this add_self_param argument is kind of a hack: Traits have the | 328 | // FIXME this add_self_param argument is kind of a hack: Traits have the |
@@ -567,12 +571,11 @@ pub(crate) fn generic_predicates_query( | |||
567 | /// Resolve the default type params from generics | 571 | /// Resolve the default type params from generics |
568 | pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs { | 572 | pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs { |
569 | let resolver = def.resolver(db); | 573 | let resolver = def.resolver(db); |
570 | let generic_params = db.generic_params(def.into()); | 574 | let generic_params = generics(db, def.into()); |
571 | 575 | ||
572 | let defaults = generic_params | 576 | let defaults = generic_params |
573 | .params_including_parent() | 577 | .iter() |
574 | .into_iter() | 578 | .map(|(_idx, p)| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t))) |
575 | .map(|p| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t))) | ||
576 | .collect(); | 579 | .collect(); |
577 | 580 | ||
578 | Substs(defaults) | 581 | Substs(defaults) |
@@ -589,7 +592,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: FunctionId) -> FnSig { | |||
589 | /// Build the declared type of a function. This should not need to look at the | 592 | /// Build the declared type of a function. This should not need to look at the |
590 | /// function body. | 593 | /// function body. |
591 | fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Ty { | 594 | fn type_for_fn(db: &impl HirDatabase, def: FunctionId) -> Ty { |
592 | let generics = db.generic_params(def.into()); | 595 | let generics = generics(db, def.into()); |
593 | let substs = Substs::identity(&generics); | 596 | let substs = Substs::identity(&generics); |
594 | Ty::apply(TypeCtor::FnDef(def.into()), substs) | 597 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
595 | } | 598 | } |
@@ -639,7 +642,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> Ty { | |||
639 | if struct_data.variant_data.is_unit() { | 642 | if struct_data.variant_data.is_unit() { |
640 | return type_for_adt(db, def.into()); // Unit struct | 643 | return type_for_adt(db, def.into()); // Unit struct |
641 | } | 644 | } |
642 | let generics = db.generic_params(def.into()); | 645 | let generics = generics(db, def.into()); |
643 | let substs = Substs::identity(&generics); | 646 | let substs = Substs::identity(&generics); |
644 | Ty::apply(TypeCtor::FnDef(def.into()), substs) | 647 | Ty::apply(TypeCtor::FnDef(def.into()), substs) |
645 | } | 648 | } |
@@ -653,7 +656,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId | |||
653 | .iter() | 656 | .iter() |
654 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) | 657 | .map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref)) |
655 | .collect::<Vec<_>>(); | 658 | .collect::<Vec<_>>(); |
656 | let generics = db.generic_params(def.parent.into()); | 659 | let generics = generics(db, def.parent.into()); |
657 | let substs = Substs::identity(&generics); | 660 | let substs = Substs::identity(&generics); |
658 | let ret = type_for_adt(db, def.parent.into()).subst(&substs); | 661 | let ret = type_for_adt(db, def.parent.into()).subst(&substs); |
659 | FnSig::from_params_and_return(params, ret) | 662 | FnSig::from_params_and_return(params, ret) |
@@ -666,18 +669,18 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariantId) | |||
666 | if var_data.is_unit() { | 669 | if var_data.is_unit() { |
667 | return type_for_adt(db, def.parent.into()); // Unit variant | 670 | return type_for_adt(db, def.parent.into()); // Unit variant |
668 | } | 671 | } |
669 | let generics = db.generic_params(def.parent.into()); | 672 | let generics = generics(db, def.parent.into()); |
670 | let substs = Substs::identity(&generics); | 673 | let substs = Substs::identity(&generics); |
671 | Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs) | 674 | Ty::apply(TypeCtor::FnDef(EnumVariantId::from(def).into()), substs) |
672 | } | 675 | } |
673 | 676 | ||
674 | fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Ty { | 677 | fn type_for_adt(db: &impl HirDatabase, adt: AdtId) -> Ty { |
675 | let generics = db.generic_params(adt.into()); | 678 | let generics = generics(db, adt.into()); |
676 | Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics)) | 679 | Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics)) |
677 | } | 680 | } |
678 | 681 | ||
679 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Ty { | 682 | fn type_for_type_alias(db: &impl HirDatabase, t: TypeAliasId) -> Ty { |
680 | let generics = db.generic_params(t.into()); | 683 | let generics = generics(db, t.into()); |
681 | let resolver = t.resolver(db); | 684 | let resolver = t.resolver(db); |
682 | let type_ref = &db.type_alias_data(t).type_ref; | 685 | let type_ref = &db.type_alias_data(t).type_ref; |
683 | let substs = Substs::identity(&generics); | 686 | 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::{ | |||
19 | 19 | ||
20 | use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; | 20 | use super::{builtin, AssocTyValue, Canonical, ChalkContext, Impl, Obligation}; |
21 | use crate::{ | 21 | use crate::{ |
22 | db::HirDatabase, display::HirDisplay, ApplicationTy, GenericPredicate, ProjectionTy, Substs, | 22 | db::HirDatabase, display::HirDisplay, utils::generics, ApplicationTy, GenericPredicate, |
23 | TraitRef, Ty, TypeCtor, TypeWalk, | 23 | ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk, |
24 | }; | 24 | }; |
25 | 25 | ||
26 | /// This represents a trait whose name we could not resolve. | 26 | /// This represents a trait whose name we could not resolve. |
@@ -547,7 +547,7 @@ pub(crate) fn associated_ty_data_query( | |||
547 | ContainerId::TraitId(t) => t, | 547 | ContainerId::TraitId(t) => t, |
548 | _ => panic!("associated type not in trait"), | 548 | _ => panic!("associated type not in trait"), |
549 | }; | 549 | }; |
550 | let generic_params = db.generic_params(type_alias.into()); | 550 | let generic_params = generics(db, type_alias.into()); |
551 | let bound_data = chalk_rust_ir::AssociatedTyDatumBound { | 551 | let bound_data = chalk_rust_ir::AssociatedTyDatumBound { |
552 | // FIXME add bounds and where clauses | 552 | // FIXME add bounds and where clauses |
553 | bounds: vec![], | 553 | bounds: vec![], |
@@ -589,7 +589,7 @@ pub(crate) fn trait_datum_query( | |||
589 | let trait_: TraitId = from_chalk(db, trait_id); | 589 | let trait_: TraitId = from_chalk(db, trait_id); |
590 | let trait_data = db.trait_data(trait_); | 590 | let trait_data = db.trait_data(trait_); |
591 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); | 591 | debug!("trait {:?} = {:?}", trait_id, trait_data.name); |
592 | let generic_params = db.generic_params(trait_.into()); | 592 | let generic_params = generics(db, trait_.into()); |
593 | let bound_vars = Substs::bound_vars(&generic_params); | 593 | let bound_vars = Substs::bound_vars(&generic_params); |
594 | let flags = chalk_rust_ir::TraitFlags { | 594 | let flags = chalk_rust_ir::TraitFlags { |
595 | auto: trait_data.auto, | 595 | auto: trait_data.auto, |
@@ -626,7 +626,7 @@ pub(crate) fn struct_datum_query( | |||
626 | let where_clauses = type_ctor | 626 | let where_clauses = type_ctor |
627 | .as_generic_def() | 627 | .as_generic_def() |
628 | .map(|generic_def| { | 628 | .map(|generic_def| { |
629 | let generic_params = db.generic_params(generic_def.into()); | 629 | let generic_params = generics(db, generic_def.into()); |
630 | let bound_vars = Substs::bound_vars(&generic_params); | 630 | let bound_vars = Substs::bound_vars(&generic_params); |
631 | convert_where_clauses(db, generic_def, &bound_vars) | 631 | convert_where_clauses(db, generic_def, &bound_vars) |
632 | }) | 632 | }) |
@@ -669,7 +669,7 @@ fn impl_block_datum( | |||
669 | let trait_ref = db.impl_trait(impl_id)?; | 669 | let trait_ref = db.impl_trait(impl_id)?; |
670 | let impl_data = db.impl_data(impl_id); | 670 | let impl_data = db.impl_data(impl_id); |
671 | 671 | ||
672 | let generic_params = db.generic_params(impl_id.into()); | 672 | let generic_params = generics(db, impl_id.into()); |
673 | let bound_vars = Substs::bound_vars(&generic_params); | 673 | let bound_vars = Substs::bound_vars(&generic_params); |
674 | let trait_ref = trait_ref.subst(&bound_vars); | 674 | let trait_ref = trait_ref.subst(&bound_vars); |
675 | let trait_ = trait_ref.trait_; | 675 | let trait_ = trait_ref.trait_; |
@@ -767,7 +767,7 @@ fn type_alias_associated_ty_value( | |||
767 | .trait_data(trait_ref.trait_) | 767 | .trait_data(trait_ref.trait_) |
768 | .associated_type_by_name(&type_alias_data.name) | 768 | .associated_type_by_name(&type_alias_data.name) |
769 | .expect("assoc ty value should not exist"); // validated when building the impl data as well | 769 | .expect("assoc ty value should not exist"); // validated when building the impl data as well |
770 | let generic_params = db.generic_params(impl_id.into()); | 770 | let generic_params = generics(db, impl_id.into()); |
771 | let bound_vars = Substs::bound_vars(&generic_params); | 771 | let bound_vars = Substs::bound_vars(&generic_params); |
772 | let ty = db.ty(type_alias.into()).subst(&bound_vars); | 772 | let ty = db.ty(type_alias.into()).subst(&bound_vars); |
773 | let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) }; | 773 | 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; | |||
5 | use hir_def::{ | 5 | use hir_def::{ |
6 | adt::VariantData, | 6 | adt::VariantData, |
7 | db::DefDatabase, | 7 | db::DefDatabase, |
8 | generics::{GenericParamData, GenericParams}, | ||
8 | resolver::{HasResolver, TypeNs}, | 9 | resolver::{HasResolver, TypeNs}, |
9 | type_ref::TypeRef, | 10 | type_ref::TypeRef, |
10 | TraitId, TypeAliasId, VariantId, | 11 | ContainerId, GenericDefId, GenericParamId, Lookup, TraitId, TypeAliasId, VariantId, |
11 | }; | 12 | }; |
12 | use hir_expand::name::{self, Name}; | 13 | use hir_expand::name::{self, Name}; |
13 | 14 | ||
@@ -82,3 +83,82 @@ pub(crate) fn make_mut_slice<T: Clone>(a: &mut Arc<[T]>) -> &mut [T] { | |||
82 | } | 83 | } |
83 | Arc::get_mut(a).unwrap() | 84 | Arc::get_mut(a).unwrap() |
84 | } | 85 | } |
86 | |||
87 | pub(crate) fn generics(db: &impl DefDatabase, def: GenericDefId) -> Generics { | ||
88 | let parent_generics = parent_generic_def(db, def).map(|def| Box::new(generics(db, def))); | ||
89 | Generics { def, params: db.generic_params(def), parent_generics } | ||
90 | } | ||
91 | |||
92 | pub(crate) struct Generics { | ||
93 | def: GenericDefId, | ||
94 | pub(crate) params: Arc<GenericParams>, | ||
95 | parent_generics: Option<Box<Generics>>, | ||
96 | } | ||
97 | |||
98 | impl Generics { | ||
99 | pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | ||
100 | self.parent_generics | ||
101 | .as_ref() | ||
102 | .into_iter() | ||
103 | .flat_map(|it| it.params.params.iter()) | ||
104 | .chain(self.params.params.iter()) | ||
105 | .enumerate() | ||
106 | .map(|(i, (_local_id, p))| (i as u32, p)) | ||
107 | } | ||
108 | |||
109 | pub(crate) fn iter_parent<'a>( | ||
110 | &'a self, | ||
111 | ) -> impl Iterator<Item = (u32, &'a GenericParamData)> + 'a { | ||
112 | self.parent_generics | ||
113 | .as_ref() | ||
114 | .into_iter() | ||
115 | .flat_map(|it| it.params.params.iter()) | ||
116 | .enumerate() | ||
117 | .map(|(i, (_local_id, p))| (i as u32, p)) | ||
118 | } | ||
119 | |||
120 | pub(crate) fn count_parent_params(&self) -> usize { | ||
121 | self.parent_generics.as_ref().map_or(0, |p| p.count_params_including_parent()) | ||
122 | } | ||
123 | |||
124 | pub(crate) fn count_params_including_parent(&self) -> usize { | ||
125 | let parent_count = self.count_parent_params(); | ||
126 | parent_count + self.params.params.len() | ||
127 | } | ||
128 | pub(crate) fn param_idx(&self, param: GenericParamId) -> u32 { | ||
129 | self.find_param(param).0 | ||
130 | } | ||
131 | pub(crate) fn param_name(&self, param: GenericParamId) -> Name { | ||
132 | self.find_param(param).1.name.clone() | ||
133 | } | ||
134 | fn find_param(&self, param: GenericParamId) -> (u32, &GenericParamData) { | ||
135 | if param.parent == self.def { | ||
136 | let (idx, (_local_id, data)) = self | ||
137 | .params | ||
138 | .params | ||
139 | .iter() | ||
140 | .enumerate() | ||
141 | .find(|(_, (idx, _))| *idx == param.local_id) | ||
142 | .unwrap(); | ||
143 | |||
144 | return ((self.count_parent_params() + idx) as u32, data); | ||
145 | } | ||
146 | self.parent_generics.as_ref().unwrap().find_param(param) | ||
147 | } | ||
148 | } | ||
149 | |||
150 | fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<GenericDefId> { | ||
151 | let container = match def { | ||
152 | GenericDefId::FunctionId(it) => it.lookup(db).container, | ||
153 | GenericDefId::TypeAliasId(it) => it.lookup(db).container, | ||
154 | GenericDefId::ConstId(it) => it.lookup(db).container, | ||
155 | GenericDefId::EnumVariantId(it) => return Some(it.parent.into()), | ||
156 | GenericDefId::AdtId(_) | GenericDefId::TraitId(_) | GenericDefId::ImplId(_) => return None, | ||
157 | }; | ||
158 | |||
159 | match container { | ||
160 | ContainerId::ImplId(it) => Some(it.into()), | ||
161 | ContainerId::TraitId(it) => Some(it.into()), | ||
162 | ContainerId::ModuleId(_) => None, | ||
163 | } | ||
164 | } | ||