Skip to content

Commit

Permalink
Emit warn event for duplicated hash keys on ripper
Browse files Browse the repository at this point in the history
Need to use `rb_warn` macro instead of calling `rb_compile_warn`
directly to emit `warn` event on ripper.
  • Loading branch information
yui-knk committed Apr 14, 2024
1 parent 38b8bdb commit e1b1624
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
1 change: 0 additions & 1 deletion internal/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 6 additions & 8 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -14890,7 +14890,6 @@ nd_type_st_key_enable_p(NODE *node)
}
}

#ifndef RIPPER
static VALUE
nd_value(struct parser_params *p, NODE *node)
{
Expand Down Expand Up @@ -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);
Expand All @@ -14942,22 +14941,21 @@ 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);
}
hash = next;
}
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);
}

Expand Down
6 changes: 6 additions & 0 deletions test/ripper/test_parser_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,12 @@ def test_warning_duplicated_when_clause
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;")}
Expand Down

0 comments on commit e1b1624

Please sign in to comment.