summary refs log tree commit diff stats
path: root/tests/overload/toverload_various.nim
blob: f913ce3ee3bac5e20b1e341a5866b94204c5fcee (plain) (blame)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
discard """
  output: '''
true012innertrue
m1
tup1
another number: 123
yay
helloa 1 b 2 x @[3, 4, 5] y 6 z 7
yay
12
ref ref T ptr S
dynamic: let
dynamic: var
static: const
static: literal
static: constant folding
static: static string
foo1
1
'''
"""


import strutils, sequtils


block overl2:
  # Test new overloading resolution rules
  proc toverl2(x: int): string = return $x
  proc toverl2(x: bool): string = return $x

  iterator toverl2(x: int): int =
    var res = 0
    while res < x:
      yield res
      inc(res)

  var
    pp: proc (x: bool): string {.nimcall.} = toverl2

  stdout.write(pp(true))

  for x in toverl2(3):
    stdout.write(toverl2(x))

  block:
    proc toverl2(x: int): string = return "inner"
    stdout.write(toverl2(5))
    stdout.write(true)

  stdout.write("\n")
  #OUT true012innertrue



block overl3:
  # Tests more specific generic match:
  proc m[T](x: T) = echo "m2"
  proc m[T](x: var ref T) = echo "m1"
  proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1"
  proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2"

  var
    obj: ref int
    tu: tuple[a: int, b: ref bool]

  m(obj)
  tup(tu)



block toverprc:
  # Test overloading of procs when used as function pointers
  proc parseInt(x: float): int {.noSideEffect.} = discard
  proc parseInt(x: bool): int {.noSideEffect.} = discard
  proc parseInt(x: float32): int {.noSideEffect.} = discard
  proc parseInt(x: int8): int {.noSideEffect.} = discard
  proc parseInt(x: File): int {.noSideEffect.} = discard
  proc parseInt(x: char): int {.noSideEffect.} = discard
  proc parseInt(x: int16): int {.noSideEffect.} = discard

  proc parseInt[T](x: T): int = echo x; 34

  type
    TParseInt = proc (x: string): int {.noSideEffect.}

  var
    q = TParseInt(parseInt)
    p: TParseInt = parseInt

  proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
    result = x("123")

  if false:
    echo "Give a list of numbers (separated by spaces): "
    var x = stdin.readline.split.map(parseInt).max
    echo x, " is the maximum!"
  echo "another number: ", takeParseInt(parseInt)


  type
    TFoo[a,b] = object
      lorem: a
      ipsum: b

  proc bar[a,b](f: TFoo[a,b], x: a) = echo(x, " ", f.lorem, f.ipsum)
  proc bar[a,b](f: TFoo[a,b], x: b) = echo(x, " ", f.lorem, f.ipsum)

  discard parseInt[string]("yay")



block toverwr:
  # Test the overloading resolution in connection with a qualifier
  proc write(t: File, s: string) =
    discard # a nop
  system.write(stdout, "hello")
  #OUT hello



block tparams_after_varargs:
  proc test(a, b: int, x: varargs[int]; y, z: int) =
    echo "a ", a, " b ", b, " x ", @x, " y ", y, " z ", z

  test 1, 2, 3, 4, 5, 6, 7

  # XXX maybe this should also work with ``varargs[untyped]``
  template takesBlockA(a, b: untyped; x: varargs[typed]; blck: untyped): untyped =
    blck
    echo a, b

  takesBlockA 1, 2, "some", 0.90, "random stuff":
    echo "yay"



block tprefer_specialized_generic:
  proc foo[T](x: T) =
    echo "only T"

  proc foo[T](x: ref T) =
    echo "ref T"

  proc foo[T, S](x: ref ref T; y: ptr S) =
    echo "ref ref T ptr S"

  proc foo[T, S](x: ref T; y: ptr S) =
    echo "ref T ptr S"

  proc foo[T](x: ref T; default = 0) =
    echo "ref T; default"

  var x: ref ref int
  var y: ptr ptr int
  foo(x, y)



