Skip to content

[ConstraintSystem] Record produced types incrementally #37981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5369,6 +5369,9 @@ class TypeVariableBinding {
TypeVariableBinding(TypeVariableType *typeVar, PotentialBinding &binding)
: TypeVar(typeVar), Binding(binding) {}

TypeVariableType *getTypeVariable() const { return TypeVar; }
Type getType() const { return Binding.BindingType; }

bool isDefaultable() const { return Binding.isDefaultableBinding(); }

bool hasDefaultedProtocol() const {
Expand Down Expand Up @@ -5453,7 +5456,21 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
if (needsToComputeNext() && !computeNext())
return None;

return TypeVariableBinding(TypeVar, Bindings[Index++]);
auto &binding = Bindings[Index++];

// Record produced type as bound/explored early, otherwise
// it could be possible to re-discover it 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.
{
auto type = binding.BindingType;

BoundTypes.insert(type.getPointer());
ExploredTypes.insert(type->getCanonicalType());
}

return TypeVariableBinding(TypeVar, binding);
}

bool needsToComputeNext() const override { return Index >= Bindings.size(); }
Expand Down
4 changes: 0 additions & 4 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1705,10 +1705,6 @@ bool TypeVarBindingProducer::computeNext() {
const auto type = binding.BindingType;
assert(!type->hasError());

// After our first pass, note that we've explored these types.
if (NumTries == 0)
ExploredTypes.insert(type->getCanonicalType());

// If we have a protocol with a default type, look for alternative
// types to the default.
if (NumTries == 0 && binding.hasDefaultedLiteralProtocol()) {
Expand Down
59 changes: 59 additions & 0 deletions unittests/Sema/BindingInferenceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,62 @@ TEST_F(SemaTest, TestTransitiveProtocolInferenceThroughEquivalenceChains) {
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,
{protocolTy0, protocolTy1});
}

TEST_F(SemaTest, TestNoDoubleVoidClosureResultInference) {
ConstraintSystemOptions options;
ConstraintSystem cs(DC, options);

auto verifyInference = [&](TypeVariableType *typeVar, unsigned numExpected) {
auto bindings = cs.getBindingsFor(typeVar);
TypeVarBindingProducer producer(bindings);

llvm::SmallPtrSet<Type, 2> inferredTypes;

while (auto binding = producer()) {
ASSERT_TRUE(binding.hasValue());
ASSERT_EQ(binding->getTypeVariable(), typeVar);
ASSERT_TRUE(inferredTypes.insert(binding->getType()).second);
}

ASSERT_EQ(inferredTypes.size(), numExpected);
};

auto *closureResultLoc =
cs.getConstraintLocator({}, ConstraintLocator::ClosureResult);

auto *closureResult = cs.createTypeVariable(closureResultLoc, /*options=*/0);

cs.addConstraint(ConstraintKind::Subtype, getStdlibType("Int"), closureResult,
closureResultLoc);
cs.addConstraint(ConstraintKind::Subtype, closureResult, getStdlibType("Void"),
closureResultLoc);

verifyInference(closureResult, 2);

auto closureResultWithTransitiveVoid = cs.createTypeVariable(closureResultLoc,
/*options=*/0);

auto contextualVar = cs.createTypeVariable({}, /*options=*/0);

cs.addConstraint(ConstraintKind::Subtype, getStdlibType("Void"),
contextualVar, cs.getConstraintLocator({}));

cs.addConstraint(ConstraintKind::Subtype, contextualVar,
closureResultWithTransitiveVoid, closureResultLoc);

cs.addConstraint(ConstraintKind::Subtype, getStdlibType("Int"),
closureResultWithTransitiveVoid, closureResultLoc);

verifyInference(closureResultWithTransitiveVoid, 2);

auto closureResultWithoutVoid =
cs.createTypeVariable(closureResultLoc, /*options=*/0);

// Supertype triggers `Void` inference
cs.addConstraint(ConstraintKind::Subtype, getStdlibType("Int"),
closureResultWithoutVoid, closureResultLoc);
cs.addConstraint(ConstraintKind::Subtype, closureResultWithoutVoid,
getStdlibType("String"), closureResultLoc);

verifyInference(closureResultWithoutVoid, 3);
}