tv2_cast.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. discard """
  2. output: '''@[1]
  3. @[116, 101, 115, 116]
  4. @[1953719668, 875770417]
  5. destroying O1'''
  6. cmd: '''nim c --gc:arc --expandArc:main --expandArc:main1 --expandArc:main2 --expandArc:main3 --hints:off --assertions:off $file'''
  7. nimout: '''--expandArc: main
  8. var
  9. data
  10. :tmpD
  11. :tmpD_1
  12. :tmpD_2
  13. data =
  14. wasMoved(:tmpD)
  15. `=copy`(:tmpD, cast[string](
  16. :tmpD_2 = encode(cast[seq[byte]](
  17. :tmpD_1 = newString(100)
  18. :tmpD_1))
  19. :tmpD_2))
  20. :tmpD
  21. `=destroy`(:tmpD_2)
  22. `=destroy_1`(:tmpD_1)
  23. `=destroy_1`(data)
  24. -- end of expandArc ------------------------
  25. --expandArc: main1
  26. var
  27. s
  28. data
  29. :tmpD
  30. :tmpD_1
  31. s = newString(100)
  32. data =
  33. wasMoved(:tmpD)
  34. `=copy`(:tmpD, cast[string](
  35. :tmpD_1 = encode(toOpenArrayByte(s, 0, len(s) - 1))
  36. :tmpD_1))
  37. :tmpD
  38. `=destroy`(:tmpD_1)
  39. `=destroy_1`(data)
  40. `=destroy_1`(s)
  41. -- end of expandArc ------------------------
  42. --expandArc: main2
  43. var
  44. s
  45. data
  46. :tmpD
  47. :tmpD_1
  48. s = newSeq(100)
  49. data =
  50. wasMoved(:tmpD)
  51. `=copy`(:tmpD, cast[string](
  52. :tmpD_1 = encode(s)
  53. :tmpD_1))
  54. :tmpD
  55. `=destroy`(:tmpD_1)
  56. `=destroy_1`(data)
  57. `=destroy`(s)
  58. -- end of expandArc ------------------------
  59. --expandArc: main3
  60. var
  61. data
  62. :tmpD
  63. :tmpD_1
  64. :tmpD_2
  65. data =
  66. wasMoved(:tmpD)
  67. `=copy`(:tmpD, cast[string](
  68. :tmpD_2 = encode do:
  69. :tmpD_1 = newSeq(100)
  70. :tmpD_1
  71. :tmpD_2))
  72. :tmpD
  73. `=destroy`(:tmpD_2)
  74. `=destroy`(:tmpD_1)
  75. `=destroy_1`(data)
  76. -- end of expandArc ------------------------'''
  77. """
  78. func encode*(src: openArray[byte]): seq[byte] =
  79. result = newSeq[byte](src.len)
  80. template compress*(src: string): string =
  81. cast[string](encode(cast[seq[byte]](src)))
  82. proc main =
  83. let data = compress(newString(100))
  84. main()
  85. proc main1 =
  86. var
  87. s = newString(100)
  88. let data = cast[string](encode(s.toOpenArrayByte(0, s.len-1)))
  89. main1()
  90. proc main2 =
  91. var
  92. s = newSeq[byte](100)
  93. let data = cast[string](encode(s))
  94. main2()
  95. proc main3 =
  96. let data = cast[string](encode(newSeq[byte](100)))
  97. main3()
  98. # bug #11018
  99. discard cast[seq[uint8]](@[1])
  100. discard cast[seq[uint8]]("test")
  101. echo cast[seq[uint8]](@[1])
  102. echo cast[seq[uint8]]("test")
  103. discard cast[string](@[116'u8, 101, 115, 116])
  104. #echo cast[string](@[116'u8, 101, 115, 116, 0])
  105. var a = cast[seq[uint32]]("test1234")
  106. a.setLen(2)
  107. echo a
  108. #issue 11204
  109. var ac {.compileTime.} = @["a", "b"]
  110. const bc = ac.len
  111. type
  112. O = object of RootRef
  113. i: int
  114. O1 = object of O
  115. O2 = object of O
  116. proc `=destroy`(o: var O) =
  117. echo "destroying O"
  118. proc `=destroy`(o: var O1) =
  119. echo "destroying O1"
  120. proc `=destroy`(o: var O2) =
  121. echo "destroying O2"
  122. proc test =
  123. let o3 = cast[ref O2]((ref O1)())
  124. test()