зеркало из https://github.com/github/ruby.git
vm.c: precedence of duplicated keys
* vm.c (kwmerge_i): override existing keys by new keys. [ruby-core:65368] [Bug #10315] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47878 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
1571682719
Коммит
8771d1a0fc
|
@ -1,4 +1,7 @@
|
||||||
Sat Oct 11 13:46:53 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Oct 11 13:46:58 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* vm.c (kwmerge_i): override existing keys by new keys.
|
||||||
|
[ruby-core:65368] [Bug #10315]
|
||||||
|
|
||||||
* parse.y (assocs): concatenate splatted literal hashes. the
|
* parse.y (assocs): concatenate splatted literal hashes. the
|
||||||
former key has precedence even if duplicated literal keys
|
former key has precedence even if duplicated literal keys
|
||||||
|
|
|
@ -109,12 +109,12 @@ class TestSyntax < Test::Unit::TestCase
|
||||||
def o.kw(k1: 1, k2: 2) [k1, k2] end
|
def o.kw(k1: 1, k2: 2) [k1, k2] end
|
||||||
h = {k1: 11, k2: 12}
|
h = {k1: 11, k2: 12}
|
||||||
assert_equal([11, 12], o.kw(**h))
|
assert_equal([11, 12], o.kw(**h))
|
||||||
assert_equal([11, 22], o.kw(k2: 22, **h))
|
assert_equal([11, 12], o.kw(k2: 22, **h))
|
||||||
assert_equal([11, 12], o.kw(**h, **{k2: 22}))
|
assert_equal([11, 22], o.kw(**h, **{k2: 22}))
|
||||||
assert_equal([11, 22], o.kw(**{k2: 22}, **h))
|
assert_equal([11, 12], o.kw(**{k2: 22}, **h))
|
||||||
|
|
||||||
bug10315 = '[ruby-core:65368] [Bug #10315]'
|
bug10315 = '[ruby-core:65368] [Bug #10315]'
|
||||||
assert_equal([22, 2], o.kw(**{k1: 22}, **{k1: 23}), bug10315)
|
assert_equal([23, 2], o.kw(**{k1: 22}, **{k1: 23}), bug10315)
|
||||||
|
|
||||||
h = {k3: 31}
|
h = {k3: 31}
|
||||||
assert_raise(ArgumentError) {o.kw(**h)}
|
assert_raise(ArgumentError) {o.kw(**h)}
|
||||||
|
|
12
vm.c
12
vm.c
|
@ -2342,21 +2342,11 @@ m_core_hash_merge_ptr(int argc, VALUE *argv, VALUE recv)
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
kwmerge_ii(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
|
|
||||||
{
|
|
||||||
if (existing) return ST_STOP;
|
|
||||||
*value = arg;
|
|
||||||
return ST_CONTINUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
kwmerge_i(VALUE key, VALUE value, VALUE hash)
|
kwmerge_i(VALUE key, VALUE value, VALUE hash)
|
||||||
{
|
{
|
||||||
if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
|
if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
|
||||||
if (st_update(RHASH_TBL_RAW(hash), key, kwmerge_ii, (st_data_t)value) == 0) { /* !existing */
|
rb_hash_aset(hash, key, value);
|
||||||
RB_OBJ_WRITTEN(hash, Qundef, value);
|
|
||||||
}
|
|
||||||
return ST_CONTINUE;
|
return ST_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче