Skip to content

Commit 1c0573c

Browse files
committed
ZJIT: Test additional arg passing scenarios
1 parent d5c7cf0 commit 1c0573c

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

test/ruby/test_zjit.rb

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ def test_send_kwarg
553553
def test(a:, b:) = [a, b]
554554
def entry = test(b: 2, a: 1) # change order
555555
entry
556-
}
556+
entry
557+
}, call_threshold: 2
557558
end
558559

559560
def test_send_kwarg_optional
@@ -565,6 +566,15 @@ def entry = test
565566
}, call_threshold: 2
566567
end
567568

569+
def test_send_kwarg_optional_too_many
570+
assert_compiles '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]', %q{
571+
def test(a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10) = [a, b, c, d, e, f, g, h, i, j]
572+
def entry = test
573+
entry
574+
entry
575+
}, call_threshold: 2
576+
end
577+
568578
def test_send_kwarg_required_and_optional
569579
assert_compiles '[3, 2]', %q{
570580
def test(a:, b: 2) = [a, b]
@@ -583,6 +593,28 @@ def entry = test(a: 3)
583593
}, call_threshold: 2
584594
end
585595

596+
def test_send_kwarg_to_ccall
597+
assert_compiles '["a", "b", "c"]', %q{
598+
def test(s) = s.each_line(chomp: true).to_a
599+
def entry = test(%(a\nb\nc))
600+
entry
601+
entry
602+
}, call_threshold: 2
603+
end
604+
605+
def test_send_kwarg_and_block_to_ccall
606+
assert_compiles '["a", "b", "c"]', %q{
607+
def test(s)
608+
a = []
609+
s.each_line(chomp: true) { |l| a << l }
610+
a
611+
end
612+
def entry = test(%(a\nb\nc))
613+
entry
614+
entry
615+
}, call_threshold: 2
616+
end
617+
586618
def test_send_kwrest
587619
assert_compiles '{a: 3}', %q{
588620
def test(**kwargs) = kwargs
@@ -592,6 +624,47 @@ def entry = test(a: 3)
592624
}, call_threshold: 2
593625
end
594626

627+
def test_send_req_kwreq
628+
assert_compiles '[1, 3]', %q{
629+
def test(a, c:) = [a, c]
630+
def entry = test(1, c: 3)
631+
entry
632+
entry
633+
}, call_threshold: 2
634+
end
635+
636+
def test_send_req_opt_kwreq_kwopt
637+
assert_compiles '[[1, 2, 3, 4], [-1, -2, -3, -4]]', %q{
638+
def test(a, b = 2, c:, d: 4) = [a, b, c, d]
639+
def entry = [test(1, c: 3), test(-1, -2, d: -4, c: -3)] # specify all, change kw order
640+
entry
641+
entry
642+
}, call_threshold: 2
643+
end
644+
645+
def test_send_unexpected_keyword
646+
assert_compiles ':error', %q{
647+
def test(a: 1) = a*2
648+
def entry
649+
test(z: 2)
650+
rescue ArgumentError
651+
:error
652+
end
653+
654+
entry
655+
entry
656+
}, call_threshold: 2
657+
end
658+
659+
def test_send_all_arg_types
660+
assert_compiles '[:req, :opt, :post, :kwr, :kwo, true]', %q{
661+
def test(a, b = :opt, c, d:, e: :kwo) = [a, b, c, d, e, block_given?]
662+
def entry = test(:req, :post, d: :kwr) {}
663+
entry
664+
entry
665+
}, call_threshold: 2
666+
end
667+
595668
def test_send_ccall_variadic_with_different_receiver_classes
596669
assert_compiles '[true, true]', %q{
597670
def test(obj) = obj.start_with?("a")

zjit/src/hir/opt_tests.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,6 +2861,70 @@ mod hir_opt_tests {
28612861
");
28622862
}
28632863

2864+
#[test]
2865+
fn dont_optimize_ccall_with_kwarg() {
2866+
eval("
2867+
def test = sprintf('%s', a: 1)
2868+
test
2869+
test
2870+
");
2871+
assert_snapshot!(hir_string("test"), @r"
2872+
fn test@<compiled>:2:
2873+
bb0():
2874+
EntryPoint interpreter
2875+
v1:BasicObject = LoadSelf
2876+
Jump bb2(v1)
2877+
bb1(v4:BasicObject):
2878+
EntryPoint JIT(0)
2879+
Jump bb2(v4)
2880+
bb2(v6:BasicObject):
2881+
v11:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
2882+
v12:StringExact = StringCopy v11
2883+
v14:Fixnum[1] = Const Value(1)
2884+
IncrCounter complex_arg_pass_caller_kwarg
2885+
v16:BasicObject = SendWithoutBlock v6, :sprintf, v12, v14
2886+
CheckInterrupts
2887+
Return v16
2888+
");
2889+
}
2890+
2891+
#[test]
2892+
fn dont_optimize_ccall_with_block_and_kwarg() {
2893+
eval("
2894+
def test(s)
2895+
a = []
2896+
s.each_line(chomp: true) { |l| a << l }
2897+
a
2898+
end
2899+
test %(a\nb\nc)
2900+
test %()
2901+
");
2902+
assert_snapshot!(hir_string("test"), @r"
2903+
fn test@<compiled>:3:
2904+
bb0():
2905+
EntryPoint interpreter
2906+
v1:BasicObject = LoadSelf
2907+
v2:BasicObject = GetLocal l0, SP@5
2908+
v3:NilClass = Const Value(nil)
2909+
Jump bb2(v1, v2, v3)
2910+
bb1(v6:BasicObject, v7:BasicObject):
2911+
EntryPoint JIT(0)
2912+
v8:NilClass = Const Value(nil)
2913+
Jump bb2(v6, v7, v8)
2914+
bb2(v10:BasicObject, v11:BasicObject, v12:NilClass):
2915+
v16:ArrayExact = NewArray
2916+
SetLocal l0, EP@3, v16
2917+
v22:TrueClass = Const Value(true)
2918+
IncrCounter complex_arg_pass_caller_kwarg
2919+
v24:BasicObject = Send v11, 0x1000, :each_line, v22
2920+
v25:BasicObject = GetLocal l0, EP@4
2921+
v26:BasicObject = GetLocal l0, EP@3
2922+
v30:BasicObject = GetLocal l0, EP@3
2923+
CheckInterrupts
2924+
Return v30
2925+
");
2926+
}
2927+
28642928
#[test]
28652929
fn dont_replace_get_constant_path_with_empty_ic() {
28662930
eval("

0 commit comments

Comments
 (0)