From ae49dbd392083f69026f2a0fff4a1d5f42d172a7 Mon Sep 17 00:00:00 2001
From: nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Wed, 1 Nov 2017 03:26:55 +0000
Subject: [PATCH] compile.c: refactored compile_return

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 compile.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/compile.c b/compile.c
index 77d6fc8672..c90db528e4 100644
--- a/compile.c
+++ b/compile.c
@@ -5109,20 +5109,26 @@ compile_return(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
 
     if (iseq) {
 	enum iseq_type type = iseq->body->type;
-	const rb_iseq_t *parent_iseq = iseq->body->parent_iseq;
-	enum iseq_type parent_type;
+	const rb_iseq_t *is = iseq;
+	enum iseq_type t = type;
 	const NODE *retval = node->nd_stts;
 	LABEL *splabel = 0;
 
-	if (type == ISEQ_TYPE_TOP) {
-	    retval = 0;
-	    type = ISEQ_TYPE_METHOD;
+	while (t == ISEQ_TYPE_RESCUE || t == ISEQ_TYPE_ENSURE) {
+	    if (!(is = is->body->parent_iseq)) break;
+	    t = is->body->type;
 	}
-	else if ((type == ISEQ_TYPE_RESCUE || type == ISEQ_TYPE_ENSURE || type == ISEQ_TYPE_MAIN) &&
-		 parent_iseq &&
-		 ((parent_type = parent_iseq->body->type) == ISEQ_TYPE_TOP ||
-		  parent_type == ISEQ_TYPE_MAIN)) {
+	switch (t) {
+	  case ISEQ_TYPE_TOP:
+	  case ISEQ_TYPE_MAIN:
+	    if (is == iseq) {
+		/* plain top-level, leave directly */
+		type = ISEQ_TYPE_METHOD;
+	    }
 	    retval = 0;
+	    break;
+	  default:
+	    break;
 	}
 
 	if (type == ISEQ_TYPE_METHOD) {