@@ -4279,21 +4279,37 @@ ConstraintSystem::matchTypesBindTypeVar(
42794279 return getTypeMatchFailure(locator);
42804280 }
42814281
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.
42854293 if (type->is<PackExpansionType>()) {
4286- if (!shouldAttemptFixes())
4287- return getTypeMatchFailure(locator);
4294+ bool representsParameterList =
4295+ typeVar->getImpl()
4296+ .getLocator()
4297+ ->isLastElement<LocatorPathElt::ApplyArgument>();
42884298
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);
42934303
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+ }
42974313 }
42984314
42994315 // We do not allow keypaths to go through AnyObject. Let's create a fix
@@ -7828,7 +7844,8 @@ ConstraintSystem::simplifyConstructionConstraint(
78287844 if (!getASTContext().isSwiftVersionAtLeast(6)) {
78297845 auto paramTypeVar = createTypeVariable(
78307846 getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
7831- TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
7847+ TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape |
7848+ TVO_CanBindToPack);
78327849 addConstraint(ConstraintKind::BindTupleOfFunctionParams, memberType,
78337850 paramTypeVar, locator);
78347851 }
0 commit comments