Skip to content

Commit a2ece9e

Browse files
committed
Gardening: Migrate test suite to GH issues: Casting
1 parent b0fb7c5 commit a2ece9e

File tree

1 file changed

+110
-86
lines changed

1 file changed

+110
-86
lines changed

test/Casting/Casts.swift

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ private func runtimeCast<T,U>(_ from: T, to: U.Type) -> U? {
4141

4242
let CastsTests = TestSuite("Casts")
4343

44-
// Test for SR-426: missing release for some types after failed conversion
44+
// https://github.com/apple/swift/issues/43043
45+
// Missing release for some types after failed conversion
4546
CastsTests.test("No leak for failed tuple casts") {
4647
let t: Any = (1, LifetimeTracked(0))
4748
expectFalse(t is Any.Type)
@@ -50,10 +51,10 @@ CastsTests.test("No leak for failed tuple casts") {
5051
protocol P {}
5152
class ErrClass : Error { }
5253

54+
// https://github.com/apple/swift/issues/43009
5355
CastsTests.test("No overrelease of existential boxes in failed casts") {
54-
// Test for crash from SR-392
55-
// We fail casts of an existential box repeatedly
56-
// to ensure it does not get over-released.
56+
// We fail casts of an existential box repeatedly to ensure it does not get
57+
// over-released.
5758
func bar<T>(_ t: T) {
5859
for _ in 0..<10 {
5960
if case let a as P = t {
@@ -68,8 +69,10 @@ CastsTests.test("No overrelease of existential boxes in failed casts") {
6869

6970
extension Int : P {}
7071

71-
// Test for SR-7664: Inconsistent optional casting behaviour with generics
72-
// Runtime failed to unwrap multiple levels of Optional when casting.
72+
/// https://github.com/apple/swift/issues/50204
73+
/// Inconsistent optional casting behaviour with generics
74+
///
75+
/// Runtime failed to unwrap multiple levels of `Optional` when casting.
7376
CastsTests.test("Multi-level optionals can be casted") {
7477
func testSuccess<From, To>(_ x: From, from: From.Type, to: To.Type) {
7578
expectNotNil(x as? To)
@@ -88,7 +91,8 @@ CastsTests.test("Multi-level optionals can be casted") {
8891
testFailure(42, from: Int???.self, to: String.self)
8992
}
9093

91-
// Test for SR-9837: Optional<T>.none not casting to Optional<U>.none in generic context
94+
/// https://github.com/apple/swift/issues/52251
95+
/// `Optional<T>.none` not casting to `Optional<U>.none` in generic context
9296
CastsTests.test("Optional<T>.none can be casted to Optional<U>.none in generic context") {
9397
func test<T>(_ type: T.Type) -> T? {
9498
return Any?.none as? T
@@ -98,10 +102,11 @@ CastsTests.test("Optional<T>.none can be casted to Optional<U>.none in generic c
98102
expectEqual(type(of: test(Bool?.self)), Bool??.self)
99103
}
100104

101-
// Test for SR-3871: Cannot cast from ObjC existential without going through AnyObject
105+
/// https://github.com/apple/swift/issues/46456
106+
/// Failure to cast from ObjC existential without going through `AnyObject`
102107
#if _runtime(_ObjC)
103108
protocol P2 {}
104-
CastsTests.test("Cast from ObjC existential to Protocol (SR-3871)") {
109+
CastsTests.test("Cast from ObjC existential to Protocol") {
105110
if #available(SwiftStdlib 5.3, *) {
106111
struct S: P2 {}
107112

@@ -111,7 +116,8 @@ CastsTests.test("Cast from ObjC existential to Protocol (SR-3871)") {
111116
}
112117
let a = ObjCWrapper().any
113118
expectTrue(a is P2)
114-
// In SR-3871, the following cast failed (everything else here succeeded)
119+
// The following cast failed in the above issue (everything else here
120+
// succeeded).
115121
expectNotNil(a as? P2)
116122
expectNotNil(a as? S)
117123
let b = a as AnyObject
@@ -139,6 +145,26 @@ CastsTests.test("Cast from Swift existential to Protocol") {
139145
expectNotNil(b as? S)
140146
}
141147

148+
/// Another test for https://github.com/apple/swift/issues/46456
149+
/// User type in a `_SwiftValue` in an `Optional<Any>` not casting to a
150+
/// protocol
151+
///
152+
/// Note: This uses the (misnamed) `_bridgeAnythingToObjectiveC` so it can
153+
/// test these paths on Linux as well.
154+
protocol P4 {}
155+
CastsTests.test("struct -> Obj-C -> Protocol") {
156+
struct S: P4 {
157+
let value: Int
158+
let tracker = LifetimeTracked(13)
159+
}
160+
161+
let a: P4 = S(value: 13)
162+
163+
let b = _bridgeAnythingToObjectiveC(a)
164+
let d = b as? Any
165+
let e = d as? P4
166+
expectNotNil(e)
167+
}
142168

143169
#if _runtime(_ObjC)
144170
extension CFBitVector : P {
@@ -160,7 +186,8 @@ func isP<T>(_ t: T) -> Bool {
160186
return t is P
161187
}
162188

163-
CastsTests.test("Dynamic casts of CF types to protocol existentials (SR-2289)")
189+
// https://github.com/apple/swift/issues/44896
190+
CastsTests.test("Dynamic casts of CF types to protocol existentials")
164191
.skip(.custom({
165192
!_isDebugAssertConfiguration()
166193
},
@@ -174,37 +201,18 @@ CastsTests.test("Dynamic casts of CF types to protocol existentials (SR-2289)")
174201
}
175202
#endif
176203

177-
// Another test for SR-3871, SR-5590, SR-6309, SR-8651:
178-
// user type in a _SwiftValue in an Optional<Any> can't be cast to a protocol.
179-
// Note: This uses the (misnamed) _bridgeAnythingToObjectiveC so it can
180-
// test these paths on Linux as well.
181-
protocol P6309 {}
182-
CastsTests.test("Casting struct -> Obj-C -> Protocol fails (SR-3871, SR-5590, SR-6309, SR-8651)") {
183-
struct S: P6309 {
184-
let value: Int
185-
let tracker = LifetimeTracked(13)
186-
}
187-
188-
let a: P6309 = S(value: 13)
189-
190-
let b = _bridgeAnythingToObjectiveC(a)
191-
let d = b as? Any
192-
let e = d as? P6309
193-
expectNotNil(e)
194-
}
195-
196-
197-
protocol P4552 {}
204+
// https://github.com/apple/swift/issues/47129
205+
protocol P_47129 {}
198206
if #available(SwiftStdlib 5.5, *) {
199-
CastsTests.test("Casting Any(Optional(T)) -> Protocol fails (SR-4552)") {
200-
struct S: P4552 {
207+
CastsTests.test("Any(Optional(T)) -> Protocol") {
208+
struct S: P_47129 {
201209
let tracker = LifetimeTracked(13)
202210
}
203211

204212
let a = S()
205213
let b: S? = a
206214
let c = b as? Any
207-
let d = c as? P4552
215+
let d = c as? P_47129
208216
expectNotNil(d)
209217
}
210218
}
@@ -264,8 +272,9 @@ CastsTests.test("Store Swift metatype in ObjC property and cast back to Any.Type
264272
}
265273
#endif
266274

267-
// rdar://37793004 ([dynamic casting] [SR-7049]: Enums don't cast back from AnyHashable)
268-
CastsTests.test("Enums don't cast back from AnyHashable (SR-7049)") {
275+
/// rdar://37793004
276+
/// https://github.com/apple/swift/issues/49597
277+
CastsTests.test("Cast enum back from AnyHashable") {
269278
enum E {
270279
case a
271280
}
@@ -282,8 +291,10 @@ CastsTests.test("Enums don't cast back from AnyHashable (SR-7049)") {
282291
expectEqual((ea as? E), E.a)
283292
}
284293

294+
/// rdar://39415812
295+
/// https://github.com/apple/swift/issues/49975
296+
/// Failure to see through boxed `_SwiftValue` when casting from `@objc` Type
285297
#if _runtime(_ObjC)
286-
//rdar://39415812 ([dynamic casting] [SR-7432]: Can't see through boxed _SwiftValue when casting from @objc Type)
287298
@objc(Exporter)
288299
protocol Exporter: NSObjectProtocol {
289300
var type: Any { get }
@@ -318,9 +329,12 @@ CastsTests.test("Conditional NSNumber -> Bool casts") {
318329
}
319330
#endif
320331

321-
// rdar://45217461 ([dynamic casting] [SR-8964]: Type check operator (is) fails for Any! variable holding an Error (struct) value)
332+
/// rdar://45217461
333+
/// https://github.com/apple/swift/issues/51469
334+
/// Type check operator (`is`) fails for `Any!` variable holding an `Error`
335+
/// (struct) value
322336
if #available(SwiftStdlib 5.5, *) {
323-
CastsTests.test("Casts from Any(struct) to Error (SR-8964)") {
337+
CastsTests.test("Casts from Any(struct) to Error") {
324338
struct MyError: Error { }
325339

326340
let a: Any! = MyError()
@@ -329,6 +343,13 @@ CastsTests.test("Casts from Any(struct) to Error (SR-8964)") {
329343
}
330344
}
331345

346+
CastsTests.test("Cast failure for Any! holding Error struct") {
347+
struct MyError: Error {}
348+
let a: Any! = MyError()
349+
let b: Any = a
350+
expectTrue(b is Error)
351+
}
352+
332353
#if _runtime(_ObjC)
333354
// rdar://15494623 (Handle dynamic cast to archetype bound to ObjC existential)
334355
CastsTests.test("Dynamic cast to ObjC protocol") {
@@ -342,9 +363,9 @@ CastsTests.test("Dynamic cast to ObjC protocol") {
342363
}
343364
#endif
344365

345-
// SR-6126
366+
// https://github.com/apple/swift/issues/48681
346367
if #available(macOS 11.3, iOS 14.5, tvOS 14.5, watchOS 7.4, *) {
347-
CastsTests.test("Nil handling for Optionals and Arrays (SR-6126)") {
368+
CastsTests.test("Nil handling for Optionals and Arrays") {
348369
func check(_ arg: Int??) -> String {
349370
switch arg {
350371
case .none:
@@ -471,39 +492,56 @@ CastsTests.test("String/NSString extension compat") {
471492
}
472493
#endif
473494

474-
protocol P1999 {}
495+
// https://github.com/apple/swift/issues/44608
496+
protocol P_44608 {}
475497
if #available(SwiftStdlib 5.5, *) {
476-
CastsTests.test("Cast Any(Optional(class)) to Protocol type (SR-1999)") {
477-
class Foo: P1999 { }
498+
CastsTests.test("Cast Any(Optional(class)) to Protocol type") {
499+
class Foo: P_44608 { }
478500

479501
let optionalFoo : Foo? = Foo()
480502
let anyValue: Any = optionalFoo
481503

482504
let foo1 = anyValue as? Foo
483505
expectNotNil(foo1)
484506

485-
let foo2 = anyValue as? P1999
507+
let foo2 = anyValue as? P_44608
486508
expectNotNil(foo2)
487509

488510
let foo3 = runtimeCast(anyValue, to: Foo.self)
489511
expectNotNil(foo3)
490512

491-
let foo4 = runtimeCast(anyValue, to: P1999.self)
513+
let foo4 = runtimeCast(anyValue, to: P_44608.self)
492514
expectNotNil(foo4)
493515
}
494516
}
495517

518+
CastsTests.test("Cast from Any? to Existential") {
519+
let a = Float(1) as Any as? Float
520+
expectNotNil(a)
521+
522+
let b = Float(1) as Any as? CustomStringConvertible
523+
expectNotNil(b)
524+
525+
let c = Optional.some(Float(1)) as Any as? Float
526+
expectNotNil(c)
527+
528+
let d = Optional.some(Float(1)) as Any as? CustomStringConvertible
529+
expectNotNil(d)
530+
}
531+
532+
// https://github.com/apple/swift/issues/45505
496533
#if _runtime(_ObjC)
497-
CastsTests.test("Dict value casting (SR-2911)") {
534+
CastsTests.test("Dict value casting") {
498535
var dict: [AnyHashable: String] = [:]
499536
dict["Key"] = "Value"
500537
expectNotNil(dict["Key"] as? NSString)
501538
expectNotNil(runtimeCast(dict["Key"], to: NSString.self))
502539
}
503540
#endif
504541

542+
// https://github.com/apple/swift-corelibs-foundation/issues/3279
505543
#if _runtime(_ObjC)
506-
CastsTests.test("String coercions should work on Linux (SR-12020)") {
544+
CastsTests.test("String coercions should work on Linux") {
507545
let a = "abc" as Substring as NSString
508546
let b = "abc" as NSString
509547
expectEqual(a, b)
@@ -535,9 +573,10 @@ CastsTests.test("AnyHashable(Class) -> Obj-C -> Class") {
535573
expectNotNil(e)
536574
}
537575

538-
#if _runtime(_ObjC)
539576
// rdar://58999120
540-
CastsTests.test("Error -> NSError -> Protocol transitivity (SR-12095)") {
577+
// https://github.com/apple/swift-corelibs-foundation/issues/3274
578+
#if _runtime(_ObjC)
579+
CastsTests.test("Error -> NSError -> Protocol transitivity") {
541580
enum NonConformingError: Error {
542581
case ok
543582
}
@@ -802,8 +841,9 @@ CastsTests.test("AnyObject.Type -> AnyObject") {
802841
}
803842
#endif
804843

844+
// https://github.com/apple/swift/issues/56209
805845
protocol Fruit {}
806-
CastsTests.test("Generic type validation [SR-13812]") {
846+
CastsTests.test("Generic type validation") {
807847
func check<A, B>(a: A.Type, b: B.Type) -> Bool {
808848
return (a is B.Type)
809849
}
@@ -813,29 +853,9 @@ CastsTests.test("Generic type validation [SR-13812]") {
813853
expectTrue(Apple.self is Fruit.Type)
814854
}
815855

816-
CastsTests.test("Cast failure for Any! holding Error struct [SR-8964]") {
817-
struct MyError: Error {}
818-
let a: Any! = MyError()
819-
let b: Any = a
820-
expectTrue(b is Error)
821-
}
822-
823-
CastsTests.test("Cannot cast from Any? to Existential [SR-1999]") {
824-
let a = Float(1) as Any as? Float
825-
expectNotNil(a)
826-
827-
let b = Float(1) as Any as? CustomStringConvertible
828-
expectNotNil(b)
829-
830-
let c = Optional.some(Float(1)) as Any as? Float
831-
expectNotNil(c)
832-
833-
let d = Optional.some(Float(1)) as Any as? CustomStringConvertible
834-
expectNotNil(d)
835-
}
836-
856+
// https://github.com/apple/swift/issues/48829
837857
protocol A {}
838-
CastsTests.test("Failing cast from Any to Optional<Protocol> [SR-6279]") {
858+
CastsTests.test("Cast from Any to Optional<Protocol>") {
839859
struct B: A {}
840860

841861
// If we have an optional instance, stored as an `Any`
@@ -865,11 +885,12 @@ CastsTests.test("Casting Objects retained from KeyPaths to Protocols is not work
865885
expectNotNil(value as? SuperProtocol)
866886
}
867887

888+
// https://github.com/apple/swift/issues/54462
889+
// FIXME: Known to still be broken, but we can document the issue here.
868890
#if _runtime(_ObjC)
869-
// Known to still be broken, but we can document the issue here
870891
public protocol SomeProtocol {}
871892
extension NSString: SomeProtocol {}
872-
CastsTests.test("NSDictionary -> Dictionary casting [SR-12025]") {
893+
CastsTests.test("NSDictionary -> Dictionary casting") {
873894
// Create NSDictionary with one entry
874895
var a = NSMutableDictionary()
875896
a[NSString("key")] = NSString("value")
@@ -897,7 +918,8 @@ CastsTests.test("Optional cast to AnyHashable") {
897918
// We've deliberately tried to preserve that behavior in Swift 5.4
898919
let d2 = d as [AnyHashable: String]
899920

900-
// After SR-9047, all four of the following should work:
921+
// After https://github.com/apple/swift/issues/51550 all four of the following
922+
// should work:
901923
let d3 = d2["FooKey" as String? as AnyHashable]
902924
expectNil(d3)
903925
let d4 = d2["FooKey" as String?]
@@ -921,7 +943,8 @@ CastsTests.test("Optional cast to AnyHashable") {
921943
// Direct casts that don't go through the runtime don't unwrap the optional
922944
// This is inconsistent with the runtime cast behavior above. We should
923945
// probably change the runtime behavior above to work the same as this,
924-
// but that should wait until SR-9047 lands.
946+
// but that should wait until https://github.com/apple/swift/issues/51550
947+
// lands.
925948
let x: String = "Baz"
926949
let xh = x as AnyHashable
927950
let y: String? = x
@@ -954,16 +977,17 @@ CastsTests.test("Recursive AnyHashable") {
954977
expectEqual(s.x, p)
955978
}
956979

957-
// SR-14635 (aka rdar://78224322)
980+
// rdar://78224322
981+
// https://github.com/apple/swift/issues/56987
958982
#if _runtime(_ObjC)
959983
CastsTests.test("Do not overuse __SwiftValue")
960-
.skip(.osxAny("SR-14635 not yet fully enabled for Apple OSes"))
961-
.skip(.iOSAny("SR-14635 not yet fully enabled for Apple OSes"))
962-
.skip(.iOSSimulatorAny("SR-14635 not yet fully enabled for Apple OSes"))
963-
.skip(.tvOSAny("SR-14635 not yet fully enabled for Apple OSes"))
964-
.skip(.tvOSSimulatorAny("SR-14635 not yet fully enabled for Apple OSes"))
965-
.skip(.watchOSAny("SR-14635 not yet fully enabled for Apple OSes"))
966-
.skip(.watchOSSimulatorAny("SR-14635 not yet fully enabled for Apple OSes"))
984+
.skip(.osxAny("Not yet fully enabled for Apple OSes"))
985+
.skip(.iOSAny("Not yet fully enabled for Apple OSes"))
986+
.skip(.iOSSimulatorAny("Not yet fully enabled for Apple OSes"))
987+
.skip(.tvOSAny("Not yet fully enabled for Apple OSes"))
988+
.skip(.tvOSSimulatorAny("Not yet fully enabled for Apple OSes"))
989+
.skip(.watchOSAny("Not yet fully enabled for Apple OSes"))
990+
.skip(.watchOSSimulatorAny("Not yet fully enabled for Apple OSes"))
967991
.code {
968992
struct Bar {}
969993
// This used to succeed because of overeager __SwiftValue

0 commit comments

Comments
 (0)