Skip to content

Commit ec45b24

Browse files
committed
Use hasAppliedSelf in getFunctionArgApplyInfo
`callee->hasCurriedSelf()` isn't the correct check here when we have an argument mismatch for the self parameter in a curried application.
1 parent b5a0d58 commit ec45b24

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
221221
if (!argExpr)
222222
return None;
223223

224-
ValueDecl *callee = nullptr;
224+
Optional<OverloadChoice> choice;
225225
Type rawFnType;
226226
if (auto overload = getChoiceFor(argLocator)) {
227227
// If we have resolved an overload for the callee, then use that to get the
228228
// function type and callee.
229-
callee = overload->choice.getDeclOrNull();
229+
choice = overload->choice;
230230
rawFnType = overload->openedType;
231231
} else {
232232
// If we didn't resolve an overload for the callee, we must be dealing with
@@ -249,6 +249,7 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
249249
// Resolve the interface type for the function. Note that this may not be a
250250
// function type, for example it could be a generic parameter.
251251
Type fnInterfaceType;
252+
auto *callee = choice ? choice->getDeclOrNull() : nullptr;
252253
if (callee && callee->hasInterfaceType()) {
253254
// If we have a callee with an interface type, we can use it. This is
254255
// preferable to resolveInterfaceType, as this will allow us to get a
@@ -261,7 +262,7 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
261262
fnInterfaceType = callee->getInterfaceType();
262263

263264
// Strip off the curried self parameter if necessary.
264-
if (callee->hasCurriedSelf())
265+
if (hasAppliedSelf(cs, *choice))
265266
fnInterfaceType = fnInterfaceType->castTo<AnyFunctionType>()->getResult();
266267

267268
if (auto *fn = fnInterfaceType->getAs<AnyFunctionType>()) {

lib/Sema/CSSimplify.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -728,21 +728,6 @@ matchCallArguments(ArrayRef<AnyFunctionType::Param> args,
728728
return listener.relabelArguments(actualArgNames);
729729
}
730730

731-
static bool hasAppliedSelf(ConstraintSystem &cs, const OverloadChoice &choice) {
732-
auto *decl = choice.getDeclOrNull();
733-
if (!decl)
734-
return false;
735-
736-
auto baseType = choice.getBaseType();
737-
if (baseType)
738-
baseType = cs.getFixedTypeRecursive(baseType, /*wantRValue=*/true);
739-
740-
// In most cases where we reference a declaration with a curried self
741-
// parameter, it gets dropped from the type of the reference.
742-
return decl->hasCurriedSelf() &&
743-
doesMemberRefApplyCurriedSelf(baseType, decl);
744-
}
745-
746731
/// Find the callee declaration and uncurry level for a given call
747732
/// locator.
748733
static std::tuple<ValueDecl *, bool, ArrayRef<Identifier>, bool,

lib/Sema/ConstraintSystem.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,22 @@ bool constraints::isAutoClosureArgument(Expr *argExpr) {
30433043
return false;
30443044
}
30453045

3046+
bool constraints::hasAppliedSelf(ConstraintSystem &cs,
3047+
const OverloadChoice &choice) {
3048+
auto *decl = choice.getDeclOrNull();
3049+
if (!decl)
3050+
return false;
3051+
3052+
auto baseType = choice.getBaseType();
3053+
if (baseType)
3054+
baseType = cs.getFixedTypeRecursive(baseType, /*wantRValue=*/true);
3055+
3056+
// In most cases where we reference a declaration with a curried self
3057+
// parameter, it gets dropped from the type of the reference.
3058+
return decl->hasCurriedSelf() &&
3059+
doesMemberRefApplyCurriedSelf(baseType, decl);
3060+
}
3061+
30463062
bool constraints::conformsToKnownProtocol(ConstraintSystem &cs, Type type,
30473063
KnownProtocolKind protocol) {
30483064
if (auto *proto = cs.TC.getProtocol(SourceLoc(), protocol))

lib/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,6 +4014,11 @@ Optional<Identifier> getOperatorName(Expr *expr);
40144014
// }
40154015
bool isAutoClosureArgument(Expr *argExpr);
40164016

4017+
/// Checks whether referencing the given overload choice results in the self
4018+
/// parameter being applied, meaning that it's dropped from the type of the
4019+
/// reference.
4020+
bool hasAppliedSelf(ConstraintSystem &cs, const OverloadChoice &choice);
4021+
40174022
/// Check whether type conforms to a given known protocol.
40184023
bool conformsToKnownProtocol(ConstraintSystem &cs, Type type,
40194024
KnownProtocolKind protocol);

0 commit comments

Comments
 (0)