[前][次][番号順一覧][スレッド一覧]

ruby-changes:57562

From: Jeremy <ko1@a...>
Date: Fri, 6 Sep 2019 01:58:02 +0900 (JST)
Subject: [ruby-changes:57562] c6464c44c0 (master): Fix code locations of array node inside hash node when multiple kw splats

https://git.ruby-lang.org/ruby.git/commit/?id=c6464c44c0

From c6464c44c02fdbb597ca8027af0bc0db28d56f66 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 4 Sep 2019 12:00:25 -0700
Subject: Fix code locations of array node inside hash node when multiple kw
 splats

This is broken at least since 2.5 (I didn't check earlier versions).
It resulted in failure in test_ast.rb when the tests were added before
the parser change.

Basically, in remove_duplicate_keys, if the node is modified, set
the location information to the previous location information. The
removal of keys should not affect the location in the code.

diff --git a/parse.y b/parse.y
index 9003f80..63d8f53 100644
--- a/parse.y
+++ b/parse.y
@@ -11336,6 +11336,7 @@ remove_duplicate_keys(struct parser_params *p, NODE *hash) https://github.com/ruby/ruby/blob/trunk/parse.y#L11336
 {
     st_table *literal_keys = st_init_numtable_with_size(hash->nd_alen / 2);
     NODE *result = 0;
+    rb_code_location_t loc = hash->nd_loc;
     while (hash && hash->nd_head && hash->nd_next) {
 	NODE *head = hash->nd_head;
 	NODE *value = hash->nd_next;
@@ -11361,6 +11362,7 @@ remove_duplicate_keys(struct parser_params *p, NODE *hash) https://github.com/ruby/ruby/blob/trunk/parse.y#L11362
 	if (!result) result = hash;
 	else list_concat(result, hash);
     }
+    result->nd_loc = loc;
     return result;
 }
 
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 56e2937..a904b0d 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -125,6 +125,13 @@ class TestSyntax < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L125
     assert_equal([kw], [kw, **kw, **kw])
     assert_equal([h], [h, **kw, **kw])
     assert_equal([h, h], [h, **kw, **kw, **h])
+
+    assert_equal([h, {:a=>2}], [h, **{}, **h, a: 2])
+    assert_equal([h, h], [h, **{}, a: 2, **h])
+    assert_equal([h, h], [h, a: 2, **{}, **h])
+    assert_equal([h, h], [h, a: 2, **h, **{}])
+    assert_equal([h, {:a=>2}], [h, **h, a: 2, **{}])
+    assert_equal([h, {:a=>2}], [h, **h, **{}, a: 2])
   end
 
   def test_normal_argument
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]