Skip to content

Commit 9339316

Browse files
authored
Merge pull request #41409 from slavapestov/fix-typematcher-protocol-composition
AST: TypeMatcher should walk into ProtocolCompositionTypes
2 parents 32e02fa + d769992 commit 9339316

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

include/swift/AST/TypeMatcher.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,33 @@ class TypeMatcher {
303303
TRIVIAL_CASE(SILFunctionType)
304304
TRIVIAL_CASE(SILBlockStorageType)
305305
TRIVIAL_CASE(SILBoxType)
306-
TRIVIAL_CASE(ProtocolCompositionType)
306+
307+
bool visitProtocolCompositionType(CanProtocolCompositionType firstProtocolComposition,
308+
Type secondType,
309+
Type sugaredFirstType) {
310+
if (auto secondProtocolComposition = secondType->getAs<ProtocolCompositionType>()) {
311+
auto firstMembers = firstProtocolComposition->getMembers();
312+
auto secondMembers = secondProtocolComposition->getMembers();
313+
314+
if (firstMembers.size() == secondMembers.size()) {
315+
for (unsigned i : indices(firstMembers)) {
316+
auto firstMember = firstMembers[i];
317+
auto secondMember = secondMembers[i];
318+
319+
// FIXME: We lose sugar here, because the sugared type might have a different
320+
// number of members, or the members might appear in a different order.
321+
if (!this->visit(CanType(firstMember), secondMember, firstMember)) {
322+
return false;
323+
}
324+
}
325+
326+
return true;
327+
}
328+
}
329+
330+
return mismatch(firstProtocolComposition.getPointer(), secondType,
331+
sugaredFirstType);
332+
}
307333

308334
bool visitParameterizedProtocolType(CanParameterizedProtocolType firstParametrizedProto,
309335
Type secondType,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures -requirement-machine-protocol-signatures=on 2>&1 | %FileCheck %s
2+
3+
class C<T> {}
4+
5+
protocol P0 {}
6+
7+
protocol P1 {
8+
associatedtype T where T == C<U> & P0
9+
associatedtype U
10+
}
11+
12+
protocol P2 {
13+
associatedtype T where T == C<Int> & P0
14+
}
15+
16+
// CHECK-LABEL: .P3@
17+
// CHECK-NEXT: Requirement signature: <Self where Self.[P3]T : P1, Self.[P3]T : P2>
18+
protocol P3 {
19+
associatedtype T : P1 & P2 where T.U == Int
20+
}

0 commit comments

Comments
 (0)