Skip to content

[5.6][SymbolGraph] consider underscored symbols as private if they're internal #41161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/SymbolGraphGen/SymbolGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,23 @@ bool SymbolGraph::isImplicitlyPrivate(const Decl *D,
}

// Don't record effectively internal declarations if specified
if (Walker.Options.MinimumAccessLevel > AccessLevel::Internal &&
D->hasUnderscoredNaming()) {
return true;
if (D->hasUnderscoredNaming()) {
AccessLevel symLevel = AccessLevel::Public;
if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
symLevel = VD->getFormalAccess();
}

// Underscored symbols should be treated as `internal`, unless they're already
// marked that way - in that case, treat them as `private`
AccessLevel effectiveLevel;
if (symLevel > AccessLevel::Internal) {
effectiveLevel = AccessLevel::Internal;
} else {
effectiveLevel = AccessLevel::Private;
}

if (Walker.Options.MinimumAccessLevel > effectiveLevel)
return true;
}

// Don't include declarations with the @_spi attribute unless the
Expand Down
51 changes: 38 additions & 13 deletions test/SymbolGraph/Symbols/SkipsPublicUnderscore.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name SkipsPublicUnderscore -emit-module -emit-module-path %t/
// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix PUBLIC

// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t -minimum-access-level internal
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix INTERNAL

// RUN: %target-swift-symbolgraph-extract -module-name SkipsPublicUnderscore -I %t -pretty-print -output-dir %t -minimum-access-level private
// RUN: %FileCheck %s --input-file %t/SkipsPublicUnderscore.symbols.json --check-prefix PRIVATE

public protocol PublicProtocol {}

// CHECK-NOT: precise:{{.*}}_ProtocolShouldntAppear
// CHECK-NOT: precise:{{.*}}PublicProtocol
public class SomeClass {
// underscored names marked `internal` or tighter should be considered `private`

// PUBLIC-NOT: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
// INTERNAL-NOT: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
// PRIVATE: "precise": "s:21SkipsPublicUnderscore9SomeClassC12_InternalVarSSvp"
internal var _InternalVar: String = ""
}

// PUBLIC-NOT: precise:{{.*}}_ProtocolShouldntAppear
// PUBLIC-NOT: precise:{{.*}}PublicProtocol
@_show_in_interface
public protocol _ProtocolShouldntAppear {}

// CHECK-NOT: _ShouldntAppear
// PUBLIC-NOT: _ShouldntAppear
// INTERNAL-DAG: _ShouldntAppear

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

// CHECK-NOT: shouldntAppear
// PUBLIC-NOT: shouldntAppear
// INTERNAL-DAG: shouldntAppear
public var shouldntAppear: Int

// CHECK-NOT: InnerShouldntAppear
// PUBLIC-NOT: InnerShouldntAppear
// INTERNAL-DAG: InnerShouldntAppear
public struct InnerShouldntAppear {

// CHECK-NOT: InnerInnerShouldntAppear
// PUBLIC-NOT: InnerInnerShouldntAppear
// INTERNAL-DAG: InnerInnerShouldntAppear
public struct InnerInnerShouldntAppear {}
}
}
Expand All @@ -34,22 +54,27 @@ public struct ShouldAppear: _ProtocolShouldntAppear {}

public struct PublicOuter {
// Nor should an "internal" type's relationship to a "public" protocol.
// CHECK-NOT: _InnerShouldntAppear
// PUBLIC-NOT: _InnerShouldntAppear
// INTERNAL-DAG: _InnerShouldntAppear
public struct _InnerShouldntAppear: PublicProtocol {}
}

extension PublicOuter {
// CHECK-NOT: _FromExtension
// PUBLIC-NOT: _FromExtension
// INTERNAL-DAG: _FromExtension
public struct _FromExtension: PublicProtocol {
// CHECK-NOT: shouldntAppear
// PUBLIC-NOT: shouldntAppear
// INTERNAL-DAG: shouldntAppear
public var shouldntAppear: Int
}
}

extension _ShouldntAppear {
// CHECK-NOT: FromExtension
// PUBLIC-NOT: FromExtension
// INTERNAL-DAG: FromExtension
public struct FromExtension: PublicProtocol {
// CHECK-NOT: shouldntAppear
// PUBLIC-NOT: shouldntAppear
// INTERNAL-DAG: shouldntAppear
public var shouldntAppear: Int
}
}
Expand All @@ -60,4 +85,4 @@ extension _ShouldntAppear.InnerShouldntAppear {

extension _ShouldntAppear.InnerShouldntAppear: Equatable {}

// CHECK: "relationships": []
// PUBLIC: "relationships": []