Skip to content

[CSBindings] Try to infer bindings from related type variables #15009

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
Mar 7, 2018
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
54 changes: 48 additions & 6 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,58 @@ Optional<ConstraintSystem::PotentialBindings>
ConstraintSystem::determineBestBindings() {
// Look for potential type variable bindings.
Optional<PotentialBindings> bestBindings;
for (auto typeVar : getTypeVariables()) {
// Skip any type variables that are bound.
llvm::SmallDenseMap<TypeVariableType *, PotentialBindings> cache;

// First, let's collect all of the possible bindings.
for (auto *typeVar : getTypeVariables()) {
if (typeVar->getImpl().hasRepresentativeOrFixed())
continue;

// Get potential bindings.
auto bindings = getPotentialBindings(typeVar);
if (!bindings)
if (auto bindings = getPotentialBindings(typeVar))
cache.insert({typeVar, std::move(bindings)});
}

// Now let's see if we could infer something for related type
// variables based on other bindings.
for (auto *typeVar : getTypeVariables()) {
auto cachedBindings = cache.find(typeVar);
if (cachedBindings == cache.end())
continue;

auto &bindings = cachedBindings->getSecond();
// All of the relevant relational constraints associated with
// current type variable should be recored by its potential bindings.
for (auto *constraint : bindings.Sources) {
if (constraint->getKind() != ConstraintKind::Subtype)
continue;

auto lhs = simplifyType(constraint->getFirstType());
auto rhs = simplifyType(constraint->getSecondType());

// We are only interested in 'subtype' constraints which have
// type variable on the left-hand side.
if (rhs->getAs<TypeVariableType>() != typeVar)
continue;

auto *tv = lhs->getAs<TypeVariableType>();
if (!tv)
continue;

auto relatedBindings = cache.find(tv);
if (relatedBindings == cache.end())
continue;

for (auto &binding : relatedBindings->getSecond().Bindings) {
auto type = binding.BindingType;

if (ConstraintSystem::typeVarOccursInType(typeVar, type))
continue;

bindings.addPotentialBinding(
{type, AllowedBindingKind::Supertypes, binding.BindingSource});
}
}

if (TC.getLangOpts().DebugConstraintSolver) {
auto &log = getASTContext().TypeCheckerDebug->getStream();
bindings.dump(typeVar, log, solverState->depth * 2);
Expand All @@ -42,7 +84,7 @@ ConstraintSystem::determineBestBindings() {
// If these are the first bindings, or they are better than what
// we saw before, use them instead.
if (!bestBindings || bindings < *bestBindings)
bestBindings = std::move(bindings);
bestBindings = bindings;
}

return bestBindings;
Expand Down
16 changes: 0 additions & 16 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2776,22 +2776,6 @@ class ConstraintSystem {
if (x.FullyBound || x.SubtypeOfExistentialType)
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;
}

// If the only difference is default types,
// prioritize bindings with fewer of them.
return x.NumDefaultableBindings < y.NumDefaultableBindings;
Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,17 @@ func rdar35541153() {
foo(x) // Ok
bar(y, "ultimate question", 42) // Ok
}

// rdar://problem/38159133 - [SR-7125]: Swift 4.1 Xcode 9.3b4 regression

protocol P_38159133 {}

do {
class Super {}
class A: Super, P_38159133 {}
class B: Super, P_38159133 {}

func rdar38159133(_ a: A?, _ b: B?) {
let _: [P_38159133] = [a, b].compactMap { $0 } // Ok
}
}