ruby-changes:66419
From: Nobuyoshi <ko1@a...>
Date: Thu, 3 Jun 2021 15:11:37 +0900 (JST)
Subject: [ruby-changes:66419] 9f3888d6a3 (master): Warn more duplicate literal hash keys
https://git.ruby-lang.org/ruby.git/commit/?id=9f3888d6a3 From 9f3888d6a3387773c8707b7971ce64c60df33d36 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Thu, 3 Jun 2021 13:26:11 +0900 Subject: Warn more duplicate literal hash keys Following non-special_const literals: * T_REGEXP --- compile.c | 5 +++++ internal/re.h | 2 ++ parse.y | 1 - re.c | 4 ++-- test/ruby/test_literal.rb | 1 + 5 files changed, 10 insertions(+), 3 deletions(-) diff --git a/compile.c b/compile.c index 1b23d21..3139e98 100644 --- a/compile.c +++ b/compile.c @@ -2011,6 +2011,9 @@ rb_iseq_cdhash_cmp(VALUE val, VALUE lit) https://github.com/ruby/ruby/blob/trunk/compile.c#L2011 const struct RComplex *comp2 = RCOMPLEX(lit); return rb_iseq_cdhash_cmp(comp1->real, comp2->real) || rb_iseq_cdhash_cmp(comp1->imag, comp2->imag); } + else if (tlit == T_REGEXP) { + return rb_reg_equal(val, lit) ? 0 : -1; + } else { UNREACHABLE_RETURN(-1); } @@ -2033,6 +2036,8 @@ rb_iseq_cdhash_hash(VALUE a) https://github.com/ruby/ruby/blob/trunk/compile.c#L2036 return rb_rational_hash(a); case T_COMPLEX: return rb_complex_hash(a); + case T_REGEXP: + return NUM2LONG(rb_reg_hash(a)); default: UNREACHABLE_RETURN(0); } diff --git a/internal/re.h b/internal/re.h index 28fa3fb..a19ad93 100644 --- a/internal/re.h +++ b/internal/re.h @@ -18,6 +18,8 @@ VALUE rb_reg_check_preprocess(VALUE); https://github.com/ruby/ruby/blob/trunk/internal/re.h#L18 long rb_reg_search0(VALUE, VALUE, long, int, int); VALUE rb_reg_match_p(VALUE re, VALUE str, long pos); bool rb_reg_start_with_p(VALUE re, VALUE str); +VALUE rb_reg_hash(VALUE re); +VALUE rb_reg_equal(VALUE re1, VALUE re2); void rb_backref_set_string(VALUE string, long pos, long len); void rb_match_unbusy(VALUE); int rb_match_count(VALUE match); diff --git a/parse.y b/parse.y index 0b69bc1..e333730 100644 --- a/parse.y +++ b/parse.y @@ -12189,7 +12189,6 @@ hash_literal_key_p(VALUE k) https://github.com/ruby/ruby/blob/trunk/parse.y#L12189 { switch (OBJ_BUILTIN_TYPE(k)) { case T_NODE: - case T_REGEXP: return false; default: return true; diff --git a/re.c b/re.c index 5809d26..64078ae 100644 --- a/re.c +++ b/re.c @@ -3009,7 +3009,7 @@ static st_index_t reg_hash(VALUE re); https://github.com/ruby/ruby/blob/trunk/re.c#L3009 * See also Object#hash. */ -static VALUE +VALUE rb_reg_hash(VALUE re) { st_index_t hashval = reg_hash(re); @@ -3043,7 +3043,7 @@ reg_hash(VALUE re) https://github.com/ruby/ruby/blob/trunk/re.c#L3043 * /abc/u == /abc/n #=> false */ -static VALUE +VALUE rb_reg_equal(VALUE re1, VALUE re2) { if (re1 == re2) return Qtrue; diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb index ed93b74..579a39d 100644 --- a/test/ruby/test_literal.rb +++ b/test/ruby/test_literal.rb @@ -484,6 +484,7 @@ class TestRubyLiteral < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_literal.rb#L484 '1.0r', '1.0i', '1.72723e-77', + '//', ) do |key| assert_warning(/key #{Regexp.quote(eval(key).inspect)} is duplicated/) do eval("{#{key} => :bar, #{key} => :foo}") -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/