forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccessibility.swift
150 lines (119 loc) · 6.52 KB
/
accessibility.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// RUN: rm -rf %t && mkdir -p %t
// RUN: cp %s %t/main.swift
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -enable-source-import -I %S/Inputs -sdk "" -enable-access-control -verify
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -enable-source-import -I %S/Inputs -sdk "" -disable-access-control -D DEFINE_VAR_FOR_SCOPED_IMPORT -D ACCESS_DISABLED
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/has_accessibility.swift -D DEFINE_VAR_FOR_SCOPED_IMPORT -enable-testing
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -I %t -sdk "" -enable-access-control -verify
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -I %t -sdk "" -disable-access-control -D ACCESS_DISABLED
// RUN: not %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -I %t -sdk "" -D TESTABLE 2>&1 | FileCheck -check-prefix=TESTABLE %s
#if TESTABLE
@testable import has_accessibility
#else
import has_accessibility
#endif
// This deliberately has the wrong import kind.
import var has_accessibility.zz // expected-error {{no such decl in module}}
func markUsed<T>(t: T) {}
markUsed(has_accessibility.x)
markUsed(has_accessibility.y) // expected-error {{module 'has_accessibility' has no member named 'y'}}
markUsed(has_accessibility.z) // expected-error {{module 'has_accessibility' has no member named 'z'}}
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE: :[[@LINE-3]]:10: error: module 'has_accessibility' has no member named 'z'
markUsed(accessibility.a)
markUsed(accessibility.b)
markUsed(accessibility.c) // expected-error {{module 'accessibility' has no member named 'c'}}
markUsed(x)
markUsed(y) // expected-error {{use of unresolved identifier 'y'}}
markUsed(z) // expected-error {{use of unresolved identifier 'z'}}
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE: :[[@LINE-3]]:10: error: use of unresolved identifier 'z'
markUsed(a)
markUsed(b)
markUsed(c) // expected-error {{use of unresolved identifier 'c'}}
Foo.x()
Foo.y() // expected-error {{type 'Foo' has no member 'y'}}
Foo.z() // expected-error {{type 'Foo' has no member 'z'}}
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE: :[[@LINE-3]]:{{[^:]+}}: error: type 'Foo' has no member 'z'
Foo.a()
Foo.b()
Foo.c() // expected-error {{type 'Foo' has no member 'c'}}
_ = Foo() // expected-error {{'Foo' cannot be constructed because it has no accessible initializers}}
// TESTABLE-NOT: :[[@LINE-1]]:{{[^:]+}}:
PrivateInit() // expected-error {{'PrivateInit' cannot be constructed because it has no accessible initializers}}
// TESTABLE: :[[@LINE-1]]:{{[^:]+}}: error: 'PrivateInit' cannot be constructed because it has no accessible initializers
var s = StructWithPrivateSetter()
s.x = 42 // expected-error {{cannot assign to property: 'x' setter is inaccessible}}
class Sub : Base {
func test() {
value = 4 // expected-error {{cannot assign to property: 'value' setter is inaccessible}}
self.value = 4 // expected-error {{cannot assign to property: 'value' setter is inaccessible}}
super.value = 4 // expected-error {{cannot assign to property: 'value' setter is inaccessible}}
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
method() // expected-error {{use of unresolved identifier 'method'}}
self.method() // expected-error {{value of type 'Sub' has no member 'method'}}
super.method() // expected-error {{value of type 'Base' has no member 'method'}}
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
// TESTABLE-NOT: :[[@LINE-3]]:{{[^:]+}}:
}
}
class ObservingOverrider : Base {
override var value: Int { // expected-error {{cannot observe read-only property 'value'; it can't change}}
willSet { markUsed(newValue) }
}
}
class ReplacingOverrider : Base {
override var value: Int {
get { return super.value }
set { super.value = newValue } // expected-error {{cannot assign to property: 'value' setter is inaccessible}}
}
}
protocol MethodProto {
func method() // expected-note * {{protocol requires function 'method()' with type '() -> ()'}}
}
extension OriginallyEmpty : MethodProto {}
extension HiddenMethod : MethodProto {} // expected-error {{type 'HiddenMethod' does not conform to protocol 'MethodProto'}}
// TESTABLE-NOT: :[[@LINE-1]]:{{[^:]+}}:
#if !ACCESS_DISABLED
extension Foo : MethodProto {} // expected-error {{type 'Foo' does not conform to protocol 'MethodProto'}}
#endif
protocol TypeProto {
typealias TheType // expected-note * {{protocol requires nested type 'TheType'}}
}
extension OriginallyEmpty {}
extension HiddenType : TypeProto {} // expected-error {{type 'HiddenType' does not conform to protocol 'TypeProto'}}
// TESTABLE-NOT: :[[@LINE-1]]:{{[^:]+}}:
#if !ACCESS_DISABLED
extension Foo : TypeProto {} // expected-error {{type 'Foo' does not conform to protocol 'TypeProto'}}
#endif
#if !ACCESS_DISABLED
private func privateInBothFiles() {} // no-warning
private func privateInPrimaryFile() {} // expected-error {{invalid redeclaration}}
func privateInOtherFile() {} // expected-note {{previously declared here}}
#endif
#if !ACCESS_DISABLED
// rdar://problem/21408035
private class PrivateBox<T> { // expected-note 2 {{type declared here}}
typealias ValueType = T
typealias AlwaysFloat = Float
}
let boxUnboxInt: PrivateBox<Int>.ValueType = 0 // expected-error {{constant must be declared private because its type uses a private type}}
let boxFloat: PrivateBox<Int>.AlwaysFloat = 0 // expected-error {{constant must be declared private because its type uses a private type}}
#endif
#if !ACCESS_DISABLED
struct ConformerByTypeAlias : TypeProto {
private typealias TheType = Int // expected-error {{type alias 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{3-10=internal}}
}
struct ConformerByLocalType : TypeProto {
private struct TheType {} // expected-error {{struct 'TheType' must be declared internal because it matches a requirement in internal protocol 'TypeProto'}} {{3-10=internal}}
}
private struct PrivateConformerByLocalType : TypeProto {
private struct TheType {} // okay
}
#endif