Skip to content

Commit 0ca0629

Browse files
committed
Suppress opened existentials for self-conforming existential metatype arguments
1 parent d00ea98 commit 0ca0629

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,15 +1501,22 @@ shouldOpenExistentialCallArgument(
15011501
if (!argTy->isAnyExistentialType())
15021502
return None;
15031503

1504-
if (argTy->isExistentialType()) {
1505-
// If the existential argument type conforms to all of its protocol
1506-
// requirements, don't open the existential.
1507-
auto layout = argTy->getExistentialLayout();
1504+
1505+
// If the existential argument type conforms to all of its protocol
1506+
// requirements, don't open the existential.
1507+
{
1508+
Type existentialObjectType;
1509+
if (auto existentialMetaTy = argTy->getAs<ExistentialMetatypeType>())
1510+
existentialObjectType = existentialMetaTy->getInstanceType();
1511+
else
1512+
existentialObjectType = argTy;
1513+
auto layout = existentialObjectType->getExistentialLayout();
15081514
auto module = cs.DC->getParentModule();
15091515
bool containsNonSelfConformance = false;
15101516
for (auto proto : layout.getProtocols()) {
15111517
auto protoDecl = proto->getDecl();
1512-
auto conformance = module->lookupExistentialConformance(argTy, protoDecl);
1518+
auto conformance = module->lookupExistentialConformance(
1519+
existentialObjectType, protoDecl);
15131520
if (conformance.isInvalid()) {
15141521
containsNonSelfConformance = true;
15151522
break;

test/Constraints/opened_existentials_suppression.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ protocol P { }
44
extension Optional: P where Wrapped: P { }
55

66
func acceptsBox<T>(_ value: T) { }
7+
func acceptsBoxType<T>(_ value: T.Type) { }
78

89
// CHECK: passBox
910
// CHECK-NOT: open_existential_expr
@@ -13,4 +14,6 @@ func passBox(p: P, obj: AnyObject, err: Error) {
1314
acceptsBox(p as? P)
1415
acceptsBox(obj)
1516
acceptsBox(err)
17+
acceptsBoxType(Any.self)
18+
acceptsBoxType(AnyObject.self)
1619
}

0 commit comments

Comments
 (0)