tarcmisc.nim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. discard """
  2. output: '''
  3. 123xyzabc
  4. destroyed: false
  5. destroyed: false
  6. destroyed2: false
  7. destroyed2: false
  8. destroying variable: 2
  9. destroying variable: 1
  10. whiley ends :(
  11. 1
  12. (x: "0")
  13. (x: "1")
  14. (x: "2")
  15. (x: "3")
  16. (x: "4")
  17. (x: "5")
  18. (x: "6")
  19. (x: "7")
  20. (x: "8")
  21. (x: "9")
  22. (x: "10")
  23. 0
  24. new line before - @['a']
  25. new line after - @['a']
  26. finalizer
  27. aaaaa
  28. hello
  29. ok
  30. true
  31. copying
  32. 123
  33. closed
  34. destroying variable: 20
  35. destroying variable: 10
  36. '''
  37. cmd: "nim c --gc:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
  38. """
  39. proc takeSink(x: sink string): bool = true
  40. proc b(x: sink string): string =
  41. if takeSink(x):
  42. return x & "abc"
  43. proc bbb(inp: string) =
  44. let y = inp & "xyz"
  45. echo b(y)
  46. bbb("123")
  47. # bug #13691
  48. type Variable = ref object
  49. value: int
  50. proc `=destroy`(self: var typeof(Variable()[])) =
  51. echo "destroying variable: ",self.value
  52. proc newVariable(value: int): Variable =
  53. result = Variable()
  54. result.value = value
  55. #echo "creating variable: ",result.value
  56. proc test(count: int) =
  57. var v {.global.} = newVariable(10)
  58. var count = count - 1
  59. if count == 0: return
  60. test(count)
  61. echo "destroyed: ", v.isNil
  62. test(3)
  63. proc test2(count: int) =
  64. #block: #XXX: Fails with block currently
  65. var v {.global.} = newVariable(20)
  66. var count = count - 1
  67. if count == 0: return
  68. test2(count)
  69. echo "destroyed2: ", v.isNil
  70. test2(3)
  71. proc whiley =
  72. var a = newVariable(1)
  73. while true:
  74. var b = newVariable(2)
  75. if true: raise newException(CatchableError, "test")
  76. try:
  77. whiley()
  78. except CatchableError:
  79. echo "whiley ends :("
  80. #------------------------------------------------------------------------------
  81. # issue #13810
  82. import streams
  83. type
  84. A = ref AObj
  85. AObj = object of RootObj
  86. io: Stream
  87. B = ref object of A
  88. x: int
  89. proc `=destroy`(x: var AObj) =
  90. close(x.io)
  91. echo "closed"
  92. var x = B(io: newStringStream("thestream"))
  93. #------------------------------------------------------------------------------
  94. # issue #14003
  95. proc cryptCTR*(nonce: var openArray[char]) =
  96. nonce[1] = 'A'
  97. proc main() =
  98. var nonce1 = "0123456701234567"
  99. cryptCTR(nonce1)
  100. doAssert(nonce1 == "0A23456701234567")
  101. var nonce2 = "01234567"
  102. cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
  103. doAssert(nonce2 == "0A234567")
  104. main()
  105. # bug #14079
  106. import std/algorithm
  107. let
  108. n = @["c", "b"]
  109. q = @[("c", "2"), ("b", "1")]
  110. doAssert n.sortedByIt(it) == @["b", "c"], "fine"
  111. doAssert q.sortedByIt(it[0]) == @[("b", "1"), ("c", "2")], "fails under arc"
  112. #------------------------------------------------------------------------------
  113. # issue #14236
  114. type
  115. MyType = object
  116. a: seq[int]
  117. proc re(x: static[string]): static MyType =
  118. MyType()
  119. proc match(inp: string, rg: static MyType) =
  120. doAssert rg.a.len == 0
  121. match("ac", re"a(b|c)")
  122. #------------------------------------------------------------------------------
  123. # issue #14243
  124. type
  125. Game* = ref object
  126. proc free*(game: Game) =
  127. let a = 5
  128. proc newGame*(): Game =
  129. new(result, free)
  130. var game*: Game
  131. #------------------------------------------------------------------------------
  132. # issue #14333
  133. type
  134. SimpleLoop = object
  135. Lsg = object
  136. loops: seq[ref SimpleLoop]
  137. root: ref SimpleLoop
  138. var lsg: Lsg
  139. lsg.loops.add lsg.root
  140. echo lsg.loops.len
  141. # bug #14495
  142. type
  143. Gah = ref object
  144. x: string
  145. proc bug14495 =
  146. var owners: seq[Gah]
  147. for i in 0..10:
  148. owners.add Gah(x: $i)
  149. var x: seq[Gah]
  150. for i in 0..10:
  151. x.add owners[i]
  152. for i in 0..100:
  153. setLen(x, 0)
  154. setLen(x, 10)
  155. for i in 0..x.len-1:
  156. if x[i] != nil:
  157. echo x[i][]
  158. for o in owners:
  159. echo o[]
  160. bug14495()
  161. # bug #14396
  162. type
  163. Spinny = ref object
  164. t: ref int
  165. text: string
  166. proc newSpinny*(): Spinny =
  167. Spinny(t: new(int), text: "hello")
  168. proc spinnyLoop(x: ref int, spinny: sink Spinny) =
  169. echo x[]
  170. proc start*(spinny: sink Spinny) =
  171. spinnyLoop(spinny.t, spinny)
  172. var spinner1 = newSpinny()
  173. spinner1.start()
  174. # bug #14345
  175. type
  176. SimpleLoopB = ref object
  177. children: seq[SimpleLoopB]
  178. parent: SimpleLoopB
  179. proc addChildLoop(self: SimpleLoopB, loop: SimpleLoopB) =
  180. self.children.add loop
  181. proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
  182. self.parent = parent
  183. self.parent.addChildLoop(self)
  184. var l = SimpleLoopB()
  185. l.setParent(l)
  186. # bug #14968
  187. import times
  188. let currentTime = now().utc
  189. # bug #14994
  190. import sequtils
  191. var newLine = @['a']
  192. let indent = newSeq[char]()
  193. echo "new line before - ", newline
  194. newline.insert(indent, 0)
  195. echo "new line after - ", newline
  196. # bug #15044
  197. type
  198. Test = ref object
  199. proc test: Test =
  200. # broken
  201. new(result, proc(x: Test) =
  202. echo "finalizer"
  203. )
  204. proc tdirectFinalizer =
  205. discard test()
  206. tdirectFinalizer()
  207. # bug #14480
  208. proc hello(): int =
  209. result = 42
  210. var leaves {.global.} = hello()
  211. doAssert leaves == 42
  212. # bug #15052
  213. proc mutstrings =
  214. var data = "hello"
  215. for c in data.mitems():
  216. c = 'a'
  217. echo data
  218. mutstrings()
  219. # bug #15038
  220. type
  221. Machine = ref object
  222. hello: string
  223. var machineTypes: seq[tuple[factory: proc(): Machine]]
  224. proc registerMachine(factory: proc(): Machine) =
  225. var mCreator = proc(): Machine =
  226. result = factory()
  227. machineTypes.add((factory: mCreator))
  228. proc facproc(): Machine =
  229. result = Machine(hello: "hello")
  230. registerMachine(facproc)
  231. proc createMachine =
  232. for machine in machineTypes:
  233. echo machine.factory().hello
  234. createMachine()
  235. # bug #15122
  236. import tables
  237. type
  238. BENodeKind = enum
  239. tkBytes,
  240. tkList,
  241. tkDict
  242. BENode = object
  243. case kind: BENodeKind
  244. of tkBytes: strVal: string
  245. of tkList: listVal: seq[BENode]
  246. of tkDict: dictVal: Table[string, BENode]
  247. var data = {
  248. "examples": {
  249. "values": BENode(
  250. kind: tkList,
  251. listVal: @[BENode(kind: tkBytes, strVal: "test")]
  252. )
  253. }.toTable()
  254. }.toTable()
  255. # For ARC listVal is empty for some reason
  256. doAssert data["examples"]["values"].listVal[0].strVal == "test"
  257. ###############################################################################
  258. # bug #15405
  259. import parsexml
  260. const test_xml_str = "<A><B>value</B></A>"
  261. var stream = newStringStream(test_xml_str)
  262. var xml: XmlParser
  263. open(xml, stream, "test")
  264. var xml2 = deepCopy(xml)
  265. proc text_parser(xml: var XmlParser) =
  266. var test_passed = false
  267. while true:
  268. xml.next()
  269. case xml.kind
  270. of xmlElementStart:
  271. if xml.elementName == "B":
  272. xml.next()
  273. if xml.kind == xmlCharData and xml.charData == "value":
  274. test_passed = true
  275. of xmlEof: break
  276. else: discard
  277. xml.close()
  278. doAssert(test_passed)
  279. text_parser(xml)
  280. text_parser(xml2)
  281. # bug #15599
  282. type
  283. PixelBuffer = ref object
  284. proc newPixelBuffer(): PixelBuffer =
  285. new(result) do (buffer: PixelBuffer):
  286. echo "ok"
  287. discard newPixelBuffer()
  288. # bug #17199
  289. proc passSeq(data: seq[string]) =
  290. # used the system.& proc initially
  291. let wat = data & "hello"
  292. proc test2 =
  293. let name = @["hello", "world"]
  294. passSeq(name)
  295. doAssert name == @["hello", "world"]
  296. static: test2() # was buggy
  297. test2()
  298. proc merge(x: sink seq[string], y: sink string): seq[string] =
  299. newSeq(result, x.len + 1)
  300. for i in 0..x.len-1:
  301. result[i] = move(x[i])
  302. result[x.len] = move(y)
  303. proc passSeq2(data: seq[string]) =
  304. # used the system.& proc initially
  305. let wat = merge(data, "hello")
  306. proc test3 =
  307. let name = @["hello", "world"]
  308. passSeq2(name)
  309. doAssert name == @["hello", "world"]
  310. static: test3() # was buggy
  311. test3()
  312. # bug #17712
  313. proc t17712 =
  314. var ppv = new int
  315. discard @[ppv]
  316. var el: ref int
  317. el = [ppv][0]
  318. echo el != nil
  319. t17712()
  320. # bug #18030
  321. type
  322. Foo = object
  323. n: int
  324. proc `=copy`(dst: var Foo, src: Foo) =
  325. echo "copying"
  326. dst.n = src.n
  327. proc `=sink`(dst: var Foo, src: Foo) =
  328. echo "sinking"
  329. dst.n = src.n
  330. var a: Foo
  331. proc putValue[T](n: T)
  332. proc useForward =
  333. putValue(123)
  334. proc putValue[T](n: T) =
  335. var b = Foo(n:n)
  336. a = b
  337. echo b.n
  338. useForward()
  339. # bug #16246
  340. proc testWeirdTypeAliases() =
  341. var values = newSeq[cuint](8)
  342. # var values: seq[cuint] does not produce codegen error
  343. var drawCb = proc(): seq[uint32] =
  344. result = newSeq[uint32](10)
  345. testWeirdTypeAliases()