tarcmisc.nim 8.3 KB

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