forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailability_weak_linking.swift
157 lines (123 loc) · 5.2 KB
/
availability_weak_linking.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
// RUN: rm -rf %t && mkdir -p %t
// RUN: cp -R %S/Inputs/FakeUnavailableObjCFramework.framework %t
// RUN: %target-clang -dynamiclib %S/Inputs/FakeUnavailableObjCFramework.m -fmodules -F %t -framework Foundation -o %t/FakeUnavailableObjCFramework.framework/FakeUnavailableObjCFramework
// RUN: %target-build-swift -F %t %s -o %t/UseWeaklinkedUnavailableObjCFramework
// RUN: %target-build-swift -O -F %t %s -o %t/UseWeaklinkedUnavailableObjCFramework.opt
// These tests emulate deploying back to an older OS where newer APIs are not
// available by linking to an Objective-C framework where APIs have been
// annotated to only be available in the far future (version 1066.0 of all
// platforms) and then moving the framework aside so that it can't be found
// at run time.
// RUN: mv %t/FakeUnavailableObjCFramework.framework %t/FakeUnavailableObjCFramework-MovedAside.framework
// RUN: %target-run %t/UseWeaklinkedUnavailableObjCFramework | %FileCheck %s
// RUN: %target-run %t/UseWeaklinkedUnavailableObjCFramework.opt | %FileCheck %s
// REQUIRES: objc_interop
// REQUIRES: executable_test
import StdlibUnittest
import FakeUnavailableObjCFramework
import Foundation
// CHECK: Running
print("Running...")
func useUnavailableObjCGlobal() {
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let g = UnavailableObjCGlobalVariable
_blackHole(g)
}
}
useUnavailableObjCGlobal()
@objc
class ClassConformingToUnavailableObjCProtocol : NSObject, UnavailableObjCProtocol {
func someMethod() {
print("Executed ClassConformingToUnavailableObjCProtocol.someMethod()")
}
}
func useClassConformingToUnavailableObjCProtocol() {
let o = ClassConformingToUnavailableObjCProtocol()
o.someMethod()
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let oAsUP: UnavailableObjCProtocol = o as UnavailableObjCProtocol
oAsUP.someMethod()
}
}
// CHECK-NEXT: Executed ClassConformingToUnavailableObjCProtocol.someMethod()
useClassConformingToUnavailableObjCProtocol()
@objc
class ClassThatWillBeExtendedToConformToUnavailableObjCProtocol : NSObject {
}
extension ClassThatWillBeExtendedToConformToUnavailableObjCProtocol : UnavailableObjCProtocol {
func someMethod() {
print("Executed ClassThatWillBeExtendedToConformToUnavailableObjCProtocol.someMethod()")
}
}
func useClassThatWillBeExtendedToConformToUnavailableObjCProtocol() {
let o = ClassThatWillBeExtendedToConformToUnavailableObjCProtocol()
o.someMethod()
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let oAsUP: UnavailableObjCProtocol = o as UnavailableObjCProtocol
oAsUP.someMethod()
}
}
// CHECK-NEXT: Executed ClassThatWillBeExtendedToConformToUnavailableObjCProtocol.someMethod()
useClassThatWillBeExtendedToConformToUnavailableObjCProtocol()
// We need to gracefully handle ObjC protocols missing availability annotations
// because it is quite common in frameworks. (Historically, for Objective-C,
// missing availability annotations on protocols has not been problematic
// because Objective-C protocol metadata is compiled into any code that
// references it -- it is not weakly linked.)
@objc
class ClassConformingToUnannotatedUnavailableObjCProtocol : NSObject, UnannotatedUnavailableObjCProtocol {
func someMethod() {
print("Executed ClassConformingToUnannotatedUnavailableObjCProtocol.someMethod()")
}
}
func useClassConformingToUnannotatedUnavailableObjCProtocol() {
let o = ClassConformingToUnannotatedUnavailableObjCProtocol()
o.someMethod()
let oAsUP: UnannotatedUnavailableObjCProtocol = (o as AnyObject) as! UnannotatedUnavailableObjCProtocol
oAsUP.someMethod()
}
// CHECK-NEXT: Executed ClassConformingToUnannotatedUnavailableObjCProtocol.someMethod()
// CHECK-NEXT: Executed ClassConformingToUnannotatedUnavailableObjCProtocol.someMethod()
useClassConformingToUnannotatedUnavailableObjCProtocol()
func printClassMetadataViaGeneric<T>() -> T {
print("\(T.self)")
fatalError("This should never be called")
}
func useUnavailableObjCClass() {
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let o = UnavailableObjCClass()
o.someMethod()
}
for i in 0 ..< getInt(5) {
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let o: UnavailableObjCClass = printClassMetadataViaGeneric()
_blackHole(o)
}
}
class SomeClass { }
let someObject: AnyObject = _opaqueIdentity(SomeClass() as AnyObject)
for i in 0 ..< getInt(5) {
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let isUnavailable = someObject is UnavailableObjCClass
_blackHole(isUnavailable)
}
}
for i in 0 ..< getInt(5) {
if #available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *) {
let asUnavailable = someObject as? UnavailableObjCClass
_blackHole(asUnavailable)
}
}
}
@available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *)
func wrapUnavailableFunction() {
someFunction()
}
useUnavailableObjCClass()
// Allow extending a weakly-linked class to conform to a protocol.
protocol SomeSwiftProtocol { }
@available(OSX 1066.0, iOS 1066.0, watchOS 1066.0, tvOS 1066.0, *)
extension UnavailableObjCClass : SomeSwiftProtocol {
}
// CHECK-NEXT: Done
print("Done")