@@ -44,9 +44,6 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
44
44
return rhs->getAs <TypeVariableType>() == TypeVar;
45
45
});
46
46
47
- if (conversions.empty ())
48
- return ;
49
-
50
47
for (auto *constraint : conversions) {
51
48
auto *tv =
52
49
cs.simplifyType (constraint->getFirstType ())->getAs <TypeVariableType>();
@@ -57,16 +54,19 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
57
54
if (relatedBindings == inferredBindings.end ())
58
55
continue ;
59
56
57
+ auto &bindings = relatedBindings->getSecond ();
58
+
60
59
// Infer transitive protocol requirements.
61
- for (auto *protocol : relatedBindings->getSecond ().Protocols ) {
62
- Protocols.push_back (protocol);
63
- }
60
+ llvm::copy (bindings.Protocols , std::back_inserter (Protocols));
61
+
62
+ // Infer transitive defaults.
63
+ llvm::copy (bindings.Defaults , std::back_inserter (Defaults));
64
64
65
65
// TODO: We shouldn't need this in the future.
66
66
if (constraint->getKind () != ConstraintKind::Subtype)
67
67
continue ;
68
68
69
- for (auto &binding : relatedBindings-> getSecond () .Bindings ) {
69
+ for (auto &binding : bindings .Bindings ) {
70
70
// We need the binding kind for the potential binding to
71
71
// either be Exact or Supertypes in order for it to make sense
72
72
// to add Supertype bindings based on the relationship between
@@ -92,6 +92,27 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
92
92
}
93
93
}
94
94
95
+ void ConstraintSystem::PotentialBindings::inferDefaultTypes (
96
+ ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes) {
97
+ // / Add defaultable constraints.
98
+ for (auto *constraint : Defaults) {
99
+ Type type = constraint->getSecondType ();
100
+ if (!existingTypes.insert (type->getCanonicalType ()).second )
101
+ continue ;
102
+
103
+ if (constraint->getKind () == ConstraintKind::DefaultClosureType) {
104
+ // If there are no other possible bindings for this closure
105
+ // let's default it to the type inferred from its parameters/body,
106
+ // otherwise we should only attempt contextual types as a
107
+ // top-level closure type.
108
+ if (!Bindings.empty ())
109
+ continue ;
110
+ }
111
+
112
+ addPotentialBinding ({type, AllowedBindingKind::Exact, constraint});
113
+ }
114
+ }
115
+
95
116
void ConstraintSystem::PotentialBindings::finalize (
96
117
ConstraintSystem &cs,
97
118
const llvm::SmallDenseMap<TypeVariableType *,
@@ -105,6 +126,8 @@ void ConstraintSystem::PotentialBindings::finalize(
105
126
106
127
inferTransitiveBindings (cs, existingTypes, inferredBindings);
107
128
129
+ inferDefaultTypes (cs, existingTypes);
130
+
108
131
// Adjust optionality of existing bindings based on presence of
109
132
// `ExpressibleByNilLiteral` requirement.
110
133
if (llvm::any_of (Protocols, [](Constraint *constraint) {
@@ -153,6 +176,17 @@ void ConstraintSystem::PotentialBindings::finalize(
153
176
154
177
addPotentialBinding (PotentialBinding::forHole (cs.getASTContext (), locator));
155
178
}
179
+
180
+ // Determine if the bindings only constrain the type variable from above with
181
+ // an existential type; such a binding is not very helpful because it's
182
+ // impossible to enumerate the existential type's subtypes.
183
+ if (!Bindings.empty ()) {
184
+ SubtypeOfExistentialType =
185
+ llvm::all_of (Bindings, [](const PotentialBinding &binding) {
186
+ return binding.BindingType ->isExistentialType () &&
187
+ binding.Kind == AllowedBindingKind::Subtypes;
188
+ });
189
+ }
156
190
}
157
191
158
192
Optional<ConstraintSystem::PotentialBindings>
@@ -516,7 +550,6 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
516
550
517
551
// Consider each of the constraints related to this type variable.
518
552
llvm::SmallPtrSet<CanType, 4 > exactTypes;
519
- SmallVector<Constraint *, 2 > defaultableConstraints;
520
553
SmallVector<PotentialBinding, 4 > literalBindings;
521
554
bool hasNonDependentMemberRelationalConstraints = false ;
522
555
bool hasDependentMemberRelationalConstraints = false ;
@@ -636,7 +669,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
636
669
// Do these in a separate pass.
637
670
if (getFixedTypeRecursive (constraint->getFirstType (), true )
638
671
->getAs <TypeVariableType>() == typeVar) {
639
- defaultableConstraints .push_back (constraint);
672
+ result. Defaults .push_back (constraint);
640
673
hasNonDependentMemberRelationalConstraints = true ;
641
674
}
642
675
break ;
@@ -854,34 +887,6 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) const {
854
887
}
855
888
}
856
889
857
- // / Add defaultable constraints last.
858
- for (auto constraint : defaultableConstraints) {
859
- Type type = constraint->getSecondType ();
860
- if (!exactTypes.insert (type->getCanonicalType ()).second )
861
- continue ;
862
-
863
- if (constraint->getKind () == ConstraintKind::DefaultClosureType) {
864
- // If there are no other possible bindings for this closure
865
- // let's default it to the type inferred from its parameters/body,
866
- // otherwise we should only attempt contextual types as a
867
- // top-level closure type.
868
- if (!result.Bindings .empty ())
869
- continue ;
870
- }
871
-
872
- result.addPotentialBinding ({type, AllowedBindingKind::Exact, constraint});
873
- }
874
-
875
- // Determine if the bindings only constrain the type variable from above with
876
- // an existential type; such a binding is not very helpful because it's
877
- // impossible to enumerate the existential type's subtypes.
878
- result.SubtypeOfExistentialType =
879
- std::all_of (result.Bindings .begin (), result.Bindings .end (),
880
- [](const PotentialBinding &binding) {
881
- return binding.BindingType ->isExistentialType () &&
882
- binding.Kind == AllowedBindingKind::Subtypes;
883
- });
884
-
885
890
// If there were both dependent-member and non-dependent-member relational
886
891
// constraints, consider this "fully bound"; we don't want to touch it.
887
892
if (hasDependentMemberRelationalConstraints) {
0 commit comments