Skip to content

Commit 218ccb1

Browse files
committed
[ConstraintSystem] Adjust openType and openUnboundGenericType to open pack expansion types
Replace all `PackExpansionType` with a special type variable which would be resolved once the solver determines that there is enough contextual information.
1 parent 62b6d01 commit 218ccb1

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,11 @@ Type ConstraintSystem::openUnboundGenericType(GenericTypeDecl *decl,
835835
result = DC->mapTypeIntoContext(result);
836836
}
837837

838-
return result;
838+
return result.transform([&](Type type) {
839+
if (auto *expansion = dyn_cast<PackExpansionType>(type.getPointer()))
840+
return openPackExpansionType(expansion, replacements);
841+
return type;
842+
});
839843
}
840844

841845
static void checkNestedTypeConstraints(ConstraintSystem &cs, Type type,
@@ -968,6 +972,24 @@ Type ConstraintSystem::openType(Type type, OpenedTypeMap &replacements) {
968972
return type.transform([&](Type type) -> Type {
969973
assert(!type->is<GenericFunctionType>());
970974

975+
// Preserve single element tuples if their element is
976+
// pack expansion, otherwise it wouldn't be expanded.
977+
if (auto *tuple = type->getAs<TupleType>()) {
978+
if (tuple->getNumElements() == 1) {
979+
const auto &elt = tuple->getElement(0);
980+
if (!elt.hasName() && elt.getType()->is<PackExpansionType>()) {
981+
return TupleType::get(
982+
{openPackExpansionType(
983+
elt.getType()->castTo<PackExpansionType>(), replacements)},
984+
tuple->getASTContext());
985+
}
986+
}
987+
}
988+
989+
if (auto *expansion = type->getAs<PackExpansionType>()) {
990+
return openPackExpansionType(expansion, replacements);
991+
}
992+
971993
// Replace a generic type parameter with its corresponding type variable.
972994
if (auto genericParam = type->getAs<GenericTypeParamType>()) {
973995
auto known = replacements.find(

test/Constraints/pack-expansion-expressions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,16 @@ func test_no_unused_result_warning(arr: inout [Any]) {
408408
((repeat arr.append(each value))) // no warning
409409
}
410410
}
411+
412+
func test_partually_flattened_expansions() {
413+
struct S<each T> {
414+
init() {}
415+
416+
func fn<each U>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {
417+
return (repeat (each t, each u))
418+
}
419+
}
420+
421+
_ = S().fn(t: 1, "hi", u: false, 1.0) // Ok
422+
_ = S<Int, String>().fn(t: 1, "hi", u: false, 1.0) // Ok
423+
}

0 commit comments

Comments
 (0)