Skip to content

Commit beb7449

Browse files
authored
Merge pull request #64601 from xedin/rdar-107151854
[CSSimplify] Account for the fact that variadic generic parameters co…
2 parents 3f6bfca + 65cfef6 commit beb7449

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,21 +1742,28 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
17421742
// inout and @autoclosure.
17431743
if (cs.getASTContext().LangOpts.hasFeature(Feature::VariadicGenerics) &&
17441744
paramInfo.isVariadicGenericParameter(paramIdx)) {
1745-
auto *paramPackExpansion = paramTy->castTo<PackExpansionType>();
1746-
1747-
SmallVector<Type, 2> argTypes;
1748-
for (auto argIdx : parameterBindings[paramIdx]) {
1749-
auto argType = argsWithLabels[argIdx].getPlainType();
1750-
argTypes.push_back(argType);
1751-
}
1745+
// If generic parameter comes from a variadic type declaration it's
1746+
// possible that it got specialized early and is no longer represented
1747+
// by a pack expansion type. For example, consider expression -
1748+
// `Test<Int>(42)` where `Test<each T>` and the initializer
1749+
// is declared as `init(_: repeat each T)`. Although declaration
1750+
// based information reports parameter at index 0 as variadic generic
1751+
// the call site specializes it to `Int`.
1752+
if (auto *paramPackExpansion = paramTy->getAs<PackExpansionType>()) {
1753+
SmallVector<Type, 2> argTypes;
1754+
for (auto argIdx : parameterBindings[paramIdx]) {
1755+
auto argType = argsWithLabels[argIdx].getPlainType();
1756+
argTypes.push_back(argType);
1757+
}
17521758

1753-
auto *argPack = PackType::get(cs.getASTContext(), argTypes);
1754-
auto *argPackExpansion = PackExpansionType::get(argPack, argPack);
1759+
auto *argPack = PackType::get(cs.getASTContext(), argTypes);
1760+
auto *argPackExpansion = PackExpansionType::get(argPack, argPack);
17551761

1756-
cs.addConstraint(
1762+
cs.addConstraint(
17571763
subKind, argPackExpansion, paramPackExpansion,
17581764
loc, /*isFavored=*/false);
1759-
continue;
1765+
continue;
1766+
}
17601767
}
17611768

17621769
// If type inference from default arguments is enabled, let's

test/Constraints/pack-expansion-expressions.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,15 @@ func test_pack_expansions_with_closures() {
271271
takesVariadicFunction { y, z in fn(y, z) } // Ok
272272
}
273273
}
274+
275+
// rdar://107151854 - crash on invalid due to specialized pack expansion
276+
func test_pack_expansion_specialization() {
277+
struct Data<each T> {
278+
init(_: repeat each T) {} // expected-note {{'init(_:)' declared here}}
279+
}
280+
281+
_ = Data<Int>() // expected-error {{missing argument for parameter #1 in call}}
282+
_ = Data<Int>(0) // Ok
283+
_ = Data<Int, String>(42, "") // Ok
284+
_ = Data<Int>(42, "") // expected-error {{extra argument in call}}
285+
}

0 commit comments

Comments
 (0)