-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathavailability_versions_objc_api.swift
219 lines (162 loc) · 9.02 KB
/
availability_versions_objc_api.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
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
// RUN: %target-typecheck-verify-swift %clang-importer-sdk -I %S/Inputs/custom-modules
// RUN: not %target-swift-frontend -typecheck %clang-importer-sdk -I %S/Inputs/custom-modules %s 2>&1 | %FileCheck %s
// REQUIRES: OS=macosx
// REQUIRES: objc_interop
import Foundation
// Tests for uses of version-based potential unavailability imported from ObjC APIs.
func callUnavailableObjC() {
// expected-note@-1 5{{add @available attribute to enclosing global function}}
_ = NSAvailableOn51() // expected-error {{'NSAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
let o = NSAvailableOn51()!
// Properties
_ = o.propertyOn52 // expected-error {{'propertyOn52' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o.propertyOn52 = 22 // expected-error {{'propertyOn52' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Methods
o.methodAvailableOn52() // expected-error {{'methodAvailableOn52()' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Initializers
_ = NSAvailableOn51(stringOn52:"Hi") // expected-error {{'init(stringOn52:)' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
// Declarations with Objective-C-originated potentially unavailable APIs
func functionWithObjCParam(o: NSAvailableOn51) { // expected-error {{'NSAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
}
class ClassExtendingUnvailableClass : NSAvailableOn51 { // expected-error {{'NSAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing class}}
}
// We allow classes to conform to potentially unavailable protocols
class ClassAdoptingUnavailableProtocol : NSProtocolAvailableOn51 {
}
class SomeSoonToBeConformingClass { }
extension SomeSoonToBeConformingClass : NSProtocolAvailableOn51 {
}
// Enums from Objective-C
let _: NSPotentiallyUnavailableOptions = .first // expected-error {{'NSPotentiallyUnavailableOptions' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSOptionsWithUnavailableElement = .third // expected-error {{'third' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSUnavailableEnum = .first // expected-error {{'NSUnavailableEnum' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSEnumWithUnavailableElement = .third // expected-error {{'third' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Differing availability on getters and setters imported from ObjC.
func gettersAndSettersFromObjC(o: NSAvailableOn10_9) {
// expected-note@-1 6{{add @available attribute to enclosing global function}}
let _: Int = o.propertyOn51WithSetterOn52After // expected-error {{'propertyOn51WithSetterOn52After' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
// Properties with unavailable accessors declared before property in Objective-C header
o.propertyOn51WithSetterOn52Before = 5 // expected-error {{setter for 'propertyOn51WithSetterOn52Before' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: Int = o.propertyOn51WithGetterOn52Before // expected-error {{getter for 'propertyOn51WithGetterOn52Before' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Properties with unavailable accessors declared after property in Objective-C header
o.propertyOn51WithSetterOn52After = 5 // expected-error {{setter for 'propertyOn51WithSetterOn52After' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: Int = o.propertyOn51WithGetterOn52After // expected-error {{getter for 'propertyOn51WithGetterOn52After' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Property with unavailable setter redeclared in Objective-C category
o.readOnlyRedeclaredWithSetterInCategory = 5 // expected-error {{setter for 'readOnlyRedeclaredWithSetterInCategory' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
// Globals from Objective-C
func useGlobalsFromObjectiveC() {
// expected-note@-1 3{{add @available attribute to enclosing global function}}
_ = globalStringAvailableOn51 // expected-error {{'globalStringAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = globalStringAvailableOn52 // expected-error {{'globalStringAvailableOn52' is only available in macOS 52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = globalClassInstanceAvailableOn51 // expected-error {{'globalClassInstanceAvailableOn51' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 51, *) {
_ = globalStringAvailableOn51
let _: NSAvailableOn51 = globalClassInstanceAvailableOn51
}
}
// Optional Protocol Requirements from Objective-C
// Make sure we're not emitting errors in the Foundation module, where the witness is.
// CHECK-NOT: Foundation.ClassWithMethodFromNSProtocolWithOptionalRequirement:
class SubclassOfNSClassWithMethodFromNSProtocolWithOptionalRequirement : NSClassWithMethodFromNSProtocolWithOptionalRequirement {
}
class SubclassWithItsOwnAvailableWitnessOfNSClassWithMethodFromNSProtocolWithOptionalRequirement : NSClassWithMethodFromNSProtocolWithOptionalRequirement {
override func optionalRequirement() { }
}
// Inference of protocol requirement availability when checking conformance to
// unannotated Objective-C protocols
class UserClass : UnannotatedFrameworkProtocol {
@available(OSX 51, *)
@objc(doSomethingWithClass:)
func doSomething(with k: AnnotatedFrameworkClass?) { }
@available(OSX 51, *)
@objc
func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) { }
@available(OSX 51, *)
@objc(doSomethingWithIUOClass:)
func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { }
@objc
@available(OSX 51, *)
func returnSomething() -> AnnotatedFrameworkClass? {
return nil
}
@objc
func noUnavailableTypesInSignature() { }
@objc(doSomethingWithClass:andLaterClass:) @available(OSX 52, *)
func doSomething(with k: AnnotatedFrameworkClass, andLaterClass lk: AnnotatedLaterFrameworkClass) { }
@objc
@available(OSX 53, *)
func someMethodWithAvailability() { }
@available(OSX 51, *)
@objc var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
}
func callViaUnannotatedFrameworkProtocol(p: UnannotatedFrameworkProtocol) {
// expected-note@-1 {{add @available attribute to enclosing global function}}
let _ = p.returnSomething() // expected-error {{'returnSomething()' is only available in macOS 51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
func callViaAnnotatedFrameworkProtocol(p: AnnotatedFrameworkProtocol) {
// We won't synthesize availability for AnnotatedFrameworkProtocol because
// the protocol has an availability annotation on it.
let _ = p.returnSomething()
}
class SubclassOfFrameworkClassConformingToUnannotatedFrameworkProtocol : FrameworkClassConformingToUnannotatedFrameworkProtocol {
@available(OSX 51, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 51, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
@available(OSX 52, *)
override func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { } // expected-error {{'doSomething' must be as available as declaration it overrides}}
}
@available(OSX 52, *)
class SubclassOfLaterFrameworkClassConformingToUnannotatedFrameworkProtocol : LaterFrameworkClassConformingToUnannotatedFrameworkProtocol {
@available(OSX 52, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 53, *)
override func someMethodWithAvailability() { }
}
class SubclassOfFrameworkClassConformingToLaterAnnotatedFrameworkProtocol : FrameworkClassConformingToLaterAnnotatedFrameworkProtocol {
@available(OSX 52, *)
override func returnSomething() -> AnnotatedFrameworkClass? {
}
@available(OSX 53, *)
override func someMethodWithAvailability() { }
@available(OSX 52, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
}