t4799.nim 6.2 KB

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