Skip to content

Commit 4568e6e

Browse files
committed
[CSBindings] Add finalize method to PotentialBindings
This is used to infer transitive information and adjust existing bindings before solver can consider them.
1 parent 8df19d2 commit 4568e6e

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace swift;
2222
using namespace constraints;
2323

2424
void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
25-
ConstraintSystem &cs,
25+
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
2626
const llvm::SmallDenseMap<TypeVariableType *,
2727
ConstraintSystem::PotentialBindings>
2828
&inferredBindings) {
@@ -43,12 +43,6 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
4343
if (subtypeOf.empty())
4444
return;
4545

46-
// We need to make sure that there are no duplicate bindings in the
47-
// set, otherwise solver would produce multiple identical solutions.
48-
llvm::SmallPtrSet<CanType, 4> existingTypes;
49-
for (const auto &binding : Bindings)
50-
existingTypes.insert(binding.BindingType->getCanonicalType());
51-
5246
for (auto *constraint : subtypeOf) {
5347
auto *tv =
5448
cs.simplifyType(constraint->getFirstType())->getAs<TypeVariableType>();
@@ -90,6 +84,20 @@ void ConstraintSystem::PotentialBindings::inferTransitiveBindings(
9084
}
9185
}
9286

87+
void ConstraintSystem::PotentialBindings::finalize(
88+
ConstraintSystem &cs,
89+
const llvm::SmallDenseMap<TypeVariableType *,
90+
ConstraintSystem::PotentialBindings>
91+
&inferredBindings) {
92+
// We need to make sure that there are no duplicate bindings in the
93+
// set, otherwise solver would produce multiple identical solutions.
94+
llvm::SmallPtrSet<CanType, 4> existingTypes;
95+
for (const auto &binding : Bindings)
96+
existingTypes.insert(binding.BindingType->getCanonicalType());
97+
98+
inferTransitiveBindings(cs, existingTypes, inferredBindings);
99+
}
100+
93101
Optional<ConstraintSystem::PotentialBindings>
94102
ConstraintSystem::determineBestBindings() {
95103
// Look for potential type variable bindings.
@@ -114,7 +122,7 @@ ConstraintSystem::determineBestBindings() {
114122

115123
auto &bindings = cachedBindings->getSecond();
116124

117-
bindings.inferTransitiveBindings(*this, cache);
125+
bindings.finalize(*this, cache);
118126

119127
if (isDebugMode()) {
120128
bindings.dump(typeVar, llvm::errs(), solverState->depth * 2);

lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4569,11 +4569,18 @@ class ConstraintSystem {
45694569
/// \param inferredBindings The set of all bindings inferred for type
45704570
/// variables in the workset.
45714571
void inferTransitiveBindings(
4572-
ConstraintSystem &cs,
4572+
ConstraintSystem &cs, llvm::SmallPtrSetImpl<CanType> &existingTypes,
45734573
const llvm::SmallDenseMap<TypeVariableType *,
45744574
ConstraintSystem::PotentialBindings>
45754575
&inferredBindings);
45764576

4577+
/// Finalize binding computation for this type variable by
4578+
/// inferring bindings from context e.g. transitive bindings.
4579+
void finalize(ConstraintSystem &cs,
4580+
const llvm::SmallDenseMap<TypeVariableType *,
4581+
ConstraintSystem::PotentialBindings>
4582+
&inferredBindings);
4583+
45774584
void dump(llvm::raw_ostream &out,
45784585
unsigned indent = 0) const LLVM_ATTRIBUTE_USED {
45794586
out.indent(indent);

0 commit comments

Comments
 (0)