Skip to content

[SymbolGraph] add sourceOrigin field for symbols implementing remote protocol requirements #37351

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 2 commits into from
May 13, 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
25 changes: 25 additions & 0 deletions lib/SymbolGraphGen/Edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@
#include "Symbol.h"
#include "SymbolGraphASTWalker.h"

#include <queue>

using namespace swift;
using namespace symbolgraphgen;

namespace {
const ValueDecl *getForeignProtocolRequirement(const ValueDecl *VD, const ModuleDecl *M) {
std::queue<const ValueDecl *> requirements;
while (true) {
for (auto *req : VD->getSatisfiedProtocolRequirements()) {
if (req->getModuleContext()->getNameStr() != M->getNameStr())
return req;
else
requirements.push(req);
}
if (requirements.empty())
return nullptr;
VD = requirements.front();
requirements.pop();
}
}
} // end anonymous namespace

void Edge::serialize(llvm::json::OStream &OS) const {
OS.object([&](){
OS.attribute("kind", Kind.Name);
Expand Down Expand Up @@ -66,6 +86,11 @@ void Edge::serialize(llvm::json::OStream &OS) const {

if (!InheritingDecl && Source.getSynthesizedBaseTypeDecl())
InheritingDecl = Source.getSymbolDecl();

if (!InheritingDecl) {
if (const auto *ID = getForeignProtocolRequirement(Source.getSymbolDecl(), &Graph->M))
InheritingDecl = ID;
}

// If our source symbol is a inheriting decl, write in information about
// where it's inheriting docs from.
Expand Down
14 changes: 14 additions & 0 deletions test/SymbolGraph/Relationships/Synthesized/Inputs/RemoteP.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Some Protocol
public protocol P {
func someFunc()

/// This one has docs!
func otherFunc()

func bonusFunc()
}
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 also test public default implementations of the protocol or is that not relevant?

e.g.,

public extension P {
    /// This one also has docs!
    func someFunc()
}

and assert that "This one also has docs!" doesn't get inherited. I'm not sure about this one because I think the extension someFunc() gets a separate USR than S's someFunc().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default implementations coming from an extension are tested as part of the existing inherited docs test, though not for protocols in a different module... 🤔

But i'm not sure i understand the situation you're asking about. Can you write an extension like that without an implementation? What happens with someFunc in that situation?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I meant

public extension P {
    /// This one also has docs!
    func someFunc() {}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that makes more sense. I've added a couple more situations to the test.


public extension P {
/// Extra default docs!
func extraFunc() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %S/Inputs/RemoteP.swift -module-name RemoteP -emit-module -emit-module-path %t/RemoteP.swiftmodule -emit-module-source-info-path %t/RemoteP.swiftsourceinfo -emit-module-doc-path %t/RemoteP.swiftdoc
// RUN: %target-swift-frontend %s -module-name RemoteInheritedDocs -emit-module -emit-module-path %t/RemoteInheritedDocs.swiftmodule -emit-module-source-info-path %t/RemoteInheritedDocs.swiftsourceinfo -emit-module-doc-path %t/RemoteInheritedDocs.swiftdoc -I %t

// RUN: %target-swift-symbolgraph-extract -module-name RemoteInheritedDocs -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SOME
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OTHER
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix BONUS
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix INHERIT
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix LOCAL
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OVERRIDE

// RUN: %target-swift-symbolgraph-extract -module-name RemoteInheritedDocs -I %t -pretty-print -output-dir %t -skip-inherited-docs
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SOME
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OTHER
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix BONUS
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix SKIP
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix LOCAL
// RUN: %FileCheck %s --input-file %t/RemoteInheritedDocs.symbols.json --check-prefix OVERRIDE

// SOME: "source": "s:19RemoteInheritedDocs1SV8someFuncyyF"
// SOME-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// SOME-NEXT: "sourceOrigin"
// SOME-NEXT: "identifier": "s:7RemoteP1PP8someFuncyyF"
// SOME-NEXT: "displayName": "P.someFunc()"

// OTHER: "source": "s:19RemoteInheritedDocs1SV9otherFuncyyF"
// OTHER-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// OTHER-NEXT: "sourceOrigin"
// OTHER-NEXT: "identifier": "s:7RemoteP1PP9otherFuncyyF"
// OTHER-NEXT: "displayName": "P.otherFunc()"

// BONUS: "source": "s:19RemoteInheritedDocs1SV9bonusFuncyyF"
// BONUS-NEXT: "target": "s:19RemoteInheritedDocs1SV"
// BONUS-NEXT: "sourceOrigin"
// BONUS-NEXT: "identifier": "s:7RemoteP1PP9bonusFuncyyF"
// BONUS-NEXT: "displayName": "P.bonusFunc()"

// INHERIT: This one has docs!
// SKIP-NOT: This one has docs!

// LOCAL: Local docs override!

// OVERRIDE-NOT: Extra default docs!
// OVERRIDE-NOT: Extension override!

import RemoteP

// The `RemoteP.P` protocol has three methods: `someFunc` and `bonusFunc` don't have docs upstream,
// but `otherFunc` does. Regardless, each one needs a `sourceOrigin` field connecting it to
// upstream.

// `RemoteP.P` also has an extension with a default implementation for `extraFunc` that does have
// docs, but overriding it here should prevent those from appearing

public struct S: P {
public func someFunc() {}

public func otherFunc() {}

/// Local docs override!
public func bonusFunc() {}

public func extraFunc() {}
}

public extension P {
/// Extension override!
func someFunc() {}
}