@@ -12,6 +12,17 @@ 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
+
15
26
let OptionalTests = TestSuite ( " Optional " )
16
27
17
28
protocol TestProtocol1 { }
@@ -82,7 +93,6 @@ OptionalTests.test("Equatable") {
82
93
83
94
struct X { }
84
95
class C { }
85
- class D { }
86
96
87
97
class E : Equatable { }
88
98
func == ( _: E , _: E ) -> Bool { return true }
@@ -180,21 +190,11 @@ OptionalTests.test("flatMap") {
180
190
expectEmpty ( ( 3 as Int32 ? ) . flatMap ( half) )
181
191
}
182
192
183
- // FIXME: @inline(never) does not inhibit specialization
184
-
185
193
@inline ( never)
186
194
func anyToAny< T, U> ( a: T , _ : U . Type ) -> U {
187
195
return a as! U
188
196
}
189
197
@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,27 +210,10 @@ 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
-
216
213
expectTrue ( anyToAny ( sx, C . self) === x)
217
- expectTrue ( anyToAnyIs ( sx, C . self) )
218
- expectFalse ( anyToAnyIs ( sx, D . self) )
219
-
220
214
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) )
224
215
225
216
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
-
234
217
expectTrue ( anyToAnyOrNil ( nx, C . self) == nil )
235
218
236
219
let i = Int . max
@@ -249,37 +232,20 @@ OptionalTests.test("Casting Optional") {
249
232
expectTrue ( anyToAnyOrNil ( ni, Int . self) == nil )
250
233
251
234
// Test for SR-459: Weakened optionals don't zero.
252
- var t = LifetimeTracked ( 0 )
253
- _ = anyToAny ( Optional ( t) , CustomDebugStringConvertible . self)
254
- expectTrue ( anyToAnyIs ( Optional ( t) , CustomDebugStringConvertible . self) )
235
+ var deinitRan = false
236
+ do {
237
+ var t = DeinitTester { deinitRan = true }
238
+ _ = anyToAny ( Optional ( t) , CustomDebugStringConvertible . self)
239
+ }
240
+ expectTrue ( deinitRan)
255
241
256
242
// Test for SR-912: Runtime exception casting an Any nil to an Optional.
257
243
let oi : Int ? = nil
258
244
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
-
264
245
// For good measure test an existential that Optional does not conform to.
265
246
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
-
271
247
// And a type that is not convertible to its target.
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) )
248
+ anyToAny ( nx as Any , Optional< Int> . self )
283
249
}
284
250
285
251
OptionalTests . test ( " Casting Optional Traps " ) {
0 commit comments