File: attr_usableFromInline.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 (354 lines) | stat: -rw-r--r-- 16,069 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
344
345
346
347
348
349
350
351
352
353
354
// RUN: %target-typecheck-verify-swift -swift-version 5 -disable-objc-attr-requires-foundation-module -enable-objc-interop -package-name myPkg
// RUN: %target-typecheck-verify-swift -swift-version 5 -disable-objc-attr-requires-foundation-module -enable-objc-interop -enable-testing -package-name myPkg

@usableFromInline private func privateVersioned() {}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'privateVersioned()' is private}}

@usableFromInline fileprivate func fileprivateVersioned() {}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'fileprivateVersioned()' is fileprivate}}

@usableFromInline internal func internalVersioned() {}
// OK

@usableFromInline package func packageVersioned() {}
// OK

@usableFromInline func implicitInternalVersioned() {}
// OK

@usableFromInline public func publicVersioned() {}
// expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but global function 'publicVersioned()' is public}}

// expected-note@+1 3{{global function 'internalFunc()' is not '@usableFromInline' or public}}
internal func internalFunc() {}
// expected-note@+1 3{{global function 'packageFunc()' is not '@usableFromInline' or public}}
package func packageFunc() {}
public func publicFunc() {}

@inlinable
public func publicInlinableFunc() {
  internalVersioned() // OK
  internalFunc() // expected-error {{global function 'internalFunc()' is internal and cannot be referenced from an '@inlinable' function}}
  packageVersioned() // OK
  packageFunc() // expected-error {{global function 'packageFunc()' is package and cannot be referenced from an '@inlinable' function}}
  publicFunc() // OK
}

@inlinable
func internalInlinableFunc() {
  internalVersioned() // OK
  internalFunc() // expected-error {{global function 'internalFunc()' is internal and cannot be referenced from an '@inlinable' function}}
  packageVersioned() // OK
  packageFunc() // expected-error {{global function 'packageFunc()' is package and cannot be referenced from an '@inlinable' function}}
  publicFunc() // OK
}

@inlinable
package func packageInlinableFunc() {
  internalVersioned() // OK
  internalFunc() // expected-error {{global function 'internalFunc()' is internal and cannot be referenced from an '@inlinable' function}}
  packageVersioned() // OK
  packageFunc() // expected-error {{global function 'packageFunc()' is package and cannot be referenced from an '@inlinable' function}}
  publicFunc() // OK
}

package class PackageClass {
  // expected-note@-1 *{{type declared here}}
  @usableFromInline public func publicVersioned() {}
  // expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
}

internal class InternalClass {
  // expected-note@-1 2{{type declared here}}
  @usableFromInline public func publicVersioned() {}
  // expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but instance method 'publicVersioned()' is public}}
}

fileprivate class filePrivateClass {
  @usableFromInline internal func internalVersioned() {}
  @usableFromInline package func packageVersioned() {}
}

@usableFromInline struct S {
  var x: Int
  @usableFromInline var y: Int
}

@usableFromInline extension S {}
// expected-error@-1 {{'@usableFromInline' attribute cannot be applied to this declaration}}

@usableFromInline package struct Pkg {
  var x: Int
  @usableFromInline var y: Int
  @usableFromInline package var z: Int
}

@usableFromInline extension Pkg {}
// expected-error@-1 {{'@usableFromInline' attribute cannot be applied to this declaration}}

@usableFromInline
protocol VersionedProtocol {
  associatedtype T

  func requirement() -> T

  public func publicRequirement() -> T
  // expected-error@-1 {{'public' modifier cannot be used in protocols}}
  // expected-note@-2 {{protocol requirements implicitly have the same access as the protocol itself}}

  @usableFromInline func versionedRequirement() -> T
  // expected-error@-1 {{'@usableFromInline' attribute cannot be used in protocols}}
}

@usableFromInline
package protocol PkgVersionedProtocol {
  associatedtype T

  func requirement() -> T

  public func publicRequirement() -> T
  // expected-error@-1 {{'public' modifier cannot be used in protocols}}
  // expected-note@-2 {{protocol requirements implicitly have the same access as the protocol itself}}

  @usableFromInline func versionedRequirement() -> T
  // expected-error@-1 {{'@usableFromInline' attribute cannot be used in protocols}}
}

// Derived conformances had issues with @usableFromInline - rdar://problem/34342955
@usableFromInline
internal enum EqEnum {
  case foo
}
@usableFromInline
package enum PkgEqEnum {
  case foo
}

@usableFromInline
internal enum RawEnum : Int {
  case foo = 0
}
@usableFromInline
package enum PkgRawEnum : Int {
  case foo = 0
}

@inlinable
public func usesEqEnum() -> Bool {
  _ = (EqEnum.foo == .foo)
  _ = EqEnum.foo.hashValue

  _ = RawEnum.foo.rawValue
  _ = RawEnum(rawValue: 0)

  _ = (PkgEqEnum.foo == .foo)
  _ = PkgEqEnum.foo.hashValue

  _ = PkgRawEnum.foo.rawValue
  _ = PkgRawEnum(rawValue: 0)
}

