Skip to content

[5.9][ConstraintSystem] Adjust the way pack parameters are opened #66927

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
Jun 26, 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
25 changes: 24 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,21 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
result = DC->mapTypeIntoContext(result);
}

return result.transform([&](Type type) {
return result.transform([&](Type type) -> Type {
// Although generic parameters are declared with just `each`
// their interface types introduce a pack expansion which
// means that the solver has to extact generic argument type
// variable from Pack{repeat ...} and drop that structure to
// make sure that generic argument gets inferred to a pack type.
if (auto *packTy = type->getAs<PackType>()) {
assert(packTy->getNumElements() == 1);
auto *expansion = packTy->getElementType(0)->castTo<PackExpansionType>();
auto *typeVar = expansion->getPatternType()->castTo<TypeVariableType>();
assert(typeVar->getImpl().getGenericParameter() &&
typeVar->getImpl().canBindToPack());
return typeVar;
}

if (auto *expansion = dyn_cast<PackExpansionType>(type.getPointer()))
return openPackExpansionType(expansion, replacements, locator);
return type;
Expand Down Expand Up @@ -990,6 +1004,15 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements,
}
}

// While opening variadic generic types that appear in other types
// we need to extract generic parameter from Pack{repeat ...} structure
// that gets introduced by the interface type, see
// \c openUnboundGenericType for more details.
if (auto *packTy = type->getAs<PackType>()) {
if (auto expansion = packTy->unwrapSingletonPackExpansion())
type = expansion->getPatternType();
}

if (auto *expansion = type->getAs<PackExpansionType>()) {
return openPackExpansionType(expansion, replacements, locator);
}
Expand Down
27 changes: 27 additions & 0 deletions test/Constraints/variadic_generic_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,30 @@ struct MissingMemberError<each T> {
// expected-error@-1 {{value of type 'MissingMemberError<repeat each T>' has no member 'doesNotExist'}}
}
}

// https://github.com/apple/swift/issues/66095
do {
struct Test<each S> {
init(_ s: repeat each S) {}
}

func test1<each T>(_ v: repeat each T) -> Test<repeat each T> {
return Test(repeat each v) // Ok
}

func test2<each T>(_ v: repeat each T) -> Test<repeat each T> {
return Test<repeat each T>(repeat each v) // Ok
}

func test3<each T>(_ v: repeat each T) -> Test<String, repeat each T, Int> {
return Test("a", repeat each v, 42) // Ok
}

func test4<each T>(_ v: repeat each T) -> Test<repeat each T, String, Int> {
return Test<repeat each T, String, Int>(repeat each v, "a", 42) // Ok
}

func test5<each T>(_ v: repeat each T) -> Test<String, Int, repeat each T> {
return Test<String, Int, repeat each T>("a", 42, repeat each v) // Ok
}
}