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

ruby-changes:56128

From: Nobuyoshi <ko1@a...>
Date: Mon, 17 Jun 2019 21:47:56 +0900 (JST)
Subject: [ruby-changes:56128] Nobuyoshi Nakada: 01b3a38043 (trunk): Fix wrong "void value expression" error

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

From 01b3a3804334be19d013526d3edde2b84399ae43 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Mon, 17 Jun 2019 21:36:41 +0900
Subject: Fix wrong "void value expression" error

* parse.y (value_expr_check): if either of `then` or `else`
  statements is not a void value expression, the whole `if` is not
  also a void value expression.  [Bug #15932]

diff --git a/parse.y b/parse.y
index 665067d..eb00b7c 100644
--- a/parse.y
+++ b/parse.y
@@ -10521,10 +10521,10 @@ node_assign(struct parser_params *p, NODE *lhs, NODE *rhs, const YYLTYPE *loc) https://github.com/ruby/ruby/blob/trunk/parse.y#L10521
     return lhs;
 }
 
-static int
-value_expr_gen(struct parser_params *p, NODE *node)
+static NODE *
+value_expr_check(struct parser_params *p, NODE *node)
 {
-    int cond = 0;
+    NODE *void_node = 0, *vn;
 
     if (!node) {
 	rb_warning0("empty expression");
@@ -10536,9 +10536,7 @@ value_expr_gen(struct parser_params *p, NODE *node) https://github.com/ruby/ruby/blob/trunk/parse.y#L10536
 	  case NODE_NEXT:
 	  case NODE_REDO:
 	  case NODE_RETRY:
-	    if (!cond) yyerror1(&node->nd_loc, "void value expression");
-	    /* or "control never reach"? */
-	    return FALSE;
+	    return void_node ? void_node : node;
 
 	  case NODE_BLOCK:
 	    while (node->nd_next) {
@@ -10561,14 +10559,15 @@ value_expr_gen(struct parser_params *p, NODE *node) https://github.com/ruby/ruby/blob/trunk/parse.y#L10559
 		node = node->nd_body;
 		break;
 	    }
-	    if (!value_expr(node->nd_body)) return FALSE;
+	    vn = value_expr_check(p, node->nd_body);
+	    if (!vn) return NULL;
+	    if (!void_node) void_node = vn;
 	    node = node->nd_else;
 	    break;
 
 	  case NODE_AND:
 	  case NODE_OR:
-	    cond = 1;
-	    node = node->nd_2nd;
+	    node = node->nd_1st;
 	    break;
 
 	  case NODE_LASGN:
@@ -10576,16 +10575,27 @@ value_expr_gen(struct parser_params *p, NODE *node) https://github.com/ruby/ruby/blob/trunk/parse.y#L10575
 	  case NODE_DASGN_CURR:
 	  case NODE_MASGN:
 	    mark_lvar_used(p, node);
-	    return TRUE;
+	    return NULL;
 
 	  default:
-	    return TRUE;
+	    return NULL;
 	}
     }
 
-    return TRUE;
+    return NULL;
 }
 
+static int
+value_expr_gen(struct parser_params *p, NODE *node)
+{
+    NODE *void_node = value_expr_check(p, node);
+    if (void_node) {
+	yyerror1(&void_node->nd_loc, "void value expression");
+	/* or "control never reach"? */
+	return FALSE;
+    }
+    return TRUE;
+}
 static void
 void_expr(struct parser_params *p, NODE *node)
 {
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 4e8ffe5..541170f 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1388,6 +1388,12 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1388
     assert_syntax_error('a|>-b', /unexpected '-'/)
   end
 
+  def test_value_expr_in_condition
+    mesg = /void value expression/
+    assert_syntax_error("tap {a = (true ? next : break)}", mesg)
+    assert_valid_syntax("tap {a = (true ? true : break)}")
+  end
+
   private
 
   def not_label(x) @result = x; @not_label ||= nil end
-- 
cgit v0.10.2


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

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