diff options
author | yui-knk <[email protected]> | 2024-02-12 13:32:01 +0900 |
---|---|---|
committer | Yuichiro Kaneko <[email protected]> | 2024-02-13 08:40:14 +0900 |
commit | 8a345860d3d16b3bca74caf8ee1b405287873eed (patch) | |
tree | 43222f467d6ff6864c2ae5e96cf7b14f80e1e3de | |
parent | f41d8f38b44c66483388b1416a25d63be0721ee4 (diff) |
Warn duplication of `__ENCODING__` on the hash
```
$ ruby -e 'h = { __ENCODING__ => 1, __ENCODING__ => 2 }'
-e:1: warning: key #<Encoding:UTF-8> is duplicated and overwritten on line 1
```
-rw-r--r-- | parse.y | 4 | ||||
-rw-r--r-- | test/ruby/test_literal.rb | 1 |
2 files changed, 5 insertions, 0 deletions
@@ -90,6 +90,7 @@ hash_literal_key_p(VALUE k) case NODE_SYM: case NODE_LINE: case NODE_FILE: + case NODE_ENCODING: return true; default: return false; @@ -193,6 +194,8 @@ node_cdhash_cmp(VALUE val, VALUE lit) return node_val->nd_loc.beg_pos.lineno != node_lit->nd_loc.beg_pos.lineno; case NODE_FILE: return rb_parser_string_hash_cmp(RNODE_FILE(node_val)->path, RNODE_FILE(node_lit)->path); + case NODE_ENCODING: + return RNODE_ENCODING(node_val)->enc != RNODE_ENCODING(node_lit)->enc; default: rb_bug("unexpected node: %s, %s", ruby_node_name(type_val), ruby_node_name(type_lit)); } @@ -15461,6 +15464,7 @@ nd_type_st_key_enable_p(NODE *node) case NODE_SYM: case NODE_LINE: case NODE_FILE: + case NODE_ENCODING: return true; default: return false; diff --git a/test/ruby/test_literal.rb b/test/ruby/test_literal.rb index a736874024..00adbff731 100644 --- a/test/ruby/test_literal.rb +++ b/test/ruby/test_literal.rb @@ -498,6 +498,7 @@ class TestRubyLiteral < Test::Unit::TestCase '//', '__LINE__', '__FILE__', + '__ENCODING__', ) do |key| assert_warning(/key #{Regexp.quote(eval(key).inspect)} is duplicated/) { eval("{#{key} => :bar, #{key} => :foo}") } end |