tv2_cast.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. data = cast[string](encode(cast[seq[byte]](
  13. :tmpD = newString(100)
  14. :tmpD)))
  15. `=destroy`(:tmpD)
  16. `=destroy`(data)
  17. -- end of expandArc ------------------------
  18. --expandArc: main1
  19. var
  20. s
  21. data
  22. s = newString(100)
  23. data = cast[string](encode(toOpenArrayByte(s, 0, len(s) - 1)))
  24. `=destroy`(data)
  25. `=destroy`(s)
  26. -- end of expandArc ------------------------
  27. --expandArc: main2
  28. var
  29. s
  30. data
  31. s = newSeq(100)
  32. data = cast[string](encode(s))
  33. `=destroy`(data)
  34. `=destroy_1`(s)
  35. -- end of expandArc ------------------------
  36. --expandArc: main3
  37. var
  38. data
  39. :tmpD
  40. data = cast[string](encode do:
  41. :tmpD = newSeq(100)
  42. :tmpD)
  43. `=destroy`(:tmpD)
  44. `=destroy_1`(data)
  45. -- end of expandArc ------------------------
  46. '''
  47. """
  48. func encode*(src: openArray[byte]): seq[byte] =
  49. result = newSeq[byte](src.len)
  50. template compress*(src: string): string =
  51. cast[string](encode(cast[seq[byte]](src)))
  52. proc main =
  53. let data = compress(newString(100))
  54. main()
  55. proc main1 =
  56. var
  57. s = newString(100)
  58. let data = cast[string](encode(s.toOpenArrayByte(0, s.len-1)))
  59. main1()
  60. proc main2 =
  61. var
  62. s = newSeq[byte](100)
  63. let data = cast[string](encode(s))
  64. main2()
  65. proc main3 =
  66. let data = cast[string](encode(newSeq[byte](100)))
  67. main3()
  68. # bug #11018
  69. discard cast[seq[uint8]](@[1])
  70. discard cast[seq[uint8]]("test")
  71. echo cast[seq[uint8]](@[1])
  72. echo cast[seq[uint8]]("test")
  73. discard cast[string](@[116'u8, 101, 115, 116])
  74. #echo cast[string](@[116'u8, 101, 115, 116, 0])
  75. var a = cast[seq[uint32]]("test1234")
  76. a.setLen(2)
  77. echo a
  78. #issue 11204
  79. var ac {.compileTime.} = @["a", "b"]
  80. const bc = ac.len
  81. type
  82. O = object of RootRef
  83. i: int
  84. O1 = object of O
  85. O2 = object of O
  86. proc `=destroy`(o: var O) =
  87. echo "destroying O"
  88. proc `=destroy`(o: var O1) =
  89. echo "destroying O1"
  90. proc `=destroy`(o: var O2) =
  91. echo "destroying O2"
  92. proc test =
  93. let o3 = cast[ref O2]((ref O1)())
  94. test()