-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathprotocols.swift
246 lines (205 loc) · 9.16 KB
/
protocols.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
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
245
246
// RUN: %target-typecheck-verify-swift
protocol MyPrintable {
func print()
}
protocol Titled {
var title : String { get set }
}
struct IsPrintable1 : FormattedPrintable, Titled, Document {
var title = ""
func print() {}
func print(_: TestFormat) {}
}
// Printability is below
struct IsPrintable2 { }
struct IsNotPrintable1 { }
struct IsNotPrintable2 {
func print(_: Int) -> Int {}
}
struct Book : Titled {
var title : String
}
struct Lackey : Titled {
var title : String {
get {}
set {}
}
}
struct Number {
var title : Int
}
func testPrintableCoercion(_ ip1: IsPrintable1,
ip2: IsPrintable2,
inp1: IsNotPrintable1,
inp2: IsNotPrintable2,
op: OtherPrintable) {
var p : MyPrintable = ip1 // okay
let _: MyPrintable & Titled = Book(title: "")
// expected-error@-1 {{value of type 'Book' does not conform to specified type 'MyPrintable'}}
p = ip1 // okay
p = ip2 // okay
p = inp1 // expected-error{{cannot assign value of type 'IsNotPrintable1' to type 'any MyPrintable'}}
let _: MyPrintable = inp1
// expected-error@-1 {{value of type 'IsNotPrintable1' does not conform to specified type 'MyPrintable'}}
p = inp2 // expected-error{{cannot assign value of type 'IsNotPrintable2' to type 'any MyPrintable'}}
p = op // expected-error{{value of type 'any OtherPrintable' does not conform to 'MyPrintable' in assignment}}
_ = p
}
func testTitledCoercion(_ ip1: IsPrintable1, book: Book, lackey: Lackey,
number: Number, ip2: IsPrintable2) {
var t : Titled = ip1 // okay
t = ip1
t = book
t = lackey
t = number // expected-error{{cannot assign value of type 'Number' to type 'any Titled'}}
t = ip2 // expected-error{{cannot assign value of type 'IsPrintable2' to type 'any Titled'}}
_ = t
}
extension IsPrintable2 : MyPrintable {
func print() {}
}
protocol OtherPrintable {
func print()
}
struct TestFormat {}
protocol FormattedPrintable : MyPrintable {
func print(_: TestFormat)
}
struct NotFormattedPrintable1 {
func print(_: TestFormat) { }
}
func testFormattedPrintableCoercion(_ ip1: IsPrintable1,
ip2: IsPrintable2,
fp: inout FormattedPrintable,
p: inout MyPrintable,
op: inout OtherPrintable,
nfp1: NotFormattedPrintable1) {
fp = ip1
fp = ip2 // expected-error{{cannot assign value of type 'IsPrintable2' to type 'any FormattedPrintable'}}
fp = nfp1 // expected-error{{cannot assign value of type 'NotFormattedPrintable1' to type 'any FormattedPrintable'}}
p = fp
op = fp // expected-error{{value of type 'any FormattedPrintable' does not conform to 'OtherPrintable' in assignment}}
fp = op // expected-error{{value of type 'any OtherPrintable' does not conform to 'FormattedPrintable' in assignment}}
}
protocol Document : Titled, MyPrintable {
}
func testMethodsAndVars(_ fp: FormattedPrintable, f: TestFormat, doc: inout Document) {
fp.print(f)
fp.print()
doc.title = "Gone with the Wind"
doc.print()
}
func testDocumentCoercion(_ doc: inout Document, ip1: IsPrintable1, l: Lackey) {
doc = ip1
doc = l // expected-error{{cannot assign value of type 'Lackey' to type 'any Document'}}
}
// Check coercion of references.
func refCoercion(_ p: inout MyPrintable) { }
var p : MyPrintable = IsPrintable1()
var fp : FormattedPrintable = IsPrintable1()
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{10-28=any MyPrintable}}
var ip1 : IsPrintable1
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'IsPrintable1'}} {{11-23=any MyPrintable}}
refCoercion(&p)
refCoercion(&fp)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&ip1)
// expected-error@-1{{inout argument could be set to a value with a type other than 'IsPrintable1'; use a value declared as type 'any MyPrintable' instead}}
do {
var fp_2 = fp
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{11-11=: any MyPrintable}}
var ip1_2 = ip1
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'IsPrintable1'}} {{12-12=: any MyPrintable}}
refCoercion(&fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&ip1_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'IsPrintable1'; use a value declared as type 'any MyPrintable' instead}}
}
do {
var fp_2 : FormattedPrintable = fp, ip1_2 = ip1
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{14-32=any MyPrintable}}
// expected-note@-2{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'IsPrintable1'}} {{44-44=: any MyPrintable}}
refCoercion(&fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&ip1_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'IsPrintable1'; use a value declared as type 'any MyPrintable' instead}}
}
do {
var fp_2, fp_3 : FormattedPrintable
// expected-note@-1{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{20-38=any MyPrintable}}
// expected-note@-2{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{20-38=any MyPrintable}}
fp_2 = fp
fp_3 = fp
refCoercion(&fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&fp_3)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
}
do {
func wrapRefCoercion1(fp_2: inout FormattedPrintable,
ip1_2: inout IsPrintable1) {
// expected-note@-2{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'any FormattedPrintable'}} {{31-55=any MyPrintable}}
// expected-note@-2{{change variable type to 'any MyPrintable' if it doesn't need to be declared as 'IsPrintable1'}} {{32-50=any MyPrintable}}
refCoercion(&fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&ip1_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'IsPrintable1'; use a value declared as type 'any MyPrintable' instead}}
}
}
do {
// Make sure we don't add the fix-it for tuples:
var (fp_2, ip1_2) = (fp, ip1)
refCoercion(&fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
refCoercion(&ip1_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'IsPrintable1'; use a value declared as type 'any MyPrintable' instead}}
}
do {
// Make sure we don't add the fix-it for vars in different scopes:
enum ParentScope { static var fp_2 = fp }
refCoercion(&ParentScope.fp_2)
// expected-error@-1{{inout argument could be set to a value with a type other than 'any FormattedPrintable'; use a value declared as type 'any MyPrintable' instead}}
}
protocol IntSubscriptable {
subscript(i: Int) -> Int { get }
}
struct IsIntSubscriptable : IntSubscriptable {
subscript(i: Int) -> Int { get {} set {} }
}
struct IsDoubleSubscriptable {
subscript(d: Double) -> Int { get {} set {} }
}
struct IsIntToStringSubscriptable {
subscript(i: Int) -> String { get {} set {} }
}
func testIntSubscripting(i_s: inout IntSubscriptable,
iis: IsIntSubscriptable,
ids: IsDoubleSubscriptable,
iiss: IsIntToStringSubscriptable) {
var x = i_s[17]
i_s[5] = 7 // expected-error{{cannot assign through subscript: subscript is get-only}}
i_s = iis
i_s = ids // expected-error{{cannot assign value of type 'IsDoubleSubscriptable' to type 'any IntSubscriptable'}}
i_s = iiss // expected-error{{cannot assign value of type 'IsIntToStringSubscriptable' to type 'any IntSubscriptable'}}
}
protocol MyREPLPrintable {
func myReplPrint()
}
extension Int : MyREPLPrintable {
func myReplPrint() {}
}
extension String : MyREPLPrintable {
func myReplPrint() {}
}
func doREPLPrint(_ p: MyREPLPrintable) {
p.myReplPrint()
}
func testREPLPrintable() {
let i : Int = 1
_ = i as MyREPLPrintable
doREPLPrint(i)
doREPLPrint(1)
doREPLPrint("foo")
}
// Bool coercion
if true as Bool {}