File: accessibility.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (343 lines) | stat: -rw-r--r-- 17,197 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// RUN: %target-typecheck-verify-swift -package-name myPkg

// CHECK PARSING
private // expected-note {{modifier already specified here}}
private // expected-error {{duplicate modifier}}
func duplicateAttr() {}

private // expected-note {{previous modifier specified here}}
public // expected-error {{multiple incompatible access-level modifiers specified}}
func duplicateAttrChanged() {}

private // expected-note 2 {{previous modifier specified here}}
public // expected-error {{multiple incompatible access-level modifiers specified}}
internal // expected-error {{multiple incompatible access-level modifiers specified}}
func triplicateAttrChanged() {}

private // expected-note 3 {{previous modifier specified here}}
public // expected-error {{multiple incompatible access-level modifiers specified}}
package // expected-error {{multiple incompatible access-level modifiers specified}}
internal // expected-error {{multiple incompatible access-level modifiers specified}}
func quadruplicateAttrChanged() {}

private // expected-note 4 {{previous modifier specified here}}
public // expected-error {{multiple incompatible access-level modifiers specified}}
package // expected-error {{multiple incompatible access-level modifiers specified}}
internal // expected-error {{multiple incompatible access-level modifiers specified}}
fileprivate // expected-error {{multiple incompatible access-level modifiers specified}}
func quintuplicateAttrChanged() {}

private(set)
public
var customSetter = 0

fileprivate(set)
public
var customSetter2 = 0

package(set)
public
var customSetter3 = 0

public
package(set)
var customSetter4 = 0

private(set)
package
var customSetter5 = 0

internal(set)
package
var customSetter6 = 0

private(set) // expected-note {{previous modifier specified here}}
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
var customSetterDuplicateAttr = 0

private(set) // expected-note {{previous modifier specified here}}
public // expected-note {{previous modifier specified here}}
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
private // expected-error {{multiple incompatible access-level modifiers specified}}
var customSetterDuplicateAttrsAllAround = 0

private(set) // expected-note {{previous modifier specified here}}
package(set) // expected-error {{multiple incompatible access-level modifiers specified}}
var customSetterDuplicateAttr2 = 0

package(set) // expected-note {{previous modifier specified here}}
public(set) // expected-error {{multiple incompatible access-level modifiers specified}}
public var customSetterDuplicateAttr3 = 0

private(get) // expected-error{{expected 'set' as subject of 'private' modifier}}
var invalidSubject = 0

private(42) // expected-error{{expected 'set' as subject of 'private' modifier}}
var invalidSubject2 = 0

private(a bunch of random tokens) // expected-error{{expected 'set' as subject of 'private' modifier}} expected-error{{expected declaration}}
var invalidSubject3 = 0


package(get) // expected-error{{expected 'set' as subject of 'package' modifier}}
var invalidSubject4 = 0

package(42) // expected-error{{expected 'set' as subject of 'package' modifier}}
var invalidSubject5 = 0

