Skip to content

[SymbolGraph] Look at inherited protocols for default implementations #31000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,29 @@ SymbolGraph::recordInheritanceRelationships(Symbol S) {
}

void SymbolGraph::recordDefaultImplementationRelationships(Symbol S) {
const auto VD = S.getSymbolDecl();
if (const auto *Extension = dyn_cast<ExtensionDecl>(VD->getDeclContext())) {
if (const auto *Protocol = Extension->getExtendedProtocolDecl()) {
for (const auto *Member : Protocol->getMembers()) {
if (const auto *MemberVD = dyn_cast<ValueDecl>(Member)) {
if (MemberVD->getFullName().compare(VD->getFullName()) == 0) {
recordEdge(Symbol(this, VD, nullptr),
Symbol(this, MemberVD, nullptr),
RelationshipKind::DefaultImplementationOf());
}
const auto *VD = S.getSymbolDecl();

/// Claim a protocol `P`'s members as default implementation targets
/// for `VD`.
auto HandleProtocol = [=](const ProtocolDecl *P) {
for (const auto *Member : P->getMembers()) {
if (const auto *MemberVD = dyn_cast<ValueDecl>(Member)) {
if (MemberVD->getFullName().compare(VD->getFullName()) == 0) {
recordEdge(Symbol(this, VD, nullptr),
Symbol(this, MemberVD, nullptr),
RelationshipKind::DefaultImplementationOf());
}
}
}
};

if (const auto *Extension = dyn_cast<ExtensionDecl>(VD->getDeclContext())) {
if (const auto *ExtendedProtocol = Extension->getExtendedProtocolDecl()) {
HandleProtocol(ExtendedProtocol);
for (const auto *Inherited : ExtendedProtocol->getInheritedProtocols()) {
HandleProtocol(Inherited);
}
}
}
}

Expand Down
16 changes: 0 additions & 16 deletions test/SymbolGraph/Relationships/ConformsTo.swift

This file was deleted.

16 changes: 16 additions & 0 deletions test/SymbolGraph/Relationships/ConformsTo/Basic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name Basic -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name Basic -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/Basic.symbols.json

public protocol P {
var x: Int { get }
}

public struct S: P {
public var x: Int
}

// CHECK: "kind": "conformsTo"
// CHECK-NEXT: "source": "s:5Basic1SV"
// CHECK-NEXT: "target": "s:5Basic1PP"
23 changes: 23 additions & 0 deletions test/SymbolGraph/Relationships/ConformsTo/Indirect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name Indirect -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name Indirect -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/Indirect.symbols.json

public protocol P {
func foo()
}

public protocol Q : P {}

public struct S : Q {
public func foo() {}
}

// Q : P
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1QP",{{[[:space:]]*}}"target": "s:8Indirect1PP"

// S : P
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1SV",{{[[:space:]]*}}"target": "s:8Indirect1PP"

// S : Q
// CHECK-DAG: "kind": "conformsTo",{{[[:space:]]*}}"source": "s:8Indirect1SV",{{[[:space:]]*}}"target": "s:8Indirect1QP"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name Indirect -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name Indirect -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/Indirect.symbols.json

public protocol P {
associatedtype Thing
func foo()
}

public protocol Q : P {}

extension Q {
public func foo() {}
}

// CHECK-DAG: "kind": "defaultImplementationOf",{{[[:space:]]*}}"source": "s:8Indirect1QPAAE3fooyyF",{{[[:space:]]*}}"target": "s:8Indirect1PP3fooyyF"