t14383.nim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. discard """
  2. cmd: "nim c --gc:arc $file"
  3. output: '''
  4. hello
  5. hello
  6. @["a", "b"]
  7. ---------------------
  8. plain:
  9. destroying: ('first', 42)
  10. destroying: ('second', 20)
  11. destroying: ('third', 12)
  12. Option[T]:
  13. destroying: ('first', 42)
  14. destroying: ('second', 20)
  15. destroying: ('third', 12)
  16. seq[T]:
  17. destroying: ('first', 42)
  18. destroying: ('second', 20)
  19. destroying: ('third', 12)
  20. 1 1
  21. '''
  22. """
  23. import dmodule
  24. var val = parseMinValue()
  25. if val.kind == minDictionary:
  26. echo val
  27. #------------------------------------------------------------------------------
  28. # Issue #15238
  29. #------------------------------------------------------------------------------
  30. proc sinkArg(x: sink seq[string]) =
  31. discard
  32. proc varArg(lst: var seq[string]) =
  33. sinkArg(lst)
  34. var x = @["a", "b"]
  35. varArg(x)
  36. echo x
  37. #------------------------------------------------------------------------------
  38. # Issue #15286
  39. #------------------------------------------------------------------------------
  40. import std/os
  41. discard getFileInfo(".")
  42. #------------------------------------------------------------------------------
  43. # Issue #15707
  44. #------------------------------------------------------------------------------
  45. type
  46. JVMObject = ref object
  47. proc freeJVMObject(o: JVMObject) =
  48. discard
  49. proc fromJObject(T: typedesc[JVMObject]): T =
  50. result.new(cast[proc(r: T) {.nimcall.}](freeJVMObject))
  51. discard JVMObject.fromJObject()
  52. #------------------------------------------------------------------------------
  53. # Issue #15910
  54. #------------------------------------------------------------------------------
  55. import options
  56. type
  57. Thing = object
  58. name: string
  59. age: int
  60. proc `=destroy`(thing: var Thing) =
  61. if thing.name != "":
  62. echo "destroying: ('", thing.name, "', ", thing.age, ")"
  63. `=destroy`(thing.name)
  64. `=destroy`(thing.age)
  65. proc plain() =
  66. var t = Thing(name: "first", age: 42)
  67. t = Thing(name: "second", age: 20)
  68. t = Thing()
  69. let u = Thing(name: "third", age: 12)
  70. proc optionT() =
  71. var t = Thing(name: "first", age: 42).some
  72. t = Thing(name: "second", age: 20).some
  73. t = none(Thing)
  74. let u = Thing(name: "third", age: 12).some
  75. proc seqT() =
  76. var t = @[Thing(name: "first", age: 42)]
  77. t = @[Thing(name: "second", age: 20)]
  78. t = @[]
  79. let u = @[Thing(name: "third", age: 12)]
  80. echo "---------------------"
  81. echo "plain:"
  82. plain()
  83. echo()
  84. echo "Option[T]:"
  85. optionT()
  86. echo()
  87. echo "seq[T]:"
  88. seqT()
  89. echo()
  90. #------------------------------------------------------------------------------
  91. # Issue #16120, const seq into sink
  92. #------------------------------------------------------------------------------
  93. proc main =
  94. let avals = @[@[1.0'f32, 4.0, 7.0, 10.0]]
  95. let rankdef = avals
  96. echo avals.len, " ", rankdef.len
  97. main()
  98. #------------------------------------------------------------------------------
  99. # Issue #16722, ref on distinct type, wrong destructors called
  100. #------------------------------------------------------------------------------
  101. type
  102. Obj = object of RootObj
  103. ObjFinal = object
  104. ObjRef = ref Obj
  105. ObjFinalRef = ref ObjFinal
  106. D = distinct Obj
  107. DFinal = distinct ObjFinal
  108. DRef = ref D
  109. DFinalRef = ref DFinal
  110. proc `=destroy`(o: var Obj) =
  111. doAssert false, "no Obj is constructed in this sample"
  112. proc `=destroy`(o: var ObjFinal) =
  113. doAssert false, "no ObjFinal is constructed in this sample"
  114. var dDestroyed: int
  115. proc `=destroy`(d: var D) =
  116. dDestroyed.inc
  117. proc `=destroy`(d: var DFinal) =
  118. dDestroyed.inc
  119. func newD(): DRef =
  120. DRef ObjRef()
  121. func newDFinal(): DFinalRef =
  122. DFinalRef ObjFinalRef()
  123. proc testRefs() =
  124. discard newD()
  125. discard newDFinal()
  126. testRefs()
  127. doAssert(dDestroyed == 2)
  128. #------------------------------------------------------------------------------
  129. # Issue #16185, complex self-assingment elimination
  130. #------------------------------------------------------------------------------
  131. type
  132. CpuStorage*[T] = ref CpuStorageObj[T]
  133. CpuStorageObj[T] = object
  134. size*: int
  135. raw_buffer*: ptr UncheckedArray[T]
  136. Tensor[T] = object
  137. buf*: CpuStorage[T]
  138. TestObject = object
  139. x: Tensor[float]
  140. proc `=destroy`[T](s: var CpuStorageObj[T]) =
  141. if s.raw_buffer != nil:
  142. s.raw_buffer.deallocShared()
  143. s.size = 0
  144. s.raw_buffer = nil
  145. proc `=`[T](a: var CpuStorageObj[T]; b: CpuStorageObj[T]) {.error.}
  146. proc allocCpuStorage[T](s: var CpuStorage[T], size: int) =
  147. new(s)
  148. s.raw_buffer = cast[ptr UncheckedArray[T]](allocShared0(sizeof(T) * size))
  149. s.size = size
  150. proc newTensor[T](size: int): Tensor[T] =
  151. allocCpuStorage(result.buf, size)
  152. proc `[]`[T](t: Tensor[T], idx: int): T = t.buf.raw_buffer[idx]
  153. proc `[]=`[T](t: Tensor[T], idx: int, val: T) = t.buf.raw_buffer[idx] = val
  154. proc toTensor[T](s: seq[T]): Tensor[T] =
  155. result = newTensor[T](s.len)
  156. for i, x in s:
  157. result[i] = x
  158. proc main2() =
  159. var t: TestObject
  160. t.x = toTensor(@[1.0, 2, 3, 4])
  161. t.x = t.x
  162. doAssert(t.x.buf != nil) # self-assignment above should be eliminated
  163. main2()