From 0036648a420f945624898568bb82bc5f83195d12 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 13 Jan 2021 21:15:13 +0900 Subject: [PATCH] Capture to reserved name variables if already defined [Bug #17533] --- parse.y | 5 +++-- test/ruby/test_regexp.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/parse.y b/parse.y index cdd64f7071..538153cc7a 100644 --- a/parse.y +++ b/parse.y @@ -12811,12 +12811,13 @@ reg_named_capture_assign_iter(const OnigUChar *name, const OnigUChar *name_end, NODE *node, *succ; if (!len) return ST_CONTINUE; - if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) - return ST_CONTINUE; if (rb_enc_symname_type(s, len, enc, (1U<loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc); succ = arg->succ_block; if (!succ) succ = NEW_BEGIN(0, arg->loc); diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 194b8c355d..495c9de919 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -229,6 +229,17 @@ class TestRegexp < Test::Unit::TestCase def test_assign_named_capture_to_reserved_word /(?.)/ =~ "a" assert_not_include(local_variables, :nil, "[ruby-dev:32675]") + + def (obj = Object.new).test(s, nil: :ng) + /(?.)/ =~ s + binding.local_variable_get(:nil) + end + assert_equal("b", obj.test("b")) + + tap do |nil: :ng| + /(?.)/ =~ "c" + assert_equal("c", binding.local_variable_get(:nil)) + end end def test_assign_named_capture_to_const