Skip to content

Commit 8eb0377

Browse files
authored
[Index] Don't index non-public imported declarations (#25875)
Apart from mildly speeding up indexing, this also keeps the compiler from running into issues with implementation-only imports that may not be present while we're trying to index. rdar://problem/52083709 (cherry picked from commit 02e1a11)
1 parent 2c06daf commit 8eb0377

File tree

9 files changed

+104
-27
lines changed

9 files changed

+104
-27
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2912,7 +2912,7 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
29122912
if (resultDC->isLocalContext() || access == AccessLevel::Private)
29132913
return AccessScope(resultDC, /*private*/true);
29142914

2915-
if (auto enclosingNominal = dyn_cast<NominalTypeDecl>(resultDC)) {
2915+
if (auto enclosingNominal = dyn_cast<GenericTypeDecl>(resultDC)) {
29162916
auto enclosingAccess =
29172917
getAdjustedFormalAccess(enclosingNominal, useDC,
29182918
treatUsableFromInlineAsPublic);

lib/Index/Index.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
601601
if (D->isImplicit() && !isa<ConstructorDecl>(D))
602602
return false;
603603

604+
// Do not handle non-public imported decls.
605+
if (IsModuleFile && !D->isAccessibleFrom(nullptr))
606+
return false;
607+
604608
if (!IdxConsumer.indexLocals() && isLocalSymbol(D))
605609
return isa<ParamDecl>(D) && !IsRef &&
606610
D->getDeclContext()->getContextKind() != DeclContextKind::AbstractClosureExpr;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public protocol SECRETProtocol {}
2+
open class SECRETClass {}

test/Index/Inputs/my_system_overlay/my_system_overlay.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
public func some_func() {}
44

55
public class BaseCls {
6-
func theMeth() {}
6+
public func theMeth() {}
7+
internal func SECRET() {}
78
}
89

910
public class SubCls : BaseCls {
10-
override func theMeth() {}
11+
public override func theMeth() {}
1112
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %empty-directory(%t)
2+
//
3+
// RUN: %target-swift-frontend -emit-module -o %t -module-name CoreFoo %S/Inputs/implementation-only-imports-helper.swift
4+
// RUN: %target-swift-frontend -emit-module -I %t -o %t -module-name FooKit %s
5+
// RUN: %target-swift-ide-test -print-indexed-symbols -module-name FooKit -source-filename %s -I %t > %t.out
6+
7+
// RUN: mv %t/CoreFoo.swiftmodule %t/SecretCoreFoo.swiftmodule
8+
// RUN: echo '---FROM MODULE---' >> %t.out
9+
// RUN: %target-swift-ide-test -print-indexed-symbols -module-to-print FooKit -source-filename %s -I %t >> %t.out
10+
// RUN: %FileCheck %s -input-file=%t.out
11+
// RUN: %FileCheck -check-prefix NEGATIVE %s -input-file=%t.out
12+
13+
// CHECK: module | user | CoreFoo | {{.+}}CoreFoo.swiftmodule
14+
@_implementationOnly import CoreFoo
15+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | module/Swift | CoreFoo | c:@M@CoreFoo | Ref | rel: 0
16+
17+
internal class SECRETSubclass : SECRETClass {}
18+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | class/Swift | SECRETSubclass | [[SECRET_SUBCLASS_USR:s:[^|]+]] | Def | rel: 0
19+
// CHECK: [[@LINE-2]]:{{[0-9]+}} | class/Swift | SECRETClass | [[SECRET_CLASS_USR:s:[^|]+]] | Ref,RelBase | rel: 1
20+
// CHECK-NEXT: RelBase | class/Swift | SECRETSubclass | [[SECRET_SUBCLASS_USR]]{{$}}
21+
22+
public class PublicClass {
23+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | class/Swift | PublicClass | [[PUBLIC_CLASS_USR:s:[^|]+]] | Def | rel: 0
24+
internal var propSECRET: SECRETClass? { nil }
25+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-property/Swift | propSECRET |
26+
internal var propSECRET2: SECRETSubclass? { nil }
27+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-property/Swift | propSECRET2 |
28+
}
29+
30+
extension SECRETClass {
31+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | extension/ext-class/Swift | SECRETClass |
32+
internal func testSECRET() {}
33+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-method/Swift | testSECRET() |
34+
}
35+
36+
extension PublicClass {
37+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | extension/ext-class/Swift | PublicClass | [[PUBLIC_CLASS_EXTENSION_USR:s:[^|]+]] |
38+
public func extensionNamingMethod() {}
39+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-method/Swift | extensionNamingMethod() |
40+
41+
internal func testSECRET() {}
42+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-method/Swift | testSECRET() |
43+
44+
internal struct SECRETStruct: SECRETProtocol {}
45+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | struct/Swift | SECRETStruct | [[SECRET_NESTED_STRUCT_USR:s:[^|]+]] |
46+
// CHECK: [[@LINE-2]]:{{[0-9]+}} | protocol/Swift | SECRETProtocol | [[SECRET_PROTOCOL_USR:s:[^|]+]] | Ref,RelBase | rel: 1
47+
// CHECK-NEXT: RelBase | struct/Swift | SECRETStruct | [[SECRET_NESTED_STRUCT_USR]]
48+
}
49+
50+
extension Optional where Wrapped: SECRETProtocol {
51+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | extension/ext-enum/Swift | Optional |
52+
internal func extensionNamingMethod() {}
53+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-method/Swift | extensionNamingMethod() |
54+
55+
internal func testSECRET() {}
56+
// CHECK: [[@LINE-1]]:{{[0-9]+}} | instance-method/Swift | testSECRET() |
57+
}
58+
59+
// CHECK-LABEL: ---FROM MODULE---
60+
// NEGATIVE-LABEL: ---FROM MODULE---
61+
62+
// CHECK: 0:0 | class/Swift | PublicClass | [[PUBLIC_CLASS_USR]] | Def | rel: 0
63+
// CHECK: 0:0 | extension/ext-class/Swift | PublicClass | [[PUBLIC_CLASS_EXTENSION_USR]] | Def | rel: 0
64+
// CHECK: 0:0 | class/Swift | PublicClass | [[PUBLIC_CLASS_USR]] | Ref,RelExt | rel: 1
65+
// CHECK-NEXT: RelExt | extension/ext-class/Swift | PublicClass | [[PUBLIC_CLASS_EXTENSION_USR]]
66+
67+
68+
// NEGATIVE-NOT: CoreFoo
69+
// NEGATIVE-NOT: SECRET

test/Index/index_module.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public class CCC {}
2020
// CHECK: [[@LINE-2]]:14 | constructor/Swift | init() | [[CCC_init_USR:.*]] | Def,Impl,RelChild | rel: 1
2121
// CHECK-NEXT: RelChild | class/Swift | CCC | [[CCC_USR]]
2222

23+
public typealias GenericTypealias<T> = Optional<T>
24+
// CHECK: [[@LINE-1]]:18 | type-alias/Swift | GenericTypealias | [[TYPEALIAS_USR:.*]] | Def | rel: 0
25+
// CHECK: [[@LINE-2]]:35 | type-alias/generic-type-param/Swift | T | s:12index_module16GenericTypealiasa1Txmfp | Def,RelChild | rel: 1
26+
// CHECK-NEXT: RelChild | type-alias/Swift | GenericTypealias | [[TYPEALIAS_USR]]
27+
2328
// --- Check the module ---
2429

2530
// CHECK: 0:0 | variable/Swift | someGlobal | [[SOMEGLOBAL_USR]] | Def | rel: 0
@@ -31,5 +36,6 @@ public class CCC {}
3136
// CHECK: 0:0 | function/Swift | someFunc() | [[SOMEFUNC_USR]] | Def | rel: 0
3237

3338
// CHECK: 0:0 | class/Swift | CCC | [[CCC_USR]] | Def | rel: 0
34-
// CHECK: 0:0 | constructor/Swift | init() | [[CCC_init_USR]] | Def,Impl,RelChild | rel: 1
35-
// CHECK-NEXT: RelChild | class/Swift | CCC | [[CCC_USR]]
39+
// CHECK-NOT: constructor/Swift
40+
41+
// CHECK: 0:0 | type-alias/Swift | GenericTypealias | [[TYPEALIAS_USR]] | Def | rel: 0

test/Index/index_system_module.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/my_system_overlay/my_system_overlay.swift -Xcc -I -Xcc %S/Inputs/my_system_overlay
44
// RUN: %target-swift-ide-test -print-indexed-symbols -module-to-print my_system_overlay -source-filename %s -I %t -Xcc -I -Xcc %S/Inputs/my_system_overlay > %t.out
55
// RUN: %FileCheck %s -input-file=%t.out
6+
// RUN: %FileCheck -check-prefix NEGATIVE %s -input-file=%t.out
67

78
// CHECK: function/Swift | some_func() | [[SOMEFUNC_USR:.*]] | Def | rel: 0
89
// CHECK: class/Swift | BaseCls | [[BASECLS_USR:.*]] | Def | rel: 0
@@ -14,3 +15,5 @@
1415
// CHECK: instance-method/Swift | theMeth() | [[SUBCLSMETH_USR:.*]] | Def,Dyn,RelChild,RelOver | rel: 2
1516
// CHECK-NEXT: RelOver | instance-method/Swift | theMeth() | [[BASECLSMETH_USR]]
1617
// CHECK-NEXT: RelChild | class/Swift | SubCls | [[SUBCLS_USR]]
18+
19+
// NEGATIVE-NOT: SECRET

test/SourceKit/Indexing/Inputs/test_module.index.response

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@
2525
{
2626
key.kind: source.lang.swift.decl.class,
2727
key.name: "Empty",
28-
key.usr: "s:11test_module5EmptyC",
29-
key.entities: [
30-
{
31-
key.kind: source.lang.swift.decl.function.constructor,
32-
key.usr: "s:11test_module5EmptyCACycfc",
33-
key.is_implicit: 1
34-
}
35-
]
28+
key.usr: "s:11test_module5EmptyC"
3629
},
3730
{
3831
key.kind: source.lang.swift.decl.class,
@@ -107,11 +100,6 @@
107100
key.is_dynamic: 1
108101
}
109102
]
110-
},
111-
{
112-
key.kind: source.lang.swift.decl.function.constructor,
113-
key.usr: "s:11test_module16ComputedPropertyCACycfc",
114-
key.is_implicit: 1
115103
}
116104
]
117105
},
@@ -140,14 +128,7 @@
140128
{
141129
key.kind: source.lang.swift.decl.class,
142130
key.name: "C2",
143-
key.usr: "s:11test_module2C2C",
144-
key.entities: [
145-
{
146-
key.kind: source.lang.swift.decl.function.constructor,
147-
key.usr: "s:11test_module2C2CACycfc",
148-
key.is_implicit: 1
149-
}
150-
]
131+
key.usr: "s:11test_module2C2C"
151132
},
152133
{
153134
key.kind: source.lang.swift.decl.extension.class,
@@ -175,6 +156,11 @@
175156
key.kind: source.lang.swift.decl.function.free,
176157
key.name: "globalFunc()",
177158
key.usr: "s:11test_module10globalFuncyyF"
159+
},
160+
{
161+
key.kind: source.lang.swift.decl.extension.class,
162+
key.name: "C2",
163+
key.usr: "s:e:s:11test_module2C2C6SECRETyyF"
178164
}
179165
]
180166
}

test/SourceKit/Indexing/Inputs/test_module.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public class Empty {}
22

33
public class TwoInts {
4-
var x, y : Int
4+
public var x, y : Int
55

66
public init(a : Int, b : Int) {
77
x = a
@@ -30,3 +30,9 @@ public class C2 { }
3030
extension C2 : Prot3, Prot1, Prot2 { }
3131

3232
public func globalFunc() {}
33+
34+
private func SECRET() {}
35+
36+
extension C2 {
37+
internal func SECRET() {}
38+
}

0 commit comments

Comments
 (0)