tarc_orc.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. result = KeyPair()
  17. ed25519_create_keypair(result.public.addr, result.private.addr)
  18. let keys = initKeyPair()
  19. doAssert keys.public[0] == 88
  20. template minIndexByIt: untyped =
  21. var other = 3
  22. other
  23. proc bug20303() =
  24. var hlibs = @["hello", "world", "how", "are", "you"]
  25. let res = hlibs[minIndexByIt()]
  26. doAssert res == "are"
  27. bug20303()
  28. proc main() = # todo bug with templates
  29. block: # bug #11267
  30. var a: seq[char] = block: @[]
  31. doAssert a == @[]
  32. # 2
  33. proc b: seq[string] =
  34. discard
  35. @[]
  36. doAssert b() == @[]
  37. static: main()
  38. main()
  39. type Obj = tuple
  40. value: int
  41. arr: seq[int]
  42. proc bug(): seq[Obj] =
  43. result = @[]
  44. result.add (value: 0, arr: @[])
  45. result[^1].value = 1
  46. result[^1].arr.add 1
  47. # bug #19990
  48. let s = bug()
  49. doAssert s[0] == (value: 1, arr: @[1])
  50. block: # bug #21974
  51. type Test[T] = ref object
  52. values : seq[T]
  53. counter: int
  54. proc newTest[T](): Test[T] =
  55. result = new(Test[T])
  56. result.values = newSeq[T](16)
  57. result.counter = 0
  58. proc push[T](self: Test[T], value: T) =
  59. self.counter += 1
  60. if self.counter >= self.values.len:
  61. self.values.setLen(self.values.len * 2)
  62. self.values[self.counter - 1] = value
  63. proc pop[T](self: Test[T]): T =
  64. result = self.values[0]
  65. self.values[0] = self.values[self.counter - 1] # <--- This line
  66. self.counter -= 1
  67. type X = tuple
  68. priority: int
  69. value : string
  70. var a = newTest[X]()
  71. a.push((1, "One"))
  72. doAssert a.pop.value == "One"
  73. # bug #21987
  74. type
  75. EmbeddedImage* = distinct Image
  76. Image = object
  77. len: int
  78. proc imageCopy*(image: Image): Image {.nodestroy.}
  79. proc `=destroy`*(x: var Image) =
  80. discard
  81. proc `=sink`*(dest: var Image; source: Image) =
  82. `=destroy`(dest)
  83. wasMoved(dest)
  84. proc `=dup`*(source: Image): Image {.nodestroy.} =
  85. result = imageCopy(source)
  86. proc `=copy`*(dest: var Image; source: Image) =
  87. dest = imageCopy(source) # calls =sink implicitly
  88. proc `=destroy`*(x: var EmbeddedImage) = discard
  89. proc `=dup`*(source: EmbeddedImage): EmbeddedImage {.nodestroy.} = source
  90. proc `=copy`*(dest: var EmbeddedImage; source: EmbeddedImage) {.nodestroy.} =
  91. dest = source
  92. proc imageCopy*(image: Image): Image =
  93. result = image
  94. proc main2 =
  95. block:
  96. var a = Image(len: 2).EmbeddedImage
  97. var b = Image(len: 1).EmbeddedImage
  98. b = a
  99. doAssert Image(a).len == 2
  100. doAssert Image(b).len == 2
  101. block:
  102. var a = Image(len: 2)
  103. var b = Image(len: 1)
  104. b = a
  105. doAssert a.len == 2
  106. doAssert b.len == 0
  107. main2()
  108. block:
  109. type
  110. TestObj = object of RootObj
  111. name: string
  112. TestSubObj = object of TestObj
  113. objname: string
  114. proc `=destroy`(x: TestObj) =
  115. `=destroy`(x.name)
  116. proc `=destroy`(x: TestSubObj) =
  117. `=destroy`(x.objname)
  118. `=destroy`(TestObj(x))
  119. proc testCase() =
  120. let t1 {.used.} = TestSubObj(objname: "tso1", name: "to1")
  121. proc main() =
  122. testCase()
  123. main()
  124. block: # bug #23858
  125. type Object = object
  126. a: int
  127. b: ref int
  128. var x = 0
  129. proc fn(): auto {.cdecl.} =
  130. inc x
  131. return Object()
  132. discard fn()
  133. doAssert x == 1
  134. block: # bug #24147
  135. type
  136. O = object of RootObj
  137. val: string
  138. OO = object of O
  139. proc `=copy`(dest: var O, src: O) =
  140. dest.val = src.val
  141. let oo = OO(val: "hello world")
  142. var ooCopy : OO
  143. `=copy`(ooCopy, oo)