1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Capture to reserved name variables if already defined [Bug #17533]

This commit is contained in:
Nobuyoshi Nakada 2021-01-13 21:15:13 +09:00
parent e9b93d67ba
commit 0036648a42
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2021-01-13 22:11:34 +09:00
2 changed files with 14 additions and 2 deletions

View file

@ -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<<ID_LOCAL)) != ID_LOCAL)
return ST_CONTINUE;
var = intern_cstr(s, len, enc);
if (len < MAX_WORD_LENGTH && rb_reserved_word(s, (int)len)) {
if (!lvar_defined(p, var)) return ST_CONTINUE;
}
node = node_assign(p, assignable(p, var, 0, arg->loc), NEW_LIT(ID2SYM(var), arg->loc), NO_LEX_CTXT, arg->loc);
succ = arg->succ_block;
if (!succ) succ = NEW_BEGIN(0, arg->loc);

View file

@ -229,6 +229,17 @@ class TestRegexp < Test::Unit::TestCase
def test_assign_named_capture_to_reserved_word
/(?<nil>.)/ =~ "a"
assert_not_include(local_variables, :nil, "[ruby-dev:32675]")
def (obj = Object.new).test(s, nil: :ng)
/(?<nil>.)/ =~ s
binding.local_variable_get(:nil)
end
assert_equal("b", obj.test("b"))
tap do |nil: :ng|
/(?<nil>.)/ =~ "c"
assert_equal("c", binding.local_variable_get(:nil))
end
end
def test_assign_named_capture_to_const