summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryui-knk <[email protected]>2024-04-07 10:59:51 +0900
committerYuichiro Kaneko <[email protected]>2024-04-15 06:29:25 +0900
commit515e52a0b1ce61ccaffe9183bcb78dda95a64907 (patch)
tree7b44099cd101906521f9e0717779892ef88f68bd
parent339128b190959ce15de2b3cecbf9c5ac3c402a1b (diff)
Emit `warn` event for duplicated hash keys on ripper
Need to use `rb_warn` macro instead of calling `rb_compile_warn` directly to emit `warn` event on ripper.
-rw-r--r--internal/parse.h1
-rw-r--r--parse.y14
-rw-r--r--test/ripper/test_parser_events.rb6
3 files changed, 12 insertions, 9 deletions
diff --git a/internal/parse.h b/internal/parse.h
index 8e82c0f897..20367730d1 100644
--- a/internal/parse.h
+++ b/internal/parse.h
@@ -65,7 +65,6 @@ int rb_ruby_parser_end_seen_p(rb_parser_t *p);
int rb_ruby_parser_set_yydebug(rb_parser_t *p, int flag);
rb_parser_string_t *rb_str_to_parser_string(rb_parser_t *p, VALUE str);
-void rb_parser_warn_duplicate_keys(struct parser_params *p, NODE *hash);
int rb_parser_dvar_defined_ref(struct parser_params*, ID, ID**);
ID rb_parser_internal_id(struct parser_params*);
int rb_parser_reg_fragment_check(struct parser_params*, rb_parser_string_t*, int);
diff --git a/parse.y b/parse.y
index bd355e308a..1a052d4757 100644
--- a/parse.y
+++ b/parse.y
@@ -14890,7 +14890,6 @@ nd_type_st_key_enable_p(NODE *node)
}
}
-#ifndef RIPPER
static VALUE
nd_value(struct parser_params *p, NODE *node)
{
@@ -14921,8 +14920,8 @@ nd_value(struct parser_params *p, NODE *node)
}
}
-void
-rb_parser_warn_duplicate_keys(struct parser_params *p, NODE *hash)
+static void
+warn_duplicate_keys(struct parser_params *p, NODE *hash)
{
/* See https://bugs.ruby-lang.org/issues/20331 for discussion about what is warned. */
st_table *literal_keys = st_init_table_with_size(&literal_type, RNODE_LIST(hash)->as.nd_alen / 2);
@@ -14942,9 +14941,9 @@ rb_parser_warn_duplicate_keys(struct parser_params *p, NODE *hash)
key = (st_data_t)head;
if (st_delete(literal_keys, &key, &data)) {
- rb_compile_warn(p->ruby_sourcefile, nd_line((NODE *)data),
- "key %+"PRIsVALUE" is duplicated and overwritten on line %d",
- nd_value(p, head), nd_line(head));
+ rb_warn2L(nd_line((NODE *)data),
+ "key %+"PRIsWARN" is duplicated and overwritten on line %d",
+ nd_value(p, head), WARN_I(nd_line(head)));
}
st_insert(literal_keys, (st_data_t)key, (st_data_t)hash);
}
@@ -14952,12 +14951,11 @@ rb_parser_warn_duplicate_keys(struct parser_params *p, NODE *hash)
}
st_free_table(literal_keys);
}
-#endif
static NODE *
new_hash(struct parser_params *p, NODE *hash, const YYLTYPE *loc)
{
- if (hash) rb_parser_warn_duplicate_keys(p, hash);
+ if (hash) warn_duplicate_keys(p, hash);
return NEW_HASH(hash, loc);
}
diff --git a/test/ripper/test_parser_events.rb b/test/ripper/test_parser_events.rb
index 0bad60b259..cbae6e7ed5 100644
--- a/test/ripper/test_parser_events.rb
+++ b/test/ripper/test_parser_events.rb
@@ -1686,6 +1686,12 @@ class TestRipper::ParserEvents < Test::Unit::TestCase
assert_equal([3], args)
end
+ def test_warn_duplicated_hash_keys
+ fmt, *args = warn("{ a: 1, a: 2 }")
+ assert_match(/is duplicated and overwritten on line/, fmt)
+ assert_equal([:a, 1], args)
+ end
+
def test_warn_cr_in_middle
fmt = nil
assert_warn("") {fmt, = warn("\r;")}