Skip to content

[CSSolver] Clarify generic disjunction choice favoring conditions #15716

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
Apr 4, 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
81 changes: 59 additions & 22 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,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 hasAny = funcType.findIf([](Type type) -> bool {
if (auto objType = type->getOptionalObjectType())
return objType->isAny();

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 !hasAny;
};

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 @@ -1389,28 +1447,7 @@ void ConstraintSystem::addOverloadSet(Type boundType,
return;
}

// Performance hack: if there are two generic overloads, and one is
// more specialized than the other, prefer the more-specialized one.
if (!favoredChoice && choices.size() == 2 &&
choices[0].isDecl() && choices[1].isDecl() &&
isa<AbstractFunctionDecl>(choices[0].getDecl()) &&
cast<AbstractFunctionDecl>(choices[0].getDecl())->isGeneric() &&
isa<AbstractFunctionDecl>(choices[1].getDecl()) &&
cast<AbstractFunctionDecl>(choices[1].getDecl())->isGeneric()) {
switch (TC.compareDeclarations(DC, choices[0].getDecl(),
choices[1].getDecl())) {
case Comparison::Better:
favoredChoice = const_cast<OverloadChoice *>(&choices[0]);
break;

case Comparison::Worse:
favoredChoice = const_cast<OverloadChoice *>(&choices[1]);
break;

case Comparison::Unordered:
break;
}
}
tryOptimizeGenericDisjunction(*this, choices, favoredChoice);

SmallVector<Constraint *, 4> overloads;

Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/rdar38625824.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-frontend -emit-sil -verify %s | %FileCheck %s
func foo<T>(_: Any) -> T {
fatalError()
}

func foo<T>(_: Any?) -> T {
fatalError()
}

// CHECK: function_ref @$S12rdar386258243fooyxyplF : $@convention(thin) <τ_0_0> (@in_guaranteed Any) -> @out τ_0_0
var _: String = foo("hello")