public class DynamicMembers {
  @usableFromInline @objc dynamic init() {}
  @usableFromInline @objc dynamic func foo() {}
  @usableFromInline @objc dynamic var bar: Int = 0
  @usableFromInline @objc dynamic package init(arg: Int) {}
  @usableFromInline @objc dynamic package func baz() {}
  @usableFromInline @objc dynamic package var cat: Int = 0
}

internal struct InternalStruct {}
// expected-note@-1 9{{type declared here}}

@usableFromInline var globalInferred = InternalStruct()
// expected-error@-1 {{type referenced from a '@usableFromInline' variable with inferred type 'InternalStruct' must be '@usableFromInline' or public}}

@usableFromInline var globalDeclared: InternalStruct = InternalStruct()
// expected-error@-1 {{type referenced from a '@usableFromInline' variable must be '@usableFromInline' or public}}

@usableFromInline typealias BadAlias = InternalStruct
// expected-error@-1 {{type referenced from the underlying type of a '@usableFromInline' type alias must be '@usableFromInline' or public}}

package struct PackageStruct {}
// expected-note@-1 *{{type declared here}}

@usableFromInline var globalInferredPkg = PackageStruct()
// expected-error@-1 {{type referenced from a '@usableFromInline' variable with inferred type 'PackageStruct' must be '@usableFromInline' or public}}

@usableFromInline var globalDeclaredPkg: PackageStruct = PackageStruct()
// expected-error@-1 {{type referenced from a '@usableFromInline' variable must be '@usableFromInline' or public}}

@usableFromInline typealias BadAliasPkg = PackageStruct
// expected-error@-1 {{type referenced from the underlying type of a '@usableFromInline' type alias must be '@usableFromInline' or public}}


protocol InternalProtocol {
  // expected-note@-1 * {{type declared here}}
  associatedtype T
}

@usableFromInline
struct BadStruct<T, U>
// expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' generic struct must be '@usableFromInline' or public}}
where T : InternalProtocol,
      T : Sequence,
      T.Element == InternalStruct {
  @usableFromInline init(x: InternalStruct) {}
  // expected-error@-1 {{the parameter of a '@usableFromInline' initializer must be '@usableFromInline' or public}}

  @usableFromInline func foo(x: InternalStruct) -> InternalClass {}
  // expected-error@-1 {{the parameter of a '@usableFromInline' method must be '@usableFromInline' or public}}
  // expected-error@-2 {{the result of a '@usableFromInline' method must be '@usableFromInline' or public}}

  @usableFromInline var propertyInferred = InternalStruct()
  // expected-error@-1 {{type referenced from a '@usableFromInline' property with inferred type 'InternalStruct' must be '@usableFromInline' or public}}

  @usableFromInline var propertyDeclared: InternalStruct = InternalStruct()
  // expected-error@-1 {{type referenced from a '@usableFromInline' property must be '@usableFromInline' or public}}

  @usableFromInline subscript(x: InternalStruct) -> Int {
    // expected-error@-1 {{index type of a '@usableFromInline' subscript must be '@usableFromInline' or public}}
    get {}
    set {}
  }

  @usableFromInline subscript(x: Int) -> InternalStruct {
    // expected-error@-1 {{element type of a '@usableFromInline' subscript must be '@usableFromInline' or public}}
    get {}
    set {}
  }
}

@usableFromInline
protocol BadProtocol : InternalProtocol {
  // expected-error@-1 {{protocol refined by '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype X : InternalProtocol
  // expected-error@-1 {{type referenced from a requirement of an associated type in a '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype Y = InternalStruct
  // expected-error@-1 {{type referenced from a default definition of an associated type in a '@usableFromInline' protocol must be '@usableFromInline' or public}}
}

@usableFromInline
protocol AnotherBadProtocol where Self.T : InternalProtocol {
  // expected-error@-1 {{protocol used by '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype T
}


package protocol PackageProtocol {
  // expected-note@-1 * {{type declared here}}
  associatedtype T
}

