Skip to content

Commit 942ff69

Browse files
committed
[CSBindings] Record produced types incrementally
Allow type variable binding producer to record types as they are being produced. Otherwise it could be possible to re-discover already explored types during `computeNext()` which leads to duplicate bindings e.g. inferring fallback `Void` for a closure result type when `Void` was already inferred as a direct/transitive binding.
1 parent 9b6e470 commit 942ff69

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5453,7 +5453,21 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
54535453
if (needsToComputeNext() && !computeNext())
54545454
return None;
54555455

5456-
return TypeVariableBinding(TypeVar, Bindings[Index++]);
5456+
auto &binding = Bindings[Index++];
5457+
5458+
// Record produced type as bound/explored early, otherwise
5459+
// it could be possible to re-discover it during `computeNext()`,
5460+
// which leads to duplicate bindings e.g. inferring fallback
5461+
// `Void` for a closure result type when `Void` was already
5462+
// inferred as a direct/transitive binding.
5463+
{
5464+
auto type = binding.BindingType;
5465+
5466+
BoundTypes.insert(type.getPointer());
5467+
ExploredTypes.insert(type->getCanonicalType());
5468+
}
5469+
5470+
return TypeVariableBinding(TypeVar, binding);
54575471
}
54585472

54595473
bool needsToComputeNext() const override { return Index >= Bindings.size(); }

lib/Sema/CSBindings.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,10 +1705,6 @@ bool TypeVarBindingProducer::computeNext() {
17051705
const auto type = binding.BindingType;
17061706
assert(!type->hasError());
17071707

1708-
// After our first pass, note that we've explored these types.
1709-
if (NumTries == 0)
1710-
ExploredTypes.insert(type->getCanonicalType());
1711-
17121708
// If we have a protocol with a default type, look for alternative
17131709
// types to the default.
17141710
if (NumTries == 0 && binding.hasDefaultedLiteralProtocol()) {

0 commit comments

Comments
 (0)