block tstaticoverload:
  proc foo(s: string) =
    echo "dynamic: ", s

  proc foo(s: static[string]) =
    echo "static: ", s

  let l = "let"
  var v = "var"
  const c = "const"

  type staticString = static[string]

  foo(l)
  foo(v)
  foo(c)
  foo("literal")
  foo("constant" & " " & "folding")
  foo(staticString("static string"))

# bug #8568 (2)

proc goo(a: int): string = "int"
proc goo(a: static[int]): string = "static int"
proc goo(a: var int): string = "var int"
proc goo[T: int](a: T): string = "T: int"
#proc goo[T](a: T): string = "nur T"

const tmp1 = 1
let tmp2 = 1
var tmp3 = 1

doAssert goo(1) == "static int"
doAssert goo(tmp1) == "static int"
doAssert goo(tmp2) == "int"
doAssert goo(tmp3) == "var int"

doAssert goo[int](1) == "T: int"

doAssert goo[int](tmp1) == "T: int"
doAssert goo[int](tmp2) == "T: int"
doAssert goo[int](tmp3) == "T: int"

# bug #6076

type A[T] = object

proc regr(a: A[void]) = echo "foo1"
proc regr[T](a: A[T]) = doAssert(false)

regr(A[void]())


type Foo[T] = object

proc regr[T](p: Foo[T]): seq[T] =
  discard

proc regr(p: Foo[void]): seq[int] =
  discard


discard regr(Foo[int]())
discard regr(Foo[void]())


type
  Sha2Context*[bits: static[int],
               bsize: static[int],
               T: uint32|uint64] = object
    count: array[2, T]
    state: array[8, T]
    buffer: array[bsize, byte]

  sha224* = Sha2Context[224, 64, uint32]
  sha256* = Sha2Context[256, 64, uint32]
  sha384* = Sha2Context[384, 128, uint64]
  sha512* = Sha2Context[512, 128, uint64]
  sha512_224* = Sha2Context[224, 128, uint64]
  sha512_256* = Sha2Context[256, 128, uint64]

type
  RipemdContext*[bits: static[int]] = object
    count: array[2, uint32]
    state: array[bits div 32, uint32]
    buffer: array[64, byte]

  ripemd128* = RipemdContext[128]
  ripemd160* = RipemdContext[160]
  ripemd256* = RipemdContext[256]
  ripemd320* = RipemdContext[320]

const
  MaxHmacBlockSize = 256

type
  HMAC*[HashType] = object
    mdctx: HashType
    opadctx: HashType

template sizeBlock*(h: HMAC[Sha2Context]): uint = 1u
template sizeBlock*(h: HMAC[RipemdContext]): uint = 0u

proc init*[T](hmctx: HMAC[T], key: ptr byte, ulen: uint) =
  const sizeBlock = hmctx.sizeBlock
  echo sizeBlock

proc hmac*[A, B](HashType: typedesc, key: openarray[A],
                 data: openarray[B]) =
  var ctx: HMAC[HashType]
  ctx.init(nil, 0)

sha256.hmac("", "")



