Skip to content

Commit eff13b7

Browse files
authored
Merge pull request #83531 from xedin/rdar-152553143
[CSSimplify] Skip member access on existential checks if base is exis…
2 parents 7283687 + 387b4ff commit eff13b7

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10431,22 +10431,33 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
1043110431
}
1043210432

1043310433
const auto isUnsupportedExistentialMemberAccess = [&] {
10434+
if (!instanceTy->isExistentialType())
10435+
return false;
10436+
10437+
// If the base type is composed with marker protocol(s) i.e.
10438+
// `<<Type>> & Sendable`, let's skip this check because such
10439+
// compositions are always opened and simplified down to a
10440+
// superclass bound post-Sema.
10441+
if (auto *existential = instanceTy->getAs<ExistentialType>()) {
10442+
auto *compositionTy =
10443+
existential->getConstraintType()->getAs<ProtocolCompositionType>();
10444+
if (compositionTy &&
10445+
!compositionTy->withoutMarkerProtocols()->isExistentialType())
10446+
return false;
10447+
}
10448+
1043410449
// We may not be able to derive a well defined type for an existential
1043510450
// member access if the member's signature references 'Self'.
10436-
if (instanceTy->isExistentialType()) {
10437-
switch (isMemberAvailableOnExistential(instanceTy, decl)) {
10438-
case ExistentialMemberAccessLimitation::Unsupported:
10439-
// TODO: Write-only accesses are not supported yet.
10440-
case ExistentialMemberAccessLimitation::WriteOnly:
10441-
return true;
10451+
switch (isMemberAvailableOnExistential(instanceTy, decl)) {
10452+
case ExistentialMemberAccessLimitation::Unsupported:
10453+
// TODO: Write-only accesses are not supported yet.
10454+
case ExistentialMemberAccessLimitation::WriteOnly:
10455+
return true;
1044210456

10443-
case ExistentialMemberAccessLimitation::ReadOnly:
10444-
case ExistentialMemberAccessLimitation::None:
10445-
break;
10446-
}
10457+
case ExistentialMemberAccessLimitation::ReadOnly:
10458+
case ExistentialMemberAccessLimitation::None:
10459+
return false;
1044710460
}
10448-
10449-
return false;
1045010461
};
1045110462

1045210463
// See if we have an instance method, instance member or static method,

test/Interpreter/protocol_composition_with_markers.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,27 @@ do {
5858
generic(Int.self)
5959
// CHECK: D<Int>
6060
}
61+
62+
protocol Q {
63+
func update(_: [Self])
64+
}
65+
66+
extension Q {
67+
func update(_ arr: [Self]) { print(Self.self) }
68+
}
69+
70+
do {
71+
class Parent : Q {}
72+
class Subclass: Parent {}
73+
74+
func test(_ v: Parent & Sendable) {
75+
v.update([])
76+
}
77+
78+
test(Subclass())
79+
// CHECK: Subclass
80+
81+
let sendableV: any Subclass & Sendable = Subclass()
82+
test(sendableV)
83+
// CHECK: Subclass
84+
}

0 commit comments

Comments
 (0)