Skip to content

Commit 6c43424

Browse files
authored
Merge pull request #11951 from xedin/add-binding-refactor
[ConstraintSolver] NFC: Move `addPotentialBinding` into PotentialBindings
2 parents d008e37 + 8c2fa12 commit 6c43424

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,6 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
142142
typeVar, constraints, ConstraintGraph::GatheringKind::EquivalenceClass);
143143

144144
PotentialBindings result;
145-
Optional<unsigned> lastSupertypeIndex;
146-
147-
// Local function to add a potential binding to the list of bindings,
148-
// coalescing supertype bounds when we are able to compute the meet.
149-
auto addPotentialBinding = [&](PotentialBinding binding,
150-
bool allowJoinMeet = true) {
151-
assert(!binding.BindingType->is<ErrorType>());
152-
// If this is a non-defaulted supertype binding, check whether we can
153-
// combine it with another supertype binding by computing the 'join' of the
154-
// types.
155-
if (binding.Kind == AllowedBindingKind::Supertypes &&
156-
!binding.BindingType->hasTypeVariable() && !binding.DefaultedProtocol &&
157-
!binding.isDefaultableBinding() && allowJoinMeet) {
158-
if (lastSupertypeIndex) {
159-
// Can we compute a join?
160-
auto &lastBinding = result.Bindings[*lastSupertypeIndex];
161-
auto lastType = lastBinding.BindingType->getWithoutSpecifierType();
162-
auto bindingType = binding.BindingType->getWithoutSpecifierType();
163-
if (auto join = Type::join(lastType, bindingType)) {
164-
// Replace the last supertype binding with the join. We're done.
165-
lastBinding.BindingType = join;
166-
return;
167-
}
168-
}
169-
170-
// Record this as the most recent supertype index.
171-
lastSupertypeIndex = result.Bindings.size();
172-
}
173-
174-
result.Bindings.push_back(std::move(binding));
175-
};
176145

177146
// Consider each of the constraints related to this type variable.
178147
llvm::SmallPtrSet<CanType, 4> exactTypes;
@@ -284,8 +253,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
284253
continue;
285254

286255
result.foundLiteralBinding(constraint->getProtocol());
287-
addPotentialBinding({defaultType, AllowedBindingKind::Subtypes,
288-
constraint->getProtocol()});
256+
result.addPotentialBinding({defaultType, AllowedBindingKind::Subtypes,
257+
constraint->getProtocol()});
289258
continue;
290259
}
291260

@@ -311,8 +280,8 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
311280
if (!matched) {
312281
result.foundLiteralBinding(constraint->getProtocol());
313282
exactTypes.insert(defaultType->getCanonicalType());
314-
addPotentialBinding({defaultType, AllowedBindingKind::Subtypes,
315-
constraint->getProtocol()});
283+
result.addPotentialBinding({defaultType, AllowedBindingKind::Subtypes,
284+
constraint->getProtocol()});
316285
}
317286

318287
continue;
@@ -484,10 +453,12 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
484453
}
485454

486455
if (exactTypes.insert(type->getCanonicalType()).second)
487-
addPotentialBinding({type, kind, None}, /*allowJoinMeet=*/!adjustedIUO);
456+
result.addPotentialBinding({type, kind, None},
457+
/*allowJoinMeet=*/!adjustedIUO);
488458
if (alternateType &&
489459
exactTypes.insert(alternateType->getCanonicalType()).second)
490-
addPotentialBinding({alternateType, kind, None}, /*allowJoinMeet=*/false);
460+
result.addPotentialBinding({alternateType, kind, None},
461+
/*allowJoinMeet=*/false);
491462
}
492463

493464
// If we have any literal constraints, check whether there is already a
@@ -565,7 +536,7 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
565536
continue;
566537

567538
++result.NumDefaultableBindings;
568-
addPotentialBinding(
539+
result.addPotentialBinding(
569540
{type, AllowedBindingKind::Exact, None, constraint->getLocator()});
570541
}
571542

lib/Sema/ConstraintSystem.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,6 +2575,9 @@ class ConstraintSystem {
25752575
/// Is this type variable on the RHS of a BindParam constraint?
25762576
bool IsRHSOfBindParam = false;
25772577

2578+
/// Tracks the position of the last known supertype in the group.
2579+
Optional<unsigned> lastSupertypeIndex;
2580+
25782581
/// Determine whether the set of bindings is non-empty.
25792582
explicit operator bool() const { return !Bindings.empty(); }
25802583

@@ -2619,6 +2622,38 @@ class ConstraintSystem {
26192622
}
26202623
}
26212624

2625+
/// \brief Add a potential binding to the list of bindings,
2626+
/// coalescing supertype bounds when we are able to compute the meet.
2627+
void addPotentialBinding(PotentialBinding binding,
2628+
bool allowJoinMeet = true) {
2629+
assert(!binding.BindingType->is<ErrorType>());
2630+
2631+
// If this is a non-defaulted supertype binding,
2632+
// check whether we can combine it with another
2633+
// supertype binding by computing the 'join' of the types.
2634+
if (binding.Kind == AllowedBindingKind::Supertypes &&
2635+
!binding.BindingType->hasTypeVariable() &&
2636+
!binding.DefaultedProtocol && !binding.isDefaultableBinding() &&
2637+
allowJoinMeet) {
2638+
if (lastSupertypeIndex) {
2639+
// Can we compute a join?
2640+
auto &lastBinding = Bindings[*lastSupertypeIndex];
2641+
auto lastType = lastBinding.BindingType->getWithoutSpecifierType();
2642+
auto bindingType = binding.BindingType->getWithoutSpecifierType();
2643+
if (auto join = Type::join(lastType, bindingType)) {
2644+
// Replace the last supertype binding with the join. We're done.
2645+
lastBinding.BindingType = join;
2646+
return;
2647+
}
2648+
}
2649+
2650+
// Record this as the most recent supertype index.
2651+
lastSupertypeIndex = Bindings.size();
2652+
}
2653+
2654+
Bindings.push_back(std::move(binding));
2655+
}
2656+
26222657
void dump(llvm::raw_ostream &out,
26232658
unsigned indent = 0) const LLVM_ATTRIBUTE_USED {
26242659
out.indent(indent);

0 commit comments

Comments
 (0)