private(set // expected-error{{expected ')' in 'private' modifier}}
var unterminatedSubject = 0

private(42 // expected-error{{expected 'set' as subject of 'private' modifier}} expected-error{{expected declaration}}
var unterminatedInvalidSubject = 0

private() // expected-error{{expected 'set' as subject of 'private' modifier}}
var emptySubject = 0

private( // expected-error{{expected 'set' as subject of 'private' modifier}}
var unterminatedEmptySubject = 0

// Check that the parser made it here.
duplicateAttr(1) // expected-error{{argument passed to call that takes no arguments}}

// CHECK ALLOWED DECLS
private import Swift
private(set) infix operator ~~~ // expected-error {{unexpected attribute private in operator declaration}} {{1-14=}}

private typealias MyInt = Int

private struct TestStruct {
  private typealias LocalInt = MyInt
  private var x = 0
  private let y = 1
  private func method() {}
  private static func method() {}
  private init() {}
  private subscript(_: MyInt) -> LocalInt { return x }
}

private class TestClass {
  private init() {}
  internal deinit {} // expected-error {{'internal' modifier cannot be applied to this declaration}} {{3-12=}}
}

private enum TestEnum {
  private case Foo, Bar // expected-error {{'private' modifier cannot be applied to this declaration}} {{3-11=}}
}

package enum PkgTestEnum {
  package case Foo, Bar // expected-error {{'package' modifier cannot be applied to this declaration}} {{3-11=}}
}

private protocol TestProtocol {
  private associatedtype Foo // expected-error {{'private' modifier cannot be applied to this declaration}} {{3-11=}}
  internal var Bar: Int { get } // expected-error {{'internal' modifier cannot be used in protocols}} {{3-12=}}
  // expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
  package func cat() // expected-error {{'package' modifier cannot be used in protocols}} {{3-11=}}
  // expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
  public func baz() // expected-error {{'public' modifier cannot be used in protocols}} {{3-10=}}
  // expected-note@-1 {{protocol requirements implicitly have the same access as the protocol itself}}
}

public(set) func publicSetFunc() {} // expected-error {{'public' modifier cannot be applied to this declaration}} {{1-13=}}

public(set) var defaultVis = 0 // expected-error {{internal variable cannot have a public setter}}
package(set) var defaultVisPkg = 0 // expected-error {{internal variable cannot have a package setter}}
package(set) package var defaultVisPkgPkg = 0 // expected-warning {{'package(set)' modifier is redundant for a package var}}
package(set) public var defaultVisPkgOK = 0 // OK
internal(set) private var privateVis = 0 // expected-error {{private variable cannot have an internal setter}}
private(set) var defaultVisOK = 0
private(set) public var publicVis = 0

private(set) var computed: Int { // expected-error {{'private(set)' modifier cannot be applied to read-only variables}} {{1-14=}}
  return 42
}
private(set) var computedRW: Int {
  get { return 42 }
  set { }
}

package(set) public var computedPkg: Int { // expected-error {{'package(set)' modifier cannot be applied to read-only variables}} {{1-14=}}
  return 42
}

private(set) let constant = 42 // expected-error {{'private(set)' modifier cannot be applied to constants}} {{1-14=}}

package(set) public let constantPkg = 42 // expected-error {{'package(set)' modifier cannot be applied to constants}} {{1-14=}}

public struct Properties {
  private(set) var stored = 42
  private(set) var computed: Int { // expected-error {{'private(set)' modifier cannot be applied to read-only properties}} {{3-16=}}
    return 42
  }
  private(set) var computedRW: Int {
    get { return 42 }
    set { }
  }

  package(set) public var computedRWPkg: Int {
    get { return 42 }
    set { }
  }

  public package(set) var computedR: Int { // expected-error {{'package(set)' modifier cannot be applied to read-only properties}} {{10-23=}}
    return 42
  }

  private(set) let constant = 42 // expected-error {{'private(set)' modifier cannot be applied to read-only properties}} {{3-16=}}
  package(set) public let constantPkg = 42 // expected-error {{'package(set)' modifier cannot be applied to read-only properties}} {{3-16=}}
  package(set) var defaultVisPkg = 42 // expected-error {{internal property cannot have a package setter}}
  public(set) var defaultVis = 0 // expected-error {{internal property cannot have a public setter}}
  open(set) var defaultVis2 = 0 // expected-error {{internal property cannot have an open setter}}

  public(set) subscript(a a: Int) -> Int { // expected-error {{internal subscript cannot have a public setter}}
    get { return 0 }
    set {}
  }
  package(set) subscript(p p: Int) -> Int { // expected-error {{internal subscript cannot have a package setter}}
    get { return 0 }
    set {}
  }
  internal(set) private subscript(b b: Int) -> Int { // expected-error {{private subscript cannot have an internal setter}}
    get { return 0 }
    set {}
  }
  private(set) subscript(c c: Int) -> Int {
    get { return 0 }
    set {}
  }
  private(set) public subscript(d d: Int) -> Int {
    get { return 0 }
    set {}
  }

  private(set) subscript(e e: Int) -> Int { return 0 } // expected-error {{'private(set)' modifier cannot be applied to read-only subscripts}} {{3-16=}}
}

private extension Properties {
  package(set) var extPropPkg: Int { // expected-error {{private property cannot have a package setter}}
    get { return 42 }
    set { }
  }
  public(set) var extProp: Int { // expected-error {{private property cannot have a public setter}}
    get { return 42 }
    set { }
  }
  open(set) var extProp2: Int { // expected-error {{private property cannot have an open setter}}
    get { return 42 }
    set { }
  }
}

internal protocol EmptyProto {}
internal protocol EmptyProto2 {}
private extension Properties : EmptyProto {} // expected-error {{'private' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
private(set) extension Properties : EmptyProto2 {} // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}

package protocol EmptyProto3 {}
package protocol EmptyProto4 {}
public protocol EmptyProto5 {}
public protocol EmptyProto6 {}

private extension Properties : EmptyProto3 {} // expected-error {{'private' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
private(set) extension Properties : EmptyProto4 {} // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}

package extension Properties : EmptyProto5 {} // expected-error {{'package' modifier cannot be used with extensions that declare protocol conformances}} {{1-9=}}
package(set) extension Properties : EmptyProto6 {} // expected-error {{'package' modifier cannot be applied to this declaration}} {{1-14=}}

public struct PublicStruct {}
package struct PackageStruct {} // expected-note * {{declared here}}
internal struct InternalStruct {} // expected-note * {{declared here}}
private struct PrivateStruct {} // expected-note * {{declared here}}

protocol InternalProto { // expected-note * {{declared here}}
  associatedtype Assoc
}
package protocol PackageProto { // expected-note * {{declared here}}
  associatedtype Assoc
}
public extension InternalProto {} // expected-error {{extension of internal protocol cannot be declared public}} {{1-8=}}
public extension PackageProto {} // expected-error {{extension of package protocol cannot be declared public}} {{1-8=}}
package extension PackageProto {} // no-error
package extension InternalProto {} // expected-error {{extension of internal protocol cannot be declared package}} {{1-9=}}
package extension PackageProto where Assoc == PublicStruct {}
package extension PackageProto where Assoc == PackageStruct {}
package extension PackageProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
package extension PackageProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses a private type}}
internal extension PackageProto where Assoc == PublicStruct {}
internal extension PackageProto where Assoc == PackageStruct {}
internal extension PackageProto where Assoc == InternalStruct {}
internal extension PackageProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension PackageProto where Assoc == PublicStruct {}
private extension PackageProto where Assoc == PackageStruct {}
private extension PackageProto where Assoc == InternalStruct {}
private extension PackageProto where Assoc == PrivateStruct {}

internal extension InternalProto where Assoc == PublicStruct {}
internal extension InternalProto where Assoc == PackageStruct {}
internal extension InternalProto where Assoc == InternalStruct {}
internal extension InternalProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension InternalProto where Assoc == PublicStruct {}
private extension InternalProto where Assoc == PackageStruct {}
private extension InternalProto where Assoc == InternalStruct {}
private extension InternalProto where Assoc == PrivateStruct {}

public protocol PublicProto {
  associatedtype Assoc
}
public extension PublicProto {}
public extension PublicProto where Assoc == PublicStruct {}
public extension PublicProto where Assoc == PackageStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses a package type}}
public extension PublicProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses an internal type}}
public extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared public because its generic requirement uses a private type}}

