toverload_issues.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. discard """
  2. output: '''
  3. Version 2 was called.
  4. This has the highest precedence.
  5. This has the second-highest precedence.
  6. This has the lowest precedence.
  7. baseobj ==
  8. true
  9. even better! ==
  10. true
  11. done extraI=0
  12. test 0 complete, loops=0
  13. done extraI=1
  14. test 1.0 complete, loops=1
  15. done extraI=0
  16. done extraI passed 0
  17. test no extra complete, loops=2
  18. 1
  19. '''
  20. """
  21. # issue 4675
  22. import importA # comment this out to make it work
  23. import importB
  24. var x: Foo[float]
  25. var y: Foo[float]
  26. let r = t1(x) + t2(y)
  27. # Bug: https://github.com/nim-lang/Nim/issues/4475
  28. # Fix: https://github.com/nim-lang/Nim/pull/4477
  29. proc test(x: varargs[string], y: int) = discard
  30. test(y = 1)
  31. # bug #2220
  32. when true:
  33. type A[T] = object
  34. type B = A[int]
  35. proc q[X](x: X) =
  36. echo "Version 1 was called."
  37. proc q(x: B) =
  38. echo "Version 2 was called."
  39. q(B()) # This call reported as ambiguous.
  40. # bug #2219
  41. template testPred(a: untyped) =
  42. block:
  43. type A = object of RootObj
  44. type B = object of A
  45. type SomeA = A|A # A hack to make "A" a typeclass.
  46. when a >= 3:
  47. proc p[X: A](x: X) =
  48. echo "This has the highest precedence."
  49. when a == 2:
  50. proc p[X: SomeA](x: X) =
  51. echo "This has the second-highest precedence."
  52. when a >= 1:
  53. proc p[X](x: X) =
  54. echo "This has the lowest precedence."
  55. p(B())
  56. testPred(3)
  57. testPred(2)
  58. testPred(1)
  59. # bug #6526
  60. type
  61. BaseObj = ref object of RootObj
  62. DerivedObj = ref object of BaseObj
  63. OtherDerivate = ref object of BaseObj
  64. proc `==`*[T1, T2: BaseObj](a: T1, b: T2): bool =
  65. echo "baseobj =="
  66. return true
  67. let a = DerivedObj()
  68. let b = DerivedObj()
  69. echo a == b
  70. proc `==`*[T1, T2: OtherDerivate](a: T1, b: T2): bool =
  71. echo "even better! =="
  72. return true
  73. let a2 = OtherDerivate()
  74. let b2 = OtherDerivate()
  75. echo a2 == b2
  76. # bug #2481
  77. import math
  78. template test(loopCount: int, extraI: int, testBody: untyped): typed =
  79. block:
  80. for i in 0..loopCount-1:
  81. testBody
  82. echo "done extraI=", extraI
  83. template test(loopCount: int, extraF: float, testBody: untyped): typed =
  84. block:
  85. test(loopCount, round(extraF).int, testBody)
  86. template test(loopCount: int, testBody: untyped): typed =
  87. block:
  88. test(loopCount, 0, testBody)
  89. echo "done extraI passed 0"
  90. var
  91. loops = 0
  92. test 0, 0:
  93. loops += 1
  94. echo "test 0 complete, loops=", loops
  95. test 1, 1.0:
  96. loops += 1
  97. echo "test 1.0 complete, loops=", loops
  98. when true:
  99. # when true we get the following compile time error:
  100. # b.nim(35, 6) Error: expression 'loops += 1' has no type (or is ambiguous)
  101. loops = 0
  102. test 2:
  103. loops += 1
  104. echo "test no extra complete, loops=", loops
  105. # bug #2229
  106. type
  107. Type1 = object
  108. id: int
  109. Type2 = object
  110. id: int
  111. proc init(self: var Type1, a: int, b: ref Type2) =
  112. echo "1"
  113. proc init(self: var Type2, a: int) =
  114. echo """
  115. Works when this proc commented out
  116. Otherwise error:
  117. test.nim(14, 4) Error: ambiguous call; both test.init(self: var Type1, a: int, b: ref Type2) and test.init(self: var Type1, a: int, b: ref Type2) match for: (Type1, int literal(1), ref Type2)
  118. """
  119. var aa: Type1
  120. init(aa, 1, (
  121. var bb = new(Type2);
  122. bb
  123. ))
  124. # bug #4545
  125. type
  126. SomeObject = object
  127. a: int
  128. AbstractObject = object
  129. objet: ptr SomeObject
  130. proc convert(this: var SomeObject): AbstractObject =
  131. AbstractObject(objet: this.addr)
  132. proc varargProc(args: varargs[AbstractObject, convert]): int =
  133. for arg in args:
  134. result += arg.objet.a
  135. var obj = SomeObject(a: 17)
  136. discard varargProc(obj)
  137. # bug #11239
  138. type MySeq*[T] = object
  139. proc foo(a: seq[int]): string = "foo: seq[int]"
  140. proc foo[T](a: seq[T]): string = "foo: seq[T]"
  141. proc foo(a: MySeq[int]): string = "foo: MySeq[int]"
  142. proc foo[T](a: MySeq[T]): string = "foo: MySeq[T]"
  143. doAssert foo(@[1,2,3]) == "foo: seq[int]"
  144. doAssert foo(@["WER"]) == "foo: seq[T]"
  145. doAssert foo(MySeq[int]()) == "foo: MySeq[int]"
  146. doAssert foo(MySeq[string]()) == "foo: MySeq[T]"