tarcmisc.nim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. discard """
  2. output: '''
  3. Destructor for TestTestObj
  4. =destroy called
  5. 123xyzabc
  6. destroyed: false
  7. destroyed: false
  8. destroyed2: false
  9. destroyed2: false
  10. destroying variable: 2
  11. destroying variable: 1
  12. whiley ends :(
  13. 1
  14. (x: "0")
  15. (x: "1")
  16. (x: "2")
  17. (x: "3")
  18. (x: "4")
  19. (x: "5")
  20. (x: "6")
  21. (x: "7")
  22. (x: "8")
  23. (x: "9")
  24. (x: "10")
  25. 0
  26. new line before - @['a']
  27. new line after - @['a']
  28. finalizer
  29. aaaaa
  30. hello
  31. true
  32. copying
  33. 123
  34. 42
  35. @["", "d", ""]
  36. ok
  37. destroying variable: 20
  38. destroying variable: 10
  39. closed
  40. '''
  41. cmd: "nim c --mm:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
  42. """
  43. block: # bug #23627
  44. type
  45. TestObj = object of RootObj
  46. Test2 = object of RootObj
  47. foo: TestObj
  48. TestTestObj = object of RootObj
  49. shit: TestObj
  50. proc `=destroy`(x: TestTestObj) =
  51. echo "Destructor for TestTestObj"
  52. let test = Test2(foo: TestObj())
  53. proc testCaseT() =
  54. let tt1 {.used.} = TestTestObj(shit: TestObj())
  55. proc main() =
  56. testCaseT()
  57. main()
  58. # bug #9401
  59. type
  60. MyObj = object
  61. len: int
  62. data: ptr UncheckedArray[float]
  63. proc `=destroy`*(m: MyObj) =
  64. echo "=destroy called"
  65. if m.data != nil:
  66. deallocShared(m.data)
  67. type
  68. MyObjDistinct = distinct MyObj
  69. proc `=copy`*(m: var MyObj, m2: MyObj) =
  70. if m.data == m2.data: return
  71. if m.data != nil:
  72. `=destroy`(m)
  73. m.len = m2.len
  74. if m.len > 0:
  75. m.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * m.len))
  76. copyMem(m.data, m2.data, sizeof(float) * m.len)
  77. proc `=sink`*(m: var MyObj, m2: MyObj) =
  78. if m.data != m2.data:
  79. if m.data != nil:
  80. `=destroy`(m)
  81. m.len = m2.len
  82. m.data = m2.data
  83. proc newMyObj(len: int): MyObj =
  84. result = MyObj()
  85. result.len = len
  86. result.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * len))
  87. proc newMyObjDistinct(len: int): MyObjDistinct =
  88. MyObjDistinct(newMyObj(len))
  89. proc fooDistinct =
  90. doAssert newMyObjDistinct(2).MyObj.len == 2
  91. fooDistinct()
  92. proc takeSink(x: sink string): bool = true
  93. proc b(x: sink string): string =
  94. if takeSink(x):
  95. return x & "abc"
  96. else:
  97. result = ""
  98. proc bbb(inp: string) =
  99. let y = inp & "xyz"
  100. echo b(y)
  101. bbb("123")
  102. # bug #13691
  103. type Variable = ref object
  104. value: int
  105. proc `=destroy`(self: typeof(Variable()[])) =
  106. echo "destroying variable: ",self.value
  107. proc newVariable(value: int): Variable =
  108. result = Variable()
  109. result.value = value
  110. #echo "creating variable: ",result.value
  111. proc test(count: int) =
  112. var v {.global.} = newVariable(10)
  113. var count = count - 1
  114. if count == 0: return
  115. test(count)
  116. echo "destroyed: ", v.isNil
  117. test(3)
  118. proc test2(count: int) =
  119. block: #XXX: Fails with block currently
  120. var v {.global.} = newVariable(20)
  121. var count = count - 1
  122. if count == 0: return
  123. test2(count)
  124. echo "destroyed2: ", v.isNil
  125. test2(3)
  126. proc whiley =
  127. var a = newVariable(1)
  128. while true:
  129. var b = newVariable(2)
  130. if true: raise newException(CatchableError, "test")
  131. try:
  132. whiley()
  133. except CatchableError:
  134. echo "whiley ends :("
  135. #------------------------------------------------------------------------------
  136. # issue #13810
  137. import streams
  138. type
  139. A = ref AObj
  140. AObj = object of RootObj
  141. io: Stream
  142. B = ref object of A
  143. x: int
  144. proc `=destroy`(x: AObj) =
  145. close(x.io)
  146. echo "closed"
  147. var x = B(io: newStringStream("thestream"))
  148. #------------------------------------------------------------------------------
  149. # issue #14003
  150. proc cryptCTR*(nonce: var openArray[char]) =
  151. nonce[1] = 'A'
  152. proc main() =
  153. var nonce1 = "0123456701234567"
  154. cryptCTR(nonce1)
  155. doAssert(nonce1 == "0A23456701234567")
  156. var nonce2 = "01234567"
  157. cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
  158. doAssert(nonce2 == "0A234567")
  159. main()
  160. # bug #14079
  161. import std/algorithm
  162. let
  163. n = @["c", "b"]
  164. q = @[("c", "2"), ("b", "1")]
  165. doAssert n.sortedByIt(it) == @["b", "c"], "fine"
  166. doAssert q.sortedByIt(it[0]) == @[("b", "1"), ("c", "2")], "fails under arc"
  167. #------------------------------------------------------------------------------
  168. # issue #14236
  169. type
  170. MyType = object
  171. a: seq[int]
  172. proc re(x: static[string]): static MyType =
  173. MyType()
  174. proc match(inp: string, rg: static MyType) =
  175. doAssert rg.a.len == 0
  176. match("ac", re"a(b|c)")
  177. #------------------------------------------------------------------------------
  178. # issue #14243
  179. type
  180. Game* = ref object
  181. proc free*(game: Game) =
  182. let a = 5
  183. proc newGame*(): Game =
  184. new(result, free)
  185. var game*: Game
  186. #------------------------------------------------------------------------------
  187. # issue #14333
  188. type
  189. SimpleLoop = object
  190. Lsg = object
  191. loops: seq[ref SimpleLoop]
  192. root: ref SimpleLoop
  193. var lsg: Lsg
  194. lsg.loops.add lsg.root
  195. echo lsg.loops.len
  196. # bug #14495
  197. type
  198. Gah = ref object
  199. x: string
  200. proc bug14495 =
  201. var owners: seq[Gah] = @[]
  202. for i in 0..10:
  203. owners.add Gah(x: $i)
  204. var x: seq[Gah] = @[]
  205. for i in 0..10:
  206. x.add owners[i]
  207. for i in 0..100:
  208. setLen(x, 0)
  209. setLen(x, 10)
  210. for i in 0..x.len-1:
  211. if x[i] != nil:
  212. echo x[i][]
  213. for o in owners:
  214. echo o[]
  215. bug14495()
  216. # bug #14396
  217. type
  218. Spinny = ref object
  219. t: ref int
  220. text: string
  221. proc newSpinny*(): Spinny =
  222. Spinny(t: new(int), text: "hello")
  223. proc spinnyLoop(x: ref int, spinny: sink Spinny) =
  224. echo x[]
  225. proc start*(spinny: sink Spinny) =
  226. spinnyLoop(spinny.t, spinny)
  227. var spinner1 = newSpinny()
  228. spinner1.start()
  229. # bug #14345
  230. type
  231. SimpleLoopB = ref object
  232. children: seq[SimpleLoopB]
  233. parent: SimpleLoopB
  234. proc addChildLoop(self: SimpleLoopB, loop: SimpleLoopB) =
  235. self.children.add loop
  236. proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
  237. self.parent = parent
  238. self.parent.addChildLoop(self)
  239. var l = SimpleLoopB()
  240. l.setParent(l)
  241. # bug #14968
  242. import times
  243. let currentTime = now().utc
  244. # bug #14994
  245. import sequtils
  246. var newLine = @['a']
  247. let indent = newSeq[char]()
  248. echo "new line before - ", newline
  249. newline.insert(indent, 0)
  250. echo "new line after - ", newline
  251. # bug #15044
  252. type
  253. Test = ref object
  254. proc test: Test =
  255. # broken
  256. new(result, proc(x: Test) =
  257. echo "finalizer"
  258. )
  259. proc tdirectFinalizer =
  260. discard test()
  261. tdirectFinalizer()
  262. # bug #14480
  263. proc hello(): int =
  264. result = 42
  265. var leaves {.global.} = hello()
  266. doAssert leaves == 42
  267. # bug #15052
  268. proc mutstrings =
  269. var data = "hello"
  270. for c in data.mitems():
  271. c = 'a'
  272. echo data
  273. mutstrings()
  274. # bug #15038
  275. type
  276. Machine = ref object
  277. hello: string
  278. var machineTypes: seq[tuple[factory: proc(): Machine]]
  279. proc registerMachine(factory: proc(): Machine) =
  280. var mCreator = proc(): Machine =
  281. result = factory()
  282. machineTypes.add((factory: mCreator))
  283. proc facproc(): Machine =
  284. result = Machine(hello: "hello")
  285. registerMachine(facproc)
  286. proc createMachine =
  287. for machine in machineTypes:
  288. echo machine.factory().hello
  289. createMachine()
  290. # bug #15122
  291. import tables
  292. type
  293. BENodeKind = enum
  294. tkBytes,
  295. tkList,
  296. tkDict
  297. BENode = object
  298. case kind: BENodeKind
  299. of tkBytes: strVal: string
  300. of tkList: listVal: seq[BENode]
  301. of tkDict: dictVal: Table[string, BENode]
  302. var data = {
  303. "examples": {
  304. "values": BENode(
  305. kind: tkList,
  306. listVal: @[BENode(kind: tkBytes, strVal: "test")]
  307. )
  308. }.toTable()
  309. }.toTable()
  310. # For ARC listVal is empty for some reason
  311. doAssert data["examples"]["values"].listVal[0].strVal == "test"
  312. ###############################################################################
  313. # bug #15405
  314. import parsexml
  315. const test_xml_str = "<A><B>value</B></A>"
  316. var stream = newStringStream(test_xml_str)
  317. var xml: XmlParser
  318. open(xml, stream, "test")
  319. var xml2 = deepCopy(xml)
  320. proc text_parser(xml: var XmlParser) =
  321. var test_passed = false
  322. while true:
  323. xml.next()
  324. case xml.kind
  325. of xmlElementStart:
  326. if xml.elementName == "B":
  327. xml.next()
  328. if xml.kind == xmlCharData and xml.charData == "value":
  329. test_passed = true
  330. of xmlEof: break
  331. else: discard
  332. xml.close()
  333. doAssert(test_passed)
  334. text_parser(xml)
  335. text_parser(xml2)
  336. # bug #15599
  337. type
  338. PixelBuffer = ref object
  339. proc newPixelBuffer(): PixelBuffer =
  340. new(result) do (buffer: PixelBuffer):
  341. echo "ok"
  342. discard newPixelBuffer()
  343. # bug #17199
  344. proc passSeq(data: seq[string]) =
  345. # used the system.& proc initially
  346. let wat = data & "hello"
  347. proc test2 =
  348. let name = @["hello", "world"]
  349. passSeq(name)
  350. doAssert name == @["hello", "world"]
  351. static: test2() # was buggy
  352. test2()
  353. proc merge(x: sink seq[string], y: sink string): seq[string] =
  354. newSeq(result, x.len + 1)
  355. for i in 0..x.len-1:
  356. result[i] = move(x[i])
  357. result[x.len] = move(y)
  358. proc passSeq2(data: seq[string]) =
  359. # used the system.& proc initially
  360. let wat = merge(data, "hello")
  361. proc test3 =
  362. let name = @["hello", "world"]
  363. passSeq2(name)
  364. doAssert name == @["hello", "world"]
  365. static: test3() # was buggy
  366. test3()
  367. # bug #17712
  368. proc t17712 =
  369. var ppv = new int
  370. discard @[ppv]
  371. var el: ref int
  372. el = [ppv][0]
  373. echo el != nil
  374. t17712()
  375. # bug #18030
  376. type
  377. Foo = object
  378. n: int
  379. proc `=copy`(dst: var Foo, src: Foo) =
  380. echo "copying"
  381. dst.n = src.n
  382. proc `=sink`(dst: var Foo, src: Foo) =
  383. echo "sinking"
  384. dst.n = src.n
  385. var a: Foo
  386. proc putValue[T](n: T)
  387. proc useForward =
  388. putValue(123)
  389. proc putValue[T](n: T) =
  390. var b = Foo(n:n)
  391. a = b
  392. echo b.n
  393. useForward()
  394. # bug #17319
  395. type
  396. BrokenObject = ref object
  397. brokenType: seq[int]
  398. proc use(obj: BrokenObject) =
  399. discard
  400. method testMethod(self: BrokenObject) {.base.} =
  401. iterator testMethodIter() {.closure.} =
  402. use(self)
  403. var nameIterVar = testMethodIter
  404. nameIterVar()
  405. let mikasa = BrokenObject()
  406. mikasa.testMethod()
  407. # bug #19205
  408. type
  409. InputSectionBase* = object of RootObj
  410. relocations*: seq[int] # traced reference. string has a similar SIGSEGV.
  411. InputSection* = object of InputSectionBase
  412. proc fooz(sec: var InputSectionBase) =
  413. if sec of InputSection: # this line SIGSEGV.
  414. echo 42
  415. var sec = create(InputSection)
  416. sec[] = InputSection(relocations: newSeq[int]())
  417. fooz sec[]
  418. block:
  419. type
  420. Data = ref object
  421. id: int
  422. proc main =
  423. var x = Data(id: 99)
  424. var y = x
  425. x[] = Data(id: 778)[]
  426. doAssert y.id == 778
  427. doAssert x[].id == 778
  428. main()
  429. block: # bug #19857
  430. type
  431. ValueKind = enum VNull, VFloat, VObject # need 3 elements. Cannot remove VNull or VObject
  432. Value = object
  433. case kind: ValueKind
  434. of VFloat: fnum: float
  435. of VObject: tab: Table[int, int] # OrderedTable[T, U] also makes it fail.
  436. # "simpler" types also work though
  437. else: discard # VNull can be like this, but VObject must be filled
  438. # required. Pure proc works
  439. FormulaNode = proc(c: OrderedTable[string, int]): Value
  440. proc toF(v: Value): float =
  441. doAssert v.kind == VFloat
  442. case v.kind
  443. of VFloat: result = v.fnum
  444. else: result = 0.0
  445. proc foo() =
  446. let fuck = initOrderedTable[string, int]()
  447. proc cb(fuck: OrderedTable[string, int]): Value =
  448. # works:
  449. #result = Value(kind: VFloat, fnum: fuck["field_that_does_not_exist"].float)
  450. # broken:
  451. result = Value()
  452. discard "actuall runs!"
  453. let t = fuck["field_that_does_not_exist"]
  454. echo "never runs, but we crash after! ", t
  455. doAssertRaises(KeyError):
  456. let fn = FormulaNode(cb)
  457. let v = fn(fuck)
  458. #echo v
  459. let res = v.toF()
  460. foo()
  461. import std/options
  462. # bug #21592
  463. type Event* = object
  464. code*: string
  465. type App* = ref object of RootObj
  466. id*: string
  467. method process*(self: App): Option[Event] {.base.} =
  468. raise Exception.new_exception("not impl")
  469. # bug #21617
  470. type Test2 = ref object of RootObj
  471. method bug(t: Test2): seq[float] {.base.} = result = @[]
  472. block: # bug #22664
  473. type
  474. ElementKind = enum String, Number
  475. Element = object
  476. case kind: ElementKind
  477. of String:
  478. str: string
  479. of Number:
  480. num: float
  481. Calc = ref object
  482. stack: seq[Element]
  483. var calc = new Calc
  484. calc.stack.add Element(kind: Number, num: 200.0)
  485. doAssert $calc.stack == "@[(kind: Number, num: 200.0)]"
  486. let calc2 = calc
  487. calc2.stack = calc.stack # This nulls out the object in the stack
  488. doAssert $calc.stack == "@[(kind: Number, num: 200.0)]"
  489. doAssert $calc2.stack == "@[(kind: Number, num: 200.0)]"
  490. block: # bug #19250
  491. type
  492. Bar[T] = object
  493. err: proc(): string
  494. Foo[T] = object
  495. run: proc(): Bar[T]
  496. proc bar[T](err: proc(): string): Bar[T] =
  497. assert not err.isNil
  498. Bar[T](err: err)
  499. proc foo(): Foo[char] =
  500. result = Foo[char]()
  501. result.run = proc(): Bar[char] =
  502. # works
  503. # result = Bar[char](err: proc(): string = "x")
  504. # not work
  505. result = bar[char](proc(): string = "x")
  506. proc bug[T](fs: Foo[T]): Foo[T] =
  507. result = Foo[T]()
  508. result.run = proc(): Bar[T] =
  509. let res = fs.run()
  510. # works
  511. # var errors = @[res.err]
  512. # not work
  513. var errors: seq[proc(): string] = @[]
  514. errors.add res.err
  515. return bar[T] do () -> string:
  516. result = ""
  517. for err in errors:
  518. result.add res.err()
  519. doAssert bug(foo()).run().err() == "x"
  520. block: # bug #22259
  521. type
  522. ProcWrapper = tuple
  523. p: proc() {.closure.}
  524. proc f(wrapper: ProcWrapper) =
  525. let s = @[wrapper.p]
  526. let a = [wrapper.p]
  527. proc main =
  528. # let wrapper: ProcWrapper = ProcWrapper(p: proc {.closure.} = echo 10)
  529. let wrapper: ProcWrapper = (p: proc {.closure.} = echo 10)
  530. f(wrapper)
  531. main()
  532. block:
  533. block: # bug #22923
  534. block:
  535. let
  536. a: int = 100
  537. b: int32 = 200'i32
  538. let
  539. x = arrayWith(a, 8) # compiles
  540. y = arrayWith(b, 8) # internal error
  541. z = arrayWith(14, 8) # integer literal also results in a crash
  542. doAssert x == [100, 100, 100, 100, 100, 100, 100, 100]
  543. doAssert $y == "[200, 200, 200, 200, 200, 200, 200, 200]"
  544. doAssert z == [14, 14, 14, 14, 14, 14, 14, 14]
  545. block:
  546. let a: string = "nim"
  547. doAssert arrayWith(a, 3) == ["nim", "nim", "nim"]
  548. let b: char = 'c'
  549. doAssert arrayWith(b, 3) == ['c', 'c', 'c']
  550. let c: uint = 300'u
  551. doAssert $arrayWith(c, 3) == "[300, 300, 300]"
  552. block: # bug #23505
  553. type
  554. K = object
  555. C = object
  556. value: ptr K
  557. proc init(T: type C): C =
  558. let tmp = new K
  559. C(value: addr tmp[])
  560. discard init(C)
  561. block: # bug #23524
  562. type MyType = object
  563. a: int
  564. proc `=destroy`(typ: MyType) = discard
  565. var t1 = MyType(a: 100)
  566. var t2 = t1 # Should be a copy?
  567. proc main() =
  568. t2 = t1
  569. doAssert t1.a == 100
  570. doAssert t2.a == 100
  571. main()
  572. block: # bug #23907
  573. type
  574. Thingy = object
  575. value: int
  576. ExecProc[C] = proc(value: sink C): int {.nimcall.}
  577. proc `=copy`(a: var Thingy, b: Thingy) {.error.}
  578. var thingyDestroyCount = 0
  579. proc `=destroy`(thingy: Thingy) =
  580. assert(thingyDestroyCount <= 0)
  581. thingyDestroyCount += 1
  582. proc store(value: sink Thingy): int =
  583. result = value.value
  584. let callback: ExecProc[Thingy] = store
  585. doAssert callback(Thingy(value: 123)) == 123
  586. import std/strutils
  587. block: # bug #23974
  588. func g(e: seq[string]): lent seq[string] = result = e
  589. proc k(f: string): seq[string] = f.split("/")
  590. proc n() =
  591. const r = "/d/"
  592. let t =
  593. if true:
  594. k(r).g()
  595. else:
  596. k("/" & r).g()
  597. echo t
  598. n()
  599. block: # bug #23973
  600. func g(e: seq[string]): lent seq[string] = result = e
  601. proc k(f: string): seq[string] = f.split("/")
  602. proc n() =
  603. const r = "/test/empty" # or "/test/empty/1"
  604. let a = k(r).g()
  605. let t =
  606. if true:
  607. k(r).g()
  608. else:
  609. k("/" & r).g() # or raiseAssert ""
  610. doAssert t == a
  611. n()
  612. block: # bug #24141
  613. func reverse(s: var openArray[char]) =
  614. s[0] = 'f'
  615. func rev(s: var string) =
  616. s.reverse
  617. proc main =
  618. var abc = "abc"
  619. abc.rev
  620. doAssert abc == "fbc"
  621. main()
  622. block:
  623. type
  624. FooObj = object
  625. data: int
  626. Foo = ref FooObj
  627. proc delete(self: FooObj) =
  628. discard
  629. var s = Foo()
  630. new(s, delete)
  631. block:
  632. type
  633. FooObj = object
  634. data: int
  635. i1, i2, i3, i4: float
  636. Foo = ref FooObj
  637. proc delete(self: FooObj) =
  638. discard
  639. var s = Foo()
  640. new(s, delete)
  641. proc test_18070() = # bug #18070
  642. try:
  643. try:
  644. raise newException(CatchableError, "something")
  645. except:
  646. raise newException(CatchableError, "something else")
  647. except:
  648. doAssert getCurrentExceptionMsg() == "something else"
  649. let msg = getCurrentExceptionMsg()
  650. doAssert msg == "", "expected empty string but got: " & $msg
  651. test_18070()