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