diff options
author | Florian Diebold <[email protected]> | 2020-01-31 14:17:48 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2020-02-07 17:28:10 +0000 |
commit | dbc14f9d570e5bc1ddae05e9ccd8f163082b3cac (patch) | |
tree | adde2b49413960c2741b18402a6cb5757b346804 /crates/ra_hir_ty | |
parent | a9430865b319ab2fc28602fe98df21146e54b7bf (diff) |
First stab at desugaring bounds for APIT
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r-- | crates/ra_hir_ty/src/lower.rs | 29 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/utils.rs | 10 |
2 files changed, 33 insertions, 6 deletions
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 459b96280..f1a11e073 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs | |||
@@ -10,7 +10,7 @@ use std::sync::Arc; | |||
10 | 10 | ||
11 | use hir_def::{ | 11 | use hir_def::{ |
12 | builtin_type::BuiltinType, | 12 | builtin_type::BuiltinType, |
13 | generics::WherePredicate, | 13 | generics::{WherePredicateTarget, WherePredicate}, |
14 | path::{GenericArg, Path, PathSegment, PathSegments}, | 14 | path::{GenericArg, Path, PathSegment, PathSegments}, |
15 | resolver::{HasResolver, Resolver, TypeNs}, | 15 | resolver::{HasResolver, Resolver, TypeNs}, |
16 | type_ref::{TypeBound, TypeRef}, | 16 | type_ref::{TypeBound, TypeRef}, |
@@ -505,7 +505,22 @@ impl GenericPredicate { | |||
505 | ctx: &'a TyLoweringContext<'a, impl HirDatabase>, | 505 | ctx: &'a TyLoweringContext<'a, impl HirDatabase>, |
506 | where_predicate: &'a WherePredicate, | 506 | where_predicate: &'a WherePredicate, |
507 | ) -> impl Iterator<Item = GenericPredicate> + 'a { | 507 | ) -> impl Iterator<Item = GenericPredicate> + 'a { |
508 | let self_ty = Ty::from_hir(ctx, &where_predicate.type_ref); | 508 | let self_ty = match &where_predicate.target { |
509 | WherePredicateTarget::TypeRef(type_ref) => Ty::from_hir(ctx, type_ref), | ||
510 | WherePredicateTarget::TypeParam(param_id) => { | ||
511 | let generic_def = ctx.resolver.generic_def().expect("generics in scope"); | ||
512 | let generics = generics(ctx.db, generic_def); | ||
513 | let param_id = hir_def::TypeParamId { parent: generic_def, local_id: *param_id }; | ||
514 | let idx = generics.param_idx(param_id); | ||
515 | match ctx.type_param_mode { | ||
516 | TypeParamLoweringMode::Placeholder => { | ||
517 | let name = generics.param_name(param_id); | ||
518 | Ty::Param { idx, name } | ||
519 | }, | ||
520 | TypeParamLoweringMode::Variable => Ty::Bound(idx), | ||
521 | } | ||
522 | }, | ||
523 | }; | ||
509 | GenericPredicate::from_type_bound(ctx, &where_predicate.bound, self_ty) | 524 | GenericPredicate::from_type_bound(ctx, &where_predicate.bound, self_ty) |
510 | } | 525 | } |
511 | 526 | ||
@@ -595,10 +610,18 @@ pub(crate) fn generic_predicates_for_param_query( | |||
595 | ) -> Arc<[GenericPredicate]> { | 610 | ) -> Arc<[GenericPredicate]> { |
596 | let resolver = def.resolver(db); | 611 | let resolver = def.resolver(db); |
597 | let ctx = TyLoweringContext::new(db, &resolver); | 612 | let ctx = TyLoweringContext::new(db, &resolver); |
613 | let generics = generics(db, def); | ||
598 | resolver | 614 | resolver |
599 | .where_predicates_in_scope() | 615 | .where_predicates_in_scope() |
600 | // we have to filter out all other predicates *first*, before attempting to lower them | 616 | // we have to filter out all other predicates *first*, before attempting to lower them |
601 | .filter(|pred| Ty::from_hir_only_param(&ctx, &pred.type_ref) == Some(param_idx)) | 617 | .filter(|pred| match &pred.target { |
618 | WherePredicateTarget::TypeRef(type_ref) => Ty::from_hir_only_param(&ctx, type_ref) == Some(param_idx), | ||
619 | WherePredicateTarget::TypeParam(local_id) => { | ||
620 | let param_id = hir_def::TypeParamId { parent: def, local_id: *local_id }; | ||
621 | let idx = generics.param_idx(param_id); | ||
622 | idx == param_idx | ||
623 | } | ||
624 | }) | ||
602 | .flat_map(|pred| GenericPredicate::from_where_predicate(&ctx, pred)) | 625 | .flat_map(|pred| GenericPredicate::from_where_predicate(&ctx, pred)) |
603 | .collect() | 626 | .collect() |
604 | } | 627 | } |
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs index f116b95e7..77b7de729 100644 --- a/crates/ra_hir_ty/src/utils.rs +++ b/crates/ra_hir_ty/src/utils.rs | |||
@@ -12,6 +12,7 @@ use hir_def::{ | |||
12 | AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, | 12 | AssocContainerId, GenericDefId, Lookup, TraitId, TypeAliasId, TypeParamId, VariantId, |
13 | }; | 13 | }; |
14 | use hir_expand::name::{name, Name}; | 14 | use hir_expand::name::{name, Name}; |
15 | use hir_def::generics::WherePredicateTarget; | ||
15 | 16 | ||
16 | fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> { | 17 | fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> { |
17 | let resolver = trait_.resolver(db); | 18 | let resolver = trait_.resolver(db); |
@@ -19,11 +20,14 @@ fn direct_super_traits(db: &impl DefDatabase, trait_: TraitId) -> Vec<TraitId> { | |||
19 | // lifetime problems, but since there usually shouldn't be more than a | 20 | // lifetime problems, but since there usually shouldn't be more than a |
20 | // few direct traits this should be fine (we could even use some kind of | 21 | // few direct traits this should be fine (we could even use some kind of |
21 | // SmallVec if performance is a concern) | 22 | // SmallVec if performance is a concern) |
22 | db.generic_params(trait_.into()) | 23 | let generic_params = db.generic_params(trait_.into()); |
24 | let trait_self = generic_params.find_trait_self_param(); | ||
25 | generic_params | ||
23 | .where_predicates | 26 | .where_predicates |
24 | .iter() | 27 | .iter() |
25 | .filter_map(|pred| match &pred.type_ref { | 28 | .filter_map(|pred| match &pred.target { |
26 | TypeRef::Path(p) if p == &Path::from(name![Self]) => pred.bound.as_path(), | 29 | WherePredicateTarget::TypeRef(TypeRef::Path(p)) if p == &Path::from(name![Self]) => pred.bound.as_path(), |
30 | WherePredicateTarget::TypeParam(local_id) if Some(*local_id) == trait_self => pred.bound.as_path(), | ||
27 | _ => None, | 31 | _ => None, |
28 | }) | 32 | }) |
29 | .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { | 33 | .filter_map(|path| match resolver.resolve_path_in_type_ns_fully(db, path.mod_path()) { |