[前][次][番号順一覧][スレッド一覧]

ruby-changes:71635

From: Jeremy <ko1@a...>
Date: Tue, 5 Apr 2022 23:35:45 +0900 (JST)
Subject: [ruby-changes:71635] 3bb70a6924 (master): Fix using anonymous block in method accepting explicit keywords

https://git.ruby-lang.org/ruby.git/commit/?id=3bb70a6924

From 3bb70a6924ddd83f90b508b5bbc4d5629b8a41c6 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Mon, 4 Apr 2022 13:14:45 -0700
Subject: Fix using anonymous block in method accepting explicit keywords

Record block ID before vtable_pop, so the incorrect one doesn't
override it.

Fixes [Bug #18673]
---
 parse.y                  |  1 +
 test/ruby/test_syntax.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/parse.y b/parse.y
index 41a09e79fe..d391354bea 100644
--- a/parse.y
+++ b/parse.y
@@ -12081,6 +12081,7 @@ new_args_tail(struct parser_params *p, NODE *kw_args, ID kw_rest_arg, ID block, https://github.com/ruby/ruby/blob/trunk/parse.y#L12081
 	struct vtable *vtargs = p->lvtbl->args;
 	NODE *kwn = kw_args;
 
+        if (block) block = vtargs->tbl[vtargs->pos-1];
 	vtable_pop(vtargs, !!block + !!kw_rest_arg);
 	required_kw_vars = kw_vars = &vtargs->tbl[vtargs->pos];
 	while (kwn) {
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 622527dafe..b1fa3e5227 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -75,6 +75,60 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L75
         a = nil
         b{|c| a = c}
         assert_equal(1, a)
+
+        def inner
+          yield
+        end
+
+        def block_only(&)
+          inner(&)
+        end
+        assert_equal(1, block_only{1})
+
+        def pos(arg1, &)
+          inner(&)
+        end
+        assert_equal(2, pos(nil){2})
+
+        def pos_kwrest(arg1, **kw, &)
+          inner(&)
+        end
+        assert_equal(3, pos_kwrest(nil){3})
+
+        def no_kw(arg1, **nil, &)
+          inner(&)
+        end
+        assert_equal(4, no_kw(nil){4})
+
+        def rest_kw(*a, kwarg: 1, &)
+          inner(&)
+        end
+        assert_equal(5, rest_kw{5})
+
+        def kw(kwarg:1, &)
+          inner(&)
+        end
+        assert_equal(6, kw{6})
+
+        def pos_kw_kwrest(arg1, kwarg:1, **kw, &)
+          inner(&)
+        end
+        assert_equal(7, pos_kw_kwrest(nil){7})
+
+        def pos_rkw(arg1, kwarg1:, &)
+          inner(&)
+        end
+        assert_equal(8, pos_rkw(nil, kwarg1: nil){8})
+
+        def all(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, &)
+          inner(&)
+        end
+        assert_equal(9, all(nil, nil, nil, nil, okw1: nil, okw2: nil){9})
+
+        def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **kw, &)
+          inner(&)
+        end
+        assert_equal(10, all_kwrest(nil, nil, nil, nil, okw1: nil, okw2: nil){10})
     end;
   end
 
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]