@usableFromInline
package struct PkgBadStruct<T, U>
// expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' generic struct must be '@usableFromInline' or public}}
where T : PackageProtocol,
      T : Sequence,
      T.Element == PackageStruct {
  @usableFromInline init(x: PackageStruct) {}
  // expected-error@-1 {{the parameter of a '@usableFromInline' initializer must be '@usableFromInline' or public}}

  @usableFromInline func foo(x: PackageStruct) -> PackageClass {}
  // expected-error@-1 {{the parameter of a '@usableFromInline' method must be '@usableFromInline' or public}}
  // expected-error@-2 {{the result of a '@usableFromInline' method must be '@usableFromInline' or public}}

  @usableFromInline var propertyInferred = PackageStruct()
  // expected-error@-1 {{type referenced from a '@usableFromInline' property with inferred type 'PackageStruct' must be '@usableFromInline' or public}}

  @usableFromInline var propertyDeclared: PackageStruct = PackageStruct()
  // expected-error@-1 {{type referenced from a '@usableFromInline' property must be '@usableFromInline' or public}}

  @usableFromInline subscript(x: PackageStruct) -> Int {
    // expected-error@-1 {{index type of a '@usableFromInline' subscript must be '@usableFromInline' or public}}
    get {}
    set {}
  }

  @usableFromInline subscript(x: Int) -> PackageStruct {
    // expected-error@-1 {{element type of a '@usableFromInline' subscript must be '@usableFromInline' or public}}
    get {}
    set {}
  }
}

@usableFromInline
package protocol PkgBadProtocol : PackageProtocol {
  // expected-error@-1 {{protocol refined by '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype X : PackageProtocol
  // expected-error@-1 {{type referenced from a requirement of an associated type in a '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype Y = PackageProtocol
  // expected-error@-1 {{type referenced from a default definition of an associated type in a '@usableFromInline' protocol must be '@usableFromInline' or public}}
}

@usableFromInline
package protocol PkgAnotherBadProtocol where Self.T : PackageProtocol {
  // expected-error@-1 {{protocol used by '@usableFromInline' protocol must be '@usableFromInline' or public}}
  associatedtype T
}


@usableFromInline
enum BadEnum {
  case bad(InternalStruct)
  // expected-error@-1 {{type of enum case in '@usableFromInline' enum must be '@usableFromInline' or public}}
}

@usableFromInline
package enum PkgBadEnum {
  case bad(PackageStruct)
  // expected-error@-1 {{type of enum case in '@usableFromInline' enum must be '@usableFromInline' or public}}
}

@usableFromInline
class BadClass : InternalClass {}
// expected-error@-1 {{type referenced from the superclass of a '@usableFromInline' class must be '@usableFromInline' or public}}

@usableFromInline
package class PkgBadClass : PackageClass {}
// expected-error@-1 {{type referenced from the superclass of a '@usableFromInline' class must be '@usableFromInline' or public}}

public struct TestGenericSubscripts {
  @usableFromInline subscript<T: InternalProtocol>(_: T) -> Int { return 0 } // expected-warning {{type referenced from a generic parameter of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
  @usableFromInline subscript<T>(where _: T) -> Int where T: InternalProtocol { return 0 } // expected-warning {{type referenced from a generic requirement of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
  @usableFromInline package subscript<T: PackageProtocol>(_: T) -> Int { return 0 } // expected-warning {{type referenced from a generic parameter of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
  @usableFromInline package subscript<T>(where _: T) -> Int where T: PackageProtocol { return 0 } // expected-warning {{type referenced from a generic requirement of a '@usableFromInline' subscript should be '@usableFromInline' or public}}
}

@usableFromInline typealias TestGenericAlias<T: InternalProtocol> = T // expected-warning {{type referenced from a generic parameter of a '@usableFromInline' type alias should be '@usableFromInline' or public}}
@usableFromInline typealias TestGenericAliasWhereClause<T> = T where T: InternalProtocol // expected-warning {{type referenced from a generic requirement of a '@usableFromInline' type alias should be '@usableFromInline' or public}}

@usableFromInline typealias PkgTestGenericAlias<T: PackageProtocol> = T // expected-warning {{type referenced from a generic parameter of a '@usableFromInline' type alias should be '@usableFromInline' or public}}
@usableFromInline typealias PkgTestGenericAliasWhereClause<T> = T where T: PackageProtocol // expected-warning {{type referenced from a generic requirement of a '@usableFromInline' type alias should be '@usableFromInline' or public}}

@usableFromInline struct GenericStruct<T> {
  @usableFromInline struct Nested where T : InternalProtocol {}
  // expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' struct must be '@usableFromInline' or public}}

  @usableFromInline func nonGenericWhereClause() where T : InternalProtocol {}
  // expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' instance method must be '@usableFromInline' or public}}

  @usableFromInline package struct PkgNested where T : PackageProtocol {}
  // expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' struct must be '@usableFromInline' or public}}

  @usableFromInline package func pkgNonGenericWhereClause() where T : PackageProtocol {}
  // expected-error@-1 {{type referenced from a generic requirement of a '@usableFromInline' instance method must be '@usableFromInline' or public}}
}

public struct IncorrectInitUse {
  public var x: Int {
    @usableFromInline
    // expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but getter for property 'x' is public}}
    get { 0 }

    @usableFromInline
    // expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but setter for property 'x' is public}}
    set { }
  }

  @usableFromInline
  // expected-error@-1 {{'@usableFromInline' attribute can only be applied to internal or package declarations, but initializer 'init(x:)' is public}}
  public init(x: Int) {
    self.x = x
  }
}