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
|
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
struct BigStruct { var a,b,c,d,e,f,g,h:Int }
// FIXME: missing symbol for Object destructor?
//class SomeClass : Object { }
func id<T>(_ x: T) -> T {
return x
}
var int = id(1)
var bigStruct = id(BigStruct(a: 1, b: 2,c: 3, d: 4, e: 5, f: 6, g: 7, h: 8))
//var someClass = SomeClass()
//var someClass2 = id(someClass)
func print(_ bs: BigStruct) {
// FIXME: typechecker is too slow to handle this as an interpolated literal
print("BigStruct(", terminator: "")
print(bs.a, terminator: "")
print(", ", terminator: "")
print(bs.b, terminator: "")
print(", ", terminator: "")
print(bs.c, terminator: "")
print(", ", terminator: "")
print(bs.d, terminator: "")
print(", ", terminator: "")
print(bs.e, terminator: "")
print(", ", terminator: "")
print(bs.f, terminator: "")
print(", ", terminator: "")
print(bs.g, terminator: "")
print(", ", terminator: "")
print(bs.h, terminator: "")
print(")")
}
// CHECK: 1
print(int)
// CHECK: BigStruct(1, 2, 3, 4, 5, 6, 7, 8)
print(bigStruct)
// FIXME: missing symbol for Object destructor?
// C/HECK: true
//print(someClass === someClass2)
//===----
// Check overload resolution of generic functions.
//===----
protocol P1 {}
protocol P2 : P1 {}
protocol P3 : P2 {}
struct S1 : P1 {}
struct S2 : P2 {}
struct S3 : P3 {}
func foo1<T : P1>(_ x: T) { print("P1") }
func foo1<T : P2>(_ x: T) { print("P2") }
func foo1<T : P3>(_ x: T) { print("P3") }
func foo2<T : P1>(_ x: T) { print("P1") }
func foo2<T : P2>(_ x: T) { print("P2") }
func foo3<T : P1>(_ x: T) { print("P1") }
func foo3<T : P3>(_ x: T) { print("P3") }
func foo4<T : P3, U : P1>(_ x: T, _ y: U) { print("P3, P1") }
func foo4<T : P3, U : P3>(_ x: T, _ y: U) { print("P3, P3") }
func checkOverloadResolution() {
print("overload resolution:")
// CHECK-LABEL: overload resolution
foo1(S1()) // CHECK-NEXT: P1
foo1(S2()) // CHECK-NEXT: P2
foo1(S3()) // CHECK-NEXT: P3
foo2(S1()) // CHECK-NEXT: P1
foo2(S2()) // CHECK-NEXT: P2
foo2(S3()) // CHECK-NEXT: P2
foo3(S1()) // CHECK-NEXT: P1
foo3(S2()) // CHECK-NEXT: P1
foo3(S3()) // CHECK-NEXT: P3
foo4(S3(), S1()) // CHECK-NEXT: P3, P1
foo4(S3(), S2()) // CHECK-NEXT: P3, P1
foo4(S3(), S3()) // CHECK-NEXT: P3, P3
}
checkOverloadResolution()
class Base {
var v = 0
required init() {}
func map() {
v = 1
}
}
class D1 : Base {
required init() {}
override func map() {
v = 2
}
}
func parse<T:Base>() -> T {
let inst = T()
inst.map()
return inst
}
var m : D1 = parse()
print(m.v)
// CHECK: 2
// rdar://problem/21770225 - infinite recursion with metadata
// instantiation when associated type contains Self
struct MySlice<T : MyIndexableType> : MySequenceType {}
struct MyMutableSlice<T : MyMutableCollectionType> : MySequenceType {}
protocol MySequenceType {}
protocol MyIndexableType {}
protocol MyCollectionType : MySequenceType, MyIndexableType {
associatedtype SubSequence = MySlice<Self>
}
protocol MyMutableCollectionType : MyCollectionType {
associatedtype SubSequence = MyMutableSlice<Self>
}
struct S : MyMutableCollectionType {}
// CHECK: MyMutableSlice<S>()
print(S.SubSequence())
// CHECK: MyMutableSlice<S>
print(S.SubSequence.self)
|