Skip to content

Commit f9610de

Browse files
authored
Merge pull request #31000 from bitjammer/acgarland/rdar-61178480-indirect-default-impls
[SymbolGraph] Look at inherited protocols for default implementations
2 parents 36667b9 + 72dbdb0 commit f9610de

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,29 @@ SymbolGraph::recordInheritanceRelationships(Symbol S) {
253253
}
254254

255255
void SymbolGraph::recordDefaultImplementationRelationships(Symbol S) {
256-
const auto VD = S.getSymbolDecl();
257-
if (const auto *Extension = dyn_cast<ExtensionDecl>(VD->getDeclContext())) {
258-
if (const auto *Protocol = Extension->getExtendedProtocolDecl()) {
259-
for (const auto *Member : Protocol->getMembers()) {
260-
if (const auto *MemberVD = dyn_cast<ValueDecl>(Member)) {
261-
if (MemberVD->getFullName().compare(VD->getFullName()) == 0) {
262-
recordEdge(Symbol(this, VD, nullptr),
263-
Symbol(this, MemberVD, nullptr),
264-
RelationshipKind::DefaultImplementationOf());
265-
}
256+
const auto *VD = S.getSymbolDecl();
257+
258+
/// Claim a protocol `P`'s members as default implementation targets
259+
/// for `VD`.
260+
auto HandleProtocol = [=](const ProtocolDecl *P) {
261+
for (const auto *Member : P->getMembers()) {
262+
if (const auto *MemberVD = dyn_cast<ValueDecl>(Member)) {
263+
if (MemberVD->getFullName().compare(VD->getFullName()) == 0) {
264+
recordEdge(Symbol(this, VD, nullptr),
265+
Symbol(this, MemberVD, nullptr),
266+
RelationshipKind::DefaultImplementationOf());
266267
}
267268
}
268269
}
270+
};
271+
272+
if (const auto *Extension = dyn_cast<ExtensionDecl>(VD->getDeclContext())) {
273+
if (const auto *ExtendedProtocol = Extension->getExtendedProtocolDecl()) {
274+
HandleProtocol(ExtendedProtocol);
275+
for (const auto *Inherited : ExtendedProtocol->getInheritedProtocols()) {
276+
HandleProtocol(Inherited);
277+
}
278+
}
269279
}
270280
}
271281

test/SymbolGraph/Relationships/ConformsTo.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name Basic -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name Basic -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/Basic.symbols.json
5+
6+
public protocol P {
7+
var x: Int { get }
8+
}
9+
10+
public struct S: P {
11+
public var x: Int
12+
}
13+
14+
// CHECK: "kind": "conformsTo"
15+
// CHECK-NEXT: "source": "s:5Basic1SV"
16+
// CHECK-NEXT: "target": "s:5Basic1PP"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name Indirect -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name Indirect -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/Indirect.symbols.json
5+
6+
public protocol P {
7+
func foo()
8+
}
9+
10+
public protocol Q : P {}
11+
12+
public struct S : Q {
13+
public func foo() {}
14+
}
15+
16+
// Q : P
17+
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1QP",{{[[:space:]]*}}"target": "s:8Indirect1PP"
18+
19+
// S : P
20+
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1SV",{{[[:space:]]*}}"target": "s:8Indirect1PP"
21+
22+
// S : Q
23+
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1SV",{{[[:space:]]*}}"target": "s:8Indirect1QP"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name Indirect -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name Indirect -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/Indirect.symbols.json
5+
6+
public protocol P {
7+
associatedtype Thing
8+
func foo()
9+
}
10+
11+
public protocol Q : P {}
12+
13+
extension Q {
14+
public func foo() {}
15+
}
16+
17+
// CHECK-DAG: "kind": "defaultImplementationOf",{{[[:space:]]*}}"source": "s:8Indirect1QPAAE3fooyyF",{{[[:space:]]*}}"target": "s:8Indirect1PP3fooyyF"

0 commit comments

Comments
 (0)