Skip to content

[5.9][ConstraintSystem] Fix support for a single pack expansion parameter … #64672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,8 @@ namespace {
if (!CS.getASTContext().isSwiftVersionAtLeast(6)) {
auto paramTypeVar = CS.createTypeVariable(
CS.getConstraintLocator(expr, ConstraintLocator::ApplyArgument),
TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape |
TVO_CanBindToPack);
CS.addConstraint(ConstraintKind::BindTupleOfFunctionParams, methodTy,
paramTypeVar, CS.getConstraintLocator(expr));
}
Expand Down
43 changes: 30 additions & 13 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4318,21 +4318,37 @@ ConstraintSystem::matchTypesBindTypeVar(
return getTypeMatchFailure(locator);
}

// Binding to a pack expansion type is always an error. This indicates
// that a pack expansion expression was used in a context that doesn't
// support it.
// Binding to a pack expansion type is always an error in Swift 6 mode.
// This indicates that a pack expansion expression was used in a context
// that doesn't support it.
//
// In Swift 5 and earlier initializer references are handled in a special
// way that uses a type variable to represent a type of the parameter
// list. Such type variables should be allowed to bind to a pack expansion
// type to support cases where initializer has a single unlabeled variadic
// generic parameter - `init(_ data: repeat each T)`.
//
// See BindTupleOfFunctionParams constraint for more details.
if (type->is<PackExpansionType>()) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);
bool representsParameterList =
typeVar->getImpl()
.getLocator()
->isLastElement<LocatorPathElt::ApplyArgument>();

auto *fix =
AllowInvalidPackExpansion::create(*this, getConstraintLocator(locator));
if (recordFix(fix))
return getTypeMatchFailure(locator);
if (!(typeVar->getImpl().canBindToPack() && representsParameterList) ||
getASTContext().isSwiftVersionAtLeast(6)) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

// Don't allow the pack expansion type to propagate to other
// bindings.
type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
auto *fix = AllowInvalidPackExpansion::create(
*this, getConstraintLocator(locator));
if (recordFix(fix))
return getTypeMatchFailure(locator);

// Don't allow the pack expansion type to propagate to other
// bindings.
type = PlaceholderType::get(typeVar->getASTContext(), typeVar);
}
}

// We do not allow keypaths to go through AnyObject. Let's create a fix
Expand Down Expand Up @@ -7867,7 +7883,8 @@ ConstraintSystem::simplifyConstructionConstraint(
if (!getASTContext().isSwiftVersionAtLeast(6)) {
auto paramTypeVar = createTypeVariable(
getConstraintLocator(locator, ConstraintLocator::ApplyArgument),
TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape);
TVO_CanBindToLValue | TVO_CanBindToInOut | TVO_CanBindToNoEscape |
TVO_CanBindToPack);
addConstraint(ConstraintKind::BindTupleOfFunctionParams, memberType,
paramTypeVar, locator);
}
Expand Down
21 changes: 21 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ func invalidRepeat<each T>(t: repeat each T) {
// expected-error@-1 {{value pack expansion can only appear inside a function argument list or tuple element}}
}

// Make sure that single parameter initializers are handled correctly because
// the have special type-checking rules in Swift < 6.
func test_init_refs_with_single_pack_expansion_param() {
struct Data<each V> {
init(_: repeat each V) {}
}

_ = Data() // Ok
_ = Data(42) // Ok
_ = Data(42, "") // Ok

struct EmptyAmbiguous<each V> {
init(_: repeat each V) {} // expected-note {{found this candidate}}
init(x: repeat each V) {} // expected-note {{found this candidate}}
}

_ = EmptyAmbiguous() // expected-error {{ambiguous use of 'init'}}
_ = EmptyAmbiguous(x: 42)
_ = EmptyAmbiguous(x: (42, "")) // Ok
}

func test_pack_expansions_with_closures() {
func takesVariadicFunction<each T>(function: (repeat each T) -> Int) {}

Expand Down