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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s
// REQUIRES: executable_test
protocol MyPrintable {
func myPrint()
}
extension Int : MyPrintable {
func myPrint() {
print(self.description, terminator: "")
}
}
extension Double : MyPrintable {
func myPrint() {
print(self.description, terminator: "")
}
}
extension String : MyPrintable {
func myPrint() {
print(self.debugDescription, terminator: "")
}
}
class BufferedPair<T, U> {
var front: UInt8
var first: T
var second: U
var back: UInt8
init(_ front: UInt8, _ first: T, _ second: U, _ back: UInt8) {
self.front = front
self.first = first
self.second = second
self.back = back
}
}
enum State : MyPrintable {
case CA, OR, WA
func myPrint() {
switch self {
case .CA:
print("California", terminator: "")
case .OR:
print("Oregon", terminator: "")
case .WA:
print("Washington", terminator: "")
}
}
}
func printPair<A: MyPrintable, B: MyPrintable>(_ p: BufferedPair<A,B>) {
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back)")
}
var p = BufferedPair(99, State.OR, "Washington's Mexico", 84)
// CHECK: 99 Oregon "Washington\'s Mexico" 84
printPair(p)
class AwkwardTriple<V, W, X> : BufferedPair<V, W> {
var third: X
init(_ front: UInt8, _ first: V, _ second: W, _ back: UInt8, _ third: X) {
self.third = third
super.init(front, first, second, back)
self.third = third
}
}
func printTriple
<D: MyPrintable, E: MyPrintable, F: MyPrintable>
(_ p: AwkwardTriple<D, E, F>)
{
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back) ", terminator: "")
p.third.myPrint()
print("")
}
var q = AwkwardTriple(123, State.CA, "Foo", 234, State.WA)
// CHECK: 123 California "Foo" 234
printPair(q)
// CHECK: 123 California "Foo" 234 Washington
printTriple(q)
class FourthWheel<P, Q, R, S> : AwkwardTriple<P, Q, R> {
var fourth: S
init(_ front: UInt8, _ first: P, _ second: Q, _ back: UInt8, _ third: R,
_ fourth: S) {
self.fourth = fourth
super.init(front, first, second, back, third)
self.fourth = fourth
}
}
func printQuad
<G: MyPrintable, H: MyPrintable, I: MyPrintable, J: MyPrintable>
(_ p: FourthWheel<G, H, I, J>)
{
print("\(p.front) ", terminator: "")
p.first.myPrint()
print(" ", terminator: "")
p.second.myPrint()
print(" \(p.back) ", terminator: "")
p.third.myPrint()
print(" ", terminator: "")
p.fourth.myPrint()
print("")
}
var r = FourthWheel(21, State.WA, "Bar", 31, State.OR, 3.125)
// CHECK: 21 Washington "Bar" 31
printPair(r)
// CHECK: 21 Washington "Bar" 31 Oregon
printTriple(r)
var rAsPair: BufferedPair<State, String> = r
// CHECK: 21 Washington "Bar" 31 Oregon
printTriple(rAsPair as! AwkwardTriple<State, String, State>)
// CHECK: 21 Washington "Bar" 31 Oregon 3.125
printQuad(r)
// CHECK: 21 Washington "Bar" 31 Oregon 3.125
printQuad(rAsPair as! FourthWheel<State, String, State, Double>)
class ConcretePair {
var first, second: UInt8
init(_ first: UInt8, _ second: UInt8) {
self.first = first
self.second = second
}
}
class SemiConcreteTriple<O> : ConcretePair {
var third: O
init(_ first: UInt8, _ second: UInt8, _ third: O) {
self.third = third
super.init(first, second)
self.third = third
}
}
func printConcretePair(_ p: ConcretePair) {
print("\(p.first) \(p.second)")
}
func printSemiTriple<O : MyPrintable>(_ p: SemiConcreteTriple<O>) {
print("\(p.first) \(p.second) ", terminator: "")
p.third.myPrint()
print("")
}
var s = SemiConcreteTriple(120, 230, State.CA)
// CHECK: 120 230
printConcretePair(s)
// CHECK: 120 230 California
printSemiTriple(s)
var t = SemiConcreteTriple(121, 231, "California's Canada")
// CHECK: 121 231
printConcretePair(t)
// CHECK: 121 231 "California\'s Canada"
printSemiTriple(t)
class MoreConcreteQuadruple : SemiConcreteTriple<State> {
var fourth: String
init(_ first: UInt8, _ second: UInt8, _ third: State, _ fourth: String) {
self.fourth = fourth
super.init(first, second, third)
}
}
// This check triggers https://github.com/apple/swift/issues/43427
// (rdar://problem/25318716) on macOS 10.9 and iOS 7. Disable it for now when
// testing on those versions.
if #available(macOS 10.10, iOS 8, *) {
var u = MoreConcreteQuadruple(10, 17, State.CA, "Hella")
// CHECK: 10 17
printConcretePair(u)
} else {
print("10 17") // Hack to satisfy FileCheck.
}
class RootGenericFixedLayout<T> {
let a: [T]
let b: Int
init(a: [T], b: Int) {
self.a = a
self.b = b
}
}
func checkRootGenericFixedLayout<T>(_ r: RootGenericFixedLayout<T>) {
print(r.a)
print(r.b)
}
let rg = RootGenericFixedLayout<Int>(a: [1, 2, 3], b: 4)
// CHECK: [1, 2, 3]
// CHECK: 4
checkRootGenericFixedLayout(rg)
class GenericInheritsGenericFixedLayout<T> : RootGenericFixedLayout<T> {
let c: Int
init(a: [T], b: Int, c: Int) {
self.c = c
super.init(a: a, b: b)
}
}
let gg = GenericInheritsGenericFixedLayout<Int>(a: [1, 2, 3], b: 4, c: 5)
func checkGenericInheritsGenericFixedLayout<T>(_ g: GenericInheritsGenericFixedLayout<T>) {
print(g.a)
print(g.b)
print(g.c)
}
// CHECK: [1, 2, 3]
// CHECK: 4
checkRootGenericFixedLayout(gg)
// CHECK: [1, 2, 3]
// CHECK: 4
// CHECK: 5
checkGenericInheritsGenericFixedLayout(gg)
|