@@ -8364,8 +8364,9 @@ ConstraintSystem::simplifyConstructionConstraint(
8364
8364
paramTypeVar, locator);
8365
8365
}
8366
8366
8367
- addConstraint(ConstraintKind::ApplicableFunction, fnType, memberType,
8368
- fnLocator);
8367
+ addApplicationConstraint(fnType, memberType,
8368
+ /*trailingClosureMatching=*/std::nullopt, useDC,
8369
+ fnLocator);
8369
8370
8370
8371
return SolutionKind::Solved;
8371
8372
}
@@ -13106,15 +13107,12 @@ createImplicitRootForCallAsFunction(ConstraintSystem &cs, Type refType,
13106
13107
}
13107
13108
13108
13109
ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13109
- Type type1 , Type type2,
13110
+ FunctionType *func1 , Type type2,
13110
13111
std::optional<TrailingClosureMatching> trailingClosureMatching,
13112
+ DeclContext *useDC,
13111
13113
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
13112
13114
auto &ctx = getASTContext();
13113
13115
13114
- // By construction, the left hand side is a type that looks like the
13115
- // following: $T1 -> $T2.
13116
- auto func1 = type1->castTo<FunctionType>();
13117
-
13118
13116
// Before stripping lvalue-ness and optional types, save the original second
13119
13117
// type for handling `func callAsFunction` and `@dynamicCallable`
13120
13118
// applications. This supports the following cases:
@@ -13171,7 +13169,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13171
13169
auto formUnsolved = [&](bool activate = false) {
13172
13170
if (flags.contains(TMF_GenerateConstraints)) {
13173
13171
auto *application = Constraint::createApplicableFunction(
13174
- *this, type1 , type2, trailingClosureMatching,
13172
+ *this, func1 , type2, trailingClosureMatching, useDC ,
13175
13173
getConstraintLocator(locator));
13176
13174
13177
13175
addUnsolvedConstraint(application);
@@ -13201,7 +13199,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13201
13199
// If the types are obviously equivalent, we're done. This optimization
13202
13200
// is not valid for operators though, where an inout parameter does not
13203
13201
// have an explicit inout argument.
13204
- if (type1.getPointer() == desugar2) {
13202
+ if (func1 == desugar2) {
13205
13203
// Note that this could throw.
13206
13204
recordPotentialThrowSite(
13207
13205
PotentialThrowSite::Application, Type(desugar2), outerLocator);
@@ -13232,8 +13230,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13232
13230
/*outerAlternatives*/ {}, memberLoc);
13233
13231
// Add new applicable function constraint based on the member type
13234
13232
// variable.
13235
- addConstraint(ConstraintKind::ApplicableFunction, func1, memberTy,
13236
- locator);
13233
+ addApplicationConstraint( func1, memberTy, trailingClosureMatching, useDC ,
13234
+ locator);
13237
13235
return SolutionKind::Solved;
13238
13236
}
13239
13237
@@ -13348,9 +13346,9 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13348
13346
// Form an unsolved constraint to apply trailing closures to a
13349
13347
// callable type produced by `.init`. This constraint would become
13350
13348
// active when `callableType` is bound.
13351
- addUnsolvedConstraint(Constraint::create (
13352
- *this, ConstraintKind::ApplicableFunction, callAsFunctionArguments ,
13353
- callableType ,
13349
+ addUnsolvedConstraint(Constraint::createApplicableFunction (
13350
+ *this, callAsFunctionArguments, callableType ,
13351
+ trailingClosureMatching, useDC ,
13354
13352
getConstraintLocator(implicitRef,
13355
13353
ConstraintLocator::ApplyFunction)));
13356
13354
break;
@@ -13368,12 +13366,13 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13368
13366
13369
13367
auto applyLocator = getConstraintLocator(locator);
13370
13368
auto forwardConstraint = Constraint::createApplicableFunction(
13371
- *this, type1, type2, TrailingClosureMatching::Forward, applyLocator);
13369
+ *this, func1, type2, TrailingClosureMatching::Forward, useDC,
13370
+ applyLocator);
13372
13371
auto backwardConstraint = Constraint::createApplicableFunction(
13373
- *this, type1 , type2, TrailingClosureMatching::Backward,
13372
+ *this, func1 , type2, TrailingClosureMatching::Backward, useDC ,
13374
13373
applyLocator);
13375
- addDisjunctionConstraint(
13376
- { forwardConstraint, backwardConstraint}, applyLocator);
13374
+ addDisjunctionConstraint({forwardConstraint, backwardConstraint},
13375
+ applyLocator);
13377
13376
break;
13378
13377
}
13379
13378
@@ -13450,7 +13449,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13450
13449
// Construct the instance from the input arguments.
13451
13450
auto simplified = simplifyConstructionConstraint(
13452
13451
instance2, func1, subflags,
13453
- /*FIXME?*/ DC , FunctionRefInfo::singleBaseNameApply(),
13452
+ useDC , FunctionRefInfo::singleBaseNameApply(),
13454
13453
getConstraintLocator(outerLocator));
13455
13454
13456
13455
// Record any fixes we attempted to get to the correct solution.
@@ -13469,7 +13468,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyApplicableFnConstraint(
13469
13468
13470
13469
// Handle applications of @dynamicCallable types.
13471
13470
auto result = simplifyDynamicCallableApplicableFnConstraint(
13472
- type1 , origType2, subflags, locator);
13471
+ func1 , origType2, subflags, locator);
13473
13472
13474
13473
if (shouldAttemptFixes() && result == SolutionKind::Error) {
13475
13474
// Skip this fix if the type is not yet resolved or
@@ -15673,15 +15672,6 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
15673
15672
case ConstraintKind::BridgingConversion:
15674
15673
return simplifyBridgingConstraint(first, second, subflags, locator);
15675
15674
15676
- case ConstraintKind::ApplicableFunction: {
15677
- // First try to simplify the overload set for the function being applied.
15678
- if (simplifyAppliedOverloads(second, first->castTo<FunctionType>(),
15679
- locator)) {
15680
- return SolutionKind::Error;
15681
- }
15682
- return simplifyApplicableFnConstraint(first, second, std::nullopt, subflags,
15683
- locator);
15684
- }
15685
15675
case ConstraintKind::DynamicCallableApplicableFunction:
15686
15676
return simplifyDynamicCallableApplicableFnConstraint(first, second,
15687
15677
subflags, locator);
@@ -15762,6 +15752,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
15762
15752
case ConstraintKind::KeyPathApplication:
15763
15753
case ConstraintKind::FallbackType:
15764
15754
case ConstraintKind::SyntacticElement:
15755
+ case ConstraintKind::ApplicableFunction:
15765
15756
llvm_unreachable("Use the correct addConstraint()");
15766
15757
}
15767
15758
@@ -15990,6 +15981,41 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
15990
15981
}
15991
15982
}
15992
15983
15984
+ void ConstraintSystem::addApplicationConstraint(
15985
+ FunctionType *appliedFn, Type calleeType,
15986
+ std::optional<TrailingClosureMatching> trailingClosureMatching,
15987
+ DeclContext *useDC,
15988
+ ConstraintLocatorBuilder locator) {
15989
+ auto recordFailure = [&]() {
15990
+ if (shouldRecordFailedConstraint()) {
15991
+ auto *c = Constraint::createApplicableFunction(
15992
+ *this, appliedFn, calleeType, trailingClosureMatching, useDC,
15993
+ getConstraintLocator(locator));
15994
+ recordFailedConstraint(c);
15995
+ }
15996
+ };
15997
+
15998
+ // First try to simplify the overload set for the function being applied.
15999
+ if (simplifyAppliedOverloads(calleeType, appliedFn, locator)) {
16000
+ recordFailure();
16001
+ return;
16002
+ }
16003
+
16004
+ switch (simplifyApplicableFnConstraint(appliedFn, calleeType,
16005
+ trailingClosureMatching, useDC,
16006
+ TMF_GenerateConstraints, locator)) {
16007
+ case SolutionKind::Error:
16008
+ recordFailure();
16009
+ break;
16010
+
16011
+ case SolutionKind::Unsolved:
16012
+ llvm_unreachable("should have generated constraints");
16013
+
16014
+ case SolutionKind::Solved:
16015
+ return;
16016
+ }
16017
+ }
16018
+
15993
16019
void ConstraintSystem::addContextualConversionConstraint(
15994
16020
Expr *expr, Type conversionType, ContextualTypePurpose purpose,
15995
16021
ConstraintLocator *locator) {
@@ -16174,8 +16200,9 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
16174
16200
16175
16201
case ConstraintKind::ApplicableFunction:
16176
16202
return simplifyApplicableFnConstraint(
16177
- constraint.getFirstType(), constraint.getSecondType(),
16178
- constraint.getTrailingClosureMatching(), std::nullopt,
16203
+ constraint.getAppliedFunctionType(), constraint.getCalleeType(),
16204
+ constraint.getTrailingClosureMatching(),
16205
+ constraint.getApplicationDC(), /*flags=*/std::nullopt,
16179
16206
constraint.getLocator());
16180
16207
16181
16208
case ConstraintKind::DynamicCallableApplicableFunction:
0 commit comments