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

ruby-changes:61258

From: Nobuyoshi <ko1@a...>
Date: Thu, 14 May 2020 19:58:58 +0900 (JST)
Subject: [ruby-changes:61258] d7d0d01401 (master): Endless method definition including `rescue` modifier

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

From d7d0d01401a8082e514eb2cb3cec5410e7acba7d Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 14 May 2020 17:27:48 +0900
Subject: Endless method definition including `rescue` modifier


diff --git a/parse.y b/parse.y
index 63d75d8..6b378b0 100644
--- a/parse.y
+++ b/parse.y
@@ -920,6 +920,17 @@ set_defun_body(struct parser_params *p, NODE *n, NODE *args, NODE *body, const Y https://github.com/ruby/ruby/blob/trunk/parse.y#L920
     set_line_body(body, loc->beg_pos.lineno);
     return n;
 }
+
+static NODE *
+rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
+	     const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
+{
+    YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
+    rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
+    loc.beg_pos = arg_loc->beg_pos;
+    return NEW_RESCUE(arg, rescue, 0, &loc);
+}
+
 #endif /* RIPPER */
 
 static void
@@ -2455,6 +2466,16 @@ arg		: lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L2466
 		    /*% ripper: def!(get_value($1), $2, $4) %*/
 			local_pop(p);
 		    }
+		| defn_head f_paren_args '=' arg modifier_rescue arg
+		    {
+			restore_defun(p, $<node>1->nd_defn);
+		    /*%%%*/
+			$4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
+			$$ = set_defun_body(p, $1, $2, $4, &@$);
+		    /*% %*/
+		    /*% ripper: def!(get_value($1), $2, rescue_mod!($4, $6)) %*/
+			local_pop(p);
+		    }
 		| defs_head f_paren_args '=' arg
 		    {
 			restore_defun(p, $<node>1->nd_defn);
@@ -2466,6 +2487,18 @@ arg		: lhs '=' arg_rhs https://github.com/ruby/ruby/blob/trunk/parse.y#L2487
 		    /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, $4) %*/
 			local_pop(p);
 		    }
+		| defs_head f_paren_args '=' arg modifier_rescue arg
+		    {
+			restore_defun(p, $<node>1->nd_defn);
+		    /*%%%*/
+			$4 = rescued_expr(p, $4, $6, &@4, &@5, &@6);
+			$$ = set_defun_body(p, $1, $2, $4, &@$);
+		    /*%
+			$1 = get_value($1);
+		    %*/
+		    /*% ripper: defs!(AREF($1, 0), AREF($1, 1), AREF($1, 2), $2, rescue_mod!($4, $6)) %*/
+			local_pop(p);
+		    }
 		| primary
 		    {
 			$$ = $1;
@@ -2525,9 +2558,8 @@ arg_rhs 	: arg   %prec tOP_ASGN https://github.com/ruby/ruby/blob/trunk/parse.y#L2558
 		| arg modifier_rescue arg
 		    {
 		    /*%%%*/
-			YYLTYPE loc = code_loc_gen(&@2, &@3);
 			value_expr($1);
-			$$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($3), 0, &loc), 0, &@$);
+			$$ = rescued_expr(p, $1, $3, &@1, &@2, &@3);
 		    /*% %*/
 		    /*% ripper: rescue_mod!($1, $3) %*/
 		    }
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index df6c2e3..23061e3 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -1423,6 +1423,12 @@ eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_syntax.rb#L1423
     assert_valid_syntax('private def obj.inc(x) = x + 1')
     eval('def self.inc(x) = x + 1 => @x')
     assert_equal(:inc, @x)
+    k = Class.new do
+      class_eval('def rescued(x) = raise("to be caught") rescue "instance #{x}"')
+      class_eval('def self.rescued(x) = raise("to be caught") rescue "class #{x}"')
+    end
+    assert_equal("class ok", k.rescued("ok"))
+    assert_equal("instance ok", k.new.rescued("ok"))
   end
 
   def test_methoddef_in_cond
-- 
cgit v0.10.2


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

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