@@ -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
0 commit comments