Skip to content

Commit 62c62bb

Browse files
authored
Merge pull request #38448 from xedin/add-eof-to-binding-producer
[ConstraintSystem] NFC: A couple of binding producer improvements
2 parents 3b78f38 + 0ee1fb2 commit 62c62bb

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5433,7 +5433,9 @@ class BindingProducer {
54335433
/// This is useful to be able to exhaustively attempt bindings
54345434
/// for type variables found at one level, before proceeding to
54355435
/// supertypes or literal defaults etc.
5436-
virtual bool needsToComputeNext() const { return false; }
5436+
virtual bool needsToComputeNext() const = 0;
5437+
5438+
virtual bool isExhausted() const = 0;
54375439
};
54385440

54395441
class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
@@ -5460,6 +5462,8 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
54605462
/// to that protocol or be wrapped in an optional.
54615463
bool CanBeNil;
54625464

5465+
bool IsExhausted = false;
5466+
54635467
public:
54645468
using Element = TypeVariableBinding;
54655469

@@ -5469,11 +5473,16 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
54695473
ArrayRef<Binding> getCurrentBindings() const { return Bindings; }
54705474

54715475
Optional<Element> operator()() override {
5476+
if (isExhausted())
5477+
return None;
5478+
54725479
// Once we reach the end of the current bindings
54735480
// let's try to compute new ones, e.g. supertypes,
54745481
// literal defaults, if that fails, we are done.
5475-
if (needsToComputeNext() && !computeNext())
5482+
if (needsToComputeNext() && !computeNext()) {
5483+
IsExhausted = true;
54765484
return None;
5485+
}
54775486

54785487
auto &binding = Bindings[Index++];
54795488

@@ -5492,7 +5501,11 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
54925501
return TypeVariableBinding(TypeVar, binding);
54935502
}
54945503

5495-
bool needsToComputeNext() const override { return Index >= Bindings.size(); }
5504+
bool needsToComputeNext() const override {
5505+
return isExhausted() ? false : Index >= Bindings.size();
5506+
}
5507+
5508+
bool isExhausted() const override { return IsExhausted; }
54965509

54975510
private:
54985511
/// Compute next batch of bindings if possible, this could
@@ -5565,10 +5578,10 @@ class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
55655578
}
55665579

55675580
Optional<Element> operator()() override {
5568-
unsigned currIndex = Index;
5569-
if (currIndex >= Choices.size())
5581+
if (isExhausted())
55705582
return None;
55715583

5584+
unsigned currIndex = Index;
55725585
bool isBeginningOfPartition = PartitionIndex < PartitionBeginning.size() &&
55735586
PartitionBeginning[PartitionIndex] == Index;
55745587
if (isBeginningOfPartition)
@@ -5592,6 +5605,10 @@ class DisjunctionChoiceProducer : public BindingProducer<DisjunctionChoice> {
55925605
IsExplicitConversion, isBeginningOfPartition);
55935606
}
55945607

5608+
bool needsToComputeNext() const override { return false; }
5609+
5610+
bool isExhausted() const override { return Index >= Choices.size(); }
5611+
55955612
private:
55965613
// Partition the choices in the disjunction into groups that we will
55975614
// iterate over in an order appropriate to attempt to stop before we

0 commit comments

Comments
 (0)