t4799.nim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. discard """
  2. output: "OK"
  3. """
  4. type
  5. GRBase[T] = ref object of RootObj
  6. val: T
  7. GRC[T] = ref object of GRBase[T]
  8. GRD[T] = ref object of GRBase[T]
  9. proc testGR[T](x: varargs[GRBase[T]]): string =
  10. result = ""
  11. for c in x:
  12. result.add $c.val
  13. block test_t4799_1:
  14. var rgv = GRBase[int](val: 3)
  15. var rgc = GRC[int](val: 4)
  16. var rgb = GRD[int](val: 2)
  17. doAssert(testGR(rgb, rgc, rgv) == "243")
  18. doAssert(testGR(rgc, rgv, rgb) == "432")
  19. doAssert(testGR(rgv, rgb, rgc) == "324")
  20. doAssert(testGR([rgb, rgc, rgv]) == "243")
  21. doAssert(testGR([rgc, rgv, rgb]) == "432")
  22. doAssert(testGR([rgv, rgb, rgc]) == "324")
  23. type
  24. PRBase[T] = object of RootObj
  25. val: T
  26. PRC[T] = object of PRBase[T]
  27. PRD[T] = object of PRBase[T]
  28. proc testPR[T](x: varargs[ptr PRBase[T]]): string =
  29. result = ""
  30. for c in x:
  31. result.add $c.val
  32. block test_t4799_2:
  33. var pgv = PRBase[int](val: 3)
  34. var pgc = PRC[int](val: 4)
  35. var pgb = PRD[int](val: 2)
  36. doAssert(testPR(pgb.addr, pgc.addr, pgv.addr) == "243")
  37. doAssert(testPR(pgc.addr, pgv.addr, pgb.addr) == "432")
  38. doAssert(testPR(pgv.addr, pgb.addr, pgc.addr) == "324")
  39. doAssert(testPR([pgb.addr, pgc.addr, pgv.addr]) == "243")
  40. doAssert(testPR([pgc.addr, pgv.addr, pgb.addr]) == "432")
  41. doAssert(testPR([pgv.addr, pgb.addr, pgc.addr]) == "324")
  42. type
  43. RBase = ref object of RootObj
  44. val: int
  45. RC = ref object of RBase
  46. RD = ref object of RBase
  47. proc testR(x: varargs[RBase]): string =
  48. result = ""
  49. for c in x:
  50. result.add $c.val
  51. block test_t4799_3:
  52. var rv = RBase(val: 3)
  53. var rc = RC(val: 4)
  54. var rb = RD(val: 2)
  55. doAssert(testR(rb, rc, rv) == "243")
  56. doAssert(testR(rc, rv, rb) == "432")
  57. doAssert(testR(rv, rb, rc) == "324")
  58. doAssert(testR([rb, rc, rv]) == "243")
  59. doAssert(testR([rc, rv, rb]) == "432")
  60. doAssert(testR([rv, rb, rc]) == "324")
  61. type
  62. PBase = object of RootObj
  63. val: int
  64. PC = object of PBase
  65. PD = object of PBase
  66. proc testP(x: varargs[ptr PBase]): string =
  67. result = ""
  68. for c in x:
  69. result.add $c.val
  70. block test_t4799_4:
  71. var pv = PBase(val: 3)
  72. var pc = PC(val: 4)
  73. var pb = PD(val: 2)
  74. doAssert(testP(pb.addr, pc.addr, pv.addr) == "243")
  75. doAssert(testP(pc.addr, pv.addr, pb.addr) == "432")
  76. doAssert(testP(pv.addr, pb.addr, pc.addr) == "324")
  77. doAssert(testP([pb.addr, pc.addr, pv.addr]) == "243")
  78. doAssert(testP([pc.addr, pv.addr, pb.addr]) == "432")
  79. doAssert(testP([pv.addr, pb.addr, pc.addr]) == "324")
  80. type
  81. PSBase[T, V] = ref object of RootObj
  82. val: T
  83. color: V
  84. PSRC[T] = ref object of PSBase[T, int]
  85. PSRD[T] = ref object of PSBase[T, int]
  86. proc testPS[T, V](x: varargs[PSBase[T, V]]): string =
  87. result = ""
  88. for c in x:
  89. result.add c.val
  90. result.add $c.color
  91. block test_t4799_5:
  92. var a = PSBase[string, int](val: "base", color: 1)
  93. var b = PSRC[string](val: "rc", color: 2)
  94. var c = PSRD[string](val: "rd", color: 3)
  95. doAssert(testPS(a, b, c) == "base1rc2rd3")
  96. doAssert(testPS(b, a, c) == "rc2base1rd3")
  97. doAssert(testPS(c, b, a) == "rd3rc2base1")
  98. doAssert(testPS([a, b, c]) == "base1rc2rd3")
  99. doAssert(testPS([b, a, c]) == "rc2base1rd3")
  100. doAssert(testPS([c, b, a]) == "rd3rc2base1")
  101. type
  102. SBase[T, V] = ref object of RootObj
  103. val: T
  104. color: V
  105. SRC = ref object of SBase[string, int]
  106. SRD = ref object of SBase[string, int]
  107. proc testS[T, V](x: varargs[SBase[T, V]]): string =
  108. result = ""
  109. for c in x:
  110. result.add c.val
  111. result.add $c.color
  112. block test_t4799_6:
  113. var a = SBase[string, int](val: "base", color: 1)
  114. var b = SRC(val: "rc", color: 2)
  115. var c = SRD(val: "rd", color: 3)
  116. doAssert(testS(a, b, c) == "base1rc2rd3")
  117. doAssert(testS(b, a, c) == "rc2base1rd3")
  118. doAssert(testS(c, b, a) == "rd3rc2base1")
  119. doAssert(testS([a, b, c]) == "base1rc2rd3")
  120. # this is not varargs bug, but array construction bug
  121. # see #7955
  122. #doAssert(testS([b, c, a]) == "rc2rd3base1")
  123. #doAssert(testS([c, b, a]) == "rd3rc2base1")
  124. proc test_inproc() =
  125. block test_inproc_1:
  126. var rgv = GRBase[int](val: 3)
  127. var rgc = GRC[int](val: 4)
  128. var rgb = GRD[int](val: 2)
  129. doAssert(testGR(rgb, rgc, rgv) == "243")
  130. doAssert(testGR(rgc, rgv, rgb) == "432")
  131. doAssert(testGR(rgv, rgb, rgc) == "324")
  132. doAssert(testGR([rgb, rgc, rgv]) == "243")
  133. doAssert(testGR([rgc, rgv, rgb]) == "432")
  134. doAssert(testGR([rgv, rgb, rgc]) == "324")
  135. block test_inproc_2:
  136. var pgv = PRBase[int](val: 3)
  137. var pgc = PRC[int](val: 4)
  138. var pgb = PRD[int](val: 2)
  139. doAssert(testPR(pgb.addr, pgc.addr, pgv.addr) == "243")
  140. doAssert(testPR(pgc.addr, pgv.addr, pgb.addr) == "432")
  141. doAssert(testPR(pgv.addr, pgb.addr, pgc.addr) == "324")
  142. doAssert(testPR([pgb.addr, pgc.addr, pgv.addr]) == "243")
  143. doAssert(testPR([pgc.addr, pgv.addr, pgb.addr]) == "432")
  144. doAssert(testPR([pgv.addr, pgb.addr, pgc.addr]) == "324")
  145. test_inproc()
  146. template reject(x) =
  147. static: assert(not compiles(x))
  148. block test_t4799_7:
  149. type
  150. Vehicle[T] = ref object of RootObj
  151. tire: T
  152. Car[T] = object of Vehicle[T]
  153. Bike[T] = object of Vehicle[T]
  154. proc testVehicle[T](x: varargs[Vehicle[T]]): string {.used.} =
  155. result = ""
  156. for c in x:
  157. result.add $c.tire
  158. var v = Vehicle[int](tire: 3)
  159. var c = Car[int](tire: 4)
  160. var b = Bike[int](tire: 2)
  161. reject:
  162. echo testVehicle(b, c, v)
  163. block test_t4799_8:
  164. type
  165. Vehicle = ref object of RootObj
  166. tire: int
  167. Car = object of Vehicle
  168. Bike = object of Vehicle
  169. proc testVehicle(x: varargs[Vehicle]): string {.used.} =
  170. result = ""
  171. for c in x:
  172. result.add $c.tire
  173. var v = Vehicle(tire: 3)
  174. var c = Car(tire: 4)
  175. var b = Bike(tire: 2)
  176. reject:
  177. echo testVehicle(b, c, v)
  178. type
  179. PGVehicle[T] = ptr object of RootObj
  180. tire: T
  181. PGCar[T] = object of PGVehicle[T]
  182. PGBike[T] = object of PGVehicle[T]
  183. proc testVehicle[T](x: varargs[PGVehicle[T]]): string {.used.} =
  184. result = ""
  185. for c in x:
  186. result.add $c.tire
  187. var pgc = PGCar[int](tire: 4)
  188. var pgb = PGBike[int](tire: 2)
  189. reject:
  190. echo testVehicle(pgb, pgc)
  191. type
  192. RVehicle = ptr object of RootObj
  193. tire: int
  194. RCar = object of RVehicle
  195. RBike = object of RVehicle
  196. proc testVehicle(x: varargs[RVehicle]): string {.used.} =
  197. result = ""
  198. for c in x:
  199. result.add $c.tire
  200. var rc = RCar(tire: 4)
  201. var rb = RBike(tire: 2)
  202. reject:
  203. echo testVehicle(rb, rc)
  204. echo "OK"