-
Notifications
You must be signed in to change notification settings - Fork 17
/
t70_locals.nim
525 lines (454 loc) Β· 10.2 KB
/
t70_locals.nim
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
include preamble
suite "locals":
var r = 0
block:
## local variables migrate into the continuation
r = 0
proc foo() {.cps: Cont.} =
inc r
let i = 3
noop()
inc r
check i == 3
foo()
check r == 2
block:
## local variable multi-assignment
r = 0
proc foo() {.cps: Cont.} =
inc r
var i, j = 2
check i == 2
check j == 2
noop()
inc r
inc i
check i == 3
check j == 2
foo()
check r == 2
block:
## out-of-scope variables operate as expected
r = 0
var j = 2
proc foo() {.cps: Cont.} =
inc r
check j == 2
j = 4
noop()
inc r
check j == 4
inc j
foo()
check r == 2
check j == 5
block:
## proc parameters pass across continuations
r = 0
proc shadow(x: int) {.cps: Cont.} =
r = 1
noop()
check x > 0, "parameter value unset"
shadow(1)
check r == 1
block:
## local variables may shadow proc parameters
r = 0
proc shadow(x: int) {.cps: Cont.} =
r = 1
let x = 3
noop()
check x == 3, "shadowed symbol wrong"
shadow(1)
check r == 1
block:
## shadowing variables may impart new mutability
r = 0
proc shadow(x: int) {.cps: Cont.} =
r = 1
block:
var x = 4
noop()
check x == 4, "shadowing symbol wrong"
check x == 3, "shadowed symbol corrupted"
shadow(3)
check r == 1
block:
## shadowing variables pass across continuations
r = 0
proc shadow(x: int) {.cps: Cont.} =
r = 1
block:
var x = 4
inc x
noop()
check x == 5, "shadowing symbol immutable"
check x == 3, "shadowed symbol corrupted"
shadow(3)
check r == 1
block:
## scope-based shadowing is also supported
r = 0
proc shadow(x: int) {.cps: Cont.} =
r = 1
block:
var x = 4
noop()
block:
inc x
check x == 5, "failed to update from lower scope"
check x == 3, "shadowed symbol corrupted"
shadow(3)
check r == 1
block:
## shadowing is unperturbed by continuation calls
r = 0
proc shadow1(x: int) {.cps: Cont.} =
inc r
check x == 3, "unexpected input of " & $x
block:
var x = x + 2
check x == 5, "failed to update from lower scope"
noop()
inc x
check x == 6, "failed to update from lower scope"
check x == 3, "shadowed symbol corrupted to " & $x
proc shadow2(x: int) {.cps: Cont.} =
inc r
var x = x + 2
check x == 3, "shadow1 is expecting x == 3"
shadow1(x)
noop()
check x == 3, "x mutated by cps call"
inc x
check x == 4, "shadowing symbol corrupted"
shadow2(1)
check r == 2
block:
## shadowing and proc param defaults are supported
r = 0
proc foo(a, b, c: int = 3) {.cps: Cont.} =
inc r
## a=1, b=2, c=3
var a = 5
## a=5, b=2, c=3
noop()
inc r
## a=5, b=2, c=3
var b = b + a
## a=5, b=7, c=3
noop()
inc r
## a=5, b=7, c=3
check "proc parameters":
a == 5
b == 7
c == 3
foo(1, 2)
check r == 3
block:
## a gratuitously complex shadowing test works
r = 0
proc b(x: int) {.cps: Cont.} =
inc r
check x == 2, "unexpected input to cps call b()"
noop()
inc r
let x = x + 1
noop()
inc r
check x == 3, "let from proc param incorrect"
noop()
inc r
var y = 8
block:
noop()
inc r
var x = 4
noop()
inc r
inc x
noop()
inc r
dec y
noop()
inc r
check x == 5, "shadowed var x could not be mutated"
check y == 7, "y could not be mutated in lower scope"
noop()
inc r
check x == 3, "lower scope mutated shadowed x"
noop()
inc r
check y == 7, "lower scope could not mutate y"
proc a(x: int) {.cps: Cont.} =
inc r
check x == 1, "unexpected input to cps call a()"
noop()
inc r
check x == 1, "noop managed to erase x"
var x = 2
noop()
inc r
check x == 2, "could not shadow proc param x"
noop()
inc r
b(x)
noop()
inc r
check x == 2, "x mutated by cps call"
a(1)
check r == 15
block:
## re-entrant scopes reset locals
proc foo() {.cps: Continuation.} =
var i = 0
while i < 2:
var j: int
check j == 0, "failed to reset j"
j = 3
inc i
check i == 2
foo()
block:
## re-entrant scopes reset locals to initialization values
proc foo() {.cps: Continuation.} =
var i = 0
while i < 2:
var j, k = 3
check j == 3, "failed to reset j"
check k == 3, "failed to reset k"
j = 4
k = 4
inc i
check i == 2
foo()
block:
## local type inference for procedure symbols
proc bar(x: int) =
discard
proc foo() {.cps: Continuation.} =
var f = bar
f(10)
foo()
block:
## naive callback semantics work with simplified syntax
type
CallBack = proc(): Continuation
proc bar(cb: CallBack) {.cps: Continuation.} =
discard
proc thing() {.cps: Continuation.} =
discard
proc foo() {.cps: Continuation.} =
proc gen(): CallBack =
result =
proc(): Continuation =
whelp thing()
bar gen()
foo()
block:
## nested procedures may be defined and passed as arguments
proc two(fn: proc(): bool) {.cps: Cont.} =
check true == fn()
proc one() {.cps: Cont.} =
noop()
proc fn(): bool = true
noop()
two(fn)
one()
block:
## lambdas may be defined and passed as arguments, with some caveats
proc two(fn: proc(): bool) {.cps: Cont.} =
check true == fn()
proc one() {.cps: Cont.} =
noop()
var fn: proc(): bool
fn = proc(): bool = true
noop()
two(fn)
one()
block:
## pragmas in identdefs are accepted for rewrite
template checkIt(list: untyped; logic: untyped): untyped =
for it {.inject.} in list.items:
logic
proc bar() {.cps: Cont.} =
checkIt [true]:
check it
checkIt [2]:
check it == 2
proc foo() {.cps: Cont.} =
while true:
bar()
break
foo()
suite "tuples":
var r = 0
block:
## tuple deconstruction with a call source works
r = 0
proc bar(): (int, float) = (4, 7.0)
proc foo() {.cps: Cont.} =
inc r
let (i, k) = bar()
noop()
inc r
noop()
check "declared variables":
i == 4
k == 7.0
foo()
check r == 2
block:
## tuple deconstruction with distinct types works
type
Goats = distinct int
Pigs = distinct float
proc `==`(a, b: Goats): bool {.borrow.}
proc `==`(a, b: Pigs): bool {.borrow.}
r = 0
proc foo() {.cps: Cont.} =
inc r
let (i, k) = (Goats 4, Pigs 7.0)
noop()
inc r
noop()
check "declared variables":
i == 4.Goats
k == Pigs 7.0
foo()
check r == 2
block:
## declaration via tuple deconstruction works
r = 0
proc foo() {.cps: Cont.} =
inc r
var (i, k) = (1, 3)
let (x, z) = (4, 6)
noop()
inc r
inc k
noop()
check "declared variables":
i == 1
k == 4
x == 4
z == 6
foo()
check r == 2
block:
## multi-variable declaration with shared initialization
r = 0
proc foo() {.cps: Cont.} =
inc r
var i, k = 3
let x, z = 4
noop()
inc r
inc k
noop()
check "declared variables":
i == 3
k == 4
x == 4
z == 4
foo()
check r == 2
type
KObj = object
K = distinct KObj
var k = initKiller 9
proc newK(): K = discard
proc reset*(v: var K) =
inc k
inc k
inc k
proc `=destroy`(v: var K) =
inc k
suite "lifetimes":
block:
## child continuation lifetimes canary
when not defined(release) and not defined(isNimSkull):
skip"triggers compiler error outside release"
else:
type
K = object
C = ref object of Cont
n: K
D = ref object of C
m: K
var k = initKiller 11
proc `=destroy`(v: var K) =
inc k
proc setup(c: C): C {.cpsMagic.} =
c.n = K()
inc k
c
proc create(d: D): D {.cpsMagic.} =
d.m = K()
inc k
d
proc bar() {.cps: D.} =
step 4
create()
check k.step == 6, "expected create/destroy in bar: " & $k.step
step 7
proc foo() {.cps: C.} =
step 1
setup()
check k.step == 3, "expected create/destroy in foo: " & $k.step
bar()
check k.step == 7, "expected child to exist"
step 8
# destroy foo.n; eg. step == 9
# destroy bar.n; eg. step == 10
# destroy bar.m; eg. step == 11
foo()
check k
block:
## lifetime canary for distinct objects
type
C = ref object of Cont
n: K
D = ref object of C
m: K
proc setup(c: C): C {.cpsMagic.} =
c.n = newK()
inc k
c
proc create(d: D): D {.cpsMagic.} =
d.m = newK()
inc k
d
proc bar() {.cps: D.} =
step 3
create()
check k.step == 4, "expected create/destroy in bar: " & $k.step
step 5
proc foo() {.cps: C.} =
step 1
setup()
check k.step == 2, "expected create/destroy in foo: " & $k.step
bar()
check k.step == 5, "expected child to exist: " & $k.step
step 6
# destroy foo.n; eg. step == 7
# destroy bar.n; eg. step == 8
# destroy bar.m; eg. step == 9
foo()
check k
import std/sugar
suite "high-cal":
var r = 0
block:
## sugary procedure arguments can be used in expressions
r = 0
proc bar(x: int): int {.cps: Cont.} =
inc r
result = x * 2
proc foo(fn: (int) -> int): int {.cps: Cont.} =
inc r
result = fn: bar(2)
inc r
check 12 == foo(x => x * 3)
check r == 3