@@ -4279,21 +4279,37 @@ ConstraintSystem::matchTypesBindTypeVar(
4279
4279
return getTypeMatchFailure(locator);
4280
4280
}
4281
4281
4282
- // Binding to a pack expansion type is always an error. This indicates
4283
- // that a pack expansion expression was used in a context that doesn't
4284
- // support it.
4282
+ // Binding to a pack expansion type is always an error in Swift 6 mode.
4283
+ // This indicates that a pack expansion expression was used in a context
4284
+ // that doesn't support it.
4285
+ //
4286
+ // In Swift 5 and earlier initializer references are handled in a special
4287
+ // way that uses a type variable to represent a type of the parameter
4288
+ // list. Such type variables should be allowed to bind to a pack expansion
4289
+ // type to support cases where initializer has a single unlabeled variadic
4290
+ // generic parameter - `init(_ data: repeat each T)`.
4291
+ //
4292
+ // See BindTupleOfFunctionParams constraint for more details.
4285
4293
if (type->is<PackExpansionType>()) {
4286
- if (!shouldAttemptFixes())
4287
- return getTypeMatchFailure(locator);
4294
+ bool representsParameterList =
4295
+ typeVar->getImpl()
4296
+ .getLocator()
4297
+ ->isLastElement<LocatorPathElt::ApplyArgument>();
4288
4298
4289
- auto *fix =
4290
- AllowInvalidPackExpansion::create(*this, getConstraintLocator(locator));
4291
- if (recordFix(fix ))
4292
- return getTypeMatchFailure(locator);
4299
+ if (!(typeVar->getImpl().canBindToPack() && representsParameterList) ||
4300
+ getASTContext().isSwiftVersionAtLeast(6)) {
4301
+ if (!shouldAttemptFixes( ))
4302
+ return getTypeMatchFailure(locator);
4293
4303
4294
- // Don't allow the pack expansion type to propagate to other
4295
- // bindings.
4296
- type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
4304
+ auto *fix = AllowInvalidPackExpansion::create(
4305
+ *this, getConstraintLocator(locator));
4306
+ if (recordFix(fix))
4307
+ return getTypeMatchFailure(locator);
4308
+
4309
+ // Don't allow the pack expansion type to propagate to other
4310
+ // bindings.
4311
+ type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
4312
+ }
4297
4313
}
4298
4314
4299
4315
// We do not allow keypaths to go through AnyObject. Let's create a fix
@@ -7828,7 +7844,8 @@ ConstraintSystem::simplifyConstructionConstraint(
7828
7844
if (!getASTContext().isSwiftVersionAtLeast(6)) {
7829
7845
auto paramTypeVar = createTypeVariable(
7830
7846
getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
7831
- TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
7847
+ TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape |
7848
+ TVO_CanBindToPack);
7832
7849
addConstraint(ConstraintKind::BindTupleOfFunctionParams, memberType,
7833
7850
paramTypeVar, locator);
7834
7851
}
0 commit comments