Skip to content

[ConstraintSystem] Reinstate favoring more specialized of two generic… #17352

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
Jun 20, 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
60 changes: 60 additions & 0 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,64 @@ ConstraintSystem::getTypeOfMemberReference(
return { openedType, type };
}

// Performance hack: if there are two generic overloads, and one is
// more specialized than the other, prefer the more-specialized one.
static void tryOptimizeGenericDisjunction(ConstraintSystem &cs,
ArrayRef<OverloadChoice> choices,
OverloadChoice *&favoredChoice) {
if (favoredChoice || choices.size() != 2)
return;

const auto &choiceA = choices[0];
const auto &choiceB = choices[1];

if (!choiceA.isDecl() || !choiceB.isDecl())
return;

auto isViable = [](ValueDecl *decl) -> bool {
assert(decl);

auto *AFD = dyn_cast<AbstractFunctionDecl>(decl);
if (!AFD || !AFD->isGeneric())
return false;

auto funcType = AFD->getInterfaceType();
auto hasAnyOrOptional = funcType.findIf([](Type type) -> bool {
if (auto objType = type->getOptionalObjectType())
return true;

return type->isAny();
});

// If function declaration references `Any` or `Any?` type
// let's not attempt it, because it's unclear
// without solving which overload is going to be better.
return !hasAnyOrOptional;
};

auto *declA = choiceA.getDecl();
auto *declB = choiceB.getDecl();

if (!isViable(declA) || !isViable(declB))
return;

auto &TC = cs.TC;
auto *DC = cs.DC;

switch (TC.compareDeclarations(DC, declA, declB)) {
case Comparison::Better:
favoredChoice = const_cast<OverloadChoice *>(&choiceA);
break;

case Comparison::Worse:
favoredChoice = const_cast<OverloadChoice *>(&choiceB);
break;

case Comparison::Unordered:
break;
}
}

void ConstraintSystem::addOverloadSet(Type boundType,
ArrayRef<OverloadChoice> choices,
DeclContext *useDC,
Expand All @@ -1500,6 +1558,8 @@ void ConstraintSystem::addOverloadSet(Type boundType,
return;
}

tryOptimizeGenericDisjunction(*this, choices, favoredChoice);

SmallVector<Constraint *, 4> overloads;

// As we do for other favored constraints, if a favored overload has been
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %scale-test --invert-result --begin 1 --end 10 --step 1 --select incrementScopeCounter %s --expected-exit-code 0
// RUN: %scale-test --begin 1 --end 10 --step 1 --select incrementScopeCounter %s --expected-exit-code 0
// REQUIRES: OS=macosx
// REQUIRES: asserts

Expand Down