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

ruby-changes:73734

From: John <ko1@a...>
Date: Mon, 26 Sep 2022 12:45:12 +0900 (JST)
Subject: [ruby-changes:73734] b361bdc200 (master): [Bug #19021] Fix safe call w/ conditional assign

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

From b361bdc20036688f17f1e39a260a70254e7db9cd Mon Sep 17 00:00:00 2001
From: John Hawthorn <john@h...>
Date: Sun, 25 Sep 2022 19:54:49 -0700
Subject: [Bug #19021] Fix safe call w/ conditional assign

As of fbaac837cfba23a9d34dc7ee144d7940248222a2, when we were performing
a safe call (`o&.x=`) with a conditional assign (`||= 1`) and discarding
the result the stack would end up in a bad state due to a missing pop.

This commit fixes that by adjusting the target label of the branchnil to
be before a pop in that case (as was previously done in the
non-conditional assignment case).
---
 compile.c              | 18 +++++++-----------
 test/ruby/test_call.rb |  7 +++++++
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/compile.c b/compile.c
index cf2ddea5e2..a5da919c0a 100644
--- a/compile.c
+++ b/compile.c
@@ -8728,10 +8728,6 @@ compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node https://github.com/ruby/ruby/blob/trunk/compile.c#L8728
         }
 
         ADD_LABEL(ret, lfin);
-        ADD_INSN(ret, node, pop);
-        if (lskip) {
-            ADD_LABEL(ret, lskip);
-        }
     }
     else {
         CHECK(COMPILE(ret, "NODE_OP_ASGN2 val", node->nd_value));
@@ -8741,13 +8737,13 @@ compile_op_asgn2(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node https://github.com/ruby/ruby/blob/trunk/compile.c#L8737
             ADD_INSN1(ret, node, topn, INT2FIX(1));
         }
         ADD_SEND_WITH_FLAG(ret, node, aid, INT2FIX(1), INT2FIX(asgnflag));
-        if (lskip && popped) {
-            ADD_LABEL(ret, lskip);
-        }
-        ADD_INSN(ret, node, pop);
-        if (lskip && !popped) {
-            ADD_LABEL(ret, lskip);
-        }
+    }
+    if (lskip && popped) {
+        ADD_LABEL(ret, lskip);
+    }
+    ADD_INSN(ret, node, pop);
+    if (lskip && !popped) {
+        ADD_LABEL(ret, lskip);
     }
     return COMPILE_OK;
 }
diff --git a/test/ruby/test_call.rb b/test/ruby/test_call.rb
index 67b3a936d4..88e2ddcb7b 100644
--- a/test/ruby/test_call.rb
+++ b/test/ruby/test_call.rb
@@ -47,12 +47,19 @@ class TestCall < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_call.rb#L47
     assert_equal(5, o.y)
     o&.z ||= 6
     assert_equal(6, o.z)
+    o&.z &&= 7
+    assert_equal(7, o.z)
 
     o = nil
     assert_nil(o&.x)
     assert_nothing_raised(NoMethodError) {o&.x = raise}
+    assert_nothing_raised(NoMethodError) {o&.x = raise; nil}
     assert_nothing_raised(NoMethodError) {o&.x *= raise}
     assert_nothing_raised(NoMethodError) {o&.x *= raise; nil}
+    assert_nothing_raised(NoMethodError) {o&.x ||= raise}
+    assert_nothing_raised(NoMethodError) {o&.x ||= raise; nil}
+    assert_nothing_raised(NoMethodError) {o&.x &&= raise}
+    assert_nothing_raised(NoMethodError) {o&.x &&= raise; nil}
   end
 
   def test_safe_call_evaluate_arguments_only_method_call_is_made
-- 
cgit v1.2.1


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

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