- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathaccessibility_package.swift
115 lines (92 loc) · 3.65 KB
/
accessibility_package.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: %target-swift-frontend -verify -module-name Utils %t/Utils.swift -emit-module -emit-module-path %t/Utils.swiftmodule -package-name myLib
// RUN: test -f %t/Utils.swiftmodule
// RUN: %target-swift-frontend -verify -module-name LibGood %t/LibGood.swift -emit-module -emit-module-path %t/LibGood.swiftmodule -package-name myLib -I %t
// RUN: test -f %t/LibGood.swiftmodule
// RUN: %target-swift-frontend -verify -module-name Client %t/Client.swift -emit-module -emit-module-path %t/Client.swiftmodule -package-name client -I %t
// RUN: %target-swift-frontend -typecheck -verify %t/LibSamePackage.swift -package-name myLib -I %t
// RUN: %target-swift-frontend -typecheck -verify %t/LibOtherPackage.swift -package-name "otherLib" -I %t
//--- Utils.swift
packageprotocolPackageProto{
varpkgVar:Double{getset}
func pkgFunc()
}
packageclassPackageKlass{
packageinit(){}
packageprivate(set)varpkgVar:Double=1.0
packagefunc pkgFunc(){}
}
classInternalKlass{}
publicclassPublicKlass{
publicinit(){}
publicvarpublicVar:Int=1
publicpackage(set)varpublicGetPkg:Int=2
publicinternal(set)varpublicGetInternal:Int=3
publicfunc publicFunc(){}
packagefunc pkgFunc(){}
}
openclassOpenKlass{
publicinit(){}
openvaropenVar:String=""
openfunc openFunc(){}
publicfunc publicFunc(){}
packagefunc packageFunc(){}
}
//--- LibSamePackage.swift
import Utils
// Test accessing public and package decls
publicfunc test(){
letx=PublicKlass()
x.publicFunc()
x.pkgFunc() // OK
x.publicGetPkg =3 // OK
x.publicGetInternal =4 // expected-error {{cannot assign to property: 'publicGetInternal' setter is inaccessible}}
lety=PackageKlass() // OK
y.pkgVar =1.5 // expected-error {{cannot assign to property: 'pkgVar' setter is inaccessible}}
y.pkgFunc() // OK
}
// Test conformance to a package protocol
packagestructLibStruct:PackageProto{ // OK
packagevarpkgVar:Double=1.0
packagefunc pkgFunc(){}
}
// Test subclassing / overrides
classSubOpenKlass:OpenKlass{
overrideopenfunc openFunc(){}
overridepublicfunc publicFunc(){} // expected-error {{overriding non-open instance method outside of its defining module}}
overridepackagefunc packageFunc(){} // expected-error {{overriding non-open instance method outside of its defining module}}
}
classSubPublicKlass:PublicKlass{} // expected-error {{cannot inherit from non-open class 'PublicKlass' outside of its defining module}}
classSubPackageKlass:PackageKlass{} // expected-error {{cannot inherit from non-open class 'PackageKlass' outside of its defining module}}
//--- LibOtherPackage.swift
import Utils
// Test accessing package decls
publicfunc test(){
letx=PublicKlass()
x.publicFunc() // OK
x.pkgFunc() // expected-error {{'pkgFunc' is inaccessible due to 'package' protection level}}
lety=PackageKlass() // expected-error {{cannot find 'PackageKlass' in scope}}
}
packagestructLibStruct:PackageProto{} // expected-error {{cannot find type 'PackageProto' in scope}}
//--- LibGood.swift
import Utils
publicfunc libFunc(){
_ =LibStruct()
}
publicstructLibStruct:PackageProto{
publicinit(){}
packagevarpkgVar:Double=1.0
packagefunc pkgFunc(){}
publicvarpublicVar:String=""
publicfunc publicFunc(){}
}
//--- Client.swift
import LibGood
func clientFunc(){
letlib=LibStruct()
_ = lib.pkgVar // expected-error {{'pkgVar' is inaccessible due to 'package' protection level}}
_ = lib.publicVar
lib.pkgFunc() // expected-error {{'pkgFunc' is inaccessible due to 'package' protection level}}
lib.publicFunc()
}