groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 02/03: GROOVY-9880: retain the break statement for potential fall-through cases
Date Fri, 15 Jan 2021 18:25:52 GMT
This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 90dbeb3678f56176d65dafb218658b1ae152a161
Author: Eric Milles <eric.milles@thomsonreuters.com>
AuthorDate: Thu Jan 14 13:26:03 2021 -0600

    GROOVY-9880: retain the break statement for potential fall-through cases
---
 src/main/java/org/codehaus/groovy/classgen/ReturnAdder.java | 12 ++++++++++--
 src/test/groovy/SwitchTest.groovy                           |  3 +--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/ReturnAdder.java b/src/main/java/org/codehaus/groovy/classgen/ReturnAdder.java
index ad952e7..cf01142 100644
--- a/src/main/java/org/codehaus/groovy/classgen/ReturnAdder.java
+++ b/src/main/java/org/codehaus/groovy/classgen/ReturnAdder.java
@@ -41,6 +41,7 @@ import java.util.List;
 import java.util.Objects;
 
 import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
+import static org.codehaus.groovy.runtime.DefaultGroovyMethods.last;
 
 /**
  * Utility class to add return statements.
@@ -215,8 +216,15 @@ public class ReturnAdder {
             int breakIndex = block.getStatements().size() - 1;
             if (block.getStatements().get(breakIndex) instanceof BreakStatement) {
                 if (doAdd) {
-                    block.getStatements().remove(breakIndex);
-                    return addReturnsIfNeeded(block, scope);
+                    Statement breakStatement = block.getStatements().remove(breakIndex);
+                    if (breakIndex == 0) block.addStatement(EmptyStatement.INSTANCE);
+                    addReturnsIfNeeded(block, scope);
+                    // GROOVY-9880: some code structures will fall through
+                    Statement lastStatement = last(block.getStatements());
+                    if (!(lastStatement instanceof ReturnStatement
+                            || lastStatement instanceof ThrowStatement)) {
+                        block.addStatement(breakStatement);
+                    }
                 } else {
                     addReturnsIfNeeded(new BlockStatement(block.getStatements().subList(0,
breakIndex), null), scope);
                 }
diff --git a/src/test/groovy/SwitchTest.groovy b/src/test/groovy/SwitchTest.groovy
index 36c9cb0..7458432 100644
--- a/src/test/groovy/SwitchTest.groovy
+++ b/src/test/groovy/SwitchTest.groovy
@@ -19,7 +19,6 @@
 package groovy
 
 import groovy.test.GroovyTestCase
-import groovy.test.NotYetImplemented
 
 class SwitchTest extends GroovyTestCase {
 
@@ -273,7 +272,7 @@ class SwitchTest extends GroovyTestCase {
         '''
     }
 
-    @NotYetImplemented // GROOVY-9880
+    // GROOVY-9880
     void testSwitchReturn4() {
         assertScript '''
             def test(sb) {


Mime
View raw message