@@ -12,17 +12,6 @@ import SwiftPrivate
12
12
import ObjectiveC
13
13
#endif
14
14
15
- class DeinitTester {
16
- private let onDeinit : ( ) -> ( )
17
-
18
- init ( onDeinit: ( ) -> ( ) ) {
19
- self . onDeinit = onDeinit
20
- }
21
- deinit {
22
- onDeinit ( )
23
- }
24
- }
25
-
26
15
let OptionalTests = TestSuite ( " Optional " )
27
16
28
17
protocol TestProtocol1 { }
@@ -93,6 +82,7 @@ OptionalTests.test("Equatable") {
93
82
94
83
struct X { }
95
84
class C { }
85
+ class D { }
96
86
97
87
class E : Equatable { }
98
88
func == ( _: E , _: E ) -> Bool { return true }
@@ -190,11 +180,21 @@ OptionalTests.test("flatMap") {
190
180
expectEmpty ( ( 3 as Int32 ? ) . flatMap ( half) )
191
181
}
192
182
183
+ // FIXME: @inline(never) does not inhibit specialization
184
+
193
185
@inline ( never)
194
186
func anyToAny< T, U> ( a: T , _ : U . Type ) -> U {
195
187
return a as! U
196
188
}
197
189
@inline ( never)
190
+ func anyToAnyIs< T, U> ( a: T , _ : U . Type ) -> Bool {
191
+ return a is U
192
+ }
193
+ @inline ( never)
194
+ func anyToAnyIsOptional< T, U> ( a: T ? , _ : U . Type ) -> Bool {
195
+ return a is U ?
196
+ }
197
+ @inline ( never)
198
198
func anyToAnyOrNil< T, U> ( a: T , _ : U . Type ) -> U ? {
199
199
return a as? U
200
200
}
@@ -210,10 +210,27 @@ OptionalTests.test("Casting Optional") {
210
210
let sx : C ? = x
211
211
let nx : C ? = nil
212
212
expectTrue ( anyToAny ( x, Optional< C> . self ) ! === x)
213
+ expectTrue ( anyToAnyIs ( x, Optional< C> . self ) )
214
+ expectFalse ( anyToAnyIs ( x, Optional< D> . self ) )
215
+
213
216
expectTrue ( anyToAny ( sx, C . self) === x)
217
+ expectTrue ( anyToAnyIs ( sx, C . self) )
218
+ expectFalse ( anyToAnyIs ( sx, D . self) )
219
+
214
220
expectTrue ( anyToAny ( sx, Optional< C> . self ) ! === x)
221
+ expectTrue ( anyToAnyIs ( sx, Optional< C> . self ) )
222
+ expectTrue ( anyToAnyIsOptional ( sx, C . self) )
223
+ expectFalse ( anyToAnyIsOptional ( sx, D . self) )
215
224
216
225
expectTrue ( anyToAny ( nx, Optional< C> . self ) == nil )
226
+ expectTrue ( anyToAnyIs ( nx, Optional< C> . self ) )
227
+
228
+ // You can cast a nil of any type to a nil of any other type
229
+ // successfully
230
+ expectTrue ( anyToAnyIs ( nx, Optional< D> . self ) )
231
+
232
+ expectTrue ( anyToAnyIsOptional ( nx, C . self) )
233
+
217
234
expectTrue ( anyToAnyOrNil ( nx, C . self) == nil )
218
235
219
236
let i = Int . max
@@ -232,20 +249,37 @@ OptionalTests.test("Casting Optional") {
232
249
expectTrue ( anyToAnyOrNil ( ni, Int . self) == nil )
233
250
234
251
// Test for SR-459: Weakened optionals don't zero.
235
- var deinitRan = false
236
- do {
237
- var t = DeinitTester { deinitRan = true }
238
- _ = anyToAny ( Optional ( t) , CustomDebugStringConvertible . self)
239
- }
240
- expectTrue ( deinitRan)
252
+ var t = LifetimeTracked ( 0 )
253
+ _ = anyToAny ( Optional ( t) , CustomDebugStringConvertible . self)
254
+ expectTrue ( anyToAnyIs ( Optional ( t) , CustomDebugStringConvertible . self) )
241
255
242
256
// Test for SR-912: Runtime exception casting an Any nil to an Optional.
243
257
let oi : Int ? = nil
244
258
expectTrue ( anyToAny ( oi as Any , Optional< Int> . self ) == nil )
259
+ expectTrue ( anyToAnyIs ( oi as Any , Optional< Int> . self ) )
260
+
261
+ // Double-wrapped optional
262
+ expectTrue ( anyToAnyIsOptional ( oi as Any , Int . self) )
263
+
245
264
// For good measure test an existential that Optional does not conform to.
246
265
expectTrue ( anyToAny ( 3 as TestExistential , Optional< Int> . self ) == 3 )
266
+
267
+ // Can't do existential + optional wrapping at once for some reason
268
+ expectTrue ( anyToAnyIs ( 3 as TestExistential , Optional< Int> . self ) )
269
+ expectTrue ( anyToAnyIsOptional ( 3 as TestExistential , Int . self) )
270
+
247
271
// And a type that is not convertible to its target.
248
- anyToAny ( nx as Any , Optional< Int> . self )
272
+ expectTrue ( anyToAny ( nx as Any , Optional< Int> . self ) == nil )
273
+ expectTrue ( anyToAnyIs ( nx as Any , Optional< Int> . self ) )
274
+ expectTrue ( anyToAnyIsOptional ( nx as Any , Int . self) )
275
+
276
+ expectTrue ( anyToAnyOrNil ( sx as Any , Optional< Int> . self ) == nil )
277
+ expectFalse ( anyToAnyIs ( sx as Any , Optional< Int> . self ) )
278
+ expectFalse ( anyToAnyIsOptional ( sx as Any , Int . self) )
279
+
280
+ // OK to convert nil of any type to optional of any other type
281
+ expectTrue ( anyToAnyIs ( Optional< ( String, String) > . none, Optional< Bool> . self ) )
282
+ expectTrue ( anyToAnyIsOptional ( Optional< ( String, String) > . none, Bool . self) )
249
283
}
250
284
251
285
OptionalTests . test ( " Casting Optional Traps " ) {
0 commit comments