Skip to content

[CSBinding] Attempt to join any existing and viable bindings with new… #34834

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
Dec 1, 2020
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
8 changes: 5 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4706,6 +4706,11 @@ class ConstraintSystem {
return {type, kind, BindingSource};
}

/// Determine whether this binding could be a viable candidate
/// to be "joined" with some other binding. It has to be at least
/// a non-default r-value supertype binding with no type variables.
bool isViableForJoin() const;

static PotentialBinding forHole(TypeVariableType *typeVar,
ConstraintLocator *locator) {
return {HoleType::get(typeVar->getASTContext(), typeVar),
Expand Down Expand Up @@ -4745,9 +4750,6 @@ class ConstraintSystem {
/// Whether this type variable has literal bindings.
LiteralBindingKind LiteralBinding = LiteralBindingKind::None;

/// Tracks the position of the last known supertype in the group.
Optional<unsigned> lastSupertypeIndex;

/// A set of all not-yet-resolved type variables this type variable
/// is a subtype of, supertype of or is equivalent to. This is used
/// to determine ordering inside of a chain of subtypes to help infer
Expand Down
53 changes: 32 additions & 21 deletions lib/Sema/CSBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
using namespace swift;
using namespace constraints;

bool ConstraintSystem::PotentialBinding::isViableForJoin() const {
return Kind == AllowedBindingKind::Supertypes &&
!BindingType->hasLValueType() &&
!BindingType->hasUnresolvedType() &&
!BindingType->hasTypeVariable() &&
!BindingType->hasHole() &&
!BindingType->hasUnboundGenericType() &&
!hasDefaultedLiteralProtocol() &&
!isDefaultableBinding();
}

bool ConstraintSystem::PotentialBindings::isPotentiallyIncomplete() const {
// Generic parameters are always potentially incomplete.
if (isGenericParameter())
Expand Down Expand Up @@ -679,30 +690,30 @@ void ConstraintSystem::PotentialBindings::addPotentialBinding(
// If this is a non-defaulted supertype binding,
// check whether we can combine it with another
// supertype binding by computing the 'join' of the types.
if (binding.Kind == AllowedBindingKind::Supertypes &&
!binding.BindingType->hasUnresolvedType() &&
!binding.BindingType->hasTypeVariable() &&
!binding.BindingType->hasHole() &&
!binding.BindingType->hasUnboundGenericType() &&
!binding.hasDefaultedLiteralProtocol() &&
!binding.isDefaultableBinding() && allowJoinMeet) {
if (lastSupertypeIndex) {
auto &lastBinding = Bindings[*lastSupertypeIndex];
auto lastType = lastBinding.BindingType->getWithoutSpecifierType();
auto bindingType = binding.BindingType->getWithoutSpecifierType();

auto join = Type::join(lastType, bindingType);
if (join && !(*join)->isAny() &&
(!(*join)->getOptionalObjectType()
|| !(*join)->getOptionalObjectType()->isAny())) {
// Replace the last supertype binding with the join. We're done.
lastBinding.BindingType = *join;
return;
if (binding.isViableForJoin() && allowJoinMeet) {
bool joined = false;

auto isAcceptableJoin = [](Type type) {
return !type->isAny() && (!type->getOptionalObjectType() ||
!type->getOptionalObjectType()->isAny());
};

for (auto &existingBinding : Bindings) {
if (!existingBinding.isViableForJoin())
continue;

auto join = Type::join(existingBinding.BindingType, binding.BindingType);

if (join && isAcceptableJoin(*join)) {
existingBinding.BindingType = *join;
joined = true;
}
}

// Record this as the most recent supertype index.
lastSupertypeIndex = Bindings.size();
// If new binding has been joined with at least one of existing
// bindings, there is no reason to include it into the set.
if (joined)
return;
}

if (auto *literalProtocol = binding.getDefaultedLiteralProtocol())
Expand Down