@@ -5851,6 +5851,65 @@ static void applyContextualClosureFlags(
5851
5851
}
5852
5852
}
5853
5853
5854
+ // For variadic generic declarations we need to compute a substituted
5855
+ // version of bindings because all of the packs are exploaded in the
5856
+ // substituted function type.
5857
+ //
5858
+ // \code
5859
+ // func fn<each T>(_: repeat each T) {}
5860
+ //
5861
+ // fn("", 42)
5862
+ // \endcode
5863
+ //
5864
+ // The type of `fn` in the call is `(String, Int) -> Void` but bindings
5865
+ // have only one parameter at index `0` with two argument positions: 0, 1.
5866
+ static bool shouldSubstituteParameterBindings (ConcreteDeclRef callee) {
5867
+ auto subst = callee.getSubstitutions ();
5868
+ if (subst.empty ())
5869
+ return false ;
5870
+
5871
+ auto sig = subst.getGenericSignature ();
5872
+ return llvm::any_of (
5873
+ sig.getGenericParams (),
5874
+ [&](const GenericTypeParamType *GP) { return GP->isParameterPack (); });
5875
+ }
5876
+
5877
+ // / Compute parameter binding substitutions by exploding pack expansions
5878
+ // / into multiple bindings (if they matched more than one argument) and
5879
+ // / ignoring empty ones.
5880
+ static void computeParameterBindingsSubstitutions (
5881
+ ConcreteDeclRef callee, ArrayRef<AnyFunctionType::Param> params,
5882
+ ArrayRef<ParamBinding> origBindings,
5883
+ SmallVectorImpl<ParamBinding> &substitutedBindings) {
5884
+ for (unsigned bindingIdx = 0 , numBindings = origBindings.size ();
5885
+ bindingIdx != numBindings; ++bindingIdx) {
5886
+ if (origBindings[bindingIdx].size () > 1 ) {
5887
+ const auto ¶m = params[substitutedBindings.size ()];
5888
+ if (!param.isVariadic ()) {
5889
+ #ifndef NDEBUG
5890
+ auto *PD = getParameterAt (callee.getDecl (), bindingIdx);
5891
+ assert (PD && PD->getInterfaceType ()->is <PackExpansionType>());
5892
+ #endif
5893
+ // Explode binding set to match substituted function parameters.
5894
+ for (auto argIdx : origBindings[bindingIdx])
5895
+ substitutedBindings.push_back ({argIdx});
5896
+ continue ;
5897
+ }
5898
+ }
5899
+
5900
+ const auto &bindings = origBindings[bindingIdx];
5901
+ if (bindings.size () == 0 ) {
5902
+ auto *PD = getParameterAt (callee.getDecl (), bindingIdx);
5903
+ // Skip pack expansions with no arguments because they are not
5904
+ // present in the substituted function type.
5905
+ if (PD->getInterfaceType ()->is <PackExpansionType>())
5906
+ continue ;
5907
+ }
5908
+
5909
+ substitutedBindings.push_back (bindings);
5910
+ }
5911
+ }
5912
+
5854
5913
ArgumentList *ExprRewriter::coerceCallArguments (
5855
5914
ArgumentList *args, AnyFunctionType *funcType, ConcreteDeclRef callee,
5856
5915
ApplyExpr *apply, ConstraintLocatorBuilder locator,
@@ -5905,8 +5964,16 @@ ArgumentList *ExprRewriter::coerceCallArguments(
5905
5964
auto parameterBindings = solution.argumentMatchingChoices .find (locatorPtr)
5906
5965
->second .parameterBindings ;
5907
5966
5967
+ SmallVector<ParamBinding, 4 > substitutedBindings;
5968
+ if (shouldSubstituteParameterBindings (callee)) {
5969
+ computeParameterBindingsSubstitutions (callee, params, parameterBindings,
5970
+ substitutedBindings);
5971
+ } else {
5972
+ substitutedBindings = parameterBindings;
5973
+ }
5974
+
5908
5975
SmallVector<Argument, 4 > newArgs;
5909
- for (unsigned paramIdx = 0 , numParams = parameterBindings .size ();
5976
+ for (unsigned paramIdx = 0 , numParams = substitutedBindings .size ();
5910
5977
paramIdx != numParams; ++paramIdx) {
5911
5978
// Extract the parameter.
5912
5979
const auto ¶m = params[paramIdx];
@@ -5920,7 +5987,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
5920
5987
5921
5988
// The first argument of this vararg parameter may have had a label;
5922
5989
// save its location.
5923
- auto &varargIndices = parameterBindings [paramIdx];
5990
+ auto &varargIndices = substitutedBindings [paramIdx];
5924
5991
SourceLoc labelLoc;
5925
5992
if (!varargIndices.empty ())
5926
5993
labelLoc = args->getLabelLoc (varargIndices[0 ]);
@@ -5969,7 +6036,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
5969
6036
}
5970
6037
5971
6038
// Handle default arguments.
5972
- if (parameterBindings [paramIdx].empty ()) {
6039
+ if (substitutedBindings [paramIdx].empty ()) {
5973
6040
auto owner = getDefaultArgOwner (callee, paramIdx);
5974
6041
auto paramTy = param.getParameterType ();
5975
6042
auto *defArg = new (ctx) DefaultArgumentExpr (
@@ -5982,8 +6049,8 @@ ArgumentList *ExprRewriter::coerceCallArguments(
5982
6049
// Otherwise, we have a plain old ordinary argument.
5983
6050
5984
6051
// Extract the argument used to initialize this parameter.
5985
- assert (parameterBindings [paramIdx].size () == 1 );
5986
- unsigned argIdx = parameterBindings [paramIdx].front ();
6052
+ assert (substitutedBindings [paramIdx].size () == 1 );
6053
+ unsigned argIdx = substitutedBindings [paramIdx].front ();
5987
6054
auto arg = args->get (argIdx);
5988
6055
auto *argExpr = arg.getExpr ();
5989
6056
auto argType = cs.getType (argExpr);
0 commit comments