Skip to content

Commit a9cce60

Browse files
committed
[CSBindings] Shrink binding "sources" to supertype and equivalence only
Instead of recording all of the binding "sources" let's only record subtype, supertype and equivalence relationships which didn't materialize as bindings (because other side is a type variable). This is the only information necessary to infer transitive bindings and protocol requirements.
1 parent 0c51159 commit a9cce60

File tree

2 files changed

+12
-38
lines changed

2 files changed

+12
-38
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4741,15 +4741,13 @@ class ConstraintSystem {
47414741
/// Tracks the position of the last known supertype in the group.
47424742
Optional<unsigned> lastSupertypeIndex;
47434743

4744-
/// A set of all constraints which contribute to pontential bindings.
4745-
llvm::SmallPtrSet<Constraint *, 8> Sources;
4746-
47474744
/// A set of all not-yet-resolved type variables this type variable
4748-
/// is a subtype of. This is used to determine ordering inside a
4749-
/// chain of subtypes because binding inference algorithm can't,
4750-
/// at the moment, determine bindings transitively through supertype
4751-
/// type variables.
4752-
llvm::SmallDenseMap<TypeVariableType *, Constraint *, 4> SubtypeOf;
4745+
/// is a subtype of, supertype of or is equivalent to. This is used
4746+
/// to determine ordering inside of a chain of subtypes to help infer
4747+
/// transitive bindings and protocol requirements.
4748+
llvm::SmallMapVector<TypeVariableType *, Constraint *, 4> SubtypeOf;
4749+
llvm::SmallMapVector<TypeVariableType *, Constraint *, 4> SupertypeOf;
4750+
llvm::SmallMapVector<TypeVariableType *, Constraint *, 4> EquivalentTo;
47534751

47544752
PotentialBindings(TypeVariableType *typeVar)
47554753
: TypeVar(typeVar), PotentiallyIncomplete(isGenericParameter()) {}

lib/Sema/CSBindings.cpp

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,8 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
2929
&inferredBindings) {
3030
using BindingKind = ConstraintSystem::AllowedBindingKind;
3131

32-
llvm::SmallVector<Constraint *, 4> conversions;
33-
// First, let's collect all of the conversions associated
34-
// with this type variable.
35-
llvm::copy_if(
36-
Sources, std::back_inserter(conversions),
37-
[&](const Constraint *constraint) -> bool {
38-
if (constraint->getKind() != ConstraintKind::Subtype &&
39-
constraint->getKind() != ConstraintKind::Conversion &&
40-
constraint->getKind() != ConstraintKind::ArgumentConversion &&
41-
constraint->getKind() != ConstraintKind::OperatorArgumentConversion)
42-
return false;
43-
44-
auto rhs = cs.simplifyType(constraint->getSecondType());
45-
return rhs->getAs<TypeVariableType>() == TypeVar;
46-
});
47-
48-
for (auto *constraint : conversions) {
49-
auto *tv =
50-
cs.simplifyType(constraint->getFirstType())->getAs<TypeVariableType>();
51-
if (!tv || tv == TypeVar)
52-
continue;
53-
54-
auto relatedBindings = inferredBindings.find(tv);
32+
for (const auto &entry : SupertypeOf) {
33+
auto relatedBindings = inferredBindings.find(entry.first);
5534
if (relatedBindings == inferredBindings.end())
5635
continue;
5736

@@ -89,7 +68,7 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
8968
llvm::copy(bindings.Defaults, std::back_inserter(Defaults));
9069

9170
// TODO: We shouldn't need this in the future.
92-
if (constraint->getKind() != ConstraintKind::Subtype)
71+
if (entry.second->getKind() != ConstraintKind::Subtype)
9372
continue;
9473

9574
for (auto &binding : bindings.Bindings) {
@@ -670,10 +649,6 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
670649

671650
auto *typeVar = result.TypeVar;
672651

673-
// Record constraint which contributes to the
674-
// finding of potential bindings.
675-
result.Sources.insert(constraint);
676-
677652
auto first = simplifyType(constraint->getFirstType());
678653
auto second = simplifyType(constraint->getSecondType());
679654

@@ -798,15 +773,16 @@ ConstraintSystem::getPotentialBindingForRelationalConstraint(
798773
if (kind == AllowedBindingKind::Subtypes) {
799774
result.SubtypeOf.insert({bindingTypeVar, constraint});
800775
} else {
801-
// TODO: record this type variable as a `supertypeOf`
776+
assert(kind == AllowedBindingKind::Supertypes);
777+
result.SupertypeOf.insert({bindingTypeVar, constraint});
802778
}
803779
break;
804780
}
805781

806782
case ConstraintKind::Bind:
807783
case ConstraintKind::BindParam:
808784
case ConstraintKind::Equal: {
809-
// TODO: record this type variable as being equal to other type variable.
785+
result.EquivalentTo.insert({bindingTypeVar, constraint});
810786
break;
811787
}
812788

0 commit comments

Comments
 (0)