Merge pull request #84501 from slavapestov/fix-rdar160389221

Sema: Workaround for broken existential opening behavior
This commit is contained in:
Slava Pestov
2025-09-26 18:36:49 -04:00
committed by GitHub
2 changed files with 35 additions and 1 deletions

View File

@@ -13394,8 +13394,12 @@ retry_after_fail:
choiceType = objectType;
}
// FIXME: The !getSelfProtocolDecl() check is load-bearing, because
// this optimization interacts poorly with existential opening
// somehow. It should all be removed.
if (auto *choiceFnType = choiceType->getAs<FunctionType>()) {
if (isa<ConstructorDecl>(choice.getDecl())) {
if (isa<ConstructorDecl>(choice.getDecl()) &&
!choice.getDecl()->getDeclContext()->getSelfProtocolDecl()) {
auto choiceResultType = choice.getBaseType()
->getRValueType()
->getMetatypeInstanceType();

View File

@@ -0,0 +1,30 @@
// RUN: %target-typecheck-verify-swift
protocol P {}
func g(_: some P) {}
// expected-note@-1 {{required by global function 'g' where 'some P' = 'any P'}}
// rdar://problem/160389221
func good(_ x: Array<any P>) {
Array(x).forEach { y in g(y) }
}
extension Array {
var ffirst: Element? { fatalError() }
func ffirst(wwhere: (Element) -> Bool) -> Element { fatalError() }
}
func bad(_ x: Array<any P>) {
let y = x.ffirst!
g(y) // ok
let yy = x.ffirst
g(yy!) // ok
// FIXME: This is broken
g(x.ffirst!)
// expected-error@-1 {{type 'any P' cannot conform to 'P'}}
// expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}}
}