Skip to content

Commit 3464cb1

Browse files
authored
Merge pull request #38882 from egorzhdan/astprinter-protocols
ASTPrinter: print public inherited protocols of the skipped private protocols
2 parents 5f268cc + b2198e2 commit 3464cb1

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
@@ -5747,10 +5747,9 @@ swift::getInheritedForPrinting(
57475747

57485748
// Collect synthesized conformances.
57495749
auto &ctx = decl->getASTContext();
5750+
llvm::SetVector<ProtocolDecl *> protocols;
57505751
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
57515752
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
5752-
if (!options.shouldPrint(proto))
5753-
continue;
57545753
// The SerialExecutor conformance is only synthesized on the root
57555754
// actor class, so we can just test resilience immediately.
57565755
if (proto->isSpecificProtocol(KnownProtocolKind::SerialExecutor) &&
@@ -5760,9 +5759,28 @@ swift::getInheritedForPrinting(
57605759
isa<EnumDecl>(decl) &&
57615760
cast<EnumDecl>(decl)->hasRawType())
57625761
continue;
5763-
Results.push_back({TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()),
5764-
/*isUnchecked=*/false});
5762+
protocols.insert(proto);
5763+
}
5764+
}
5765+
5766+
for (size_t i = 0; i < protocols.size(); i++) {
5767+
auto proto = protocols[i];
5768+
5769+
if (!options.shouldPrint(proto)) {
5770+
// If private stdlib protocols are skipped and this is a private stdlib
5771+
// protocol, see if any of its inherited protocols are public. Those
5772+
// protocols can affect the user-visible behavior of the declaration, and
5773+
// should be printed.
5774+
if (options.SkipPrivateStdlibDecls &&
5775+
proto->isPrivateStdlibDecl(!options.SkipUnderscoredStdlibProtocols)) {
5776+
auto inheritedProtocols = proto->getInheritedProtocols();
5777+
protocols.insert(inheritedProtocols.begin(), inheritedProtocols.end());
5778+
}
5779+
continue;
57655780
}
5781+
5782+
Results.push_back({TypeLoc::withoutLoc(proto->getDeclaredInterfaceType()),
5783+
/*isUnchecked=*/false});
57665784
}
57675785
}
57685786

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)