tarcmisc.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. discard """
  2. output: '''
  3. 123xyzabc
  4. destroyed: false
  5. destroyed: false
  6. 1
  7. (x: "0")
  8. (x: "1")
  9. (x: "2")
  10. (x: "3")
  11. (x: "4")
  12. (x: "5")
  13. (x: "6")
  14. (x: "7")
  15. (x: "8")
  16. (x: "9")
  17. (x: "10")
  18. 0
  19. closed
  20. destroying variable
  21. '''
  22. cmd: "nim c --gc:arc $file"
  23. """
  24. proc takeSink(x: sink string): bool = true
  25. proc b(x: sink string): string =
  26. if takeSink(x):
  27. return x & "abc"
  28. proc bbb(inp: string) =
  29. let y = inp & "xyz"
  30. echo b(y)
  31. bbb("123")
  32. # bug #13691
  33. type Variable = ref object
  34. value: int
  35. proc `=destroy`(self: var typeof(Variable()[])) =
  36. echo "destroying variable"
  37. proc newVariable(value: int): Variable =
  38. result = Variable()
  39. result.value = value
  40. proc test(count: int) =
  41. var v {.global.} = newVariable(10)
  42. var count = count - 1
  43. if count == 0: return
  44. test(count)
  45. echo "destroyed: ", v.isNil
  46. test(3)
  47. #------------------------------------------------------------------------------
  48. # issue #13810
  49. import streams
  50. type
  51. A = ref AObj
  52. AObj = object of RootObj
  53. io: Stream
  54. B = ref object of A
  55. x: int
  56. proc `=destroy`(x: var AObj) =
  57. close(x.io)
  58. echo "closed"
  59. var x = B(io: newStringStream("thestream"))
  60. #------------------------------------------------------------------------------
  61. # issue #14003
  62. proc cryptCTR*(nonce: var openArray[char]) =
  63. nonce[1] = 'A'
  64. proc main() =
  65. var nonce1 = "0123456701234567"
  66. cryptCTR(nonce1)
  67. doAssert(nonce1 == "0A23456701234567")
  68. var nonce2 = "01234567"
  69. cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
  70. doAssert(nonce2 == "0A234567")
  71. main()
  72. # bug #14079
  73. import std/algorithm
  74. let
  75. n = @["c", "b"]
  76. q = @[("c", "2"), ("b", "1")]
  77. assert n.sortedByIt(it) == @["b", "c"], "fine"
  78. assert q.sortedByIt(it[0]) == @[("b", "1"), ("c", "2")], "fails under arc"
  79. #------------------------------------------------------------------------------
  80. # issue #14236
  81. type
  82. MyType = object
  83. a: seq[int]
  84. proc re(x: static[string]): static MyType =
  85. MyType()
  86. proc match(inp: string, rg: static MyType) =
  87. doAssert rg.a.len == 0
  88. match("ac", re"a(b|c)")
  89. #------------------------------------------------------------------------------
  90. # issue #14243
  91. type
  92. Game* = ref object
  93. proc free*(game: Game) =
  94. let a = 5
  95. proc newGame*(): Game =
  96. new(result, free)
  97. var game*: Game
  98. #------------------------------------------------------------------------------
  99. # issue #14333
  100. type
  101. SimpleLoop = object
  102. Lsg = object
  103. loops: seq[ref SimpleLoop]
  104. root: ref SimpleLoop
  105. var lsg: Lsg
  106. lsg.loops.add lsg.root
  107. echo lsg.loops.len
  108. # bug #14495
  109. type
  110. Gah = ref object
  111. x: string
  112. proc bug14495 =
  113. var owners: seq[Gah]
  114. for i in 0..10:
  115. owners.add Gah(x: $i)
  116. var x: seq[Gah]
  117. for i in 0..10:
  118. x.add owners[i]
  119. for i in 0..100:
  120. setLen(x, 0)
  121. setLen(x, 10)
  122. for i in 0..x.len-1:
  123. if x[i] != nil:
  124. echo x[i][]
  125. for o in owners:
  126. echo o[]
  127. bug14495()
  128. # bug #14396
  129. type
  130. Spinny = ref object
  131. t: ref int
  132. text: string
  133. proc newSpinny*(): Spinny =
  134. Spinny(t: new(int), text: "hello")
  135. proc spinnyLoop(x: ref int, spinny: sink Spinny) =
  136. echo x[]
  137. proc start*(spinny: sink Spinny) =
  138. spinnyLoop(spinny.t, spinny)
  139. var spinner1 = newSpinny()
  140. spinner1.start()
  141. # bug #14345
  142. type
  143. SimpleLoopB = ref object
  144. children: seq[SimpleLoopB]
  145. parent: SimpleLoopB
  146. proc addChildLoop(self: SimpleLoopB, loop: SimpleLoopB) =
  147. self.children.add loop
  148. proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
  149. self.parent = parent
  150. self.parent.addChildLoop(self)
  151. var l = SimpleLoopB()
  152. l.setParent(l)