toverload_issues.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. block: # bug #6526
  60. type
  61. BaseObj = ref object of RootObj
  62. DerivedObj = ref object of BaseObj
  63. OtherDerivate = ref object of BaseObj
  64. proc p[T](a: T, b: T): bool =
  65. assert false
  66. proc p[T1, T2: BaseObj](a: T1, b: T2): bool =
  67. echo "baseobj =="
  68. return true
  69. let a = DerivedObj()
  70. let b = DerivedObj()
  71. echo p(a,b)
  72. proc p[T1, T2: OtherDerivate](a: T1, b: T2): bool =
  73. echo "even better! =="
  74. return true
  75. let a2 = OtherDerivate()
  76. let b2 = OtherDerivate()
  77. echo p(a2, b2)
  78. # bug #2481
  79. import math
  80. template test(loopCount: int, extraI: int, testBody: untyped): typed =
  81. block:
  82. for i in 0..loopCount-1:
  83. testBody
  84. echo "done extraI=", extraI
  85. template test(loopCount: int, extraF: float, testBody: untyped): typed =
  86. block:
  87. test(loopCount, round(extraF).int, testBody)
  88. template test(loopCount: int, testBody: untyped): typed =
  89. block:
  90. test(loopCount, 0, testBody)
  91. echo "done extraI passed 0"
  92. var
  93. loops = 0
  94. test 0, 0:
  95. loops += 1
  96. echo "test 0 complete, loops=", loops
  97. test 1, 1.0:
  98. loops += 1
  99. echo "test 1.0 complete, loops=", loops
  100. when true:
  101. # when true we get the following compile time error:
  102. # b.nim(35, 6) Error: expression 'loops += 1' has no type (or is ambiguous)
  103. loops = 0
  104. test 2:
  105. loops += 1
  106. echo "test no extra complete, loops=", loops
  107. # bug #2229
  108. type
  109. Type1 = object
  110. id: int
  111. Type2 = object
  112. id: int
  113. proc init(self: var Type1, a: int, b: ref Type2) =
  114. echo "1"
  115. proc init(self: var Type2, a: int) =
  116. echo """
  117. Works when this proc commented out
  118. Otherwise error:
  119. 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)
  120. """
  121. var aa: Type1
  122. init(aa, 1, (
  123. var bb = new(Type2);
  124. bb
  125. ))
  126. # bug #4545
  127. type
  128. SomeObject = object
  129. a: int
  130. AbstractObject = object
  131. objet: ptr SomeObject
  132. proc convert(this: var SomeObject): AbstractObject =
  133. AbstractObject(objet: this.addr)
  134. proc varargProc(args: varargs[AbstractObject, convert]): int =
  135. for arg in args:
  136. result += arg.objet.a
  137. var obj = SomeObject(a: 17)
  138. discard varargProc(obj)
  139. # bug #11239
  140. type MySeq*[T] = object
  141. proc foo(a: seq[int]): string = "foo: seq[int]"
  142. proc foo[T](a: seq[T]): string = "foo: seq[T]"
  143. proc foo(a: MySeq[int]): string = "foo: MySeq[int]"
  144. proc foo[T](a: MySeq[T]): string = "foo: MySeq[T]"
  145. doAssert foo(@[1,2,3]) == "foo: seq[int]"
  146. doAssert foo(@["WER"]) == "foo: seq[T]"
  147. doAssert foo(MySeq[int]()) == "foo: MySeq[int]"
  148. doAssert foo(MySeq[string]()) == "foo: MySeq[T]"