@@ -5837,8 +5837,9 @@ Expr *ExprRewriter::coerceCallArguments(
5837
5837
// Apply labels to arguments.
5838
5838
AnyFunctionType::relabelParams (args, argLabels);
5839
5839
5840
- auto unlabeledTrailingClosureIndex =
5840
+ auto oldTrailingClosureIndex =
5841
5841
arg->getUnlabeledTrailingClosureIndexOfPackedArgument ();
5842
+ Optional<unsigned > newTrailingClosureIndex;
5842
5843
5843
5844
// Determine the parameter bindings that were applied.
5844
5845
auto *locatorPtr = cs.getConstraintLocator (locator);
@@ -5912,6 +5913,12 @@ Expr *ExprRewriter::coerceCallArguments(
5912
5913
auto arg = getArg (argIdx);
5913
5914
auto argType = cs.getType (arg);
5914
5915
5916
+ // Update the trailing closure index if needed.
5917
+ if (oldTrailingClosureIndex && *oldTrailingClosureIndex == argIdx) {
5918
+ assert (!newTrailingClosureIndex);
5919
+ newTrailingClosureIndex = newArgs.size ();
5920
+ }
5921
+
5915
5922
// If the argument type exactly matches, this just works.
5916
5923
if (argType->isEqual (param.getPlainType ())) {
5917
5924
variadicArgs.push_back (arg);
@@ -5974,6 +5981,12 @@ Expr *ExprRewriter::coerceCallArguments(
5974
5981
auto arg = getArg (argIdx);
5975
5982
auto argType = cs.getType (arg);
5976
5983
5984
+ // Update the trailing closure index if needed.
5985
+ if (oldTrailingClosureIndex && *oldTrailingClosureIndex == argIdx) {
5986
+ assert (!newTrailingClosureIndex);
5987
+ newTrailingClosureIndex = newArgs.size ();
5988
+ }
5989
+
5977
5990
// Save the original label location.
5978
5991
newLabelLocs.push_back (getLabelLoc (argIdx));
5979
5992
@@ -6089,14 +6102,17 @@ Expr *ExprRewriter::coerceCallArguments(
6089
6102
assert (newArgs.size () == newParams.size ());
6090
6103
assert (newArgs.size () == newLabels.size ());
6091
6104
assert (newArgs.size () == newLabelLocs.size ());
6105
+ assert (oldTrailingClosureIndex.hasValue () ==
6106
+ newTrailingClosureIndex.hasValue ());
6107
+ assert (!newTrailingClosureIndex || *newTrailingClosureIndex < newArgs.size ());
6092
6108
6093
6109
// This is silly. SILGen gets confused if a 'self' parameter is wrapped
6094
6110
// in a ParenExpr sometimes.
6095
6111
if (!argTuple && !argParen &&
6096
6112
(params[0 ].getValueOwnership () == ValueOwnership::Default ||
6097
6113
params[0 ].getValueOwnership () == ValueOwnership::InOut)) {
6098
6114
assert (newArgs.size () == 1 );
6099
- assert (!unlabeledTrailingClosureIndex );
6115
+ assert (!newTrailingClosureIndex );
6100
6116
return newArgs[0 ];
6101
6117
}
6102
6118
@@ -6111,7 +6127,7 @@ Expr *ExprRewriter::coerceCallArguments(
6111
6127
bool isImplicit = arg->isImplicit ();
6112
6128
arg = new (ctx) ParenExpr (
6113
6129
lParenLoc, newArgs[0 ], rParenLoc,
6114
- static_cast <bool >(unlabeledTrailingClosureIndex ));
6130
+ static_cast <bool >(newTrailingClosureIndex ));
6115
6131
arg->setImplicit (isImplicit);
6116
6132
}
6117
6133
} else {
@@ -6126,7 +6142,7 @@ Expr *ExprRewriter::coerceCallArguments(
6126
6142
} else {
6127
6143
// Build a new TupleExpr, re-using source location information.
6128
6144
arg = TupleExpr::create (ctx, lParenLoc, rParenLoc, newArgs, newLabels,
6129
- newLabelLocs, unlabeledTrailingClosureIndex ,
6145
+ newLabelLocs, newTrailingClosureIndex ,
6130
6146
/* implicit=*/ arg->isImplicit ());
6131
6147
}
6132
6148
}
0 commit comments