Sema: Partially revert existential opening fix

Selectively revert 36683a804c to resolve
a source compatibility regression. See inline comment for use case. We
are going to consider acknowledging this use case in the rules in a
future release.
This commit is contained in:
Anthony Latsis
2025-02-11 14:30:40 +00:00
parent d620886106
commit 43e82b4f7e
4 changed files with 39 additions and 18 deletions

View File

@@ -256,11 +256,22 @@ findGenericParameterReferencesRec(CanGenericSignature genericSig,
if (auto *pack = type->getAs<PackType>()) {
auto info = GenericParameterReferenceInfo();
for (auto arg : pack->getElementTypes()) {
info |= findGenericParameterReferencesRec(
genericSig, origParam, openedParam, arg,
TypePosition::Invariant, /*canBeCovariantResult=*/false);
}
// FIXME: Source compatibility remedy to allow existential opening in
// the following case:
// ```
// protocol P {}
// struct S<each T> {}
// func foo<T: P>(_: T, _: S<T>? = nil) {}
// let p: any P
// foo(p)
// ```
//
// for (auto arg : pack->getElementTypes()) {
// info |= findGenericParameterReferencesRec(
// genericSig, origParam, openedParam, arg,
// TypePosition::Invariant, /*canBeCovariantResult=*/false);
// }
(void)pack;
return info;
}

View File

@@ -0,0 +1,11 @@
// RUN: %target-typecheck-verify-swift -target %target-swift-5.9-abi-triple
do {
protocol P {}
struct S<each T> {}
func foo<T: P>(_: T, _: Optional<S<T>> = nil) {}
let p: any P
foo(p) // OK
}

View File

@@ -50,19 +50,6 @@ func bar(a: any HasSameShape) -> (Int, String) {
a.foo(t: 1, u: "hi")
}
// Make sure we look through a pack type when evaluating the variance of the result
struct Variadic<each A> {}
protocol VariadicResult {
associatedtype A
func foo() -> Variadic<A>
}
func variadicResult(a: any VariadicResult) {
a.foo()
// expected-error@-1 {{member 'foo' cannot be used on value of type 'any VariadicResult'; consider using a generic constraint instead}}
}
// Pack expansions are invariant
struct Pair<X, Y> {}

View File

@@ -0,0 +1,12 @@
// RUN: not --crash %target-swift-frontend -typecheck -target %target-swift-5.9-abi-triple %s
struct Variadic<each A> {}
protocol VariadicResult {
associatedtype A
func foo() -> Variadic<A>
}
func variadicResult(a: any VariadicResult) {
a.foo()
}