Skip to content

Commit a535948

Browse files
committed
AST/SourceKit: Fix printing of inheritance clauses (sort of)
This still needs a lot more cleanup.
1 parent 37dbd05 commit a535948

File tree

6 files changed

+73
-12
lines changed

6 files changed

+73
-12
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7722,11 +7722,69 @@ void swift::printEnumElementsAsCases(
77227722
OS << ": " << getCodePlaceholder() << "\n";
77237723
});
77247724
}
7725+
/// For a protocol, don't consult getInherited() at all. Instead, rebuild
7726+
/// the inherited types from getInheritedProtocols(), getSuperclass(), and
7727+
/// the inverse requirement transform.
7728+
///
7729+
/// FIXME: This seems generally useful and should be moved elsewhere.
7730+
static void getSyntacticInheritanceClause(const ProtocolDecl *proto,
7731+
llvm::SmallVectorImpl<InheritedEntry> &Results) {
7732+
auto &ctx = proto->getASTContext();
7733+
7734+
if (auto superclassTy = proto->getSuperclass()) {
7735+
Results.emplace_back(TypeLoc::withoutLoc(superclassTy),
7736+
/*isUnchecked=*/false,
7737+
/*isRetroactive=*/false,
7738+
/*isPreconcurrency=*/false);
7739+
}
7740+
7741+
InvertibleProtocolSet inverses = InvertibleProtocolSet::full();
7742+
7743+
for (auto *inherited : proto->getInheritedProtocols()) {
7744+
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
7745+
if (auto ip = inherited->getInvertibleProtocolKind()) {
7746+
inverses.remove(*ip);
7747+
continue;
7748+
}
7749+
7750+
for (auto ip : InvertibleProtocolSet::full()) {
7751+
auto *proto = ctx.getProtocol(getKnownProtocolKind(ip));
7752+
if (inherited->inheritsFrom(proto))
7753+
inverses.remove(ip);
7754+
}
7755+
}
7756+
7757+
Results.emplace_back(TypeLoc::withoutLoc(inherited->getDeclaredInterfaceType()),
7758+
/*isUnchecked=*/false,
7759+
/*isRetroactive=*/false,
7760+
/*isPreconcurrency=*/false);
7761+
}
7762+
7763+
if (ctx.LangOpts.hasFeature(Feature::NoncopyableGenerics)) {
7764+
for (auto ip : inverses) {
7765+
InvertibleProtocolSet singleton;
7766+
singleton.insert(ip);
7767+
7768+
auto inverseTy = ProtocolCompositionType::get(
7769+
ctx, ArrayRef<Type>(), singleton,
7770+
/*hasExplicitAnyObject=*/false);
7771+
Results.emplace_back(TypeLoc::withoutLoc(inverseTy),
7772+
/*isUnchecked=*/false,
7773+
/*isRetroactive=*/false,
7774+
/*isPreconcurrency=*/false);
7775+
}
7776+
}
7777+
}
77257778

77267779
void
77277780
swift::getInheritedForPrinting(
77287781
const Decl *decl, const PrintOptions &options,
77297782
llvm::SmallVectorImpl<InheritedEntry> &Results) {
7783+
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
7784+
getSyntacticInheritanceClause(proto, Results);
7785+
return;
7786+
}
7787+
77307788
InheritedTypes inherited = InheritedTypes(decl);
77317789

77327790
// Collect explicit inherited types.
@@ -7753,6 +7811,10 @@ swift::getInheritedForPrinting(
77537811
llvm::TinyPtrVector<ProtocolDecl *> uncheckedProtocols;
77547812
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
77557813
if (auto *proto = attr->getProtocol()) {
7814+
// FIXME: Reconstitute inverses here
7815+
if (proto->getInvertibleProtocolKind())
7816+
continue;
7817+
77567818
// The SerialExecutor conformance is only synthesized on the root
77577819
// actor class, so we can just test resilience immediately.
77587820
if (proto->isSpecificProtocol(KnownProtocolKind::SerialExecutor) &&
@@ -7788,6 +7850,10 @@ swift::getInheritedForPrinting(
77887850
continue;
77897851
}
77907852

7853+
// FIXME: Reconstitute inverses here
7854+
if (proto->getInvertibleProtocolKind())
7855+
continue;
7856+
77917857
Results.push_back({TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()),
77927858
isUnchecked,
77937859
/*isRetroactive=*/false,

test/SourceKit/DocSupport/doc_swift_module.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
// RUN: %sourcekitd-test -req=doc-info -module cake -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -I %t.mod > %t.response
44
// RUN: %diff -u %s.response %t.response
55

6-
// XFAIL: noncopyable_generics
7-

test/SourceKit/DocSupport/doc_swift_module1.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@
22
// RUN: %swift -emit-module -o %t.mod/cake1.swiftmodule %S/Inputs/cake1.swift -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-as-library
33
// RUN: %sourcekitd-test -req=doc-info -module cake1 -- -Xfrontend -disable-implicit-concurrency-module-import -Xfrontend -disable-implicit-string-processing-module-import -I %t.mod > %t.response
44
// RUN: %diff -u %s.response %t.response
5-
6-
// XFAIL: noncopyable_generics
7-

test/SourceKit/DocSupport/doc_swift_module_class_extension.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
// RUN: %diff -u %s.response %t.response
55

66
// rdar://76868074: Make sure we print the extensions for C.
7-
8-
// XFAIL: noncopyable_generics
9-

test/SourceKit/DocSupport/doc_system_module_underscored.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@
66

77
// RUN: %sourcekitd-test -req=doc-info -synthesized-extension -module UnderscoredProto -- -target %target-triple -Fsystem %t -module-cache-path %t/mcp > %t.response
88
// RUN: %diff -u %s.response %t.response
9-
10-
// XFAIL: noncopyable_generics
11-

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,13 @@ static void initDocGenericParams(const Decl *D, DocEntityInfo &Info,
335335
if (auto *typeDC = GC->getInnermostTypeContext())
336336
Proto = typeDC->getSelfProtocolDecl();
337337

338-
for (auto Req: GenericSig.getRequirements()) {
338+
SmallVector<Requirement, 2> Reqs;
339+
SmallVector<InverseRequirement, 2> InverseReqs;
340+
GenericSig->getRequirementsWithInverses(Reqs, InverseReqs);
341+
// FIXME: Handle InverseReqs, or change the above to getRequirements()
342+
// and update tests to include Copyable/Escapable
343+
344+
for (auto Req: Reqs) {
339345
if (Proto &&
340346
Req.getKind() == RequirementKind::Conformance &&
341347
Req.getFirstType()->isEqual(Proto->getSelfInterfaceType()) &&

0 commit comments

Comments
 (0)