Skip to content

[ConstraintSolver] Prioritize certain type variables while looking for bindings #12198

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 1 commit into from
Oct 2, 2017
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
4 changes: 4 additions & 0 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ ConstraintSystem::getPotentialBindings(TypeVariableType *typeVar) {
ConstraintClassification::Relational &&
"only relational constraints handled here");

// Record constraint which contributes to the
// finding of pontential bindings.
result.Sources.insert(constraint);

auto first = simplifyType(constraint->getFirstType());
auto second = simplifyType(constraint->getSecondType());

Expand Down
13 changes: 7 additions & 6 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1695,13 +1695,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
}

// If we have a binding for the right-hand side
// (argument type) don't try to bind it to the left-hand
// side (parameter type) directly, because their
// relationship is contravariant and the actual
// binding can only come from the left-hand side.
// (argument type used in the body) don't try
// to bind it to the left-hand side (parameter type)
// directly, because there could be an implicit
// conversion between them, and actual binding
// can only come from the left-hand side.
addUnsolvedConstraint(
Constraint::create(*this, ConstraintKind::ArgumentConversion, type2,
typeVar1, getConstraintLocator(locator)));
Constraint::create(*this, ConstraintKind::Equal, typeVar1, type2,
getConstraintLocator(locator)));
return SolutionKind::Solved;
}

Expand Down
28 changes: 27 additions & 1 deletion lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "swift/AST/TypeCheckerDebugConsumer.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -2577,6 +2578,9 @@ class ConstraintSystem {
/// Tracks the position of the last known supertype in the group.
Optional<unsigned> lastSupertypeIndex;

/// A set of all constraints which contribute to pontential bindings.
llvm::SmallPtrSet<Constraint *, 8> Sources;

PotentialBindings(TypeVariableType *typeVar) : TypeVar(typeVar) {}

/// Determine whether the set of bindings is non-empty.
Expand All @@ -2600,7 +2604,29 @@ class ConstraintSystem {
/// \c x is a better set of bindings that \c y.
friend bool operator<(const PotentialBindings &x,
const PotentialBindings &y) {
return formBindingScore(x) < formBindingScore(y);
if (formBindingScore(x) < formBindingScore(y))
return true;

if (!x.hasNonDefaultableBindings())
return false;

llvm::SmallPtrSet<Constraint *, 8> intersection(x.Sources);
llvm::set_intersect(intersection, y.Sources);

// Some relational constraints dictate certain
// ordering when it comes to attempting binding
// of type variables, where left-hand side is
// always more preferrable than right-hand side.
for (const auto *constraint : intersection) {
if (constraint->getKind() != ConstraintKind::Subtype)
continue;

auto lhs = constraint->getFirstType();
if (auto *typeVar = lhs->getAs<TypeVariableType>())
return x.TypeVar == typeVar;
}

return false;
}

void foundLiteralBinding(ProtocolDecl *proto) {
Expand Down
24 changes: 24 additions & 0 deletions test/Constraints/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,3 +504,27 @@ func rdar27700622<E: Comparable>(_ input: [E]) -> [E] {

return rdar27700622(lhs) + [pivot] + rdar27700622(rhs) // Ok
}

// rdar://problem/22898292 - Type inference failure with constrained subclass
protocol P_22898292 {}

do {
func construct_generic<T: P_22898292>(_ construct: () -> T) -> T { return construct() }

class A {}
class B : A, P_22898292 {}

func foo() -> B { return B() }
func bar(_ value: A) {}
func baz<T: A>(_ value: T) {}

func rdar_22898292_1() {
let x = construct_generic { foo() } // returns A
bar(x) // Ok
bar(construct_generic { foo() }) // Ok
}

func rdar22898292_2<T: B>(_ d: T) {
_ = { baz($0) }(construct_generic { d }) // Ok
}
}