summary refs log tree commit diff stats
path: root/tests/destructor/tv2_cast.nim
blob: 4ff2dc9a07f19746a0f246495a7869e6f3fe78a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
discard """
  output: '''@[1]
@[116, 101, 115, 116]
@[1953719668, 875770417]
destroying O1'''
  cmd: '''nim c --mm:arc --expandArc:main --expandArc:main1 --expandArc:main2 --expandArc:main3 --hints:off --assertions:off $file'''
  nimout: '''
--expandArc: main

var
  data
  :tmpD
  :tmpD_1
  :tmpD_2
data =
  :tmpD = `=dup`(cast[string](
    :tmpD_2 = encode(cast[seq[byte]](
      :tmpD_1 = newString(100)
      :tmpD_1))
    :tmpD_2))
  :tmpD
`=destroy`(:tmpD_2)
`=destroy_1`(:tmpD_1)
`=destroy_1`(data)
-- end of expandArc ------------------------
--expandArc: main1

var
  s
  data
  :tmpD
  :tmpD_1
s = newString(100)
data =
  :tmpD = `=dup`(cast[string](
    :tmpD_1 = encode(toOpenArrayByte(s, 0, len(s) - 1))
    :tmpD_1))
  :tmpD
`=destroy`(:tmpD_1)
`=destroy_1`(data)
`=destroy_1`(s)
-- end of expandArc ------------------------
--expandArc: main2

var
  s
  data
  :tmpD
  :tmpD_1
s = newSeq(100)
data =
  :tmpD = `=dup`(cast[string](
    :tmpD_1 = encode(s)
    :tmpD_1))
  :tmpD
`=destroy`(:tmpD_1)
`=destroy_1`(data)
`=destroy`(s)
-- end of expandArc ------------------------
--expandArc: main3

var
  data
  :tmpD
  :tmpD_1
  :tmpD_2
data =
  :tmpD = `=dup`(cast[string](
    :tmpD_2 = encode do:
      :tmpD_1 = newSeq(100)
      :tmpD_1
    :tmpD_2))
  :tmpD
`=destroy`(:tmpD_2)
`=destroy`(:tmpD_1)
`=destroy_1`(data)
-- end of expandArc ------------------------
'''
"""

func encode*(src: openArray[byte]): seq[byte] =
  result = newSeq[byte](src.len)

template compress*(src: string): string =
  cast[string](encode(cast[seq[byte]](src)))

proc main =
  let data = compress(newString(100))
main()

proc main1 =
  var
    s = newString(100)
  let data = cast[string](encode(s.toOpenArrayByte(0, s.len-1)))
main1()

proc main2 =
  var
    s = newSeq[byte](100)
  let data = cast[string](encode(s))
main2()

proc main3 =
  let data = cast[string](encode(newSeq[byte](100)))
main3()

# bug #11018
discard cast[seq[uint8]](@[1])
discard cast[seq[uint8]]("test")
echo cast[seq[uint8]](@[1])
echo cast[seq[uint8]]("test")

discard cast[string](@[116'u8, 101, 115, 116])
#echo cast[string](@[116'u8, 101, 115, 116, 0])
var a = cast[seq[uint32]]("test1234")
a.setLen(2)
echo a


#issue 11204
var ac {.compileTime.} = @["a", "b"]
const bc = ac.len


type
  O = object of RootRef
    i: int

  O1 = object of O
  O2 = object of O

proc `=destroy`(o: var O) =
  echo "destroying O"

proc `=destroy`(o: var O1) =
  echo "destroying O1"

proc `=destroy`(o: var O2) =
  echo "destroying O2"

proc test =
  let o3 = cast[ref O2]((ref O1)())

test()