# nested generic types
block:
  type
    Foo[T] = object
      f: T
    Bar[T] = object
      b: T
    Baz[T] = object
      z: T
    FooBar[T] = Foo[Bar[T]]
    FooBarBaz[T] = FooBar[Baz[T]]
    #Int = int
    Int = SomeInteger
    FooBarBazInt = FooBarBaz[Int]
    FooBarBazX = FooBarBaz[int]

  proc p00(x: Foo): auto = x.f
  proc p01[T](x: Foo[T]): auto = x.f
  proc p02[T:Foo](x: T): auto = x.f

  proc p10(x: FooBar): auto = x.f
  proc p11[T](x: FooBar[T]): auto = x.f
  proc p12[T:FooBar](x: T): auto = x.f
  proc p13(x: Foo[Bar]): auto = x.f
  proc p14[T](x: Foo[Bar[T]]): auto = x.f
  proc p15[T:Bar](x: Foo[T]): auto = x.f
  proc p16[T:Foo[Bar]](x: T): auto = x.f

  proc p20(x: FooBarBaz): auto = x.f
  proc p21[T](x: FooBarBaz[T]): auto = x.f
  proc p22[T:FooBarBaz](x: T): auto = x.f
  proc p23(x: FooBar[Baz]): auto = x.f
  proc p24[T](x: FooBar[Baz[T]]): auto = x.f
  proc p25[T:Baz](x: FooBar[T]): auto = x.f
  proc p26[T:FooBar[Baz]](x: T): auto = x.f
  proc p27(x: Foo[Bar[Baz]]): auto = x.f
  proc p28[T](x: Foo[Bar[Baz[T]]]): auto = x.f
  proc p29[T:Baz](x: Foo[Bar[T]]): auto = x.f
  proc p2A[T:Bar[Baz]](x: Foo[T]): auto = x.f
  proc p2B[T:Foo[Bar[Baz]]](x: T): auto = x.f

  proc p30(x: FooBarBazInt): auto = x.f
  proc p31[T:FooBarBazInt](x: T): auto = x.f
  proc p32(x: FooBarBaz[Int]): auto = x.f
  proc p33[T:Int](x: FooBarBaz[T]): auto = x.f
  proc p34[T:FooBarBaz[Int]](x: T): auto = x.f
  proc p35(x: FooBar[Baz[Int]]): auto = x.f
  proc p36[T:Int](x: FooBar[Baz[T]]): auto = x.f
  proc p37[T:Baz[Int]](x: FooBar[T]): auto = x.f
  proc p38[T:FooBar[Baz[Int]]](x: T): auto = x.f
  proc p39(x: Foo[Bar[Baz[Int]]]): auto = x.f
  proc p3A[T:Int](x: Foo[Bar[Baz[T]]]): auto = x.f
  proc p3B[T:Baz[Int]](x: Foo[Bar[T]]): auto = x.f
  proc p3C[T:Bar[Baz[Int]]](x: Foo[T]): auto = x.f
  proc p3D[T:Foo[Bar[Baz[Int]]]](x: T): auto = x.f

  template test(x: typed) =
    let t00 = p00(x)
    let t01 = p01(x)
    let t02 = p02(x)
    let t10 = p10(x)
    let t11 = p11(x)
    let t12 = p12(x)
    #let t13 = p13(x)
    let t14 = p14(x)
    #let t15 = p15(x)
    #let t16 = p16(x)
    let t20 = p20(x)
    let t21 = p21(x)
    let t22 = p22(x)
    #let t23 = p23(x)
    let t24 = p24(x)
    #let t25 = p25(x)
    #let t26 = p26(x)
    #let t27 = p27(x)
    let t28 = p28(x)
    #let t29 = p29(x)
    #let t2A = p2A(x)
    #let t2B = p2B(x)
    let t30 = p30(x)
    let t31 = p31(x)
    let t32 = p32(x)
    let t33 = p33(x)
    let t34 = p34(x)
    let t35 = p35(x)
    let t36 = p36(x)
    let t37 = p37(x)
    let t38 = p38(x)
    let t39 = p39(x)
    let t3A = p3A(x)
    let t3B = p3B(x)
    let t3C = p3C(x)
    let t3D = p3D(x)

  var a: Foo[Bar[Baz[int]]]
  test(a)
  var b: FooBar[Baz[int]]
  test(b)
  var c: FooBarBaz[int]
  test(c)
  var d: FooBarBazX
  test(d)


# overloading on tuples with generic alias
block:
  type
    Foo[F,T] = object
      exArgs: T
    FooUn[F,T] = Foo[F,tuple[a:T]]
    FooBi[F,T1,T2] = Foo[F,tuple[a:T1,b:T2]]

  proc foo1[F,T](x: Foo[F,tuple[a:T]]): int = 1
  proc foo1[F,T1,T2](x: Foo[F,tuple[a:T1,b:T2]]): int = 2
  proc foo2[F,T](x: FooUn[F,T]): int = 1
  proc foo2[F,T1,T2](x: FooBi[F,T1,T2]):int = 2

  template bar1[F,T](x: Foo[F,tuple[a:T]]): int = 1
  template bar1[F,T1,T2](x: Foo[F,tuple[a:T1,b:T2]]): int = 2
  template bar2[F,T](x: FooUn[F,T]): int = 1
  template bar2[F,T1,T2](x: FooBi[F,T1,T2]): int = 2

  proc test(x: any, n: int) =
    doAssert(foo1(x) == n)
    doAssert(foo2(x) == n)
    doAssert(bar1(x) == n)
    doAssert(bar2(x) == n)

  var a: Foo[int, tuple[a:int]]
  test(a, 1)
  var b: FooUn[int, int]
  test(b, 1)
  var c: Foo[int, tuple[a:int,b:int]]
  test(c, 2)
  var d: FooBi[int, int, int]
  test(d, 2)


