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
|
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
import StdlibUnittest
var MetadataCycleTests = TestSuite("Metadata cycle tests")
// This no longer crashes because we emit type layouts and
// not full metadata for fixed-size class fields.
class List<T> {
var value: T
var next: List<T>?
init(value: T) {
self.value = value
}
init(value: T, next: List<T>) {
self.value = value
self.next = next
}
}
MetadataCycleTests.test("rdar://18067671") {
let a = List(value: 0.0)
let b = List(value: 1.0, next: a)
let c = List(value: 2.0, next: b)
b.value = 4.0
a.value = 8.0
expectEqual(c.value, 2.0)
expectEqual(c.next!.value, 4.0)
expectEqual(c.next!.next!.value, 8.0)
}
// This no longer crashes because nested type metadata
// does not refer to the parent type metadata anymore.
struct X<T> {
enum S {
case some(T), none
}
init() { a = .none }
var a: S
}
MetadataCycleTests.test("rdar://33767511") {
var str = ""
print(X<()>.self, to: &str)
expectEqual("X<()>\n", str)
}
// The remaining test cases are "bona fide" examples of recursive metadata.
// rdar://18448285
class test0_GenericClass<T> {
func foo() {}
}
class test0_GenericSubclass<T> : test0_GenericClass<test0_GenericSubclass> {}
class test0_NonGenericSubclass : test0_GenericSubclass<test0_NonGenericSubclass> {}
MetadataCycleTests.test("rdar://18448285") {
test0_GenericSubclass<Int>().foo()
test0_NonGenericSubclass().foo()
}
// rdar://18685206
final class test1_Box<T> {
init(_ value: T) {
self.value = value
}
let value: T
}
enum test1_List<T> {
case Nil
case Cons(T, test1_Box<test1_List<T>>)
var head: T? {
switch self {
case .Nil:
return nil
case .Cons(let h, _):
return h
}
}
}
MetadataCycleTests.test("rdar://18685206") {
let x: test1_List<Int> = .Nil
_ = x.head
}
// rdar://18847269
struct test2_Thunk<T> {}
enum test2_List<T> {
case Nil
case Cons(T, test2_Thunk<test2_List>)
}
MetadataCycleTests.test("rdar://18847269") {
let il0: test2_List<Int> = .Nil
_ = il0
}
// rdar://18903483
final class test3_Box<T> {
private let _value : () -> T
init(_ value : T) {
self._value = { value }
}
}
enum test3_List<A> {
case Nil
case Cons(A, test3_Box<test3_List<A>>)
}
MetadataCycleTests.test("rdar://18903483") {
let x : test3_List<Int> = .Nil
_ = x
// rdar://19371082
_ = test3_List.Cons(7, test3_Box(test3_List.Nil))
}
// rdar://19320857
struct test4_RoseTree<T> {
let value: T
let branches: [test4_RoseTree]
}
MetadataCycleTests.test("rdar://19320857") {
_ = test4_RoseTree(value: 1, branches: [])
}
runAllTests()
|