Skip to content

Commit ca534ef

Browse files
committed
[ConstraintSystem] Handle presence of pack expansion type variables while matching
1 parent c7ef47d commit ca534ef

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ static Optional<unsigned> scoreParamAndArgNameTypo(StringRef paramName,
117117
return dist;
118118
}
119119

120+
static bool isPackExpansionType(Type type) {
121+
if (type->is<PackExpansionType>())
122+
return true;
123+
124+
if (auto *typeVar = type->getAs<TypeVariableType>())
125+
return typeVar->getImpl().isPackExpansion();
126+
127+
return false;
128+
}
129+
120130
bool constraints::doesMemberRefApplyCurriedSelf(Type baseTy,
121131
const ValueDecl *decl) {
122132
assert(decl->getDeclContext()->isTypeContext() &&
@@ -534,8 +544,7 @@ static bool matchCallArgumentsImpl(
534544
}
535545

536546
// Handle variadic parameters.
537-
if (param.isVariadic() ||
538-
param.getPlainType()->is<PackExpansionType>()) {
547+
if (param.isVariadic() || isPackExpansionType(param.getPlainType())) {
539548
// Claim the next argument with the name of this parameter.
540549
auto claimed =
541550
claimNextNamed(nextArgIdx, paramLabel, ignoreNameMismatch);
@@ -795,8 +804,7 @@ static bool matchCallArgumentsImpl(
795804
const auto &param = params[paramIdx];
796805

797806
// Variadic parameters can be unfulfilled.
798-
if (param.isVariadic() ||
799-
param.getPlainType()->is<PackExpansionType>())
807+
if (param.isVariadic() || isPackExpansionType(param.getPlainType()))
800808
continue;
801809

802810
// Parameters with defaults can be unfulfilled.
@@ -886,7 +894,7 @@ static bool matchCallArgumentsImpl(
886894

887895
// Does nothing for variadic tail.
888896
if ((params[paramIdx].isVariadic() ||
889-
params[paramIdx].getPlainType()->is<PackExpansionType>()) &&
897+
isPackExpansionType(params[paramIdx].getPlainType())) &&
890898
paramBindIdx > 0) {
891899
assert(args[fromArgIdx].getLabel().empty());
892900
continue;
@@ -1763,7 +1771,7 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
17631771
// is declared as `init(_: repeat each T)`. Although declaration
17641772
// based information reports parameter at index 0 as variadic generic
17651773
// the call site specializes it to `Int`.
1766-
if (auto *paramPackExpansion = paramTy->getAs<PackExpansionType>()) {
1774+
if (isPackExpansionType(paramTy)) {
17671775
SmallVector<Type, 2> argTypes;
17681776
for (auto argIdx : parameterBindings[paramIdx]) {
17691777
auto argType = argsWithLabels[argIdx].getPlainType();
@@ -1773,9 +1781,7 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
17731781
auto *argPack = PackType::get(cs.getASTContext(), argTypes);
17741782
auto *argPackExpansion = PackExpansionType::get(argPack, argPack);
17751783

1776-
cs.addConstraint(
1777-
subKind, argPackExpansion, paramPackExpansion,
1778-
loc, /*isFavored=*/false);
1784+
cs.addConstraint(subKind, argPackExpansion, paramTy, loc);
17791785
continue;
17801786
}
17811787
}
@@ -2081,16 +2087,6 @@ static bool isInPatternMatchingContext(ConstraintLocatorBuilder locator) {
20812087

20822088
namespace {
20832089

2084-
static bool isPackExpansionType(Type type) {
2085-
if (type->is<PackExpansionType>())
2086-
return true;
2087-
2088-
if (auto *typeVar = type->getAs<TypeVariableType>())
2089-
return typeVar->getImpl().isPackExpansion();
2090-
2091-
return false;
2092-
}
2093-
20942090
class TupleMatcher {
20952091
TupleType *tuple1;
20962092
TupleType *tuple2;
@@ -2421,8 +2417,9 @@ static PackType *replaceTypeVariablesWithFreshPacks(ConstraintSystem &cs,
24212417
for (unsigned i = 0, e = pack->getNumElements(); i < e; ++i) {
24222418
auto *packExpansionElt = pack->getElementType(i)->getAs<PackExpansionType>();
24232419

2424-
auto instantiatedPattern = pattern.transformRec([&](Type t) -> Optional<Type> {
2425-
if (t->is<PackExpansionType>())
2420+
auto instantiatedPattern = pattern.transformRec([&](Type t)
2421+
-> Optional<Type> {
2422+
if (isPackExpansionType(t))
24262423
return t;
24272424

24282425
if (auto *typeVar = t->getAs<TypeVariableType>()) {
@@ -2658,8 +2655,7 @@ static bool isSingleTupleParam(ASTContext &ctx,
26582655
return false;
26592656

26602657
const auto &param = params.front();
2661-
if ((param.isVariadic() ||
2662-
param.getPlainType()->is<PackExpansionType>()) ||
2658+
if ((param.isVariadic() || isPackExpansionType(param.getPlainType())) ||
26632659
param.isInOut() || param.hasLabel() || param.isIsolated())
26642660
return false;
26652661

@@ -11033,7 +11029,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1103311029

1103411030
// Cannot propagate pack expansion type from context,
1103511031
// it has to be handled by type matching logic.
11036-
if (contextualTy->is<PackExpansionType>())
11032+
if (isPackExpansionType(contextualTy))
1103711033
return false;
1103811034

1103911035
// If contextual type has an error, let's wait for inference,

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3784,7 +3784,9 @@ struct TypeSimplifier {
37843784
// Flatten single-element tuples containing type variables that cannot
37853785
// bind to packs.
37863786
auto typeVar = elementType->getAs<TypeVariableType>();
3787-
if (!element.hasName() && typeVar && !typeVar->getImpl().canBindToPack()) {
3787+
if (!element.hasName() && typeVar &&
3788+
!typeVar->getImpl().canBindToPack() &&
3789+
!typeVar->getImpl().isPackExpansion()) {
37883790
return typeVar;
37893791
}
37903792
}

0 commit comments

Comments
 (0)