Skip to content

Commit 00fbdc7

Browse files
committed
[CSApply] Use correct parameter index for default argument that follow pack expansions
`coerceCallArguments` has to be `ParameterList::getOrigParamIndex` to determine "original" parameter version which could be used with un-substituted parameter list.
1 parent 1900813 commit 00fbdc7

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/Sema/CSApply.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5963,9 +5963,10 @@ ArgumentList *ExprRewriter::coerceCallArguments(
59635963
assert(solution.argumentMatchingChoices.count(locatorPtr) == 1);
59645964
auto parameterBindings = solution.argumentMatchingChoices.find(locatorPtr)
59655965
->second.parameterBindings;
5966+
bool shouldSubstituteBindings = shouldSubstituteParameterBindings(callee);
59665967

59675968
SmallVector<ParamBinding, 4> substitutedBindings;
5968-
if (shouldSubstituteParameterBindings(callee)) {
5969+
if (shouldSubstituteBindings) {
59695970
computeParameterBindingsSubstitutions(callee, params, parameterBindings,
59705971
substitutedBindings);
59715972
} else {
@@ -6037,10 +6038,21 @@ ArgumentList *ExprRewriter::coerceCallArguments(
60376038

60386039
// Handle default arguments.
60396040
if (substitutedBindings[paramIdx].empty()) {
6041+
auto paramIdxForDefault = paramIdx;
6042+
// If bindings were substituted we need to find "original"
6043+
// (or contextless) parameter index for the default argument.
6044+
if (shouldSubstituteBindings) {
6045+
auto *paramList = getParameterList(callee.getDecl());
6046+
assert(paramList);
6047+
paramIdxForDefault =
6048+
paramList->getOrigParamIndex(callee.getSubstitutions(), paramIdx);
6049+
}
6050+
60406051
auto owner = getDefaultArgOwner(callee, paramIdx);
60416052
auto paramTy = param.getParameterType();
60426053
auto *defArg = new (ctx) DefaultArgumentExpr(
6043-
owner, paramIdx, args->getStartLoc(), paramTy, dc);
6054+
owner, paramIdxForDefault, args->getStartLoc(), paramTy, dc);
6055+
60446056
cs.cacheType(defArg);
60456057
newArgs.emplace_back(SourceLoc(), param.getLabel(), defArg);
60466058
continue;

test/Constraints/pack-expansion-expressions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,13 @@ do {
379379
defaults3(t: "", 3.14) // Ok
380380
defaults3(t: "", 3.14, u: 0, v) // Ok
381381
defaults3(t: "", 3.14, u: 0, v, extra: 42) // Ok
382+
383+
struct Defaulted<each T> {
384+
init(t: repeat each T, extra: Int? = nil) {}
385+
init<each U>(t: repeat each T, u: repeat each U, other: Int? = nil) {}
386+
}
387+
388+
_ = Defaulted(t: "a", 0, 1.0) // Ok
389+
_ = Defaulted(t: "b", 0) // Ok
390+
_ = Defaulted(t: "c", 1.0, u: "d", 0) // Ok
382391
}

0 commit comments

Comments
 (0)