# inheritance and generics
block:
  type
    Foo[T] = object of RootObj
      x: T
    Bar[T] = object of Foo[T]
      y: T
    Baz[T] = object of Bar[T]
      z: T

  template t0(x: Foo[int]): int = 0
  template t0(x: Bar[int]): int = 1
  template t0(x: Foo[bool or int]): int = 10
  template t0(x: Bar[bool or int]): int = 11
  #template t0[T:bool or int](x: Bar[T]): int = 11
  template t0[T](x: Foo[T]): int = 20
  template t0[T](x: Bar[T]): int = 21
  proc p0(x: Foo[int]): int = 0
  proc p0(x: Bar[int]): int = 1
  #proc p0(x: Foo[bool or int]): int = 10
  #proc p0(x: Bar[bool or int]): int = 11
  proc p0[T](x: Foo[T]): int = 20
  proc p0[T](x: Bar[T]): int = 21

  var a: Foo[int]
  var b: Bar[int]
  var c: Baz[int]
  var d: Foo[bool]
  var e: Bar[bool]
  var f: Baz[bool]
  var g: Foo[float]
  var h: Bar[float]
  var i: Baz[float]
  doAssert(t0(a) == 0)
  doAssert(t0(b) == 1)
  doAssert(t0(c) == 1)
  doAssert(t0(d) == 10)
  doAssert(t0(e) == 11)
  doAssert(t0(f) == 11)
  doAssert(t0(g) == 20)
  doAssert(t0(h) == 21)
  #doAssert(t0(i) == 21)
  doAssert(p0(a) == 0)
  doAssert(p0(b) == 1)
  doAssert(p0(c) == 1)
  #doAssert(p0(d) == 10)
  #doAssert(p0(e) == 11)
  #doAssert(p0(f) == 11)
  doAssert(p0(g) == 20)
  doAssert(p0(h) == 21)
  doAssert(p0(i) == 21)

  #type
  #  f0 = proc(x:Foo)


block:
  type
    TilesetCT[n: static[int]] = distinct int
    TilesetRT = int
    Tileset = TilesetCT | TilesetRT

  func prepareTileset(tileset: var Tileset) = discard

  func prepareTileset(tileset: Tileset): Tileset =
    result = tileset
    result.prepareTileset

  var parsedTileset: TilesetRT
  prepareTileset(parsedTileset)


block:
  proc p1[T,U: SomeInteger|SomeFloat](x: T, y: U): int|float =
    when T is SomeInteger and U is SomeInteger:
      result = int(x) + int(y)
    else:
      result = float(x) + float(y)
  doAssert(p1(1,2) == 3)
  doAssert(p1(1.0,2) == 3.0)
  doAssert(p1(1,2.0) == 3.0)
  doAssert(p1(1.0,2.0) == 3.0)

  type Foo[T,U] = U
  template F[T,U](t: typedesc[T], x: U): untyped = Foo[T,U](x)
  proc p2[T; U,V:Foo[T,SomeNumber]](x: U, y: V): T =
    T(x) + T(y)
  #proc p2[T; U:Foo[T,SomeNumber], V:Foo[not T,SomeNumber]](x: U, y: V): T =
  #  T(x) + T(y)
  doAssert(p2(F(int,1),F(int,2)) == 3)
  doAssert(p2(F(float,1),F(float,2)) == 3.0)
  doAssert(p2(F(float,1),F(float,2.0)) == 3.0)
  doAssert(p2(F(float,1.0),F(float,2)) == 3.0)
  doAssert(p2(F(float,1.0),F(float,2.0)) == 3.0)
  #doAssert(p2(F(float,1),F(int,2.0)) == 3.0)