diff options
author | Florian Diebold <[email protected]> | 2021-03-21 12:22:22 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-03-21 17:01:14 +0000 |
commit | 590c41635952e19c3caae525a827499dbd360049 (patch) | |
tree | eca7c162975ef901b723d255512e61c047e838b8 /crates/hir_ty/src/lower.rs | |
parent | 35868c4f7dc479dd5f731a2785ec6a203046ea9c (diff) |
Introduce QuantifiedWhereClause and DynTy analogous to Chalk
This introduces a bunch of new binders in lots of places, which we have
to be careful about, but we had to add them at some point.
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index fd451a823..45591e920 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -33,9 +33,10 @@ use crate::{ | |||
33 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, | 33 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, |
34 | variant_data, | 34 | variant_data, |
35 | }, | 35 | }, |
36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, ImplTraitId, | 36 | AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, |
37 | OpaqueTy, PolyFnSig, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, | 37 | ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, |
38 | TraitEnvironment, TraitRef, Ty, TyKind, TypeWalk, WhereClause, | 38 | ReturnTypeImplTrait, ReturnTypeImplTraits, Substitution, TraitEnvironment, TraitRef, Ty, |
39 | TyKind, TypeWalk, WhereClause, | ||
39 | }; | 40 | }; |
40 | 41 | ||
41 | #[derive(Debug)] | 42 | #[derive(Debug)] |
@@ -188,13 +189,14 @@ impl<'a> TyLoweringContext<'a> { | |||
188 | TypeRef::DynTrait(bounds) => { | 189 | TypeRef::DynTrait(bounds) => { |
189 | let self_ty = | 190 | let self_ty = |
190 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner); | 191 | TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)).intern(&Interner); |
191 | let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 192 | let bounds = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
192 | bounds | 193 | QuantifiedWhereClauses::from_iter( |
193 | .iter() | 194 | &Interner, |
194 | .flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)) | 195 | bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)), |
195 | .collect() | 196 | ) |
196 | }); | 197 | }); |
197 | TyKind::Dyn(predicates).intern(&Interner) | 198 | let bounds = Binders::new(1, bounds); |
199 | TyKind::Dyn(DynTy { bounds }).intern(&Interner) | ||
198 | } | 200 | } |
199 | TypeRef::ImplTrait(bounds) => { | 201 | TypeRef::ImplTrait(bounds) => { |
200 | match self.impl_trait_mode { | 202 | match self.impl_trait_mode { |
@@ -376,7 +378,16 @@ impl<'a> TyLoweringContext<'a> { | |||
376 | // FIXME report error (ambiguous associated type) | 378 | // FIXME report error (ambiguous associated type) |
377 | TyKind::Unknown.intern(&Interner) | 379 | TyKind::Unknown.intern(&Interner) |
378 | } else { | 380 | } else { |
379 | TyKind::Dyn(Arc::new([WhereClause::Implemented(trait_ref)])).intern(&Interner) | 381 | let dyn_ty = DynTy { |
382 | bounds: Binders::new( | ||
383 | 1, | ||
384 | QuantifiedWhereClauses::from_iter( | ||
385 | &Interner, | ||
386 | Some(Binders::wrap_empty(WhereClause::Implemented(trait_ref))), | ||
387 | ), | ||
388 | ), | ||
389 | }; | ||
390 | TyKind::Dyn(dyn_ty).intern(&Interner) | ||
380 | }; | 391 | }; |
381 | return (ty, None); | 392 | return (ty, None); |
382 | } | 393 | } |
@@ -670,7 +681,7 @@ impl<'a> TyLoweringContext<'a> { | |||
670 | &'a self, | 681 | &'a self, |
671 | where_predicate: &'a WherePredicate, | 682 | where_predicate: &'a WherePredicate, |
672 | ignore_bindings: bool, | 683 | ignore_bindings: bool, |
673 | ) -> impl Iterator<Item = WhereClause> + 'a { | 684 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
674 | match where_predicate { | 685 | match where_predicate { |
675 | WherePredicate::ForLifetime { target, bound, .. } | 686 | WherePredicate::ForLifetime { target, bound, .. } |
676 | | WherePredicate::TypeBound { target, bound } => { | 687 | | WherePredicate::TypeBound { target, bound } => { |
@@ -705,12 +716,12 @@ impl<'a> TyLoweringContext<'a> { | |||
705 | bound: &'a TypeBound, | 716 | bound: &'a TypeBound, |
706 | self_ty: Ty, | 717 | self_ty: Ty, |
707 | ignore_bindings: bool, | 718 | ignore_bindings: bool, |
708 | ) -> impl Iterator<Item = WhereClause> + 'a { | 719 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
709 | let mut bindings = None; | 720 | let mut bindings = None; |
710 | let trait_ref = match bound { | 721 | let trait_ref = match bound { |
711 | TypeBound::Path(path) => { | 722 | TypeBound::Path(path) => { |
712 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); | 723 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); |
713 | bindings.clone().map(WhereClause::Implemented) | 724 | bindings.clone().map(WhereClause::Implemented).map(|b| Binders::wrap_empty(b)) |
714 | } | 725 | } |
715 | TypeBound::Lifetime(_) => None, | 726 | TypeBound::Lifetime(_) => None, |
716 | TypeBound::Error => None, | 727 | TypeBound::Error => None, |
@@ -727,7 +738,7 @@ impl<'a> TyLoweringContext<'a> { | |||
727 | &'a self, | 738 | &'a self, |
728 | bound: &'a TypeBound, | 739 | bound: &'a TypeBound, |
729 | trait_ref: TraitRef, | 740 | trait_ref: TraitRef, |
730 | ) -> impl Iterator<Item = WhereClause> + 'a { | 741 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
731 | let last_segment = match bound { | 742 | let last_segment = match bound { |
732 | TypeBound::Path(path) => path.segments().last(), | 743 | TypeBound::Path(path) => path.segments().last(), |
733 | TypeBound::Error | TypeBound::Lifetime(_) => None, | 744 | TypeBound::Error | TypeBound::Lifetime(_) => None, |
@@ -743,7 +754,7 @@ impl<'a> TyLoweringContext<'a> { | |||
743 | &binding.name, | 754 | &binding.name, |
744 | ); | 755 | ); |
745 | let (super_trait_ref, associated_ty) = match found { | 756 | let (super_trait_ref, associated_ty) = match found { |
746 | None => return SmallVec::<[WhereClause; 1]>::new(), | 757 | None => return SmallVec::<[QuantifiedWhereClause; 1]>::new(), |
747 | Some(t) => t, | 758 | Some(t) => t, |
748 | }; | 759 | }; |
749 | let projection_ty = ProjectionTy { | 760 | let projection_ty = ProjectionTy { |
@@ -757,7 +768,7 @@ impl<'a> TyLoweringContext<'a> { | |||
757 | let ty = self.lower_ty(type_ref); | 768 | let ty = self.lower_ty(type_ref); |
758 | let alias_eq = | 769 | let alias_eq = |
759 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; | 770 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; |
760 | preds.push(WhereClause::AliasEq(alias_eq)); | 771 | preds.push(Binders::wrap_empty(WhereClause::AliasEq(alias_eq))); |
761 | } | 772 | } |
762 | for bound in &binding.bounds { | 773 | for bound in &binding.bounds { |
763 | preds.extend(self.lower_type_bound( | 774 | preds.extend(self.lower_type_bound( |
@@ -888,6 +899,9 @@ pub(crate) fn generic_predicates_for_param_query( | |||
888 | db: &dyn HirDatabase, | 899 | db: &dyn HirDatabase, |
889 | param_id: TypeParamId, | 900 | param_id: TypeParamId, |
890 | ) -> Arc<[Binders<WhereClause>]> { | 901 | ) -> Arc<[Binders<WhereClause>]> { |
902 | // FIXME: these binders are for the type parameters of the def. We need to | ||
903 | // introduce another level of binders for quantified where clauses (for<'a> | ||
904 | // ...) | ||
891 | let resolver = param_id.parent.resolver(db.upcast()); | 905 | let resolver = param_id.parent.resolver(db.upcast()); |
892 | let ctx = | 906 | let ctx = |
893 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 907 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
@@ -906,7 +920,7 @@ pub(crate) fn generic_predicates_for_param_query( | |||
906 | WherePredicate::Lifetime { .. } => false, | 920 | WherePredicate::Lifetime { .. } => false, |
907 | }) | 921 | }) |
908 | .flat_map(|pred| { | 922 | .flat_map(|pred| { |
909 | ctx.lower_where_predicate(pred, true).map(|p| Binders::new(generics.len(), p)) | 923 | ctx.lower_where_predicate(pred, true).map(|p| Binders::new(generics.len(), p.value)) |
910 | }) | 924 | }) |
911 | .collect() | 925 | .collect() |
912 | } | 926 | } |
@@ -930,7 +944,7 @@ pub(crate) fn trait_environment_query( | |||
930 | let mut clauses = Vec::new(); | 944 | let mut clauses = Vec::new(); |
931 | for pred in resolver.where_predicates_in_scope() { | 945 | for pred in resolver.where_predicates_in_scope() { |
932 | for pred in ctx.lower_where_predicate(pred, false) { | 946 | for pred in ctx.lower_where_predicate(pred, false) { |
933 | if let WhereClause::Implemented(tr) = &pred { | 947 | if let WhereClause::Implemented(tr) = &pred.skip_binders() { |
934 | traits_in_scope.push((tr.self_type_parameter().clone(), tr.hir_trait_id())); | 948 | traits_in_scope.push((tr.self_type_parameter().clone(), tr.hir_trait_id())); |
935 | } | 949 | } |
936 | let program_clause: chalk_ir::ProgramClause<Interner> = | 950 | let program_clause: chalk_ir::ProgramClause<Interner> = |
@@ -971,6 +985,9 @@ pub(crate) fn generic_predicates_query( | |||
971 | db: &dyn HirDatabase, | 985 | db: &dyn HirDatabase, |
972 | def: GenericDefId, | 986 | def: GenericDefId, |
973 | ) -> Arc<[Binders<WhereClause>]> { | 987 | ) -> Arc<[Binders<WhereClause>]> { |
988 | // FIXME: these binders are for the type parameters of the def. We need to | ||
989 | // introduce another level of binders for quantified where clauses (for<'a> | ||
990 | // ...) | ||
974 | let resolver = def.resolver(db.upcast()); | 991 | let resolver = def.resolver(db.upcast()); |
975 | let ctx = | 992 | let ctx = |
976 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 993 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
@@ -978,7 +995,7 @@ pub(crate) fn generic_predicates_query( | |||
978 | resolver | 995 | resolver |
979 | .where_predicates_in_scope() | 996 | .where_predicates_in_scope() |
980 | .flat_map(|pred| { | 997 | .flat_map(|pred| { |
981 | ctx.lower_where_predicate(pred, false).map(|p| Binders::new(generics.len(), p)) | 998 | ctx.lower_where_predicate(pred, false).map(|p| Binders::new(generics.len(), p.value)) |
982 | }) | 999 | }) |
983 | .collect() | 1000 | .collect() |
984 | } | 1001 | } |