@@ -4311,21 +4311,37 @@ ConstraintSystem::matchTypesBindTypeVar(
4311
4311
return getTypeMatchFailure(locator);
4312
4312
}
4313
4313
4314
- // Binding to a pack expansion type is always an error. This indicates
4315
- // that a pack expansion expression was used in a context that doesn't
4316
- // support it.
4314
+ // Binding to a pack expansion type is always an error in Swift 6 mode.
4315
+ // This indicates that a pack expansion expression was used in a context
4316
+ // that doesn't support it.
4317
+ //
4318
+ // In Swift 5 and earlier initializer references are handled in a special
4319
+ // way that uses a type variable to represent a type of the parameter
4320
+ // list. Such type variables should be allowed to bind to a pack expansion
4321
+ // type to support cases where initializer has a single unlabeled variadic
4322
+ // generic parameter - `init(_ data: repeat each T)`.
4323
+ //
4324
+ // See BindTupleOfFunctionParams constraint for more details.
4317
4325
if (type->is<PackExpansionType>()) {
4318
- if (!shouldAttemptFixes())
4319
- return getTypeMatchFailure(locator);
4326
+ bool representsParameterList =
4327
+ typeVar->getImpl()
4328
+ .getLocator()
4329
+ ->isLastElement<LocatorPathElt::ApplyArgument>();
4320
4330
4321
- auto *fix =
4322
- AllowInvalidPackExpansion::create(*this, getConstraintLocator(locator));
4323
- if (recordFix(fix ))
4324
- return getTypeMatchFailure(locator);
4331
+ if (!(typeVar->getImpl().canBindToPack() && representsParameterList) ||
4332
+ getASTContext().isSwiftVersionAtLeast(6)) {
4333
+ if (!shouldAttemptFixes( ))
4334
+ return getTypeMatchFailure(locator);
4325
4335
4326
- // Don't allow the pack expansion type to propagate to other
4327
- // bindings.
4328
- type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
4336
+ auto *fix = AllowInvalidPackExpansion::create(
4337
+ *this, getConstraintLocator(locator));
4338
+ if (recordFix(fix))
4339
+ return getTypeMatchFailure(locator);
4340
+
4341
+ // Don't allow the pack expansion type to propagate to other
4342
+ // bindings.
4343
+ type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
4344
+ }
4329
4345
}
4330
4346
4331
4347
// We do not allow keypaths to go through AnyObject. Let's create a fix
@@ -7860,7 +7876,8 @@ ConstraintSystem::simplifyConstructionConstraint(
7860
7876
if (!getASTContext().isSwiftVersionAtLeast(6)) {
7861
7877
auto paramTypeVar = createTypeVariable(
7862
7878
getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
7863
- TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
7879
+ TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape |
7880
+ TVO_CanBindToPack);
7864
7881
addConstraint(ConstraintKind::BindTupleOfFunctionParams, memberType,
7865
7882
paramTypeVar, locator);
7866
7883
}
0 commit comments