tmove_objconstr.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. discard """
  2. output: '''test created
  3. test destroyed 0
  4. 1
  5. 2
  6. 3
  7. 4
  8. Pony is dying!'''
  9. joinable: false
  10. targets: "c"
  11. """
  12. # bug #4214
  13. type
  14. Data = object
  15. data: string
  16. rc: int
  17. proc `=destroy`(d: var Data) =
  18. dec d.rc
  19. echo d.data, " destroyed ", d.rc
  20. proc `=`(dst: var Data, src: Data) =
  21. echo src.data, " copied"
  22. dst.data = src.data & " (copy)"
  23. dec dst.rc
  24. inc dst.rc
  25. proc initData(s: string): Data =
  26. result = Data(data: s, rc: 1)
  27. echo s, " created"
  28. proc pointlessWrapper(s: string): Data =
  29. result = initData(s)
  30. proc main =
  31. var x = pointlessWrapper"test"
  32. when true:
  33. main()
  34. # bug #985
  35. type
  36. Pony = object
  37. name: string
  38. proc `=destroy`(o: var Pony) =
  39. echo "Pony is dying!"
  40. proc getPony: Pony =
  41. result = Pony(name: "Sparkles")
  42. iterator items(p: Pony): int =
  43. for i in 1..4:
  44. yield i
  45. for x in getPony():
  46. echo x
  47. #------------------------------------------------------------
  48. #-- Move into tuple constructor and move on tuple unpacking
  49. #------------------------------------------------------------
  50. type
  51. MySeqNonCopyable* = object
  52. len: int
  53. data: ptr UncheckedArray[float]
  54. proc `=destroy`*(m: var MySeqNonCopyable) {.inline.} =
  55. if m.data != nil:
  56. deallocShared(m.data)
  57. m.data = nil
  58. proc `=`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.error.}
  59. proc `=sink`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.inline.} =
  60. if m.data != m2.data:
  61. if m.data != nil:
  62. `=destroy`(m)
  63. m.len = m2.len
  64. m.data = m2.data
  65. proc len*(m: MySeqNonCopyable): int {.inline.} = m.len
  66. proc `[]`*(m: MySeqNonCopyable; i: int): float {.inline.} =
  67. m.data[i.int]
  68. proc `[]=`*(m: var MySeqNonCopyable; i, val: float) {.inline.} =
  69. m.data[i.int] = val
  70. proc setTo(s: var MySeqNonCopyable, val: float) =
  71. for i in 0..<s.len.int:
  72. s.data[i] = val
  73. proc newMySeq*(size: int, initial_value = 0.0): MySeqNonCopyable =#
  74. result.len = size
  75. if size > 0:
  76. result.data = cast[ptr UncheckedArray[float]](createShared(float, size))
  77. result.setTo(initial_value)
  78. proc myfunc(x, y: int): (MySeqNonCopyable, MySeqNonCopyable) =
  79. result = (newMySeq(x, 1.0), newMySeq(y, 5.0))
  80. proc myfunc2(x, y: int): tuple[a: MySeqNonCopyable, b:int, c:MySeqNonCopyable] =
  81. var cc = newMySeq(y, 5.0)
  82. (a: case x:
  83. of 1:
  84. let (z1, z2) = myfunc(x, y)
  85. z2
  86. elif x > 5: raise newException(ValueError, "new error")
  87. else: newMySeq(x, 1.0),
  88. b: 0,
  89. c: block:
  90. var tmp = if y > 0: move(cc) else: newMySeq(1, 3.0)
  91. tmp[0] = 5
  92. tmp
  93. )
  94. let (seq1, seq2) = myfunc(2, 3)
  95. doAssert seq1.len == 2
  96. doAssert seq1[0] == 1.0
  97. doAssert seq2.len == 3
  98. doAssert seq2[0] == 5.0
  99. var (seq3, i, _) = myfunc2(2, 3)
  100. doAssert seq3.len == 2
  101. doAssert seq3[0] == 1.0
  102. var seq4, seq5: MySeqNonCopyable
  103. (seq4, i, seq5) = myfunc2(2, 3)
  104. seq4 = block:
  105. var tmp = newMySeq(4, 1.0)
  106. tmp[0] = 3.0
  107. tmp
  108. doAssert seq4[0] == 3.0
  109. import macros
  110. seq4 =
  111. if i > 0: newMySeq(2, 5.0)
  112. elif i < -100: raise newException(ValueError, "Parse Error")
  113. else: newMySeq(2, 3.0)
  114. seq4 =
  115. case (char) i:
  116. of 'A', {'W'..'Z'}: newMySeq(2, 5.0)
  117. of 'B': quit(-1)
  118. else:
  119. let (x1, x2, x3) = myfunc2(2, 3)
  120. x3
  121. #------------------------------------------------------------
  122. #-- Move into array constructor
  123. #------------------------------------------------------------
  124. var ii = 1
  125. let arr2 = [newMySeq(2, 5.0), if i > 1: newMySeq(3, 1.0) else: newMySeq(0, 0.0)]
  126. var seqOfSeq2 = @[newMySeq(2, 5.0), newMySeq(3, 1.0)]
  127. ## issue #10462
  128. proc myfuncLoop(x: int): MySeqNonCopyable =
  129. for i in 0..<x:
  130. var cc = newMySeq(i, 5.0)
  131. result = cc
  132. discard myfuncLoop(3)
  133. #------------------------------------------------------------
  134. # Move into table via openArray
  135. #------------------------------------------------------------
  136. type
  137. TableNonCopyable = object
  138. x: seq[(string, MySeqNonCopyable)]
  139. proc toTable(pairs: sink openArray[(string, MySeqNonCopyable)]): TableNonCopyable =
  140. discard
  141. let mytable = {"a": newMySeq(2, 5.0)}.toTable