Skip to content

Commit d74acb7

Browse files
Merge pull request #41161 from apple/QuietMisdreavus/5.6/underscore-syms
[5.6][SymbolGraph] consider underscored symbols as `private` if they're `internal` rdar://86294802
2 parents 31aafc7 + ae55c0b commit d74acb7

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,23 @@ bool SymbolGraph::isImplicitlyPrivate(const Decl *D,
582582
}
583583

584584
// Don't record effectively internal declarations if specified
585-
if (Walker.Options.MinimumAccessLevel > AccessLevel::Internal &&
586-
D->hasUnderscoredNaming()) {
587-
return true;
585+
if (D->hasUnderscoredNaming()) {
586+
AccessLevel symLevel = AccessLevel::Public;
587+
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
588+
symLevel = VD->getFormalAccess();
589+
}
590+
591+
// Underscored symbols should be treated as `internal`, unless they're already
592+
// marked that way - in that case, treat them as `private`
593+
AccessLevel effectiveLevel;
594+
if (symLevel > AccessLevel::Internal) {
595+
effectiveLevel = AccessLevel::Internal;
596+
} else {
597+
effectiveLevel = AccessLevel::Private;
598+
}
599+
600+
if (Walker.Options.MinimumAccessLevel > effectiveLevel)
601+
return true;
588602
}
589603

590604
// Don't include declarations with the @_spi attribute unless the
Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,49 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -module-name SkipsPublicUnderscore -emit-module -emit-module-path %t/
33
// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t
4-
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json
4+
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix PUBLIC
5+
6+
// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t -minimum-access-level internal
7+
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix INTERNAL
8+
9+
// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t -minimum-access-level private
10+
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix PRIVATE
511

612
public protocol PublicProtocol {}
713

8-
// CHECK-NOT: precise:{{.*}}_ProtocolShouldntAppear
9-
// CHECK-NOT: precise:{{.*}}PublicProtocol
14+
public class SomeClass {
15+
// underscored names marked `internal` or tighter should be considered `private`
16+
17+
// PUBLIC-NOT: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
18+
// INTERNAL-NOT: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
19+
// PRIVATE: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
20+
internal var _InternalVar: String = ""
21+
}
22+
23+
// PUBLIC-NOT: precise:{{.*}}_ProtocolShouldntAppear
24+
// PUBLIC-NOT: precise:{{.*}}PublicProtocol
1025
@_show_in_interface
1126
public protocol _ProtocolShouldntAppear {}
1227

13-
// CHECK-NOT: _ShouldntAppear
28+
// PUBLIC-NOT: _ShouldntAppear
29+
// INTERNAL-DAG: _ShouldntAppear
1430

31+
// INTERNAL-DAG: "precise": "s:21SkipsPublicUnderscore23_ProtocolShouldntAppearP"
1532
public struct _ShouldntAppear: PublicProtocol, _ProtocolShouldntAppear {
1633
// Although these are public and not underscored,
1734
// they are inside an underscored type,
1835
// so shouldn't be allowed through.
1936

20-
// CHECK-NOT: shouldntAppear
37+
// PUBLIC-NOT: shouldntAppear
38+
// INTERNAL-DAG: shouldntAppear
2139
public var shouldntAppear: Int
2240

23-
// CHECK-NOT: InnerShouldntAppear
41+
// PUBLIC-NOT: InnerShouldntAppear
42+
// INTERNAL-DAG: InnerShouldntAppear
2443
public struct InnerShouldntAppear {
2544

26-
// CHECK-NOT: InnerInnerShouldntAppear
45+
// PUBLIC-NOT: InnerInnerShouldntAppear
46+
// INTERNAL-DAG: InnerInnerShouldntAppear
2747
public struct InnerInnerShouldntAppear {}
2848
}
2949
}
@@ -34,22 +54,27 @@ public struct ShouldAppear: _ProtocolShouldntAppear {}
3454

3555
public struct PublicOuter {
3656
// Nor should an "internal" type's relationship to a "public" protocol.
37-
// CHECK-NOT: _InnerShouldntAppear
57+
// PUBLIC-NOT: _InnerShouldntAppear
58+
// INTERNAL-DAG: _InnerShouldntAppear
3859
public struct _InnerShouldntAppear: PublicProtocol {}
3960
}
4061

4162
extension PublicOuter {
42-
// CHECK-NOT: _FromExtension
63+
// PUBLIC-NOT: _FromExtension
64+
// INTERNAL-DAG: _FromExtension
4365
public struct _FromExtension: PublicProtocol {
44-
// CHECK-NOT: shouldntAppear
66+
// PUBLIC-NOT: shouldntAppear
67+
// INTERNAL-DAG: shouldntAppear
4568
public var shouldntAppear: Int
4669
}
4770
}
4871

4972
extension _ShouldntAppear {
50-
// CHECK-NOT: FromExtension
73+
// PUBLIC-NOT: FromExtension
74+
// INTERNAL-DAG: FromExtension
5175
public struct FromExtension: PublicProtocol {
52-
// CHECK-NOT: shouldntAppear
76+
// PUBLIC-NOT: shouldntAppear
77+
// INTERNAL-DAG: shouldntAppear
5378
public var shouldntAppear: Int
5479
}
5580
}
@@ -60,4 +85,4 @@ extension _ShouldntAppear.InnerShouldntAppear {
6085

6186
extension _ShouldntAppear.InnerShouldntAppear: Equatable {}
6287

63-
// CHECK: "relationships": []
88+
// PUBLIC: "relationships": []

0 commit comments

Comments
 (0)