Skip to content

[CSSimplify] Allow converting pack expansion types and tuples with pack expansions to Void for closure result #65830

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 2 commits into from
May 16, 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
40 changes: 24 additions & 16 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6985,6 +6985,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
return formUnsolvedResult();
}

// Closure result is allowed to convert to Void in certain circumstances,
// let's forego tuple matching because it is guaranteed to fail and jump
// to `() -> T` to `() -> Void` rule.
if (locator.endsWith<LocatorPathElt::ClosureBody>()) {
if (containsPackExpansionType(tuple1) && tuple2->isVoid())
break;
}

// Add each tuple type to the locator before matching the element types.
// This is useful for diagnostics, because the error message can use the
// full tuple type for several element mismatches. Use the original types
Expand Down Expand Up @@ -7282,22 +7290,6 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
}
}

// Matching types where one side is a pack expansion and the other is not
// means a pack expansion was used where it isn't supported.
if (type1->is<PackExpansionType>() != type2->is<PackExpansionType>()) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

if (type1->isPlaceholder() || type2->isPlaceholder())
return getTypeMatchSuccess();

auto *loc = getConstraintLocator(locator);
if (recordFix(AllowInvalidPackExpansion::create(*this, loc)))
return getTypeMatchFailure(locator);

return getTypeMatchSuccess();
}

if (kind >= ConstraintKind::Conversion) {
// An lvalue of type T1 can be converted to a value of type T2 so long as
// T1 is convertible to T2 (by loading the value). Note that we cannot get
Expand Down Expand Up @@ -7720,6 +7712,22 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
}
}

// Matching types where one side is a pack expansion and the other is not
// means a pack expansion was used where it isn't supported.
if (type1->is<PackExpansionType>() != type2->is<PackExpansionType>()) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

if (type1->isPlaceholder() || type2->isPlaceholder())
return getTypeMatchSuccess();

auto *loc = getConstraintLocator(locator);
if (recordFix(AllowInvalidPackExpansion::create(*this, loc)))
return getTypeMatchFailure(locator);

return getTypeMatchSuccess();
}

// Attempt fixes iff it's allowed, both types are concrete and
// we are not in the middle of attempting one already.
if (shouldAttemptFixes() && !flags.contains(TMF_ApplyingFix)) {
Expand Down
8 changes: 8 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,11 @@ do {
_ = (repeat overloaded(42, each v)) // Ok
}
}

// rdar://108904190 - top-level 'repeat' not allowed in single-expression closures
func test_pack_expansion_to_void_conv_for_closure_result<each T>(x: repeat each T) {
let _: () -> Void = { repeat print(each x) } // Ok
let _: () -> Void = { (repeat print(each x)) } // Ok
let _: (Int) -> Void = { repeat ($0, print(each x)) } // expected-warning {{'repeat (Int, ())' is unused}}
let _: (Int, String) -> Void = { ($0, repeat ($1, print(each x))) } // expected-warning {{'(Int, repeat (String, ()))' is unused}}
}