Newer
Older
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
require "../../spec_helper"
describe "Code gen: block" do
it "generate inline" do
run("
def foo
yield
end
foo do
1
end
").to_i.should eq(1)
end
it "passes yield arguments" do
run("
def foo
yield 1
end
foo do |x|
x + 1
end
").to_i.should eq(2)
end
it "pass arguments to yielder function" do
run("
def foo(a)
yield a
end
foo(3) do |x|
x + 1
end
").to_i.should eq(4)
end
it "pass self to yielder function" do
run("
Ary Borenszweig
committed
struct Int
def foo
yield self
end
end
3.foo do |x|
x + 1
end
").to_i.should eq(4)
end
it "pass self and arguments to yielder function" do
run("
Ary Borenszweig
committed
struct Int
def foo(i)
yield self, i
end
end
3.foo(2) do |x, i|
x + i
end
").to_i.should eq(5)
end
it "allows access to local variables" do
run("
def foo
yield
end
x = 1
foo do
x + 1
end
").to_i.should eq(2)
end
it "can access instance vars from yielder function" do
run("
class Foo
def initialize
@x = 1
end
def foo
yield @x
end
end
Foo.new.foo do |x|
x + 1
end
").to_i.should eq(2)
end
it "can set instance vars from yielder function" do
run("
class Foo
def initialize
@x = 1
end
def foo
@x = yield
end
def value
@x
end
end
a = Foo.new
a.foo { 2 }
a.value
").to_i.should eq(2)
end
it "can use instance methods from yielder function" do
run("
class Foo
def foo
yield value
end
def value
1
end
end
Foo.new.foo { |x| x + 1 }
").to_i.should eq(2)
end
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
it "can call methods from block when yielder is an instance method" do
run("
class Foo
def foo
yield
end
end
def bar
1
end
Foo.new.foo { bar }
").to_i.should eq(1)
end
it "nested yields" do
run("
def bar
yield
end
def foo
bar { yield }
end
a = foo { 1 }
").to_i.should eq(1)
end
it "assigns yield to argument" do
run("
def foo(x)
yield
x = 1
end
foo(1) { 1 }
").to_i.should eq(1)
end
it "can use global constant" do
run("
FOO = 1
def foo
yield
FOO
end
foo { }
").to_i.should eq(1)
end
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
it "return from yielder function" do
run("
def foo
yield
return 1
end
foo { }
2
").to_i.should eq(2)
end
it "return from block" do
run("
def foo
yield
end
def bar
foo { return 1 }
2
end
bar
").to_i.should eq(1)
end
it "return from yielder function (2)" do
run("
def foo
yield
return 1 if true
return 2
end
def bar
foo {}
end
bar
").to_i.should eq(1)
end
it "union value of yielder function" do
run("
def foo
yield
a = 1.1
a = 1
a
end
foo {}.to_i
").to_i.should eq(1)
end
it "allow return from function called from yielder function" do
run("
def foo
return 2
end
def bar
yield
foo
1
end
bar {}
").to_i.should eq(1)
end
it "" do
run("
def foo
yield
true ? return 1 : return 1.1
end
foo {}.to_i
").to_i.should eq(1)
end
it "return from block that always returns from function that always yields inside if block" do
run("
def bar
yield
2
end
def foo
if true
bar { return 1 }
else
0
end
end
foo
").to_i.should eq(1)
end
it "return from block that always returns from function that conditionally yields" do
run("
def bar
if true
yield
end
end
def foo
bar { return 1 }
2
end
foo
").to_i.should eq(1)
end
it "call block from dispatch" do
run("
def bar(y)
yield y
end
def foo
x = 1.1
x = 1
bar(x) { |z| z }
end
foo.to_i
").to_i.should eq(1)
end
it "call block from dispatch and use local vars" do
run("
def bar(y)
yield y
end
def foo
total = 0
x = 1.5
bar(x) { |z| total += z }
x = 1
bar(x) { |z| total += z }
x = 1.5
bar(x) { |z| total += z }
total
end
foo.to_i
").to_i.should eq(4)
end
it "break without value returns nil" do
run("
require \"nil\"
require \"value\"
def foo
yield
1
end
x = foo do
break if 1 == 1
end
x.nil?
").to_b.should be_true
end
it "break block with yielder inside while" do
run("
Ary Borenszweig
committed
require \"prelude\"
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
a = 0
10.times do
a += 1
break if a > 5
end
a
").to_i.should eq(6)
end
it "break from block returns from yielder" do
run("
def foo
yield
yield
end
a = 0
foo { a += 1; break }
a
").to_i.should eq(1)
end
it "break from block with value" do
run("
def foo
while true
yield
a = 3
end
end
foo do
break 1
end
").to_i.should eq(1)
end
Ary Borenszweig
committed
it "returns from block with value" do
Ary Borenszweig
committed
require \"prelude\"
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
def foo
while true
yield
a = 3
end
end
def bar
foo do
return 1
end
end
bar.to_i
").to_i.should eq(1)
end
it "doesn't codegen after while that always yields and breaks" do
run("
def foo
while true
yield
end
1
end
foo do
break 2
end
").to_i.should eq(2)
end
Ary Borenszweig
committed
it "break from block with value" do
Ary Borenszweig
committed
require \"prelude\"
10.times { break 20 }
").to_i.should eq(20)
end
it "doesn't codegen call if arg yields and always breaks" do
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
run("
require \"nil\"
def foo
1 + yield
end
foo { break 2 }.to_i
").to_i.should eq(2)
end
it "codegens nested return" do
run("
def bar
yield
a = 1
end
def foo
bar { yield }
end
def z
foo { return 2 }
end
z
").to_i.should eq(2)
end
it "codegens nested break" do
run("
def bar
yield
a = 1
end
def foo
bar { yield }
end
foo { break 2 }
").to_i.should eq(2)
end
it "codegens call with block with call with arg that yields" do
run("
def bar
yield
a = 2
end
def foo
bar { 1 + yield }
end
foo { break 3 }
").to_i.should eq(3)
end
it "can break without value from yielder that returns nilable (1)" do
run("
require \"nil\"
require \"reference\"
def foo
yield
\"\"
end
a = foo do
break
end
a.nil?
").to_b.should be_true
end
it "can break without value from yielder that returns nilable (2)" do
run("
require \"nil\"
require \"reference\"
def foo
yield
\"\"
end
a = foo do
break nil
end
a.nil?
").to_b.should be_true
end
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
it "break with value from yielder that returns a nilable" do
run("
require \"nil\"
require \"reference\"
def foo
yield
\"\"
end
a = foo do
break if false
break \"\"
end
a.nil?
").to_b.should be_false
end
it "can use self inside a block called from dispatch" do
run("
require \"nil\"
class Foo
def do; yield; end
end
class Bar < Foo
end
Ary Borenszweig
committed
struct Int
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
def foo
x = Foo.new
x = Bar.new
x.do { $x = self }
end
end
123.foo
$x.to_i
").to_i.should eq(123)
end
it "return from block called from dispatch" do
run("
class Foo
def do; yield; end
end
class Bar < Foo
end
def foo
x = Foo.new
x = Bar.new
x.do { return 1 }
0
end
foo
").to_i.should eq(1)
end
it "breaks from while in function called from block" do
run("
def foo
yield
end
def bar
while true
break 1
end
2
end
foo do
bar
end
").to_i.should eq(2)
end
it "allows modifying yielded value (with literal)" do
run("
def foo
yield 1
end
foo { |x| x = 2; x }
").to_i.should eq(2)
end
it "allows modifying yielded value (with variable)" do
run("
def foo
a = 1
yield a
a
end
foo { |x| x = 2; x }
").to_i.should eq(1)
end
it "it yields nil from another call" do
run("
require \"bool\"
def foo(key, default)
foo(key) { default }
end
def foo(key)
if !(true)
return yield key
end
yield key
end
foo(1, nil)
")
end
it "allows yield from dispatch call" do
run("
def foo(x : Value)
yield 1
end
def foo(x : Int)
yield 2
end
def bar
a = 1; a = 1.1
foo(a) do |i|
yield i
end
end
x = 0
bar { |i| x = i }
x
").to_i.should eq(1)
end
it "block with nilable type" do
run("
class Foo
def foo
yield 1
end
end
class Bar
def foo
yield 2
Reference.new
end
end
a = Foo.new || Bar.new
a.foo {}
")
end
it "block with nilable type 2" do
run("
class Foo
def foo
yield 1
nil
end
end
class Bar
def foo
yield 2
Reference.new
end
end
a = Foo.new || Bar.new
a.foo {}
")
end
it "allows yields with less arguments than in block" do
run("
Ary Borenszweig
committed
struct Nil
def to_i
0
end
end
def foo
yield 1
end
a = 0
foo do |x, y|
a += x + y.to_i
end
a
").to_i.should eq(1)
end
Ary Borenszweig
committed
it "codegens block with nilable type with return (1)" do
Ary Borenszweig
committed
struct Nil; def nil?; true; end; end
class Reference; def nil?; false; end; end
def foo
if yield
return Reference.new
end
nil
end
Ary Borenszweig
committed
foo { false }.nil?
").to_b.should be_true
end
it "codegens block with nilable type with return (2)" do
run("
struct Nil; def nil?; true; end; end
class Reference; def nil?; false; end; end
def foo
if yield
return nil
end
Reference.new
end
foo { false }.nil?
").to_b.should be_false
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
end
it "codegens block with union with return" do
run("
def foo
yield
return 1 if 1 == 2
nil
end
x = foo { }
1
")
end
it "codegens if with call with block (ssa issue)" do
run("
def bar
yield
end
def foo
if 1 == 2
bar do
x = 1
end
else
3
end
end
foo
").to_i.should eq(3)
end
it "codegens block with return and yield and no return" do
run("
fun exit : NoReturn
end
def foo(key)
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
end
def foo(key)
if 1 == 1
return 2
end
yield
end
foo 1
").to_i.should eq(2)
end
it "codegens while/break inside block" do
run("
def foo
yield
end
foo do
while true
break
end
1
end
").to_i.should eq(1)
end
it "codegens block with union arg (1)" do
run("
def foo
yield 1 || 1.5
end
foo { |x| x }.to_i
").to_i.should eq(1)
end
it "codegens block with union arg (2)" do
Ary Borenszweig
committed
struct Number
def abs
self
end
end
class Foo(T)
def initialize(x : T)
@x = x
end
def each
yield @x
end
end
a = Foo.new(1) || Foo.new(1.5)
a.each do |x|
x.abs
end.to_i
").to_i.should eq(1)
end
it "codegens block with virtual type arg" do
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
run("
class Var(T)
def initialize(x : T)
@x = x
end
def each
yield @x
end
end
class Foo
def bar
1
end
end
class Bar < Foo
def bar
2
end
end
a = Var.new(Foo.new) || Var.new(Bar.new)
a.each do |x|
x.bar
end
").to_i.should eq(1)
end
it "codegens call with blocks of different type without args" do
run("
def foo
yield
end
foo { 1.1 }
foo { 1 }
").to_i.should eq(1)
end
it "codegens dispatch with block and break (1)" do
run("
class Foo(T)
def initialize(@x : T)
end
def each
yield @x
end
end
n = 0
f = Foo.new(1) || Foo.new(1.5)
f.each do |x|
break if x > 2
n += x
end
n.to_i
").to_i.should eq(1)
end
it "codegens dispatch with block and break (2)" do
Ary Borenszweig
committed
require \"prelude\"
a = [1, 2, 3] || [1.5]
n = 0
a.each do |x|
break if x > 2
n += x
end
n.to_i
").to_i.should eq(3)
end
it "codegens block call when argument type changes" do
run("
def foo(x)
while 1 == 2
x = 1.5
yield
end
end
foo(1) do
end
")
end
it "returns void when called with block" do
run("
fun foo : Void
end
def bar
yield
foo
end
bar {}
")
end
Ary Borenszweig
committed
it "executes yield expression if no arg is given for block" do
run("
def foo
a = 1
yield (a = 2)
a
end
foo { }
").to_i.should eq(2)
end