@@ -1542,12 +1542,11 @@ namespace {
1542
1542
ConstraintLocatorBuilder locator,
1543
1543
ConstraintLocatorBuilder calleeLocator);
1544
1544
1545
- // Resolve `@dynamicCallable` applications.
1546
- Expr *finishApplyDynamicCallable (ApplyExpr *apply,
1547
- SelectedOverload selected,
1548
- FuncDecl *method,
1549
- AnyFunctionType *methodType,
1550
- ConstraintLocatorBuilder applyFunctionLoc);
1545
+ // / Build the function and argument for a `@dynamicCallable` application.
1546
+ std::pair</* fn*/ Expr *, /* arg*/ Expr *>
1547
+ buildDynamicCallable (ApplyExpr *apply, SelectedOverload selected,
1548
+ FuncDecl *method, AnyFunctionType *methodType,
1549
+ ConstraintLocatorBuilder applyFunctionLoc);
1551
1550
1552
1551
private:
1553
1552
// / Simplify the given type by substituting all occurrences of
@@ -7146,12 +7145,11 @@ static Expr *buildCallAsFunctionMethodRef(
7146
7145
}
7147
7146
7148
7147
// Resolve `@dynamicCallable` applications.
7149
- Expr *
7150
- ExprRewriter::finishApplyDynamicCallable (ApplyExpr *apply,
7151
- SelectedOverload selected,
7152
- FuncDecl *method,
7153
- AnyFunctionType *methodType,
7154
- ConstraintLocatorBuilder loc) {
7148
+ std::pair<Expr *, Expr *>
7149
+ ExprRewriter::buildDynamicCallable (ApplyExpr *apply, SelectedOverload selected,
7150
+ FuncDecl *method,
7151
+ AnyFunctionType *methodType,
7152
+ ConstraintLocatorBuilder loc) {
7155
7153
auto &ctx = cs.getASTContext ();
7156
7154
auto *fn = apply->getFn ();
7157
7155
@@ -7203,8 +7201,7 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
7203
7201
handleStringLiteralExpr (cast<LiteralExpr>(labelExpr));
7204
7202
7205
7203
Expr *valueExpr = coerceToType (arg->getElement (i), valueType, loc);
7206
- if (!valueExpr)
7207
- return nullptr ;
7204
+ assert (valueExpr && " Failed to coerce?" );
7208
7205
Expr *pair = TupleExpr::createImplicit (ctx, {labelExpr, valueExpr}, {});
7209
7206
auto eltTypes = { TupleTypeElt (keyType), TupleTypeElt (valueType) };
7210
7207
cs.setType (pair, TupleType::get (eltTypes, ctx));
@@ -7217,22 +7214,21 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
7217
7214
}
7218
7215
argument->setImplicit ();
7219
7216
7220
- // Construct call to the `dynamicallyCall` method.
7221
- auto result = CallExpr::createImplicit (ctx, member, argument,
7222
- { argumentLabel });
7223
- cs.setType (result->getArg (), AnyFunctionType::composeInput (ctx, params,
7224
- false ));
7225
- cs.setType (result, methodType->getResult ());
7226
- cs.cacheExprTypes (result);
7227
- return result;
7217
+ // Build the argument list expr.
7218
+ argument = TupleExpr::createImplicit (ctx, {argument}, {argumentLabel});
7219
+ cs.setType (argument,
7220
+ TupleType::get ({TupleTypeElt (argumentType, argumentLabel)}, ctx));
7221
+
7222
+ return std::make_pair (member, argument);
7228
7223
}
7229
7224
7230
7225
Expr *ExprRewriter::finishApply (ApplyExpr *apply, Type openedType,
7231
7226
ConstraintLocatorBuilder locator,
7232
7227
ConstraintLocatorBuilder calleeLocator) {
7233
7228
auto &ctx = cs.getASTContext ();
7234
-
7235
- auto fn = apply->getFn ();
7229
+
7230
+ auto *arg = apply->getArg ();
7231
+ auto *fn = apply->getFn ();
7236
7232
7237
7233
bool hasTrailingClosure =
7238
7234
isa<CallExpr>(apply) && cast<CallExpr>(apply)->hasTrailingClosure ();
@@ -7401,7 +7397,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7401
7397
if (method && methodType) {
7402
7398
// Handle a call to a @dynamicCallable method.
7403
7399
if (isValidDynamicCallableMethod (method, methodType))
7404
- return finishApplyDynamicCallable (
7400
+ std::tie (fn, arg) = buildDynamicCallable (
7405
7401
apply, *selected, method, methodType, applyFunctionLoc);
7406
7402
}
7407
7403
}
@@ -7458,13 +7454,11 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
7458
7454
// the function.
7459
7455
SmallVector<Identifier, 2 > argLabelsScratch;
7460
7456
if (auto fnType = cs.getType (fn)->getAs <FunctionType>()) {
7461
- auto origArg = apply->getArg ();
7462
- Expr *arg = coerceCallArguments (origArg, fnType, callee,
7463
- apply,
7464
- apply->getArgumentLabels (argLabelsScratch),
7465
- hasTrailingClosure,
7466
- locator.withPathElement (
7467
- ConstraintLocator::ApplyArgument));
7457
+ arg = coerceCallArguments (arg, fnType, callee, apply,
7458
+ apply->getArgumentLabels (argLabelsScratch),
7459
+ hasTrailingClosure,
7460
+ locator.withPathElement (
7461
+ ConstraintLocator::ApplyArgument));
7468
7462
if (!arg) {
7469
7463
return nullptr ;
7470
7464
}
0 commit comments