You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2021/03/02 20:12:42 UTC
[groovy] 02/02: GROOVY-9880: retain the break statement for
potential fall-through cases
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 9b74ab31b7dfb68e0a6cd99674b185d9fd4f7cea
Author: Eric Milles <er...@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 109b266..24d2061 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) {