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

ruby-changes:46285

From: nobu <ko1@a...>
Date: Tue, 18 Apr 2017 22:14:14 +0900 (JST)
Subject: [ruby-changes:46285] nobu:r58398 (trunk): compile.c: wrong optimization

nobu	2017-04-18 22:14:06 +0900 (Tue, 18 Apr 2017)

  New Revision: 58398

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

  Log:
    compile.c: wrong optimization
    
    * compile.c (compile_branch_condition): expression which has side
      effects should not be eliminated.
      [ruby-core:80740] [Bug #13444]

  Modified files:
    trunk/compile.c
    trunk/test/ruby/test_optimization.rb
Index: compile.c
===================================================================
--- compile.c	(revision 58397)
+++ compile.c	(revision 58398)
@@ -2249,7 +2249,7 @@ iseq_peephole_optimize(rb_iseq_t *iseq, https://github.com/ruby/ruby/blob/trunk/compile.c#L2249
 			    OPERAND_AT(pobj, 0) == Qfalse :
 			    FALSE);
 		}
-		else if (IS_INSN_ID(pobj, putstring)) {
+		else if (IS_INSN_ID(pobj, putstring) || IS_INSN_ID(pobj, duparray)) {
 		    cond = IS_INSN_ID(iobj, branchif);
 		}
 		else if (IS_INSN_ID(pobj, putnil)) {
@@ -2907,18 +2907,8 @@ compile_branch_condition(rb_iseq_t *iseq https://github.com/ruby/ruby/blob/trunk/compile.c#L2907
       case NODE_LIT:		/* NODE_LIT is always true */
       case NODE_TRUE:
       case NODE_STR:
-      case NODE_DSTR:
-      case NODE_XSTR:
-      case NODE_DXSTR:
-      case NODE_DREGX:
-      case NODE_DREGX_ONCE:
-      case NODE_DSYM:
-      case NODE_ARRAY:
       case NODE_ZARRAY:
-      case NODE_HASH:
       case NODE_LAMBDA:
-      case NODE_DEFN:
-      case NODE_DEFS:
 	/* printf("useless condition eliminate (%s)\n",  ruby_node_name(nd_type(cond))); */
 	ADD_INSNL(ret, nd_line(cond), jump, then_label);
 	break;
Index: test/ruby/test_optimization.rb
===================================================================
--- test/ruby/test_optimization.rb	(revision 58397)
+++ test/ruby/test_optimization.rb	(revision 58398)
@@ -524,4 +524,47 @@ class TestRubyOptimization < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/ruby/test_optimization.rb#L524
     assert_no_match(/putstring/, insn)
     assert_no_match(/newrange/, insn)
   end
+
+  def test_branch_condition_backquote
+    bug = '[ruby-core:80740] [Bug #13444] redefined backquote should be called'
+    class << self
+      def `(s)
+        @q = s
+        @r
+      end
+    end
+
+    @q = nil
+    @r = nil
+    assert_equal("bar", ("bar" unless `foo`), bug)
+    assert_equal("foo", @q, bug)
+
+    @q = nil
+    @r = true
+    assert_equal("bar", ("bar" if `foo`), bug)
+    assert_equal("foo", @q, bug)
+
+    @q = nil
+    @r = "z"
+    assert_equal("bar", ("bar" if `foo#{@r}`))
+    assert_equal("fooz", @q, bug)
+  end
+
+  def test_branch_condition_def
+    bug = '[ruby-core:80740] [Bug #13444] method should be defined'
+    c = Class.new do
+      raise "bug" unless def t;:ok;end
+    end
+    assert_nothing_raised(NoMethodError, bug) do
+      assert_equal(:ok, c.new.t)
+    end
+  end
+
+  def test_branch_condition_defs
+    bug = '[ruby-core:80740] [Bug #13444] singleton method should be defined'
+    raise "bug" unless def self.t;:ok;end
+    assert_nothing_raised(NameError, bug) do
+      assert_equal(:ok, t)
+    end
+  end
 end

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

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