Skip to content

Commit b2198e2

Browse files
committed
ASTPrinter: print public inherited protocols of the skipped private protocols
When printing the list of inherited protocols in the module interface, if private stdlib protocols are requested to be hidden, make sure to print public inherited protocols of the hidden protocols.
1 parent ce660f5 commit b2198e2

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5742,10 +5742,9 @@ swift::getInheritedForPrinting(
57425742

57435743
// Collect synthesized conformances.
57445744
auto &ctx = decl->getASTContext();
5745+
llvm::SetVector<ProtocolDecl *> protocols;
57455746
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
57465747
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
5747-
if (!options.shouldPrint(proto))
5748-
continue;
57495748
// The SerialExecutor conformance is only synthesized on the root
57505749
// actor class, so we can just test resilience immediately.
57515750
if (proto->isSpecificProtocol(KnownProtocolKind::SerialExecutor) &&
@@ -5755,9 +5754,28 @@ swift::getInheritedForPrinting(
57555754
isa<EnumDecl>(decl) &&
57565755
cast<EnumDecl>(decl)->hasRawType())
57575756
continue;
5758-
Results.push_back({TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()),
5759-
/*isUnchecked=*/false});
5757+
protocols.insert(proto);
5758+
}
5759+
}
5760+
5761+
for (size_t i = 0; i < protocols.size(); i++) {
5762+
auto proto = protocols[i];
5763+
5764+
if (!options.shouldPrint(proto)) {
5765+
// If private stdlib protocols are skipped and this is a private stdlib
5766+
// protocol, see if any of its inherited protocols are public. Those
5767+
// protocols can affect the user-visible behavior of the declaration, and
5768+
// should be printed.
5769+
if (options.SkipPrivateStdlibDecls &&
5770+
proto->isPrivateStdlibDecl(!options.SkipUnderscoredStdlibProtocols)) {
5771+
auto inheritedProtocols = proto->getInheritedProtocols();
5772+
protocols.insert(inheritedProtocols.begin(), inheritedProtocols.end());
5773+
}
5774+
continue;
57605775
}
5776+
5777+
Results.push_back({TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()),
5778+
/*isUnchecked=*/false});
57615779
}
57625780
}
57635781

test/ClangImporter/enum-error.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// RUN: echo '#include "enum-error.h"' > %t.m
1515
// RUN: %target-swift-ide-test -source-filename %s -print-header -header-to-print %S/Inputs/enum-error.h -import-objc-header %S/Inputs/enum-error.h -print-regular-comments --cc-args %target-cc-options -fsyntax-only %t.m -I %S/Inputs > %t.txt
1616
// RUN: %FileCheck -check-prefix=HEADER %s < %t.txt
17+
// RUN: %target-swift-ide-test -source-filename %s -print-header -header-to-print %S/Inputs/enum-error.h -import-objc-header %S/Inputs/enum-error.h -print-regular-comments --skip-private-stdlib-decls -skip-underscored-stdlib-protocols --cc-args %target-cc-options -fsyntax-only %t.m -I %S/Inputs > %t2.txt
18+
// RUN: %FileCheck -check-prefix=HEADER-NO-PRIVATE %s < %t2.txt
1719

1820
import Foundation
1921

@@ -121,12 +123,25 @@ class ObjCTest {
121123
}
122124
#endif
123125

124-
// HEADER: enum Code : Int32, _ErrorCodeProtocol {
125-
// HEADER: init?(rawValue: Int32)
126-
// HEADER: var rawValue: Int32 { get }
127-
// HEADER: typealias _ErrorType = TestError
128-
// HEADER: case TENone
129-
// HEADER: case TEOne
130-
// HEADER: case TETwo
126+
// HEADER: struct TestError : _BridgedStoredNSError {
127+
// HEADER: enum Code : Int32, _ErrorCodeProtocol {
128+
// HEADER: init?(rawValue: Int32)
129+
// HEADER: var rawValue: Int32 { get }
130+
// HEADER: typealias _ErrorType = TestError
131+
// HEADER: case TENone
132+
// HEADER: case TEOne
133+
// HEADER: case TETwo
134+
// HEADER: }
131135
// HEADER: }
132136
// HEADER: func getErr() -> TestError.Code
137+
138+
// HEADER-NO-PRIVATE: struct TestError : CustomNSError, Hashable, Error {
139+
// HEADER-NO-PRIVATE: enum Code : Int32, Equatable {
140+
// HEADER-NO-PRIVATE: init?(rawValue: Int32)
141+
// HEADER-NO-PRIVATE: var rawValue: Int32 { get }
142+
// HEADER-NO-PRIVATE: typealias _ErrorType = TestError
143+
// HEADER-NO-PRIVATE: case TENone
144+
// HEADER-NO-PRIVATE: case TEOne
145+
// HEADER-NO-PRIVATE: case TETwo
146+
// HEADER-NO-PRIVATE: }
147+
// HEADER-NO-PRIVATE: }

test/SourceKit/DocSupport/doc_error_domain.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// RUN: -sdk %sdk | %sed_clean > %t.response
44
// RUN: %FileCheck -input-file=%t.response %s
55

6-
// CHECK: struct MyError {
7-
// CHECK: enum Code : Int32 {
6+
// CHECK: struct MyError : CustomNSError, Hashable, Error {
7+
// CHECK: enum Code : Int32, Equatable {
88
// CHECK: case errFirst
99
// CHECK: case errSecond
1010
// CHECK: }

0 commit comments

Comments
 (0)