diff options
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 78 |
1 files changed, 49 insertions, 29 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index fd451a823..3153b5b74 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 | } |
@@ -510,7 +521,7 @@ impl<'a> TyLoweringContext<'a> { | |||
510 | TyDefId::AdtId(it) => Some(it.into()), | 521 | TyDefId::AdtId(it) => Some(it.into()), |
511 | TyDefId::TypeAliasId(it) => Some(it.into()), | 522 | TyDefId::TypeAliasId(it) => Some(it.into()), |
512 | }; | 523 | }; |
513 | let substs = self.substs_from_path_segment(segment, generic_def, infer_args); | 524 | let substs = self.substs_from_path_segment(segment, generic_def, infer_args, None); |
514 | self.db.ty(typeable).subst(&substs) | 525 | self.db.ty(typeable).subst(&substs) |
515 | } | 526 | } |
516 | 527 | ||
@@ -547,7 +558,7 @@ impl<'a> TyLoweringContext<'a> { | |||
547 | (segment, Some(var.parent.into())) | 558 | (segment, Some(var.parent.into())) |
548 | } | 559 | } |
549 | }; | 560 | }; |
550 | self.substs_from_path_segment(segment, generic_def, infer_args) | 561 | self.substs_from_path_segment(segment, generic_def, infer_args, None) |
551 | } | 562 | } |
552 | 563 | ||
553 | fn substs_from_path_segment( | 564 | fn substs_from_path_segment( |
@@ -555,6 +566,7 @@ impl<'a> TyLoweringContext<'a> { | |||
555 | segment: PathSegment<'_>, | 566 | segment: PathSegment<'_>, |
556 | def_generic: Option<GenericDefId>, | 567 | def_generic: Option<GenericDefId>, |
557 | infer_args: bool, | 568 | infer_args: bool, |
569 | explicit_self_ty: Option<Ty>, | ||
558 | ) -> Substitution { | 570 | ) -> Substitution { |
559 | let mut substs = Vec::new(); | 571 | let mut substs = Vec::new(); |
560 | let def_generics = def_generic.map(|def| generics(self.db.upcast(), def)); | 572 | let def_generics = def_generic.map(|def| generics(self.db.upcast(), def)); |
@@ -565,11 +577,19 @@ impl<'a> TyLoweringContext<'a> { | |||
565 | 577 | ||
566 | substs.extend(iter::repeat(TyKind::Unknown.intern(&Interner)).take(parent_params)); | 578 | substs.extend(iter::repeat(TyKind::Unknown.intern(&Interner)).take(parent_params)); |
567 | 579 | ||
580 | let fill_self_params = || { | ||
581 | substs.extend( | ||
582 | explicit_self_ty | ||
583 | .into_iter() | ||
584 | .chain(iter::repeat(TyKind::Unknown.intern(&Interner))) | ||
585 | .take(self_params), | ||
586 | ) | ||
587 | }; | ||
568 | let mut had_explicit_type_args = false; | 588 | let mut had_explicit_type_args = false; |
569 | 589 | ||
570 | if let Some(generic_args) = &segment.args_and_bindings { | 590 | if let Some(generic_args) = &segment.args_and_bindings { |
571 | if !generic_args.has_self_type { | 591 | if !generic_args.has_self_type { |
572 | substs.extend(iter::repeat(TyKind::Unknown.intern(&Interner)).take(self_params)); | 592 | fill_self_params(); |
573 | } | 593 | } |
574 | let expected_num = | 594 | let expected_num = |
575 | if generic_args.has_self_type { self_params + type_params } else { type_params }; | 595 | if generic_args.has_self_type { self_params + type_params } else { type_params }; |
@@ -591,6 +611,8 @@ impl<'a> TyLoweringContext<'a> { | |||
591 | GenericArg::Lifetime(_) => {} | 611 | GenericArg::Lifetime(_) => {} |
592 | } | 612 | } |
593 | } | 613 | } |
614 | } else { | ||
615 | fill_self_params(); | ||
594 | } | 616 | } |
595 | 617 | ||
596 | // handle defaults. In expression or pattern path segments without | 618 | // handle defaults. In expression or pattern path segments without |
@@ -639,10 +661,7 @@ impl<'a> TyLoweringContext<'a> { | |||
639 | segment: PathSegment<'_>, | 661 | segment: PathSegment<'_>, |
640 | explicit_self_ty: Option<Ty>, | 662 | explicit_self_ty: Option<Ty>, |
641 | ) -> TraitRef { | 663 | ) -> TraitRef { |
642 | let mut substs = self.trait_ref_substs_from_path(segment, resolved); | 664 | let substs = self.trait_ref_substs_from_path(segment, resolved, explicit_self_ty); |
643 | if let Some(self_ty) = explicit_self_ty { | ||
644 | substs.0[0] = self_ty; | ||
645 | } | ||
646 | TraitRef { trait_id: to_chalk_trait_id(resolved), substitution: substs } | 665 | TraitRef { trait_id: to_chalk_trait_id(resolved), substitution: substs } |
647 | } | 666 | } |
648 | 667 | ||
@@ -662,15 +681,16 @@ impl<'a> TyLoweringContext<'a> { | |||
662 | &self, | 681 | &self, |
663 | segment: PathSegment<'_>, | 682 | segment: PathSegment<'_>, |
664 | resolved: TraitId, | 683 | resolved: TraitId, |
684 | explicit_self_ty: Option<Ty>, | ||
665 | ) -> Substitution { | 685 | ) -> Substitution { |
666 | self.substs_from_path_segment(segment, Some(resolved.into()), false) | 686 | self.substs_from_path_segment(segment, Some(resolved.into()), false, explicit_self_ty) |
667 | } | 687 | } |
668 | 688 | ||
669 | pub(crate) fn lower_where_predicate( | 689 | pub(crate) fn lower_where_predicate( |
670 | &'a self, | 690 | &'a self, |
671 | where_predicate: &'a WherePredicate, | 691 | where_predicate: &'a WherePredicate, |
672 | ignore_bindings: bool, | 692 | ignore_bindings: bool, |
673 | ) -> impl Iterator<Item = WhereClause> + 'a { | 693 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
674 | match where_predicate { | 694 | match where_predicate { |
675 | WherePredicate::ForLifetime { target, bound, .. } | 695 | WherePredicate::ForLifetime { target, bound, .. } |
676 | | WherePredicate::TypeBound { target, bound } => { | 696 | | WherePredicate::TypeBound { target, bound } => { |
@@ -705,12 +725,12 @@ impl<'a> TyLoweringContext<'a> { | |||
705 | bound: &'a TypeBound, | 725 | bound: &'a TypeBound, |
706 | self_ty: Ty, | 726 | self_ty: Ty, |
707 | ignore_bindings: bool, | 727 | ignore_bindings: bool, |
708 | ) -> impl Iterator<Item = WhereClause> + 'a { | 728 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
709 | let mut bindings = None; | 729 | let mut bindings = None; |
710 | let trait_ref = match bound { | 730 | let trait_ref = match bound { |
711 | TypeBound::Path(path) => { | 731 | TypeBound::Path(path) => { |
712 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); | 732 | bindings = self.lower_trait_ref_from_path(path, Some(self_ty)); |
713 | bindings.clone().map(WhereClause::Implemented) | 733 | bindings.clone().map(WhereClause::Implemented).map(|b| Binders::wrap_empty(b)) |
714 | } | 734 | } |
715 | TypeBound::Lifetime(_) => None, | 735 | TypeBound::Lifetime(_) => None, |
716 | TypeBound::Error => None, | 736 | TypeBound::Error => None, |
@@ -727,7 +747,7 @@ impl<'a> TyLoweringContext<'a> { | |||
727 | &'a self, | 747 | &'a self, |
728 | bound: &'a TypeBound, | 748 | bound: &'a TypeBound, |
729 | trait_ref: TraitRef, | 749 | trait_ref: TraitRef, |
730 | ) -> impl Iterator<Item = WhereClause> + 'a { | 750 | ) -> impl Iterator<Item = QuantifiedWhereClause> + 'a { |
731 | let last_segment = match bound { | 751 | let last_segment = match bound { |
732 | TypeBound::Path(path) => path.segments().last(), | 752 | TypeBound::Path(path) => path.segments().last(), |
733 | TypeBound::Error | TypeBound::Lifetime(_) => None, | 753 | TypeBound::Error | TypeBound::Lifetime(_) => None, |
@@ -743,7 +763,7 @@ impl<'a> TyLoweringContext<'a> { | |||
743 | &binding.name, | 763 | &binding.name, |
744 | ); | 764 | ); |
745 | let (super_trait_ref, associated_ty) = match found { | 765 | let (super_trait_ref, associated_ty) = match found { |
746 | None => return SmallVec::<[WhereClause; 1]>::new(), | 766 | None => return SmallVec::<[QuantifiedWhereClause; 1]>::new(), |
747 | Some(t) => t, | 767 | Some(t) => t, |
748 | }; | 768 | }; |
749 | let projection_ty = ProjectionTy { | 769 | let projection_ty = ProjectionTy { |
@@ -757,7 +777,7 @@ impl<'a> TyLoweringContext<'a> { | |||
757 | let ty = self.lower_ty(type_ref); | 777 | let ty = self.lower_ty(type_ref); |
758 | let alias_eq = | 778 | let alias_eq = |
759 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; | 779 | AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty }; |
760 | preds.push(WhereClause::AliasEq(alias_eq)); | 780 | preds.push(Binders::wrap_empty(WhereClause::AliasEq(alias_eq))); |
761 | } | 781 | } |
762 | for bound in &binding.bounds { | 782 | for bound in &binding.bounds { |
763 | preds.extend(self.lower_type_bound( | 783 | preds.extend(self.lower_type_bound( |
@@ -814,7 +834,7 @@ pub fn associated_type_shorthand_candidates<R>( | |||
814 | let predicates = db.generic_predicates_for_param(param_id); | 834 | let predicates = db.generic_predicates_for_param(param_id); |
815 | let mut traits_: Vec<_> = predicates | 835 | let mut traits_: Vec<_> = predicates |
816 | .iter() | 836 | .iter() |
817 | .filter_map(|pred| match &pred.value { | 837 | .filter_map(|pred| match &pred.value.value { |
818 | WhereClause::Implemented(tr) => Some(tr.clone()), | 838 | WhereClause::Implemented(tr) => Some(tr.clone()), |
819 | _ => None, | 839 | _ => None, |
820 | }) | 840 | }) |
@@ -887,7 +907,7 @@ pub(crate) fn field_types_query( | |||
887 | pub(crate) fn generic_predicates_for_param_query( | 907 | pub(crate) fn generic_predicates_for_param_query( |
888 | db: &dyn HirDatabase, | 908 | db: &dyn HirDatabase, |
889 | param_id: TypeParamId, | 909 | param_id: TypeParamId, |
890 | ) -> Arc<[Binders<WhereClause>]> { | 910 | ) -> Arc<[Binders<QuantifiedWhereClause>]> { |
891 | let resolver = param_id.parent.resolver(db.upcast()); | 911 | let resolver = param_id.parent.resolver(db.upcast()); |
892 | let ctx = | 912 | let ctx = |
893 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 913 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
@@ -915,7 +935,7 @@ pub(crate) fn generic_predicates_for_param_recover( | |||
915 | _db: &dyn HirDatabase, | 935 | _db: &dyn HirDatabase, |
916 | _cycle: &[String], | 936 | _cycle: &[String], |
917 | _param_id: &TypeParamId, | 937 | _param_id: &TypeParamId, |
918 | ) -> Arc<[Binders<WhereClause>]> { | 938 | ) -> Arc<[Binders<QuantifiedWhereClause>]> { |
919 | Arc::new([]) | 939 | Arc::new([]) |
920 | } | 940 | } |
921 | 941 | ||
@@ -930,7 +950,7 @@ pub(crate) fn trait_environment_query( | |||
930 | let mut clauses = Vec::new(); | 950 | let mut clauses = Vec::new(); |
931 | for pred in resolver.where_predicates_in_scope() { | 951 | for pred in resolver.where_predicates_in_scope() { |
932 | for pred in ctx.lower_where_predicate(pred, false) { | 952 | for pred in ctx.lower_where_predicate(pred, false) { |
933 | if let WhereClause::Implemented(tr) = &pred { | 953 | if let WhereClause::Implemented(tr) = &pred.skip_binders() { |
934 | traits_in_scope.push((tr.self_type_parameter().clone(), tr.hir_trait_id())); | 954 | traits_in_scope.push((tr.self_type_parameter().clone(), tr.hir_trait_id())); |
935 | } | 955 | } |
936 | let program_clause: chalk_ir::ProgramClause<Interner> = | 956 | let program_clause: chalk_ir::ProgramClause<Interner> = |
@@ -970,7 +990,7 @@ pub(crate) fn trait_environment_query( | |||
970 | pub(crate) fn generic_predicates_query( | 990 | pub(crate) fn generic_predicates_query( |
971 | db: &dyn HirDatabase, | 991 | db: &dyn HirDatabase, |
972 | def: GenericDefId, | 992 | def: GenericDefId, |
973 | ) -> Arc<[Binders<WhereClause>]> { | 993 | ) -> Arc<[Binders<QuantifiedWhereClause>]> { |
974 | let resolver = def.resolver(db.upcast()); | 994 | let resolver = def.resolver(db.upcast()); |
975 | let ctx = | 995 | let ctx = |
976 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 996 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |