Skip to content

Commit 96cbc01

Browse files
authored
Merge pull request #65861 from xedin/rdar-109160060
[CSSimplify] Allow conversions between tuples with pack expansions and `Any`
2 parents 406b7a6 + 181d2d1 commit 96cbc01

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6831,9 +6831,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
68316831
// becuase expansion could be defaulted to an empty pack which means
68326832
// that under substitution that element would disappear and the type
68336833
// would be just `(Int)`.
6834+
//
6835+
// Notable exception here is `Any` which doesn't require wrapping and
6836+
// would be handled by existental promotion in cases where it's allowed.
68346837
if (isTupleWithUnresolvedPackExpansion(origType1) ||
68356838
isTupleWithUnresolvedPackExpansion(origType2)) {
6836-
if (desugar1->is<TupleType>() != desugar2->is<TupleType>()) {
6839+
if (desugar1->is<TupleType>() != desugar2->is<TupleType>() &&
6840+
(!desugar1->isAny() && !desugar2->isAny())) {
68376841
return matchTypes(
68386842
desugar1->is<TupleType>() ? type1
68396843
: TupleType::get({type1}, getASTContext()),

test/Constraints/pack-expansion-expressions.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,26 @@ func test_partually_flattened_expansions() {
447447
_ = S<Int, String>().fn(t: 1, "hi", u: false, 1.0) // Ok
448448
}
449449

450+
// rdar://109160060 - tuple with pack expansions is not convertible to Any
451+
do {
452+
func test1<each T>(_: repeat (each T).Type) -> (repeat each T) {}
453+
print(test1(Int.self, String.self))
454+
455+
func test2<each T>(_ s: [Any], t: repeat (each T).Type) -> (repeat each T) {
456+
var iter = s.makeIterator()
457+
return (repeat (iter.next()! as! (each T)))
458+
}
459+
460+
print(test2([]))
461+
print(test2([1], t: Int.self))
462+
print(test2([1, "hi"], t: Int.self, String.self))
463+
print(test2([1, "hi", false], t: Int.self, String.self, Bool.self))
464+
465+
func test3<each T>(v: Any) -> (Int, repeat each T) {
466+
return v // expected-error {{cannot convert return expression of type 'Any' to return type '(Int, repeat each T)'}}
467+
}
468+
}
469+
450470
// rdar://107675464 - misplaced `each` results in `type of expression is ambiguous without more context`
451471
do {
452472
func test_correct_each<each T: P>(_ value: repeat each T) -> (repeat each T.A) {

0 commit comments

Comments
 (0)