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

ruby-changes:48642

From: nobu <ko1@a...>
Date: Tue, 14 Nov 2017 08:53:06 +0900 (JST)
Subject: [ruby-changes:48642] nobu:r60756 (trunk): compile.c: fixup r60727

nobu	2017-11-14 08:53:00 +0900 (Tue, 14 Nov 2017)

  New Revision: 60756

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60756

  Log:
    compile.c: fixup r60727
    
    * compile.c (iseq_peephole_optimize): skip next `freezestring`
      instruction after `concatstrings` instruction when frozen string
      literal is enabled.

  Modified files:
    trunk/compile.c
    trunk/test/ruby/test_literal.rb
Index: test/ruby/test_literal.rb
===================================================================
--- test/ruby/test_literal.rb	(revision 60755)
+++ test/ruby/test_literal.rb	(revision 60756)
@@ -177,6 +177,20 @@ class TestRubyLiteral < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_literal.rb#L177
     end
   end
 
+  if defined?(RubyVM::InstructionSequence.compile_option) and
+    RubyVM::InstructionSequence.compile_option.key?(:debug_frozen_string_literal)
+    def test_debug_frozen_string
+      src = 'n = 1; "foo#{n ? "-#{n}" : ""}"'; f = "test.rb"; n = 1
+      opt = {frozen_string_literal: true, debug_frozen_string_literal: true}
+      str = RubyVM::InstructionSequence.compile(src, f, f, n, opt).eval
+      assert_equal("foo-1", str)
+      assert_predicate(str, :frozen?)
+      assert_raise_with_message(RuntimeError, /created at #{Regexp.quote(f)}:#{n}/) {
+        str << "x"
+      }
+    end
+  end
+
   def test_regexp
     assert_instance_of Regexp, //
     assert_match(//, 'a')
Index: compile.c
===================================================================
--- compile.c	(revision 60755)
+++ compile.c	(revision 60756)
@@ -2302,6 +2302,21 @@ iseq_pop_newarray(rb_iseq_t *iseq, INSN https://github.com/ruby/ruby/blob/trunk/compile.c#L2302
 }
 
 static int
+same_debug_pos_p(LINK_ELEMENT *iobj1, LINK_ELEMENT *iobj2)
+{
+    VALUE debug1 = OPERAND_AT(iobj1, 0);
+    VALUE debug2 = OPERAND_AT(iobj2, 0);
+    if (debug1 == debug2) return TRUE;
+    if (!RB_TYPE_P(debug1, T_ARRAY)) return FALSE;
+    if (!RB_TYPE_P(debug2, T_ARRAY)) return FALSE;
+    if (RARRAY_LEN(debug1) != 2) return FALSE;
+    if (RARRAY_LEN(debug2) != 2) return FALSE;
+    if (RARRAY_AREF(debug1, 0) != RARRAY_AREF(debug2, 0)) return FALSE;
+    if (RARRAY_AREF(debug1, 1) != RARRAY_AREF(debug2, 1)) return FALSE;
+    return TRUE;
+}
+
+static int
 iseq_peephole_optimize(rb_iseq_t *iseq, LINK_ELEMENT *list, const int do_tailcallopt)
 {
     INSN *const iobj = (INSN *)list;
@@ -2637,6 +2652,14 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L2652
 		    OPERAND_AT(jump, 0) = (VALUE)label;
 		}
 		label->refcnt++;
+		if (freeze && IS_NEXT_INSN_ID(next, freezestring)) {
+		    if (same_debug_pos_p(freeze, next->next)) {
+			REMOVE_ELEM(freeze);
+		    }
+		    else {
+			next = next->next;
+		    }
+		}
 		INSERT_ELEM_NEXT(next, &label->link);
 		CHECK(iseq_peephole_optimize(iseq, get_next_insn(jump), do_tailcallopt));
 	    }

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

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