Skip to content

Commit 4987c3b

Browse files
authored
Merge pull request #76122 from hamishknight/platform-2
[AST] Account for non-user modules in `isPrivateStdlibDecl`
2 parents 406b8bd + 1cb0f8f commit 4987c3b

File tree

18 files changed

+96
-64
lines changed

18 files changed

+96
-64
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
13031303

13041304
bool hasUnderscoredNaming() const;
13051305

1306-
bool isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic = true) const;
1307-
1306+
/// Whether this declaration is from a system module and should be considered
1307+
/// implicitly private.
1308+
bool isPrivateSystemDecl(bool treatNonBuiltinProtocolsAsPublic = true) const;
1309+
13081310
/// Check if this is a declaration defined at the top level of the Swift module
13091311
bool isStdlibDecl() const;
13101312

include/swift/AST/PrintOptions.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ struct PrintOptions {
272272

273273
bool SkipSwiftPrivateClangDecls = false;
274274

275-
/// Whether to skip internal stdlib declarations.
276-
bool SkipPrivateStdlibDecls = false;
275+
/// Whether to skip underscored declarations from system modules.
276+
bool SkipPrivateSystemDecls = false;
277277

278-
/// Whether to skip underscored stdlib protocols.
278+
/// Whether to skip underscored protocols from system modules.
279279
/// Protocols marked with @_show_in_interface are still printed.
280-
bool SkipUnderscoredStdlibProtocols = false;
280+
bool SkipUnderscoredSystemProtocols = false;
281281

282282
/// Whether to skip unsafe C++ class methods that were renamed
283283
/// (e.g. __fooUnsafe). See IsSafeUseOfCxxDecl.
@@ -679,8 +679,8 @@ struct PrintOptions {
679679
result.SkipUnavailable = true;
680680
result.SkipImplicit = true;
681681
result.SkipSwiftPrivateClangDecls = true;
682-
result.SkipPrivateStdlibDecls = true;
683-
result.SkipUnderscoredStdlibProtocols = true;
682+
result.SkipPrivateSystemDecls = true;
683+
result.SkipUnderscoredSystemProtocols = true;
684684
result.SkipUnsafeCXXMethods = true;
685685
result.SkipDeinit = true;
686686
result.EmptyLineBetweenDecls = true;
@@ -788,7 +788,7 @@ struct PrintOptions {
788788
PrintOptions::FunctionRepresentationMode::None;
789789
PO.PrintDocumentationComments = false;
790790
PO.ExcludeAttrList.push_back(DeclAttrKind::Available);
791-
PO.SkipPrivateStdlibDecls = true;
791+
PO.SkipPrivateSystemDecls = true;
792792
PO.SkipUnsafeCXXMethods = true;
793793
PO.ExplodeEnumCaseDecls = true;
794794
PO.ShouldQualifyNestedDeclarations = QualifyNestedDeclarations::TypesOnly;

include/swift/AST/Type.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ class Type {
330330
/// subsystem.
331331
Type subst(InFlightSubstitution &subs) const;
332332

333-
bool isPrivateStdlibType(bool treatNonBuiltinProtocolsAsPublic = true) const;
333+
/// Whether this type is from a system module and should be considered
334+
/// implicitly private.
335+
bool isPrivateSystemType(bool treatNonBuiltinProtocolsAsPublic = true) const;
334336

335337
SWIFT_DEBUG_DUMP;
336338
void dump(raw_ostream &os, unsigned indent = 0) const;

include/swift/SymbolGraphGen/SymbolGraphOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ struct SymbolGraphOptions {
6060
/// members and conformances with the extended nominal.
6161
bool EmitExtensionBlockSymbols = false;
6262

63-
/// Whether to print information for private symbols in the standard library.
63+
/// Whether to print information for private symbols in system modules.
6464
/// This should be left as `false` when printing a full-module symbol graph,
6565
/// but SourceKit should be able to load the information when pulling symbol
6666
/// information for individual queries.
67-
bool PrintPrivateStdlibSymbols = false;
67+
bool PrintPrivateSystemSymbols = false;
6868

6969
/// If this has a value specifies an explicit allow list of reexported module
7070
/// names that should be included symbol graph.

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ SDKContext::shouldIgnore(Decl *D, const Decl* Parent) const {
17721772
if (D->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>())
17731773
return true;
17741774
} else {
1775-
if (D->isPrivateStdlibDecl(false))
1775+
if (D->isPrivateSystemDecl(false))
17761776
return true;
17771777
}
17781778
if (AvailableAttr::isUnavailable(D))

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,8 +2174,8 @@ bool ShouldPrintChecker::shouldPrint(const Decl *D,
21742174
}
21752175
}
21762176

2177-
if (Options.SkipPrivateStdlibDecls &&
2178-
D->isPrivateStdlibDecl(!Options.SkipUnderscoredStdlibProtocols))
2177+
if (Options.SkipPrivateSystemDecls &&
2178+
D->isPrivateSystemDecl(!Options.SkipUnderscoredSystemProtocols))
21792179
return false;
21802180

21812181
auto &ctx = D->getASTContext();
@@ -3386,7 +3386,7 @@ void PrintAST::visitTypeAliasDecl(TypeAliasDecl *decl) {
33863386
Type Ty = decl->getUnderlyingType();
33873387

33883388
// If the underlying type is private, don't print it.
3389-
if (Options.SkipPrivateStdlibDecls && Ty && Ty.isPrivateStdlibType())
3389+
if (Options.SkipPrivateSystemDecls && Ty && Ty.isPrivateSystemType())
33903390
ShouldPrint = false;
33913391

33923392
if (ShouldPrint) {
@@ -7813,8 +7813,8 @@ swift::getInheritedForPrinting(
78137813
// protocol, see if any of its inherited protocols are public. Those
78147814
// protocols can affect the user-visible behavior of the declaration, and
78157815
// should be printed.
7816-
if (options.SkipPrivateStdlibDecls &&
7817-
proto->isPrivateStdlibDecl(!options.SkipUnderscoredStdlibProtocols)) {
7816+
if (options.SkipPrivateSystemDecls &&
7817+
proto->isPrivateSystemDecl(!options.SkipUnderscoredSystemProtocols)) {
78187818
auto inheritedProtocols = proto->getInheritedProtocols();
78197819
protocols.insert(inheritedProtocols.begin(), inheritedProtocols.end());
78207820
if (isUnchecked)

lib/AST/Decl.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,25 +1133,24 @@ bool Decl::hasUnderscoredNaming() const {
11331133
return false;
11341134
}
11351135

1136-
bool Decl::isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic) const {
1136+
bool Decl::isPrivateSystemDecl(bool treatNonBuiltinProtocolsAsPublic) const {
11371137
const Decl *D = this;
11381138
if (auto ExtD = dyn_cast<ExtensionDecl>(D)) {
11391139
Type extTy = ExtD->getExtendedType();
1140-
return extTy.isPrivateStdlibType(treatNonBuiltinProtocolsAsPublic);
1140+
return extTy.isPrivateSystemType(treatNonBuiltinProtocolsAsPublic);
11411141
}
11421142

11431143
DeclContext *DC = D->getDeclContext()->getModuleScopeContext();
1144-
if (DC->getParentModule()->isBuiltinModule() ||
1145-
DC->getParentModule()->isSwiftShimsModule())
1144+
auto *M = DC->getParentModule();
1145+
if (M->isBuiltinModule() || M->isSwiftShimsModule())
11461146
return true;
1147-
if (!DC->getParentModule()->isSystemModule())
1147+
if (!M->isSystemModule() && !M->isNonUserModule())
11481148
return false;
11491149
auto FU = dyn_cast<FileUnit>(DC);
11501150
if (!FU)
11511151
return false;
1152-
// Check for Swift module and overlays.
1153-
if (!DC->getParentModule()->isStdlibModule() &&
1154-
FU->getKind() != FileUnitKind::SerializedAST)
1152+
// Check for stdlib and imported Swift modules.
1153+
if (!M->isStdlibModule() && FU->getKind() != FileUnitKind::SerializedAST)
11551154
return false;
11561155

11571156
if (isa<ProtocolDecl>(D)) {
@@ -4236,7 +4235,7 @@ bool ValueDecl::isInterfacePackageEffectivelyPublic() const {
42364235

42374236
bool ValueDecl::shouldHideFromEditor() const {
42384237
// Hide private stdlib declarations.
4239-
if (isPrivateStdlibDecl(/*treatNonBuiltinProtocolsAsPublic*/ false) ||
4238+
if (isPrivateSystemDecl(/*treatNonBuiltinProtocolsAsPublic*/ false) ||
42404239
// ShowInInterfaceAttr is for decls to show in interface as exception but
42414240
// they are not intended to be used directly.
42424241
getAttrs().hasAttribute<ShowInInterfaceAttr>())

lib/AST/Type.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4174,40 +4174,40 @@ TypeTraitResult TypeBase::canBeClass() {
41744174
return TypeTraitResult::IsNot;
41754175
}
41764176

4177-
bool Type::isPrivateStdlibType(bool treatNonBuiltinProtocolsAsPublic) const {
4177+
bool Type::isPrivateSystemType(bool treatNonBuiltinProtocolsAsPublic) const {
41784178
Type Ty = *this;
41794179
if (!Ty)
41804180
return false;
41814181

41824182
if (auto existential = dyn_cast<ExistentialType>(Ty.getPointer()))
4183-
return existential->getConstraintType()
4184-
.isPrivateStdlibType(treatNonBuiltinProtocolsAsPublic);
4183+
return existential->getConstraintType().isPrivateSystemType(
4184+
treatNonBuiltinProtocolsAsPublic);
41854185

41864186
// A 'public' typealias can have an 'internal' type.
41874187
if (auto *NAT = dyn_cast<TypeAliasType>(Ty.getPointer())) {
41884188
auto *AliasDecl = NAT->getDecl();
41894189
if (auto parent = NAT->getParent()) {
4190-
if (parent.isPrivateStdlibType(treatNonBuiltinProtocolsAsPublic))
4190+
if (parent.isPrivateSystemType(treatNonBuiltinProtocolsAsPublic))
41914191
return true;
41924192
}
41934193

4194-
if (AliasDecl->isPrivateStdlibDecl(treatNonBuiltinProtocolsAsPublic))
4194+
if (AliasDecl->isPrivateSystemDecl(treatNonBuiltinProtocolsAsPublic))
41954195
return true;
41964196

4197-
return Type(NAT->getSinglyDesugaredType()).isPrivateStdlibType(
4198-
treatNonBuiltinProtocolsAsPublic);
4197+
return Type(NAT->getSinglyDesugaredType())
4198+
.isPrivateSystemType(treatNonBuiltinProtocolsAsPublic);
41994199
}
42004200

42014201
if (auto Paren = dyn_cast<ParenType>(Ty.getPointer())) {
42024202
Type Underlying = Paren->getUnderlyingType();
4203-
return Underlying.isPrivateStdlibType(treatNonBuiltinProtocolsAsPublic);
4203+
return Underlying.isPrivateSystemType(treatNonBuiltinProtocolsAsPublic);
42044204
}
42054205

42064206
if (Type Unwrapped = Ty->getOptionalObjectType())
4207-
return Unwrapped.isPrivateStdlibType(treatNonBuiltinProtocolsAsPublic);
4207+
return Unwrapped.isPrivateSystemType(treatNonBuiltinProtocolsAsPublic);
42084208

42094209
if (auto TyD = Ty->getAnyNominal())
4210-
if (TyD->isPrivateStdlibDecl(treatNonBuiltinProtocolsAsPublic))
4210+
if (TyD->isPrivateSystemDecl(treatNonBuiltinProtocolsAsPublic))
42114211
return true;
42124212

42134213
return false;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ static void dumpAPIIfNeeded(const CompilerInstance &Instance) {
916916
PO.PrintOriginalSourceText = true;
917917
PO.Indent = 2;
918918
PO.PrintAccess = false;
919-
PO.SkipUnderscoredStdlibProtocols = true;
919+
PO.SkipUnderscoredSystemProtocols = true;
920920
SF->print(TempOS, PO);
921921
if (TempOS.str().trim().empty())
922922
return false; // nothing to show.

lib/IDE/CommentConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ visitDocComment(const DocComment *DC, TypeOrExtensionDecl SynthesizedTarget) {
381381
PO.VarInitializers = false;
382382
PO.ShouldQualifyNestedDeclarations =
383383
PrintOptions::QualifyNestedDeclarations::TypesOnly;
384-
PO.SkipUnderscoredStdlibProtocols = false;
384+
PO.SkipUnderscoredSystemProtocols = false;
385385
if (SynthesizedTarget)
386386
PO.initForSynthesizedExtension(SynthesizedTarget);
387387

lib/IDE/IDETypeChecking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
501501
// Members from underscored system protocols should still appear as
502502
// members of the target type, even if the protocols themselves are not
503503
// printed.
504-
AdjustedOpts.SkipUnderscoredStdlibProtocols = false;
504+
AdjustedOpts.SkipUnderscoredSystemProtocols = false;
505505
}
506506
if (AdjustedOpts.shouldPrint(E)) {
507507
auto Pair = isApplicable(E, Synthesized, EnablingE, Conf);

lib/Index/Index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10941094
return isa<ParamDecl>(D) && !IsRef &&
10951095
D->getDeclContext()->getContextKind() != DeclContextKind::AbstractClosureExpr;
10961096

1097-
if (D->isPrivateStdlibDecl())
1097+
if (D->isPrivateSystemDecl())
10981098
return false;
10991099

11001100
return true;

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ PrintOptions SymbolGraph::getDeclarationFragmentsPrintOptions() const {
6464
Opts.PrintFunctionRepresentationAttrs =
6565
PrintOptions::FunctionRepresentationMode::None;
6666
Opts.PrintUserInaccessibleAttrs = false;
67-
Opts.SkipPrivateStdlibDecls = !Walker.Options.PrintPrivateStdlibSymbols;
68-
Opts.SkipUnderscoredStdlibProtocols = !Walker.Options.PrintPrivateStdlibSymbols;
67+
Opts.SkipPrivateSystemDecls = !Walker.Options.PrintPrivateSystemSymbols;
68+
Opts.SkipUnderscoredSystemProtocols =
69+
!Walker.Options.PrintPrivateSystemSymbols;
6970
Opts.PrintGenericRequirements = true;
7071
Opts.PrintInherited = false;
7172
Opts.ExplodeEnumCaseDecls = true;
@@ -666,7 +667,7 @@ const ValueDecl *getProtocolRequirement(const ValueDecl *VD) {
666667
bool SymbolGraph::isImplicitlyPrivate(const Decl *D,
667668
bool IgnoreContext) const {
668669
// Don't record unconditionally private declarations
669-
if (D->isPrivateStdlibDecl(/*treatNonBuiltinProtocolsAsPublic=*/false)) {
670+
if (D->isPrivateSystemDecl(/*treatNonBuiltinProtocolsAsPublic=*/false)) {
670671
return true;
671672
}
672673

test/ClangImporter/enum-error.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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 --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 --skip-private-stdlib-decls -skip-underscored-stdlib-protocols --cc-args %target-cc-options -fsyntax-only %t.m -I %S/Inputs > %t2.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 --skip-private-system-decls -skip-underscored-system-protocols --cc-args %target-cc-options -fsyntax-only %t.m -I %S/Inputs > %t2.txt
1818
// RUN: %FileCheck -check-prefix=HEADER-NO-PRIVATE %s < %t2.txt
1919

2020
import Foundation

test/IDE/complete_rdar131854240.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %empty-directory(%t/completions)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %empty-directory(%t/Mock.platform/Developer/SDKs)
5+
// RUN: cp -r %clang-importer-sdk-path %t/Mock.platform/Developer/SDKs/Mock.sdk
6+
7+
// RUN: %empty-directory(%t/Mock.platform/Developer/usr/lib/Lib.swiftmodule)
8+
// RUN: %target-swift-frontend -emit-module %t/lib.swift -module-name Lib -swift-version 5 -emit-module-path %t/Mock.platform/Developer/usr/lib/Lib.swiftmodule/%module-target-triple.swiftmodule -enable-library-evolution -emit-module-interface-path %t/Mock.platform/Developer/usr/lib/Lib.swiftmodule/%module-target-triple.swiftinterface
9+
10+
// RUN: %target-swift-ide-test(mock-sdk: -sdk %t/Mock.platform/Developer/SDKs/Mock.sdk -I %t/Mock.platform/Developer/usr/lib) -batch-code-completion -source-filename %t/client.swift -filecheck %raw-FileCheck -completion-output-dir %t/completions
11+
12+
// REQUIRES: objc_interop
13+
14+
// rdar://131854240 - Make sure we don't show underscored decls in non-user
15+
// modules.
16+
17+
//--- lib.swift
18+
19+
public struct SomeNonUnderscoredType {}
20+
public struct _SomeUnderscoredType {}
21+
22+
//--- client.swift
23+
24+
import Lib
25+
26+
#^TOPLEVEL?check=TOPLEVEL;check=TOPLEVEL_NOT^#
27+
// TOPLEVEL: Decl[Struct]/OtherModule[Lib]/IsSystem: SomeNonUnderscoredType[#SomeNonUnderscoredType#]
28+
// TOPLEVEL_NOT-NOT: _SomeUnderscoredType

test/IDE/print_stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// RUN: %target-swift-ide-test -print-module -module-to-print=Swift -source-filename %s -print-interface-doc > %t-doc.txt
2020
// RUN: %FileCheck %s < %t-doc.txt
2121

22-
// RUN: %target-swift-ide-test -print-module -module-to-print=Swift -source-filename %s -print-interface -skip-underscored-stdlib-protocols > %t-prot.txt
22+
// RUN: %target-swift-ide-test -print-module -module-to-print=Swift -source-filename %s -print-interface -skip-underscored-system-protocols > %t-prot.txt
2323

2424
// CHECK-ARGC: static var argc: Int32 { get }
2525

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ fillSymbolInfo(CursorSymbolInfo &Symbol, const DeclInfo &DInfo,
10391039
Options.MinimumAccessLevel = AccessLevel::Private;
10401040
Options.IncludeSPISymbols = true;
10411041
Options.IncludeClangDocs = true;
1042-
Options.PrintPrivateStdlibSymbols = true;
1042+
Options.PrintPrivateSystemSymbols = true;
10431043

10441044
symbolgraphgen::printSymbolGraphForDecl(DInfo.VD, DInfo.BaseType,
10451045
DInfo.InSynthesizedExtension,

0 commit comments

Comments
 (0)