tnonvardestructor.nim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. discard """
  2. targets: "c cpp"
  3. matrix: "--mm:arc; --mm:orc"
  4. """
  5. block:
  6. type
  7. PublicKey = array[32, uint8]
  8. PrivateKey = array[64, uint8]
  9. proc ed25519_create_keypair(publicKey: ptr PublicKey; privateKey: ptr PrivateKey) =
  10. publicKey[][0] = uint8(88)
  11. type
  12. KeyPair = object
  13. public: PublicKey
  14. private: PrivateKey
  15. proc initKeyPair(): KeyPair =
  16. ed25519_create_keypair(result.public.addr, result.private.addr)
  17. let keys = initKeyPair()
  18. doAssert keys.public[0] == 88
  19. template minIndexByIt: untyped =
  20. var other = 3
  21. other
  22. proc bug20303() =
  23. var hlibs = @["hello", "world", "how", "are", "you"]
  24. let res = hlibs[minIndexByIt()]
  25. doAssert res == "are"
  26. bug20303()
  27. proc main() = # todo bug with templates
  28. block: # bug #11267
  29. var a: seq[char] = block: @[]
  30. doAssert a == @[]
  31. # 2
  32. proc b: seq[string] =
  33. discard
  34. @[]
  35. doAssert b() == @[]
  36. static: main()
  37. main()
  38. type Obj = tuple
  39. value: int
  40. arr: seq[int]
  41. proc bug(): seq[Obj] =
  42. result.add (value: 0, arr: @[])
  43. result[^1].value = 1
  44. result[^1].arr.add 1
  45. # bug #19990
  46. let s = bug()
  47. doAssert s[0] == (value: 1, arr: @[1])
  48. block: # bug #21974
  49. type Test[T] = ref object
  50. values : seq[T]
  51. counter: int
  52. proc newTest[T](): Test[T] =
  53. result = new(Test[T])
  54. result.values = newSeq[T](16)
  55. result.counter = 0
  56. proc push[T](self: Test[T], value: T) =
  57. self.counter += 1
  58. if self.counter >= self.values.len:
  59. self.values.setLen(self.values.len * 2)
  60. self.values[self.counter - 1] = value
  61. proc pop[T](self: Test[T]): T =
  62. result = self.values[0]
  63. self.values[0] = self.values[self.counter - 1] # <--- This line
  64. self.counter -= 1
  65. type X = tuple
  66. priority: int
  67. value : string
  68. var a = newTest[X]()
  69. a.push((1, "One"))
  70. doAssert a.pop.value == "One"
  71. # bug #21987
  72. type
  73. EmbeddedImage* = distinct Image
  74. Image = object
  75. len: int
  76. proc imageCopy*(image: Image): Image {.nodestroy.}
  77. proc `=destroy`*(x: Image) =
  78. discard
  79. proc `=sink`*(dest: var Image; source: Image) =
  80. `=destroy`(dest)
  81. wasMoved(dest)
  82. proc `=dup`*(source: Image): Image {.nodestroy.} =
  83. result = imageCopy(source)
  84. proc `=copy`*(dest: var Image; source: Image) =
  85. dest = imageCopy(source) # calls =sink implicitly
  86. proc `=destroy`*(x: EmbeddedImage) = discard
  87. proc `=dup`*(source: EmbeddedImage): EmbeddedImage {.nodestroy.} = source
  88. proc `=copy`*(dest: var EmbeddedImage; source: EmbeddedImage) {.nodestroy.} =
  89. dest = source
  90. proc imageCopy*(image: Image): Image =
  91. result = image
  92. proc main2 =
  93. block:
  94. var a = Image(len: 2).EmbeddedImage
  95. var b = Image(len: 1).EmbeddedImage
  96. b = a
  97. doAssert Image(a).len == 2
  98. doAssert Image(b).len == 2
  99. block:
  100. var a = Image(len: 2)
  101. var b = Image(len: 1)
  102. b = a
  103. doAssert a.len == 2
  104. doAssert b.len == 0
  105. main2()
  106. type
  107. Edge = object
  108. neighbor {.cursor.}: Node
  109. NodeObj = object
  110. neighbors: seq[Edge]
  111. label: string
  112. visited: bool
  113. Node = ref NodeObj
  114. Graph = object
  115. nodes: seq[Node]
  116. proc `=destroy`(x: NodeObj) =
  117. `=destroy`(x.neighbors)
  118. `=destroy`(x.label)
  119. proc addNode(self: var Graph; label: string): Node =
  120. self.nodes.add(Node(label: label))
  121. result = self.nodes[^1]
  122. proc addEdge(self: Graph; source, neighbor: Node) =
  123. source.neighbors.add(Edge(neighbor: neighbor))
  124. block:
  125. proc main =
  126. var graph: Graph
  127. let nodeA = graph.addNode("a")
  128. let nodeB = graph.addNode("b")
  129. let nodeC = graph.addNode("c")
  130. graph.addEdge(nodeA, neighbor = nodeB)
  131. graph.addEdge(nodeA, neighbor = nodeC)
  132. main()
  133. block:
  134. type RefObj = ref object
  135. proc `[]`(val: static[int]) = # works with different name/overload or without static arg
  136. discard
  137. template noRef(T: typedesc): typedesc = # works without template indirection
  138. typeof(default(T)[])
  139. proc `=destroy`(x: noRef(RefObj)) =
  140. discard
  141. proc foo =
  142. var x = new RefObj
  143. doAssert $(x[]) == "()"
  144. # bug #11705
  145. foo()
  146. block: # bug #22197
  147. type
  148. H5IdObj = object
  149. H5Id = ref H5IdObj
  150. FileID = distinct H5Id
  151. H5GroupObj = object
  152. file_id: FileID
  153. H5Group = ref H5GroupObj
  154. ## This would make it work!
  155. #proc `=destroy`*(x: FileID) = `=destroy`(cast[H5Id](x))
  156. ## If this does not exist, it also works!
  157. proc newFileID(): FileID = FileID(H5Id())
  158. proc `=destroy`(grp: H5GroupObj) =
  159. ## Closes the group and resets all references to nil.
  160. if cast[pointer](grp.fileId) != nil:
  161. `=destroy`(grp.file_id)
  162. var grp = H5Group()
  163. reset(grp.file_id)
  164. reset(grp)
  165. import std/tables
  166. block: # bug #22286
  167. type
  168. A = object
  169. B = object
  170. a: A
  171. C = object
  172. b: B
  173. proc `=destroy`(self: A) =
  174. echo "destroyed"
  175. proc `=destroy`(self: C) =
  176. `=destroy`(self.b)
  177. var c = C()
  178. block: # https://forum.nim-lang.org/t/10642
  179. type AObj = object
  180. name: string
  181. tableField: Table[string, string]
  182. proc `=destroy`(x: AObj) =
  183. `=destroy`(x.name)
  184. `=destroy`(x.tableField)