forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol_lookup.swift
155 lines (129 loc) · 3.41 KB
/
protocol_lookup.swift
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
// RUN: %target-run-simple-swift | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-runtime
// REQUIRES: executable_test
// Note: JIT mode is checked in Interpreter/protocol_lookup_jit.swift.
protocol Fooable {
func foo()
}
struct S: Fooable {
func foo() { print("S") }
}
class C: Fooable {
func foo() { print("C") }
}
class D: C {
override func foo() { print("D") }
}
class E: D {
override func foo() { print("E") }
}
struct X {}
extension Int: Fooable {
func foo() { print("Int") }
}
func fooify<T>(x: T) {
if let foo = x as? Fooable {
foo.foo()
} else {
print("not fooable")
}
}
struct G<T>: Fooable {
func foo() { print("G") }
}
struct H<T> {}
fooify(1) // CHECK: Int
fooify(2) // CHECK-NEXT: Int
fooify(S()) // CHECK-NEXT: S
fooify(S()) // CHECK-NEXT: S
fooify(C()) // CHECK-NEXT: C
fooify(C()) // CHECK-NEXT: C
fooify(D()) // CHECK-NEXT: D
fooify(D()) // CHECK-NEXT: D
fooify(E()) // CHECK-NEXT: E
fooify(E()) // CHECK-NEXT: E
fooify(X()) // CHECK-NEXT: not fooable
fooify(X()) // CHECK-NEXT: not fooable
fooify(G<Int>()) // CHECK-NEXT: G
fooify(G<Float>()) // CHECK-NEXT: G
fooify(G<Int>()) // CHECK-NEXT: G
fooify(H<Int>()) // CHECK-NEXT: not fooable
fooify(H<Float>()) // CHECK-NEXT: not fooable
fooify(H<Int>()) // CHECK-NEXT: not fooable
// TODO: generics w/ dependent witness tables
// Check casts from existentials.
fooify(1 as Any) // CHECK: Int
fooify(2 as Any) // CHECK-NEXT: Int
fooify(S() as Any) // CHECK-NEXT: S
fooify(S() as Any) // CHECK-NEXT: S
fooify(C() as Any) // CHECK-NEXT: C
fooify(C() as Any) // CHECK-NEXT: C
fooify(D() as Any) // CHECK-NEXT: D
fooify(D() as Any) // CHECK-NEXT: D
fooify(E() as Any) // CHECK-NEXT: E
fooify(E() as Any) // CHECK-NEXT: E
fooify(X() as Any) // CHECK-NEXT: not fooable
fooify(X() as Any) // CHECK-NEXT: not fooable
fooify(G<Int>() as Any) // CHECK-NEXT: G
fooify(G<Float>() as Any) // CHECK-NEXT: G
fooify(G<Int>() as Any) // CHECK-NEXT: G
fooify(H<Int>() as Any) // CHECK-NEXT: not fooable
fooify(H<Float>() as Any) // CHECK-NEXT: not fooable
fooify(H<Int>() as Any) // CHECK-NEXT: not fooable
protocol Barrable {
func bar()
}
extension Int: Barrable {
func bar() { print("Int.bar") }
}
let foo: Fooable = 2
if let bar = foo as? Barrable {
bar.bar() // CHECK-NEXT: Int.bar
} else {
print("not barrable")
}
let foo2: Fooable = S()
if let bar2 = foo2 as? Barrable {
bar2.bar()
} else {
print("not barrable") // CHECK-NEXT: not barrable
}
protocol Runcible: class {
func runce()
}
extension C: Runcible {
func runce() { print("C") }
}
let c1: AnyObject = C()
let c2: Any = C()
if let fruncible = c1 as? protocol<Fooable, Runcible> {
fruncible.foo() // CHECK-NEXT: C
fruncible.runce() // CHECK-NEXT: C
} else {
print("not fooable and runcible")
}
if let fruncible = c2 as? protocol<Fooable, Runcible> {
fruncible.foo() // CHECK-NEXT: C
fruncible.runce() // CHECK-NEXT: C
} else {
print("not fooable and runcible")
}
// Protocol lookup for metatypes.
protocol StaticFoo {
static func foo() -> String
}
class StaticBar {
class func mightHaveFoo() -> String {
if let selfAsFoo = self as? StaticFoo.Type {
return selfAsFoo.foo()
} else {
return "no Foo for you"
}
}
}
class StaticWibble : StaticBar, StaticFoo {
static func foo() -> String { return "StaticWibble.foo" }
}
// CHECK: no Foo for you
print(StaticBar.mightHaveFoo())
// CHECK: StaticWibble.foo
print(StaticWibble.mightHaveFoo())