ruby-changes:66855
From: eileencodes <ko1@a...>
Date: Thu, 22 Jul 2021 02:07:02 +0900 (JST)
Subject: [ruby-changes:66855] b940a45357 (master): Fix interpolated heredoc
https://git.ruby-lang.org/ruby.git/commit/?id=b940a45357 From b940a453572b5c3ed5c0951647929e14f5843a7d Mon Sep 17 00:00:00 2001 From: eileencodes <eileencodes@g...> Date: Tue, 20 Jul 2021 13:53:22 -0400 Subject: Fix interpolated heredoc This fixes https://bugs.ruby-lang.org/issues/18038. The provided reproduction showed that this happens in heredocs with double interpolation. In this case `DSTR` was getting returned but needs to be convered to a `EVSTR` which is what is returned by the function. There may be an additional bug here that we weren't able to produce. It seems odd that `STR` returns `DSTR` while everything else should return `EVSTR` since the function is `new_evstr`. [Bug #18038][ruby-core:104597] Co-authored-by: Aaron Patterson <tenderlove@r...> --- parse.y | 5 ++++- test/ruby/test_parse.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/parse.y b/parse.y index 0bea812..4c4cbcf 100644 --- a/parse.y +++ b/parse.y @@ -10181,7 +10181,10 @@ new_evstr(struct parser_params *p, NODE *node, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L10181 switch (nd_type(node)) { case NODE_STR: nd_set_type(node, NODE_DSTR); - case NODE_DSTR: case NODE_EVSTR: + return node; + case NODE_DSTR: + break; + case NODE_EVSTR: return node; } } diff --git a/test/ruby/test_parse.rb b/test/ruby/test_parse.rb index 0acbaed..65b8f87 100644 --- a/test/ruby/test_parse.rb +++ b/test/ruby/test_parse.rb @@ -1080,6 +1080,32 @@ x = __ENCODING__ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_parse.rb#L1080 end; end + def test_heredoc_interpolation + var = 1 + + v1 = <<~HEREDOC + something + #{"/#{var}"} + HEREDOC + + v2 = <<~HEREDOC + something + #{other = "/#{var}"} + HEREDOC + + v3 = <<~HEREDOC + something + #{("/#{var}")} + HEREDOC + + assert_equal "something\n/1\n", v1 + assert_equal "something\n/1\n", v2 + assert_equal "something\n/1\n", v3 + assert_equal v1, v2 + assert_equal v2, v3 + assert_equal v1, v3 + end + def test_unexpected_token_error assert_syntax_error('"x"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', /unexpected/) end -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/