tv2_cast.nim 2.5 KB

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