Skip to content

Commit 233cf6f

Browse files
committed
[Constraint solver] Address feedback from Pavel and Slava.
1 parent 61caa51 commit 233cf6f

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4801,13 +4801,11 @@ ConstraintSystem::simplifyKeyPathApplicationConstraint(
48014801
}
48024802

48034803
Type ConstraintSystem::simplifyAppliedOverloads(
4804-
Type fnType,
4804+
TypeVariableType *fnTypeVar,
48054805
const FunctionType *argFnType,
48064806
Optional<ArgumentLabelState> argumentLabels,
48074807
ConstraintLocatorBuilder locator) {
4808-
auto fnTypeVar = fnType->getAs<TypeVariableType>();
4809-
if (!fnTypeVar)
4810-
return fnType;
4808+
Type fnType(fnTypeVar);
48114809

48124810
// Always work on the representation.
48134811
fnTypeVar = getRepresentative(fnTypeVar);
@@ -4850,25 +4848,12 @@ Type ConstraintSystem::simplifyAppliedOverloads(
48504848

48514849
// Consider each of the constraints in the disjunction.
48524850
retry_after_fail:
4853-
bool skippedAnyConstraints = false;
4851+
bool hasUnhandledConstraints = false;
48544852
bool labelMismatch = false;
48554853
auto filterResult =
4856-
filterDisjunctions(disjunction, /*restoreOnFail=*/shouldAttemptFixes(),
4854+
filterDisjunction(disjunction, /*restoreOnFail=*/shouldAttemptFixes(),
48574855
[&](Constraint *constraint) {
4858-
// We must have bind-overload constraints.
4859-
// FIXME: This isn't entirely true.
4860-
if (constraint->getKind() != ConstraintKind::BindOverload) {
4861-
skippedAnyConstraints = true;
4862-
return true;
4863-
}
4864-
4865-
// We must be binding the type variable (or a type variable
4866-
// equivalent to it).
4867-
auto boundTypeVar = constraint->getFirstType()->getAs<TypeVariableType>();
4868-
if (!boundTypeVar || getRepresentative(boundTypeVar) != fnTypeVar) {
4869-
skippedAnyConstraints = true;
4870-
return true;
4871-
}
4856+
assert(constraint->getKind() == ConstraintKind::BindOverload);
48724857

48734858
auto choice = constraint->getOverloadChoice();
48744859

@@ -4887,7 +4872,7 @@ Type ConstraintSystem::simplifyAppliedOverloads(
48874872
getEffectiveOverloadType(choice, /*allowMembers=*/true,
48884873
constraint->getOverloadUseDC());
48894874
if (!choiceType) {
4890-
skippedAnyConstraints = true;
4875+
hasUnhandledConstraints = true;
48914876
return true;
48924877
}
48934878

@@ -4917,7 +4902,7 @@ Type ConstraintSystem::simplifyAppliedOverloads(
49174902

49184903
// If there was a constraint that we couldn't reason about, don't use the
49194904
// results of any common-type computations.
4920-
if (skippedAnyConstraints)
4905+
if (hasUnhandledConstraints)
49214906
return fnType;
49224907

49234908
// If we have a common result type, bind the expected result type to it.
@@ -4997,7 +4982,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
49974982
if (auto typeVar = desugar2->getAs<TypeVariableType>()) {
49984983
auto argumentLabels = getArgumentLabels(*this, locator);
49994984
Type newType2 =
5000-
simplifyAppliedOverloads(type2, func1, argumentLabels, locator);
4985+
simplifyAppliedOverloads(typeVar, func1, argumentLabels, locator);
50014986
if (!newType2)
50024987
return SolutionKind::Error;
50034988

lib/Sema/CSSolver.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ void ConstraintSystem::collectDisjunctions(
12921292
}
12931293

12941294
ConstraintSystem::SolutionKind
1295-
ConstraintSystem::filterDisjunctions(
1295+
ConstraintSystem::filterDisjunction(
12961296
Constraint *disjunction, bool restoreOnFail,
12971297
llvm::function_ref<bool(Constraint *)> pred) {
12981298
assert(disjunction->getKind() == ConstraintKind::Disjunction);
@@ -1656,10 +1656,19 @@ Constraint *ConstraintSystem::getUnboundBindOverloadDisjunction(
16561656
llvm::SetVector<Constraint *> disjunctions;
16571657
getConstraintGraph().gatherConstraints(
16581658
rep, disjunctions, ConstraintGraph::GatheringKind::EquivalenceClass,
1659-
[](Constraint *match) {
1660-
return match->getKind() == ConstraintKind::Disjunction &&
1661-
match->getNestedConstraints().front()->getKind() ==
1662-
ConstraintKind::BindOverload;
1659+
[this, rep](Constraint *match) {
1660+
if (match->getKind() != ConstraintKind::Disjunction ||
1661+
match->getNestedConstraints().front()->getKind() !=
1662+
ConstraintKind::BindOverload)
1663+
return false;
1664+
1665+
auto lhsTypeVar =
1666+
match->getNestedConstraints().front()->getFirstType()
1667+
->getAs<TypeVariableType>();
1668+
if (!lhsTypeVar)
1669+
return false;
1670+
1671+
return getRepresentative(lhsTypeVar) == rep;
16631672
});
16641673

16651674
if (disjunctions.empty())

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,7 @@ Type ConstraintSystem::getEffectiveOverloadType(const OverloadChoice &overload,
15341534
->castTo<AnyFunctionType>()->getParams();
15351535
type = FunctionType::get(indices, elementTy);
15361536
} else if (auto var = dyn_cast<VarDecl>(decl)) {
1537-
type = var->getInterfaceType();
1537+
type = var->getValueInterfaceType();
15381538
if (doesStorageProduceLValue(var, overload.getBaseType(), useDC))
15391539
type = LValueType::get(type);
15401540
} else if (isa<FuncDecl>(decl) || isa<EnumElementDecl>(decl)) {

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,9 +2353,8 @@ class ConstraintSystem {
23532353
/// Attempt to simplify the set of overloads corresponding to a given
23542354
/// function application constraint.
23552355
///
2356-
/// \param fnType The type that describes the function being applied.
2357-
/// When there is a set of overloads, this will generally be a type
2358-
/// variable that can be used to find the set of overloads.
2356+
/// \param fnTypeVar The type variable that describes the set of
2357+
/// overloads for the function.
23592358
///
23602359
/// \param argFnType The call signature, which includes the call arguments
23612360
/// (as the function parameters) and the expected result type of the
@@ -2367,7 +2366,7 @@ class ConstraintSystem {
23672366
/// \returns \c fnType, or some simplified form of it if this function
23682367
/// was able to find a single overload or derive some common structure
23692368
/// among the overloads.
2370-
Type simplifyAppliedOverloads(Type fnType,
2369+
Type simplifyAppliedOverloads(TypeVariableType *fnTypeVar,
23712370
const FunctionType *argFnType,
23722371
Optional<ArgumentLabelState> argumentLabels,
23732372
ConstraintLocatorBuilder locator);
@@ -3129,7 +3128,7 @@ class ConstraintSystem {
31293128
/// \returns One of \c Solved (only a single term remained),
31303129
/// \c Unsolved (more than one disjunction terms remain), or
31313130
/// \c Error (all terms were filtered out).
3132-
SolutionKind filterDisjunctions(Constraint *disjunction,
3131+
SolutionKind filterDisjunction(Constraint *disjunction,
31333132
bool restoreOnFail,
31343133
llvm::function_ref<bool(Constraint *)> pred);
31353134

0 commit comments

Comments
 (0)