tarcmisc.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. discard """
  2. output: '''
  3. =destroy called
  4. 123xyzabc
  5. destroyed: false
  6. destroyed: false
  7. destroyed2: false
  8. destroyed2: false
  9. destroying variable: 2
  10. destroying variable: 1
  11. whiley ends :(
  12. 1
  13. (x: "0")
  14. (x: "1")
  15. (x: "2")
  16. (x: "3")
  17. (x: "4")
  18. (x: "5")
  19. (x: "6")
  20. (x: "7")
  21. (x: "8")
  22. (x: "9")
  23. (x: "10")
  24. 0
  25. new line before - @['a']
  26. new line after - @['a']
  27. finalizer
  28. aaaaa
  29. hello
  30. true
  31. copying
  32. 123
  33. 42
  34. ok
  35. destroying variable: 20
  36. destroying variable: 10
  37. closed
  38. '''
  39. cmd: "nim c --gc:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
  40. """
  41. # bug #9401
  42. type
  43. MyObj = object
  44. len: int
  45. data: ptr UncheckedArray[float]
  46. proc `=destroy`*(m: var MyObj) =
  47. echo "=destroy called"
  48. if m.data != nil:
  49. deallocShared(m.data)
  50. m.data = nil
  51. type
  52. MyObjDistinct = distinct MyObj
  53. proc `=copy`*(m: var MyObj, m2: MyObj) =
  54. if m.data == m2.data: return
  55. if m.data != nil:
  56. `=destroy`(m)
  57. m.len = m2.len
  58. if m.len > 0:
  59. m.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * m.len))
  60. copyMem(m.data, m2.data, sizeof(float) * m.len)
  61. proc `=sink`*(m: var MyObj, m2: MyObj) =
  62. if m.data != m2.data:
  63. if m.data != nil:
  64. `=destroy`(m)
  65. m.len = m2.len
  66. m.data = m2.data
  67. proc newMyObj(len: int): MyObj =
  68. result.len = len
  69. result.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * len))
  70. proc newMyObjDistinct(len: int): MyObjDistinct =
  71. MyObjDistinct(newMyObj(len))
  72. proc fooDistinct =
  73. doAssert newMyObjDistinct(2).MyObj.len == 2
  74. fooDistinct()
  75. proc takeSink(x: sink string): bool = true
  76. proc b(x: sink string): string =
  77. if takeSink(x):
  78. return x & "abc"
  79. proc bbb(inp: string) =
  80. let y = inp & "xyz"
  81. echo b(y)
  82. bbb("123")
  83. # bug #13691
  84. type Variable = ref object
  85. value: int
  86. proc `=destroy`(self: var typeof(Variable()[])) =
  87. echo "destroying variable: ",self.value
  88. proc newVariable(value: int): Variable =
  89. result = Variable()
  90. result.value = value
  91. #echo "creating variable: ",result.value
  92. proc test(count: int) =
  93. var v {.global.} = newVariable(10)
  94. var count = count - 1
  95. if count == 0: return
  96. test(count)
  97. echo "destroyed: ", v.isNil
  98. test(3)
  99. proc test2(count: int) =
  100. block: #XXX: Fails with block currently
  101. var v {.global.} = newVariable(20)
  102. var count = count - 1
  103. if count == 0: return
  104. test2(count)
  105. echo "destroyed2: ", v.isNil
  106. test2(3)
  107. proc whiley =
  108. var a = newVariable(1)
  109. while true:
  110. var b = newVariable(2)
  111. if true: raise newException(CatchableError, "test")
  112. try:
  113. whiley()
  114. except CatchableError:
  115. echo "whiley ends :("
  116. #------------------------------------------------------------------------------
  117. # issue #13810
  118. import streams
  119. type
  120. A = ref AObj
  121. AObj = object of RootObj
  122. io: Stream
  123. B = ref object of A
  124. x: int
  125. proc `=destroy`(x: var AObj) =
  126. close(x.io)
  127. echo "closed"
  128. var x = B(io: newStringStream("thestream"))
  129. #------------------------------------------------------------------------------
  130. # issue #14003
  131. proc cryptCTR*(nonce: var openArray[char]) =
  132. nonce[1] = 'A'
  133. proc main() =
  134. var nonce1 = "0123456701234567"
  135. cryptCTR(nonce1)
  136. doAssert(nonce1 == "0A23456701234567")
  137. var nonce2 = "01234567"
  138. cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
  139. doAssert(nonce2 == "0A234567")
  140. main()
  141. # bug #14079
  142. import std/algorithm
  143. let
  144. n = @["c", "b"]
  145. q = @[("c", "2"), ("b", "1")]
  146. doAssert n.sortedByIt(it) == @["b", "c"], "fine"
  147. doAssert q.sortedByIt(it[0]) == @[("b", "1"), ("c", "2")], "fails under arc"
  148. #------------------------------------------------------------------------------
  149. # issue #14236
  150. type
  151. MyType = object
  152. a: seq[int]
  153. proc re(x: static[string]): static MyType =
  154. MyType()
  155. proc match(inp: string, rg: static MyType) =
  156. doAssert rg.a.len == 0
  157. match("ac", re"a(b|c)")
  158. #------------------------------------------------------------------------------
  159. # issue #14243
  160. type
  161. Game* = ref object
  162. proc free*(game: Game) =
  163. let a = 5
  164. proc newGame*(): Game =
  165. new(result, free)
  166. var game*: Game
  167. #------------------------------------------------------------------------------
  168. # issue #14333
  169. type
  170. SimpleLoop = object
  171. Lsg = object
  172. loops: seq[ref SimpleLoop]
  173. root: ref SimpleLoop
  174. var lsg: Lsg
  175. lsg.loops.add lsg.root
  176. echo lsg.loops.len
  177. # bug #14495
  178. type
  179. Gah = ref object
  180. x: string
  181. proc bug14495 =
  182. var owners: seq[Gah]
  183. for i in 0..10:
  184. owners.add Gah(x: $i)
  185. var x: seq[Gah]
  186. for i in 0..10:
  187. x.add owners[i]
  188. for i in 0..100:
  189. setLen(x, 0)
  190. setLen(x, 10)
  191. for i in 0..x.len-1:
  192. if x[i] != nil:
  193. echo x[i][]
  194. for o in owners:
  195. echo o[]
  196. bug14495()
  197. # bug #14396
  198. type
  199. Spinny = ref object
  200. t: ref int
  201. text: string
  202. proc newSpinny*(): Spinny =
  203. Spinny(t: new(int), text: "hello")
  204. proc spinnyLoop(x: ref int, spinny: sink Spinny) =
  205. echo x[]
  206. proc start*(spinny: sink Spinny) =
  207. spinnyLoop(spinny.t, spinny)
  208. var spinner1 = newSpinny()
  209. spinner1.start()
  210. # bug #14345
  211. type
  212. SimpleLoopB = ref object
  213. children: seq[SimpleLoopB]
  214. parent: SimpleLoopB
  215. proc addChildLoop(self: SimpleLoopB, loop: SimpleLoopB) =
  216. self.children.add loop
  217. proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
  218. self.parent = parent
  219. self.parent.addChildLoop(self)
  220. var l = SimpleLoopB()
  221. l.setParent(l)
  222. # bug #14968
  223. import times
  224. let currentTime = now().utc
  225. # bug #14994
  226. import sequtils
  227. var newLine = @['a']
  228. let indent = newSeq[char]()
  229. echo "new line before - ", newline
  230. newline.insert(indent, 0)
  231. echo "new line after - ", newline
  232. # bug #15044
  233. type
  234. Test = ref object
  235. proc test: Test =
  236. # broken
  237. new(result, proc(x: Test) =
  238. echo "finalizer"
  239. )
  240. proc tdirectFinalizer =
  241. discard test()
  242. tdirectFinalizer()
  243. # bug #14480
  244. proc hello(): int =
  245. result = 42
  246. var leaves {.global.} = hello()
  247. doAssert leaves == 42
  248. # bug #15052
  249. proc mutstrings =
  250. var data = "hello"
  251. for c in data.mitems():
  252. c = 'a'
  253. echo data
  254. mutstrings()
  255. # bug #15038
  256. type
  257. Machine = ref object
  258. hello: string
  259. var machineTypes: seq[tuple[factory: proc(): Machine]]
  260. proc registerMachine(factory: proc(): Machine) =
  261. var mCreator = proc(): Machine =
  262. result = factory()
  263. machineTypes.add((factory: mCreator))
  264. proc facproc(): Machine =
  265. result = Machine(hello: "hello")
  266. registerMachine(facproc)
  267. proc createMachine =
  268. for machine in machineTypes:
  269. echo machine.factory().hello
  270. createMachine()
  271. # bug #15122
  272. import tables
  273. type
  274. BENodeKind = enum
  275. tkBytes,
  276. tkList,
  277. tkDict
  278. BENode = object
  279. case kind: BENodeKind
  280. of tkBytes: strVal: string
  281. of tkList: listVal: seq[BENode]
  282. of tkDict: dictVal: Table[string, BENode]
  283. var data = {
  284. "examples": {
  285. "values": BENode(
  286. kind: tkList,
  287. listVal: @[BENode(kind: tkBytes, strVal: "test")]
  288. )
  289. }.toTable()
  290. }.toTable()
  291. # For ARC listVal is empty for some reason
  292. doAssert data["examples"]["values"].listVal[0].strVal == "test"
  293. ###############################################################################
  294. # bug #15405
  295. import parsexml
  296. const test_xml_str = "<A><B>value</B></A>"
  297. var stream = newStringStream(test_xml_str)
  298. var xml: XmlParser
  299. open(xml, stream, "test")
  300. var xml2 = deepCopy(xml)
  301. proc text_parser(xml: var XmlParser) =
  302. var test_passed = false
  303. while true:
  304. xml.next()
  305. case xml.kind
  306. of xmlElementStart:
  307. if xml.elementName == "B":
  308. xml.next()
  309. if xml.kind == xmlCharData and xml.charData == "value":
  310. test_passed = true
  311. of xmlEof: break
  312. else: discard
  313. xml.close()
  314. doAssert(test_passed)
  315. text_parser(xml)
  316. text_parser(xml2)
  317. # bug #15599
  318. type
  319. PixelBuffer = ref object
  320. proc newPixelBuffer(): PixelBuffer =
  321. new(result) do (buffer: PixelBuffer):
  322. echo "ok"
  323. discard newPixelBuffer()
  324. # bug #17199
  325. proc passSeq(data: seq[string]) =
  326. # used the system.& proc initially
  327. let wat = data & "hello"
  328. proc test2 =
  329. let name = @["hello", "world"]
  330. passSeq(name)
  331. doAssert name == @["hello", "world"]
  332. static: test2() # was buggy
  333. test2()
  334. proc merge(x: sink seq[string], y: sink string): seq[string] =
  335. newSeq(result, x.len + 1)
  336. for i in 0..x.len-1:
  337. result[i] = move(x[i])
  338. result[x.len] = move(y)
  339. proc passSeq2(data: seq[string]) =
  340. # used the system.& proc initially
  341. let wat = merge(data, "hello")
  342. proc test3 =
  343. let name = @["hello", "world"]
  344. passSeq2(name)
  345. doAssert name == @["hello", "world"]
  346. static: test3() # was buggy
  347. test3()
  348. # bug #17712
  349. proc t17712 =
  350. var ppv = new int
  351. discard @[ppv]
  352. var el: ref int
  353. el = [ppv][0]
  354. echo el != nil
  355. t17712()
  356. # bug #18030
  357. type
  358. Foo = object
  359. n: int
  360. proc `=copy`(dst: var Foo, src: Foo) =
  361. echo "copying"
  362. dst.n = src.n
  363. proc `=sink`(dst: var Foo, src: Foo) =
  364. echo "sinking"
  365. dst.n = src.n
  366. var a: Foo
  367. proc putValue[T](n: T)
  368. proc useForward =
  369. putValue(123)
  370. proc putValue[T](n: T) =
  371. var b = Foo(n:n)
  372. a = b
  373. echo b.n
  374. useForward()
  375. # bug #17319
  376. type
  377. BrokenObject = ref object
  378. brokenType: seq[int]
  379. proc use(obj: BrokenObject) =
  380. discard
  381. method testMethod(self: BrokenObject) {.base.} =
  382. iterator testMethodIter() {.closure.} =
  383. use(self)
  384. var nameIterVar = testMethodIter
  385. nameIterVar()
  386. let mikasa = BrokenObject()
  387. mikasa.testMethod()
  388. # bug #19205
  389. type
  390. InputSectionBase* = object of RootObj
  391. relocations*: seq[int] # traced reference. string has a similar SIGSEGV.
  392. InputSection* = object of InputSectionBase
  393. proc fooz(sec: var InputSectionBase) =
  394. if sec of InputSection: # this line SIGSEGV.
  395. echo 42
  396. var sec = create(InputSection)
  397. sec[] = InputSection(relocations: newSeq[int]())
  398. fooz sec[]
  399. block:
  400. type
  401. Data = ref object
  402. id: int
  403. proc main =
  404. var x = Data(id: 99)
  405. var y = x
  406. x[] = Data(id: 778)[]
  407. doAssert y.id == 778
  408. doAssert x[].id == 778
  409. main()
  410. block: # bug #19857
  411. type
  412. ValueKind = enum VNull, VFloat, VObject # need 3 elements. Cannot remove VNull or VObject
  413. Value = object
  414. case kind: ValueKind
  415. of VFloat: fnum: float
  416. of VObject: tab: Table[int, int] # OrderedTable[T, U] also makes it fail.
  417. # "simpler" types also work though
  418. else: discard # VNull can be like this, but VObject must be filled
  419. # required. Pure proc works
  420. FormulaNode = proc(c: OrderedTable[string, int]): Value
  421. proc toF(v: Value): float =
  422. doAssert v.kind == VFloat
  423. case v.kind
  424. of VFloat: result = v.fnum
  425. else: discard
  426. proc foo() =
  427. let fuck = initOrderedTable[string, int]()
  428. proc cb(fuck: OrderedTable[string, int]): Value =
  429. # works:
  430. #result = Value(kind: VFloat, fnum: fuck["field_that_does_not_exist"].float)
  431. # broken:
  432. discard "actuall runs!"
  433. let t = fuck["field_that_does_not_exist"]
  434. echo "never runs, but we crash after! ", t
  435. doAssertRaises(KeyError):
  436. let fn = FormulaNode(cb)
  437. let v = fn(fuck)
  438. #echo v
  439. let res = v.toF()
  440. foo()
  441. import std/options
  442. # bug #21592
  443. type Event* = object
  444. code*: string
  445. type App* = ref object of RootObj
  446. id*: string
  447. method process*(self: App): Option[Event] {.base.} =
  448. raise Exception.new_exception("not impl")
  449. # bug #21617
  450. type Test2 = ref object of RootObj
  451. method bug(t: Test2): seq[float] {.base.} = discard