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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
discard """
targets: "cpp"
output: '''
cat
cat
dog
dog
cat
cat
dog
dog X
cat
cat
dog
dog
dog
dog
dog 1
dog 2
'''
"""
template accept(x) =
static: assert(compiles(x))
template reject(x) =
static: assert(not compiles(x))
import macros
macro skipElse(n: untyped): untyped = n[0]
template acceptWithCovariance(x, otherwise): untyped =
when nimEnableCovariance:
x
else:
reject(x)
skipElse(otherwise)
type
Animal = object of RootObj
x: string
Dog = object of Animal
y: int
Cat = object of Animal
z: int
AnimalRef = ref Animal
AnimalPtr = ptr Animal
RefAlias[T] = ref T
var dog = new(Dog)
dog.x = "dog"
var cat = new(Cat)
cat.x = "cat"
proc makeDerivedRef(x: string): ref Dog =
new(result)
result.x = x
proc makeDerived(x: string): Dog =
result.x = x
var covariantSeq = @[makeDerivedRef("dog 1"), makeDerivedRef("dog 2")]
var nonCovariantSeq = @[makeDerived("dog 1"), makeDerived("dog 2")]
var covariantArr = [makeDerivedRef("dog 1"), makeDerivedRef("dog 2")]
var nonCovariantArr = [makeDerived("dog 1"), makeDerived("dog 2")]
proc wantsCovariantSeq1(s: seq[ref Animal]) =
for a in s: echo a.x
proc wantsCovariantSeq2(s: seq[AnimalRef]) =
for a in s: echo a.x
proc wantsCovariantSeq3(s: seq[RefAlias[Animal]]) =
for a in s: echo a.x
proc wantsCovariantOpenArray(s: openarray[ref Animal]) =
for a in s: echo a.x
proc modifiesCovariantOpenArray(s: var openarray[ref Animal]) =
for a in s: echo a.x
proc modifiesDerivedOpenArray(s: var openarray[ref Dog]) =
for a in s: echo a.x
proc wantsNonCovariantOpenArray(s: openarray[Animal]) =
for a in s: echo a.x
proc wantsCovariantArray(s: array[2, ref Animal]) =
for a in s: echo a.x
proc wantsNonCovariantSeq(s: seq[Animal]) =
for a in s: echo a.x
proc wantsNonCovariantArray(s: array[2, Animal]) =
for a in s: echo a.x
proc modifiesCovariantSeq(s: var seq[ref Animal]) =
for a in s: echo a.x
proc modifiesCovariantArray(s: var array[2, ref Animal]) =
for a in s: echo a.x
proc modifiesCovariantSeq(s: ptr seq[ref Animal]) =
for a in s[]: echo a.x
proc modifiesCovariantArray(s: ptr array[2, ref Animal]) =
for a in s[]: echo a.x
proc modifiesDerivedSeq(s: var seq[ref Dog]) =
for a in s: echo a.x
proc modifiesDerivedArray(s: var array[2, ref Dog]) =
for a in s: echo a.x
proc modifiesDerivedSeq(s: ptr seq[ref Dog]) =
for a in s[]: echo a.x
proc modifiesDerivedArray(s: ptr array[2, ref Dog]) =
for a in s[]: echo a.x
accept:
wantsCovariantArray([AnimalRef(dog), AnimalRef(dog)])
wantsCovariantArray([AnimalRef(cat), AnimalRef(dog)])
wantsCovariantArray([AnimalRef(cat), dog])
# there is a special rule that detects the base
# type of polymorphic arrays
wantsCovariantArray([cat, dog])
acceptWithCovariance:
wantsCovariantArray([cat, cat])
else:
echo "cat"
echo "cat"
var animalRefArray: array[2, ref Animal]
accept:
animalRefArray = [AnimalRef(dog), AnimalRef(dog)]
animalRefArray = [AnimalRef(cat), dog]
acceptWithCovariance:
animalRefArray = [dog, dog]
wantsCovariantArray animalRefArray
else:
echo "dog"
echo "dog"
accept:
var animal: AnimalRef = dog
animal = cat
var vdog: Dog
vdog.x = "dog value"
var vcat: Cat
vcat.x = "cat value"
reject:
vcat = vdog
# XXX: The next two cases seem inconsistent, perhaps we should change the rules
accept:
# truncating copies are allowed
var vanimal: Animal = vdog
vanimal = vdog
reject:
# truncating copies are not allowed with arrays
var vanimalArray: array[2, Animal]
var vdogArray = [vdog, vdog]
vanimalArray = vdogArray
accept:
# a more explicit version of a truncating copy that
# should probably always remain allowed
var vnextnimal: Animal = Animal(vdog)
proc wantsRefSeq(x: seq[AnimalRef]) = discard
accept:
wantsCovariantSeq1(@[AnimalRef(dog), AnimalRef(dog)])
wantsCovariantSeq1(@[AnimalRef(cat), AnimalRef(dog)])
wantsCovariantSeq1(@[AnimalRef(cat), dog])
wantsCovariantSeq1(@[cat, dog])
wantsCovariantSeq2(@[AnimalRef(dog), AnimalRef(dog)])
wantsCovariantSeq2(@[AnimalRef(cat), AnimalRef(dog)])
wantsCovariantSeq2(@[AnimalRef(cat), dog])
wantsCovariantSeq2(@[cat, dog])
wantsCovariantSeq3(@[AnimalRef(dog), AnimalRef(dog)])
wantsCovariantSeq3(@[AnimalRef(cat), AnimalRef(dog)])
wantsCovariantSeq3(@[AnimalRef(cat), dog])
wantsCovariantSeq3(@[cat, dog])
wantsCovariantOpenArray([cat, dog])
acceptWithCovariance:
wantsCovariantSeq1(@[cat, cat])
wantsCovariantSeq2(@[dog, makeDerivedRef("dog X")])
# XXX: wantsCovariantSeq3(@[cat, cat])
wantsCovariantOpenArray(@[cat, cat])
wantsCovariantOpenArray([dog, dog])
else:
echo "cat"
echo "cat"
echo "dog"
echo "dog X"
echo "cat"
echo "cat"
echo "dog"
echo "dog"
var dogRefs = @[dog, dog]
var dogRefsArray = [dog, dog]
var animalRefs = @[dog, cat]
accept:
modifiesDerivedArray(dogRefsArray)
modifiesDerivedSeq(dogRefs)
reject modifiesCovariantSeqd(ogRefs)
reject modifiesCovariantSeq(addr(dogRefs))
reject modifiesCovariantSeq(dogRefs.addr)
reject modifiesCovariantArray([dog, dog])
reject modifiesCovariantArray(dogRefsArray)
reject modifiesCovariantArray(addr(dogRefsArray))
reject modifiesCovariantArray(dogRefsArray.addr)
var dogValues = @[vdog, vdog]
var dogValuesArray = [vdog, vdog]
when false:
var animalValues = @[Animal(vdog), Animal(vcat)]
var animalValuesArray = [Animal(vdog), Animal(vcat)]
wantsNonCovariantSeq animalValues
wantsNonCovariantArray animalValuesArray
reject wantsNonCovariantSeq(dogRefs)
reject modifiesCovariantOpenArray(dogRefs)
reject wantsNonCovariantArray(dogRefsArray)
reject wantsNonCovariantSeq(dogValues)
reject wantsNonCovariantArray(dogValuesArray)
reject modifiesValueArray()
modifiesDerivedOpenArray dogRefs
reject modifiesDerivedOpenArray(dogValues)
reject modifiesDerivedOpenArray(animalRefs)
reject wantsNonCovariantOpenArray(animalRefs)
reject wantsNonCovariantOpenArray(dogRefs)
reject wantsNonCovariantOpenArray(dogValues)
var animalRefSeq: seq[ref Animal]
accept:
animalRefSeq = @[AnimalRef(dog), AnimalRef(dog)]
animalRefSeq = @[AnimalRef(cat), dog]
acceptWithCovariance:
animalRefSeq = @[makeDerivedRef("dog 1"), makeDerivedRef("dog 2")]
wantsCovariantSeq1(animalRefSeq)
else:
echo "dog 1"
echo "dog 2"
var pdog: ptr Dog
var pcat: ptr Cat
proc wantsPointer(x: ptr Animal) =
discard
accept:
wantsPointer pdog
wantsPointer pcat
# covariance should be disabled when var is involved
proc wantsVarPointer1(x: var ptr Animal) =
discard
proc wantsVarPointer2(x: var AnimalPtr) =
discard
reject wantsVarPointer1(pdog)
reject wantsVarPointer2(pcat)
# covariance may be allowed for certain extern types
{.emit: """/*TYPESECTION*/
template <class T> struct FN { typedef void (*type)(T); };
template <class T> struct ARR { typedef T DataType[2]; DataType data; };
""".}
type
MyPtr[out T] {.importcpp: "'0 *"} = object
MySeq[out T] {.importcpp: "ARR<'0>", nodecl} = object
data: array[2, T]
MyAction[in T] {.importcpp: "FN<'0>::type"} = object
var
cAnimal: MyPtr[Animal]
cDog: MyPtr[Dog]
cCat: MyPtr[Cat]
cAnimalFn: MyAction[Animal]
cCatFn: MyAction[Cat]
cDogFn: MyAction[Dog]
cRefAnimalFn: MyAction[ref Animal]
cRefCatFn: MyAction[ref Cat]
cRefDogFn: MyAction[ref Dog]
accept:
cAnimal = cDog
cAnimal = cCat
cDogFn = cAnimalFn
cCatFn = cAnimalFn
cRefDogFn = cRefAnimalFn
cRefCatFn = cRefAnimalFn
reject: cDogFn = cRefAnimalFn
reject: cCatFn = cRefAnimalFn
reject: cCat = cDog
reject: cAnimalFn = cDogFn
reject: cAnimalFn = cCatFn
reject: cRefAnimalFn = cRefDogFn
reject: cRefAnimalFn = cRefCatFn
reject: cRefAnimalFn = cDogFn
var
ptrPtrDog: ptr ptr Dog
ptrPtrAnimal: ptr ptr Animal
reject: ptrPtrDog = ptrPtrAnimal
# Try to break the rules by introducing some tricky
# double indirection types:
var
cPtrRefAnimal: MyPtr[ref Animal]
cPtrRefDog: MyPtr[ref Dog]
cPtrAliasRefAnimal: MyPtr[RefAlias[Animal]]
cPtrAliasRefDog: MyPtr[RefAlias[Dog]]
cDoublePtrAnimal: MyPtr[MyPtr[Animal]]
cDoublePtrDog: MyPtr[MyPtr[Dog]]
reject: cPtrRefAnimal = cPtrRefDog
reject: cDoublePtrAnimal = cDoublePtrDog
reject: cRefAliasPtrAnimal = cRefAliasPtrDog
reject: cPtrRefAnimal = cRefAliasPtrDog
reject: cPtrAliasRefAnimal = cPtrRefDog
var
# Array and Sequence types are covariant only
# when instantiated with ref or ptr types:
cAnimals: MySeq[ref Animal]
cDogs: MySeq[ref Dog]
# "User-defined" pointer types should be OK too:
cAnimalPtrSeq: MySeq[MyPtr[Animal]]
cDogPtrSeq: MySeq[MyPtr[Dog]]
# Value types shouldn't work:
cAnimalValues: MySeq[Animal]
cDogValues: MySeq[Dog]
# Double pointer types should not work either:
cAnimalRefPtrSeq: MySeq[ref MyPtr[Animal]]
cDogRefPtrSeq: MySeq[ref MyPtr[Dog]]
cAnimalPtrPtrSeq: MySeq[ptr ptr Animal]
cDogPtrPtrSeq: MySeq[ptr ptr Dog]
accept:
cAnimals = cDogs
cAnimalPtrSeq = cDogPtrSeq
reject: cAnimalValues = cDogValues
reject: cAnimalRefPtrSeq = cDogRefPtrSeq
reject: cAnimalPtrPtrSeq = cDogPtrPtrSeq
proc wantsAnimalSeq(x: MySeq[Animal]) = discard
proc wantsAnimalRefSeq(x: MySeq[ref Animal]) = discard
proc modifiesAnimalRefSeq(x: var MySeq[ref Animal]) = discard
proc usesAddressOfAnimalRefSeq(x: ptr MySeq[ref Animal]) = discard
accept wantsAnimalSeq(cAnimalValues)
reject wantsAnimalSeq(cDogValues)
reject wantsAnimalSeq(cAnimals)
reject wantsAnimalRefSeq(cAnimalValues)
reject wantsAnimalRefSeq(cDogValues)
accept wantsAnimalRefSeq(cAnimals)
accept wantsAnimalRefSeq(cDogs)
reject modifiesAnimalRefSeq(cAnimalValues)
reject modifiesAnimalRefSeq(cDogValues)
accept modifiesAnimalRefSeq(cAnimals)
reject modifiesAnimalRefSeq(cDogs)
reject usesAddressOfAnimalRefSeq(addr cAnimalValues)
reject usesAddressOfAnimalRefSeq(addr cDogValues)
accept usesAddressOfAnimalRefSeq(addr cAnimals)
reject usesAddressOfAnimalRefSeq(addr cDogs)
|