Skip to content

[CSSimplify] Account for the fact that variadic generic parameters co… #64601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1742,21 +1742,28 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
// inout and @autoclosure.
if (cs.getASTContext().LangOpts.hasFeature(Feature::VariadicGenerics) &&
paramInfo.isVariadicGenericParameter(paramIdx)) {
auto *paramPackExpansion = paramTy->castTo<PackExpansionType>();

SmallVector<Type, 2> argTypes;
for (auto argIdx : parameterBindings[paramIdx]) {
auto argType = argsWithLabels[argIdx].getPlainType();
argTypes.push_back(argType);
}
// If generic parameter comes from a variadic type declaration it's
// possible that it got specialized early and is no longer represented
// by a pack expansion type. For example, consider expression -
// `Test<Int>(42)` where `Test<each T>` and the initializer
// is declared as `init(_: repeat each T)`. Although declaration
// based information reports parameter at index 0 as variadic generic
// the call site specializes it to `Int`.
if (auto *paramPackExpansion = paramTy->getAs<PackExpansionType>()) {
SmallVector<Type, 2> argTypes;
for (auto argIdx : parameterBindings[paramIdx]) {
auto argType = argsWithLabels[argIdx].getPlainType();
argTypes.push_back(argType);
}

auto *argPack = PackType::get(cs.getASTContext(), argTypes);
auto *argPackExpansion = PackExpansionType::get(argPack, argPack);
auto *argPack = PackType::get(cs.getASTContext(), argTypes);
auto *argPackExpansion = PackExpansionType::get(argPack, argPack);

cs.addConstraint(
cs.addConstraint(
subKind, argPackExpansion, paramPackExpansion,
loc, /*isFavored=*/false);
continue;
continue;
}
}

// If type inference from default arguments is enabled, let's
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,15 @@ func test_pack_expansions_with_closures() {
takesVariadicFunction { y, z in fn(y, z) } // Ok
}
}

// rdar://107151854 - crash on invalid due to specialized pack expansion
func test_pack_expansion_specialization() {
struct Data<each T> {
init(_: repeat each T) {} // expected-note {{'init(_:)' declared here}}
}

_ = Data<Int>() // expected-error {{missing argument for parameter #1 in call}}
_ = Data<Int>(0) // Ok
_ = Data<Int, String>(42, "") // Ok
_ = Data<Int>(42, "") // expected-error {{extra argument in call}}
}