Skip to content

Commit b1b1698

Browse files
authored
Merge pull request #66910 from xedin/issue-66095
[ConstraintSystem] Adjust the way pack parameters are opened
2 parents 3122440 + 2227930 commit b1b1698

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,21 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
837837
result = DC->mapTypeIntoContext(result);
838838
}
839839

840-
return result.transform([&](Type type) {
840+
return result.transform([&](Type type) -> Type {
841+
// Although generic parameters are declared with just `each`
842+
// their interface types introduce a pack expansion which
843+
// means that the solver has to extact generic argument type
844+
// variable from Pack{repeat ...} and drop that structure to
845+
// make sure that generic argument gets inferred to a pack type.
846+
if (auto *packTy = type->getAs<PackType>()) {
847+
assert(packTy->getNumElements() == 1);
848+
auto *expansion = packTy->getElementType(0)->castTo<PackExpansionType>();
849+
auto *typeVar = expansion->getPatternType()->castTo<TypeVariableType>();
850+
assert(typeVar->getImpl().getGenericParameter() &&
851+
typeVar->getImpl().canBindToPack());
852+
return typeVar;
853+
}
854+
841855
if (auto *expansion = dyn_cast<PackExpansionType>(type.getPointer()))
842856
return openPackExpansionType(expansion, replacements, locator);
843857
return type;
@@ -990,6 +1004,15 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements,
9901004
}
9911005
}
9921006

1007+
// While opening variadic generic types that appear in other types
1008+
// we need to extract generic parameter from Pack{repeat ...} structure
1009+
// that gets introduced by the interface type, see
1010+
// \c openUnboundGenericType for more details.
1011+
if (auto *packTy = type->getAs<PackType>()) {
1012+
if (auto expansion = packTy->unwrapSingletonPackExpansion())
1013+
type = expansion->getPatternType();
1014+
}
1015+
9931016
if (auto *expansion = type->getAs<PackExpansionType>()) {
9941017
return openPackExpansionType(expansion, replacements, locator);
9951018
}

test/Constraints/variadic_generic_types.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,30 @@ struct MissingMemberError<each T> {
2929
// expected-error@-1 {{value of type 'MissingMemberError<repeat each T>' has no member 'doesNotExist'}}
3030
}
3131
}
32+
33+
// https://github.com/apple/swift/issues/66095
34+
do {
35+
struct Test<each S> {
36+
init(_ s: repeat each S) {}
37+
}
38+
39+
func test1<each T>(_ v: repeat each T) -> Test<repeat each T> {
40+
return Test(repeat each v) // Ok
41+
}
42+
43+
func test2<each T>(_ v: repeat each T) -> Test<repeat each T> {
44+
return Test<repeat each T>(repeat each v) // Ok
45+
}
46+
47+
func test3<each T>(_ v: repeat each T) -> Test<String, repeat each T, Int> {
48+
return Test("a", repeat each v, 42) // Ok
49+
}
50+
51+
func test4<each T>(_ v: repeat each T) -> Test<repeat each T, String, Int> {
52+
return Test<repeat each T, String, Int>(repeat each v, "a", 42) // Ok
53+
}
54+
55+
func test5<each T>(_ v: repeat each T) -> Test<String, Int, repeat each T> {
56+
return Test<String, Int, repeat each T>("a", 42, repeat each v) // Ok
57+
}
58+
}

0 commit comments

Comments
 (0)