Skip to content

Commit 4804f21

Browse files
committed
[CSSimplify] Narrow down tuple wrapping for pack expansion matching
Follow-up for #82326. The optional injection is only viable is the wrapped type is not yet resolved, otherwise it's safe to wrap the optional.
1 parent 644f364 commit 4804f21

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7353,11 +7353,16 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
73537353
// and `Optional<T>` which would be handled by optional injection.
73547354
if (isTupleWithUnresolvedPackExpansion(origType1) ||
73557355
isTupleWithUnresolvedPackExpansion(origType2)) {
7356+
auto isTypeVariableWrappedInOptional = [](Type type) {
7357+
if (type->getOptionalObjectType()) {
7358+
return type->lookThroughAllOptionalTypes()->isTypeVariableOrMember();
7359+
}
7360+
return false;
7361+
};
73567362
if (isa<TupleType>(desugar1) != isa<TupleType>(desugar2) &&
7357-
!isa<InOutType>(desugar1) &&
7358-
!isa<InOutType>(desugar2) &&
7359-
!desugar1->getOptionalObjectType() &&
7360-
!desugar2->getOptionalObjectType() &&
7363+
!isa<InOutType>(desugar1) && !isa<InOutType>(desugar2) &&
7364+
!isTypeVariableWrappedInOptional(desugar1) &&
7365+
!isTypeVariableWrappedInOptional(desugar2) &&
73617366
!desugar1->isAny() &&
73627367
!desugar2->isAny()) {
73637368
return matchTypes(

test/Constraints/variadic_generic_constraints.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,28 @@ func badCallToZip<each T, each U>(t: repeat each T, u: repeat each U) {
7777
// expected-error@-2 {{pack expansion requires that 'each U' and 'each T' have the same shape}}
7878
}
7979

80-
do {
81-
func test<A, B, each C>(
80+
func variadicGenericsInOptionalContext(v: Int?) {
81+
func test1<A, B, each C>(
8282
_: A,
8383
_: B,
8484
_: repeat each C
8585
) throws -> (A, B, repeat each C) {
8686
fatalError()
8787
}
8888

89+
func test2<A, B, each C>(
90+
_: A,
91+
_: B,
92+
_: (repeat each C)
93+
) throws -> (A, B, repeat each C) {
94+
fatalError()
95+
}
96+
8997
func test() {
90-
guard let _ = try? test(1, 2, 3) else { return } // Ok
98+
guard let _ = try? test1(1, 2, 3) else { return } // Ok
99+
guard let _ = try? test1(1, 2, v) else { return } // Ok
100+
101+
guard let _ = try? test2(1, 2, 3) else { return } // Ok
102+
guard let _ = try? test2(1, 2, v) else { return } // Ok
91103
}
92104
}

0 commit comments

Comments
 (0)