Skip to content

[SymbolGraph] do not synthesize subclass methods #38240

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
Jul 12, 2021
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
40 changes: 0 additions & 40 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ void SymbolGraph::recordNode(Symbol S) {
// with this declaration.
recordMemberRelationship(S);
recordConformanceSynthesizedMemberRelationships(S);
recordSuperclassSynthesizedMemberRelationships(S);
recordConformanceRelationships(S);
recordInheritanceRelationships(S);
recordDefaultImplementationRelationships(S);
Expand Down Expand Up @@ -259,45 +258,6 @@ void SymbolGraph::recordMemberRelationship(Symbol S) {
}
}

void SymbolGraph::recordSuperclassSynthesizedMemberRelationships(Symbol S) {
if (!Walker.Options.EmitSynthesizedMembers) {
return;
}
// Via class inheritance...
if (const auto *C = dyn_cast<ClassDecl>(S.getSymbolDecl())) {
// Collect all superclass members up the inheritance chain.
SmallPtrSet<const ValueDecl *, 32> SuperClassMembers;
const auto *Super = C->getSuperclassDecl();
while (Super) {
for (const auto *SuperMember : Super->getMembers()) {
if (const auto *SuperMemberVD = dyn_cast<ValueDecl>(SuperMember)) {
SuperClassMembers.insert(SuperMemberVD);
}
}
Super = Super->getSuperclassDecl();
}
// Remove any that are overridden by this class.
for (const auto *DerivedMember : C->getMembers()) {
if (const auto *DerivedMemberVD = dyn_cast<ValueDecl>(DerivedMember)) {
if (const auto *Overridden = DerivedMemberVD->getOverriddenDecl()) {
SuperClassMembers.erase(Overridden);
}
}
}
// What remains in SuperClassMembers are inherited members that
// haven't been overridden by the class.
// Add a synthesized relationship.
for (const auto *InheritedMember : SuperClassMembers) {
if (canIncludeDeclAsNode(InheritedMember)) {
Symbol Source(this, InheritedMember, C);
Symbol Target(this, C, nullptr);
Nodes.insert(Source);
recordEdge(Source, Target, RelationshipKind::MemberOf());
}
}
}
}

bool SymbolGraph::synthesizedMemberIsBestCandidate(const ValueDecl *VD,
const NominalTypeDecl *Owner) const {
const auto *FD = dyn_cast<FuncDecl>(VD);
Expand Down
9 changes: 0 additions & 9 deletions lib/SymbolGraphGen/SymbolGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ struct SymbolGraph {
*/
void recordConformanceSynthesizedMemberRelationships(Symbol S);

/**
If a declaration has members by subclassing, record a symbol with a
"synthesized" USR to disambiguate from the superclass's real implementation.

This makes it more convenient
to curate symbols on a subclass's documentation.
*/
void recordSuperclassSynthesizedMemberRelationships(Symbol S);

/**
Record InheritsFrom relationships for every class from which the
declaration inherits.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,24 @@
// RUN: %target-swift-symbolgraph-extract -module-name SuperclassImplementation -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/SuperclassImplementation.symbols.json

// This test references code that has been removed. The implementation that synthesized superclass
// methods was inconsistent; it failed to generate symbols for superclasses from another module.
// From a symbol-relation perspective, the `inheritsFrom` relation from a subclass to its superclass
// still exists, which already implies that all of the superclass's methods are available on the
// subclass. The synthesized methods for subclasses were removed to provide consistency between
// superclasses from the same module and those from a different one. If the implementation is
// brought back, ensure that it consistently adds synthesized methods for superclasses from
// different modules.

public class Base {
public init() {}
public func foo() {}
}

public class Derived: Base {
// CHECK-DAG: "precise": "s:24SuperclassImplementation4BaseC3fooyyF::SYNTHESIZED::s:24SuperclassImplementation7DerivedC"
// CHECK-NOT: "precise": "s:24SuperclassImplementation4BaseC3fooyyF::SYNTHESIZED::s:24SuperclassImplementation7DerivedC"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that an inheritsFrom relationship exists instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inheritsFrom relationship is already tested in the "SymbolGraph/Relationships/InheritsFrom" test.

}

public class DerivedDerived: Derived {
// CHECK-DAG: "precise": "s:24SuperclassImplementation4BaseC3fooyyF::SYNTHESIZED::s:24SuperclassImplementation07DerivedC0C"
// CHECK-NOT: "precise": "s:24SuperclassImplementation4BaseC3fooyyF::SYNTHESIZED::s:24SuperclassImplementation07DerivedC0C"
}