package extension PublicProto where Assoc == PublicStruct {}
package extension PublicProto where Assoc == PackageStruct {}
package extension PublicProto where Assoc == InternalStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
package extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared package because its generic requirement uses a private type}}

internal extension PublicProto where Assoc == PublicStruct {}
internal extension PublicProto where Assoc == PackageStruct {}
internal extension PublicProto where Assoc == InternalStruct {}
internal extension PublicProto where Assoc == PrivateStruct {} // expected-error {{extension cannot be declared internal because its generic requirement uses a private type}}
private extension PublicProto where Assoc == PublicStruct {}
private extension PublicProto where Assoc == PackageStruct {}
private extension PublicProto where Assoc == InternalStruct {}
private extension PublicProto where Assoc == PrivateStruct {}

extension PublicProto where Assoc == PackageStruct {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with package requirements}} {{3-9=package}}
  open func bar() {} // expected-error {{cannot declare an open instance method in an extension with package requirements}} {{3-7=package}}
}

extension PublicProto where Assoc == InternalStruct {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
  open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
}
extension InternalProto {
  public func foo() {} // no effect, but no warning
}
extension InternalProto where Assoc == PublicStruct {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
  open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
}
extension InternalProto where Assoc == PackageStruct {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
  open func bar() {} // expected-error {{cannot declare an open instance method in an extension with internal requirements}} {{3-7=internal}}
}

public struct GenericStruct<Param> {}
public extension GenericStruct where Param: InternalProto {} // expected-error {{extension cannot be declared public because its generic requirement uses an internal type}}
extension GenericStruct where Param: InternalProto {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with internal requirements}} {{3-9=internal}}
}
public extension GenericStruct where Param: PackageProto {} // expected-error {{extension cannot be declared public because its generic requirement uses a package type}}
extension GenericStruct where Param: PackageProto {
  public func foo() {} // expected-error {{cannot declare a public instance method in an extension with package requirements}} {{3-9=package}}
}

package struct PkgGenericStruct<Param> {}
package extension PkgGenericStruct where Param: InternalProto {} // expected-error {{extension cannot be declared package because its generic requirement uses an internal type}}
extension PkgGenericStruct where Param: InternalProto {
  package func foo() {} // expected-error {{cannot declare a package instance method in an extension with internal requirements}} {{3-10=internal}}
}