Skip to content

Sema: Partially revert existential opening fix #79404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions lib/Sema/OpenedExistentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/opened_existentials_default_arg.swift
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 0 additions & 13 deletions test/decl/protocol/existential_member_access/misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> {}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}