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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
discard """
matrix: "-d:nimPreviewRangeDefault --mm:refc; -d:nimPreviewRangeDefault --warningAsError:ProveInit --mm:orc"
targets: "c cpp js"
"""
import times
type
Guess = object
poi: DateTime
GuessDistinct = distinct Guess
block:
var x: Guess
discard Guess()
var y: GuessDistinct
discard y
discard GuessDistinct(x)
import mobject_default_value
block:
let x = Default()
doAssert x.se == 0'i32
# echo Default(poi: 12)
# echo Default(poi: 17)
# echo NonDefault(poi: 77)
block:
var x: Default
doAssert x.se == 0'i32
type
ObjectBase = object of RootObj
value = 12
ObjectBaseDistinct = distinct ObjectBase
DinstinctInObject = object
data: ObjectBaseDistinct
Object = object of ObjectBase
time: float = 1.2
date: int
scale: range[1..10]
Object2 = object
name: Object
Object3 = object
obj: Object2
ObjectTuple = tuple
base: ObjectBase
typ: int
obj: Object
TupleInObject = object
size = 777
data: ObjectTuple
Ref = ref object of ObjectBase
RefInt = ref object of Ref
data = 73
Ref2 = ref object of ObjectBase
RefInt2 = ref object of Ref
data = 73
var t {.threadvar.}: Default
# var m1, m2 {.threadvar.}: Default
block:
doAssert t.se == 0'i32
block: # ARC/ORC cannot bind destructors twice, so it cannot
# be moved into main
block:
var x: Ref
new(x)
doAssert x.value == 12, "Ref.value = " & $x.value
var y: RefInt
new(y)
doAssert y.value == 12
doAssert y.data == 73
block:
var x: Ref2
new(x, proc (x: Ref2) {.nimcall.} = discard "call Ref")
doAssert x.value == 12, "Ref.value = " & $x.value
proc call(x: RefInt2) =
discard "call RefInt"
var y: RefInt2
new(y, call)
doAssert y.value == 12
doAssert y.data == 73
template main {.dirty.} =
block: # bug #16744
type
R = range[1..10]
Obj = object
r: R
var
rVal: R # Works fine
objVal: Obj
doAssert rVal == 0 # it should be 1
doAssert objVal.r == 1
block: # bug #3608
type
abc = ref object
w: range[2..100]
proc createABC(): abc =
new(result)
result.w = 20
doAssert createABC().w == 20
block:
var x = new ObjectBase
doAssert x.value == 12
proc hello(): ref ObjectBase =
new result
let z = hello()
doAssert z.value == 12
block:
var base: ObjectBase
var x: ObjectBaseDistinct = ObjectBaseDistinct(base)
doAssert ObjectBase(x).value == 12
let y = ObjectBaseDistinct(default(ObjectBase))
doAssert ObjectBase(y).value == 12
proc hello(): ObjectBaseDistinct =
result = ObjectBaseDistinct(default(ObjectBase))
let z = hello()
doAssert ObjectBase(z).value == 12
block:
var x: DinstinctInObject
x.data = ObjectBaseDistinct(default(ObjectBase))
doAssert ObjectBase(x.data).value == 12
block:
var x: Object
doAssert x.value == 12
doAssert x.time == 1.2
doAssert x.scale == 1
let y = default(Object)
doAssert y.value == 12
doAssert y.time == 1.2
doAssert y.scale == 1
var x1, x2, x3: Object
doAssert x1.value == 12
doAssert x1.time == 1.2
doAssert x1.scale == 1
doAssert x2.value == 12
doAssert x2.time == 1.2
doAssert x2.scale == 1
doAssert x3.value == 12
doAssert x3.time == 1.2
doAssert x3.scale == 1
block:
var x = new Object
doAssert x[] == default(Object)
block:
var x: Object2
doAssert x.name.value == 12
doAssert x.name.time == 1.2
doAssert x.name.scale == 1
block:
var x: ref Object2
new x
doAssert x[] == default(Object2)
block:
var x: Object3
doAssert x.obj.name.value == 12
doAssert x.obj.name.time == 1.2
doAssert x.obj.name.scale == 1
when nimvm:
discard "fixme"
else:
when defined(gcArc) or defined(gcOrc):
block: #seq
var x = newSeq[Object](10)
let y = x[0]
doAssert y.value == 12
doAssert y.time == 1.2
doAssert y.scale == 1
block:
var x: seq[Object]
setLen(x, 5)
let y = x[^1]
doAssert y.value == 12
doAssert y.time == 1.2
doAssert y.scale == 1
block: # array
var x: array[10, Object]
let y = x[0]
doAssert y.value == 12
doAssert y.time == 1.2
doAssert y.scale == 1
block: # array
var x {.noinit.}: array[10, Object]
discard x
block: # tuple
var x: ObjectTuple
doAssert x.base.value == 12
doAssert x.typ == 0
doAssert x.obj.time == 1.2
doAssert x.obj.date == 0
doAssert x.obj.scale == 1
doAssert x.obj.value == 12
block: # tuple in object
var x: TupleInObject
doAssert x.data.base.value == 12
doAssert x.data.typ == 0
doAssert x.data.obj.time == 1.2
doAssert x.data.obj.date == 0
doAssert x.data.obj.scale == 1
doAssert x.data.obj.value == 12
doAssert x.size == 777
type
ObjectArray = object
data: array[10, Object]
block:
var x: ObjectArray
let y = x.data[0]
doAssert y.value == 12
doAssert y.time == 1.2
doAssert y.scale == 1
block:
var x: PrellDeque[int]
doAssert x.pendingTasks == 0
type
Color = enum
Red, Blue, Yellow
ObjectVarint = object
case kind: Color
of Red:
data: int = 10
of Blue:
fill = "123"
of Yellow:
time = 1.8'f32
ObjectVarint1 = object
case kind: Color = Blue
of Red:
data1: int = 10
of Blue:
fill2 = "123"
cry: float
of Yellow:
time3 = 1.8'f32
him: int
block:
var x = ObjectVarint(kind: Red)
doAssert x.kind == Red
doAssert x.data == 10
block:
var x = ObjectVarint(kind: Blue)
doAssert x.kind == Blue
doAssert x.fill == "123"
block:
var x = ObjectVarint(kind: Yellow)
doAssert x.kind == Yellow
doAssert typeof(x.time) is float32
block:
var x: ObjectVarint1
doAssert x.kind == Blue
doAssert x.fill2 == "123"
x.cry = 326
type
ObjectVarint2 = object
case kind: Color
of Red:
data: int = 10
of Blue:
fill = "123"
of Yellow:
time = 1.8'f32
block:
var x = ObjectVarint2(kind: Blue)
doAssert x.fill == "123"
block:
type
Color = enum
Red, Blue, Yellow
type
ObjectVarint3 = object # fixme it doesn't work with static
case kind: Color = Blue
of Red:
data1: int = 10
of Blue:
case name: Color = Blue
of Blue:
go = 12
else:
temp = 66
fill2 = "123"
cry: float
of Yellow:
time3 = 1.8'f32
him: int
block:
var x: ObjectVarint3
doAssert x.kind == Blue
doAssert x.name == Blue
doAssert x.go == 12
block:
var x = ObjectVarint3(kind: Blue, name: Red, temp: 99)
doAssert x.kind == Blue
doAssert x.name == Red
doAssert x.temp == 99
block:
type
Default = tuple
id: int = 1
obj: ObjectBase
name: string
Class = object
def: Default
Member = object
def: Default = (id: 777, obj: ObjectBase(), name: "fine")
block:
var x: Default
doAssert x.id == 1
doAssert x.obj == default(ObjectBase)
doAssert x.name == ""
block:
var x: Class
doAssert x.def == default(Default)
doAssert x.def.id == 1
doAssert x.def.obj == default(ObjectBase)
doAssert x.def.name == ""
when not defined(cpp):
block:
var x: Member
doAssert x.def.id == 777
doAssert x.def.obj == default(ObjectBase)
doAssert x.def.name == "fine"
block:
var x {.noinit.} = 12
doAssert x == 12
type
Pure = object
id: int = 12
var y {.noinit.}: Pure
doAssert y.id == 0
var z {.noinit.}: Pure = Pure(id: 77)
doAssert z.id == 77
proc main1 =
var my = @[1, 2, 3, 4, 5]
my.setLen(0)
my.setLen(5)
doAssert my == @[0, 0, 0, 0, 0]
proc main2 =
var my = "hello"
my.setLen(0)
my.setLen(5)
doAssert $(@my) == """@['\x00', '\x00', '\x00', '\x00', '\x00']"""
when defined(gcArc) or defined(gcOrc):
main1()
main2()
static: main()
main()
|