summaryrefslogtreecommitdiff
path: root/parse.y
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-06-03 12:32:44 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-06-03 15:11:18 +0900
commit37eb5e74395f148782f7d67b5218fb2e66b113d7 (patch)
treef92f0414ab29ff9ff6b6dca6a13b600574d165b0 /parse.y
parenta023db49bfbbbe119638bae6abf8113f0de371de (diff)
Warn more duplicate literal hash keys
Following non-special_const literals: * T_BIGNUM * T_FLOAT (non-flonum) * T_RATIONAL * T_COMPLEX
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4548
Diffstat (limited to 'parse.y')
-rw-r--r--parse.y34
1 files changed, 33 insertions, 1 deletions
diff --git a/parse.y b/parse.y
index e669adbcd5..0b69bc1d7e 100644
--- a/parse.y
+++ b/parse.y
@@ -12184,10 +12184,42 @@ append_literal_keys(st_data_t k, st_data_t v, st_data_t h)
return ST_CONTINUE;
}
+static bool
+hash_literal_key_p(VALUE k)
+{
+ switch (OBJ_BUILTIN_TYPE(k)) {
+ case T_NODE:
+ case T_REGEXP:
+ return false;
+ default:
+ return true;
+ }
+}
+
+static int
+literal_cmp(VALUE val, VALUE lit)
+{
+ if (val == lit) return 0;
+ if (!hash_literal_key_p(val) || !hash_literal_key_p(lit)) return -1;
+ return rb_iseq_cdhash_cmp(val, lit);
+}
+
+static st_index_t
+literal_hash(VALUE a)
+{
+ if (!hash_literal_key_p(a)) return (st_index_t)a;
+ return rb_iseq_cdhash_hash(a);
+}
+
+static const struct st_hash_type literal_type = {
+ literal_cmp,
+ literal_hash,
+};
+
static NODE *
remove_duplicate_keys(struct parser_params *p, NODE *hash)
{
- st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
+ st_table *literal_keys = st_init_table_with_size(&literal_type, hash->nd_alen / 2);
NODE *result = 0;
rb_code_location_t loc = hash->nd_loc;
while (hash && hash->nd_head && hash->nd_next) {