Skip to content

Commit de51fed

Browse files
authored
Merge pull request #85410 from hamishknight/pack-fix
[CS] Handle packs in `increaseScoreForGenericParamPointerConversion`
2 parents b640ae7 + ded97ba commit de51fed

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15421,7 +15421,23 @@ static void increaseScoreForGenericParamPointerConversion(
1542115421
return;
1542215422

1542315423
// Check to see if the parameter is a generic parameter, or dependent member.
15424-
auto paramTy = param->getInterfaceType()->lookThroughAllOptionalTypes();
15424+
// Look though optionals and pack expansions.
15425+
auto paramTy = param->getInterfaceType();
15426+
while (true) {
15427+
paramTy = paramTy->lookThroughAllOptionalTypes();
15428+
if (auto packExpansion = paramTy->getAs<PackExpansionType>()) {
15429+
paramTy = packExpansion->getPatternType();
15430+
continue;
15431+
}
15432+
// Also look through "vanishing" tuples.
15433+
if (auto *tupleTy = paramTy->getAs<TupleType>()) {
15434+
if (tupleTy->getNumElements() == 1 && !tupleTy->getElement(0).hasName()) {
15435+
paramTy = tupleTy->getElement(0).getType();
15436+
continue;
15437+
}
15438+
}
15439+
break;
15440+
}
1542515441
if (!paramTy->isTypeParameter())
1542615442
return;
1542715443

test/Constraints/valid_pointer_conversions.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,30 @@ func testGenericPointerConversions(
116116
// Make sure this is ambiguous.
117117
ptr.foo(chars) // expected-error {{ambiguous use of 'foo'}}
118118
}
119+
120+
// Make sure we prefer non-pack overloads when pointer conversions are involved.
121+
func testOverloadedPackPointerConversions() {
122+
func takesPtr(_ ptr: UnsafePointer<CChar>) {}
123+
124+
// Deprecation does not impact solution score, so we can use it to ensure
125+
// we don't pick it.
126+
@available(*, deprecated, message: "shouldn't have picked this overload")
127+
func packOverloaded1<each T, R>(_ xs: repeat each T, fn: (repeat each T) -> R?) {}
128+
func packOverloaded1<T, R>(_ x: T, fn: (T) -> R?) {}
129+
packOverloaded1("") { takesPtr($0) }
130+
131+
@available(*, deprecated, message: "shouldn't have picked this overload")
132+
func packOverloaded2<each T, R>(_ xs: (repeat each T)?, fn: (repeat each T) -> R?) {}
133+
func packOverloaded2<T, R>(_ x: T?, fn: (T) -> R?) {}
134+
packOverloaded2("") { takesPtr($0) }
135+
136+
@available(*, deprecated, message: "shouldn't have picked this overload")
137+
func packOverloaded3<each T, R>(_ xs: repeat (each T)?, fn: (repeat each T) -> R?) {}
138+
func packOverloaded3<T, R>(_ x: T?, fn: (T) -> R?) {}
139+
packOverloaded3("") { takesPtr($0) }
140+
141+
@available(*, deprecated, message: "shouldn't have picked this overload")
142+
func packOverloaded4<each T, R>(_ xs: (repeat (each T)?)?, fn: (repeat each T) -> R?) {}
143+
func packOverloaded4<T, R>(_ x: T??, fn: (T) -> R?) {}
144+
packOverloaded4("") { takesPtr($0) }
145+
}

0 commit comments

Comments
 (0)