forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailability_versions_objc_api.swift
219 lines (162 loc) · 9.28 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}}
_ = NSAvailableOn10_51() // expected-error {{'NSAvailableOn10_51' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
let o = NSAvailableOn10_51()!
// Properties
_ = o.propertyOn10_52 // expected-error {{'propertyOn10_52' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
o.propertyOn10_52 = 22 // expected-error {{'propertyOn10_52' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Methods
o.methodAvailableOn10_52() // expected-error {{'methodAvailableOn10_52()' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Initializers
_ = NSAvailableOn10_51(stringOn10_52:"Hi") // expected-error {{'init(stringOn10_52:)' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
}
}
// Declarations with Objective-C-originated potentially unavailable APIs
func functionWithObjCParam(o: NSAvailableOn10_51) { // expected-error {{'NSAvailableOn10_51' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing global function}}
}
class ClassExtendingUnvailableClass : NSAvailableOn10_51 { // expected-error {{'NSAvailableOn10_51' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add @available attribute to enclosing class}}
}
// We allow classes to conform to potentially unavailable protocols
class ClassAdoptingUnavailableProtocol : NSProtocolAvailableOn10_51 {
}
class SomeSoonToBeConformingClass { }
extension SomeSoonToBeConformingClass : NSProtocolAvailableOn10_51 {
}
// Enums from Objective-C
let _: NSPotentiallyUnavailableOptions = .first // expected-error {{'NSPotentiallyUnavailableOptions' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSOptionsWithUnavailableElement = .third // expected-error {{'third' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSUnavailableEnum = .first // expected-error {{'NSUnavailableEnum' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: NSEnumWithUnavailableElement = .third // expected-error {{'third' is only available in macOS 10.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.propertyOn10_51WithSetterOn10_52After // expected-error {{'propertyOn10_51WithSetterOn10_52After' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
// Properties with unavailable accessors declared before property in Objective-C header
o.propertyOn10_51WithSetterOn10_52Before = 5 // expected-error {{setter for 'propertyOn10_51WithSetterOn10_52Before' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: Int = o.propertyOn10_51WithGetterOn10_52Before // expected-error {{getter for 'propertyOn10_51WithGetterOn10_52Before' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
// Properties with unavailable accessors declared after property in Objective-C header
o.propertyOn10_51WithSetterOn10_52After = 5 // expected-error {{setter for 'propertyOn10_51WithSetterOn10_52After' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
let _: Int = o.propertyOn10_51WithGetterOn10_52After // expected-error {{getter for 'propertyOn10_51WithGetterOn10_52After' is only available in macOS 10.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 10.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}}
_ = globalStringAvailableOn10_51 // expected-error {{'globalStringAvailableOn10_51' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = globalStringAvailableOn10_52 // expected-error {{'globalStringAvailableOn10_52' is only available in macOS 10.52 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
_ = globalClassInstanceAvailableOn10_51 // expected-error {{'globalClassInstanceAvailableOn10_51' is only available in macOS 10.51 or newer}}
// expected-note@-1 {{add 'if #available' version check}}
if #available(OSX 10.51, *) {
_ = globalStringAvailableOn10_51
let _: NSAvailableOn10_51 = globalClassInstanceAvailableOn10_51
}
}
// 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 10.51, *)
@objc(doSomethingWithClass:)
func doSomething(with k: AnnotatedFrameworkClass?) { }
@available(OSX 10.51, *)
@objc
func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) { }
@available(OSX 10.51, *)
@objc(doSomethingWithIUOClass:)
func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { }
@objc
@available(OSX 10.51, *)
func returnSomething() -> AnnotatedFrameworkClass? {
return nil
}
@objc
func noUnavailableTypesInSignature() { }
@objc(doSomethingWithClass:andLaterClass:) @available(OSX 10.52, *)
func doSomething(with k: AnnotatedFrameworkClass, andLaterClass lk: AnnotatedLaterFrameworkClass) { }
@objc
@available(OSX 10.53, *)
func someMethodWithAvailability() { }
@available(OSX 10.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 10.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 10.51, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 10.51, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
@available(OSX 10.52, *)
override func doSomething(withIUOClass k: AnnotatedFrameworkClass!) { } // expected-error {{'doSomething' must be as available as declaration it overrides}}
}
@available(OSX 10.52, *)
class SubclassOfLaterFrameworkClassConformingToUnannotatedFrameworkProtocol : LaterFrameworkClassConformingToUnannotatedFrameworkProtocol {
@available(OSX 10.52, *)
override func doSomething(withNonNullableClass k: AnnotatedFrameworkClass) {
}
@available(OSX 10.53, *)
override func someMethodWithAvailability() { }
}
class SubclassOfFrameworkClassConformingToLaterAnnotatedFrameworkProtocol : FrameworkClassConformingToLaterAnnotatedFrameworkProtocol {
@available(OSX 10.52, *)
override func returnSomething() -> AnnotatedFrameworkClass? {
}
@available(OSX 10.53, *)
override func someMethodWithAvailability() { }
@available(OSX 10.52, *)
override var someProperty: AnnotatedFrameworkClass {
get { return AnnotatedFrameworkClass() }
set(newValue) { }
}
}