Skip to content

Commit dc4ee4b

Browse files
committed
[CSSimplify] Allow converting tuple with pack expansions to Void for closure result
Maintains existing rule where single-expression closure is allowed to convert to contextual function type with `Void` result.
1 parent effa261 commit dc4ee4b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6985,6 +6985,14 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
69856985
return formUnsolvedResult();
69866986
}
69876987

6988+
// Closure result is allowed to convert to Void in certain circumstances,
6989+
// let's forego tuple matching because it is guaranteed to fail and jump
6990+
// to `() -> T` to `() -> Void` rule.
6991+
if (locator.endsWith<LocatorPathElt::ClosureBody>()) {
6992+
if (containsPackExpansionType(tuple1) && tuple2->isVoid())
6993+
break;
6994+
}
6995+
69886996
// Add each tuple type to the locator before matching the element types.
69896997
// This is useful for diagnostics, because the error message can use the
69906998
// full tuple type for several element mismatches. Use the original types

test/Constraints/pack-expansion-expressions.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ do {
497497
}
498498

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

0 commit comments

Comments
 (0)