Skip to content

Commit 797b022

Browse files
committed
GROOVY-12035: Avoid emitting unreachable GOTO in if/else bytecode generation
1 parent 650ff80 commit 797b022

2 files changed

Lines changed: 104 additions & 1 deletion

File tree

src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,9 @@ public void writeIfElse(final IfStatement statement) {
449449
if (statement.getElseBlock().isEmpty()) {
450450
mv.visitLabel(elsePath);
451451
} else {
452-
mv.visitJumpInsn(GOTO, exitPath);
452+
if (maybeFallsThrough(statement.getIfBlock())) {
453+
mv.visitJumpInsn(GOTO, exitPath);
454+
}
453455
mv.visitLabel(elsePath);
454456
statement.getElseBlock().visit(controller.getAcg());
455457
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.groovy.classgen.asm
20+
21+
import org.junit.jupiter.api.Test
22+
23+
final class IfElseBytecodeReachabilityTest extends AbstractBytecodeTestCase {
24+
25+
@Test
26+
void testDynamicIfElseWithReturnDoesNotEmitSyntheticExitJump() {
27+
assertNoUnreachableBytecodeAndResult('''
28+
boolean flag = [true].first()
29+
if (flag) {
30+
return 1
31+
} else {
32+
return 2
33+
}
34+
''', 1)
35+
}
36+
37+
@Test
38+
void testStaticIfElseWithReturnDoesNotEmitSyntheticExitJump() {
39+
assertNoUnreachableBytecodeAndResult(method: 'runIt', '''
40+
import groovy.transform.CompileStatic
41+
42+
@CompileStatic
43+
int runIt() {
44+
boolean flag = Boolean.TRUE
45+
if (flag) {
46+
return 1
47+
} else {
48+
return 2
49+
}
50+
}
51+
52+
runIt()
53+
''', 1)
54+
}
55+
56+
@Test
57+
void testDynamicIfElseWithThrowDoesNotEmitSyntheticExitJump() {
58+
assertNoUnreachableBytecodeAndResult(method: 'pick', '''
59+
int pick(final boolean flag) {
60+
if (flag) {
61+
throw new IllegalStateException('boom')
62+
} else {
63+
return 2
64+
}
65+
}
66+
67+
pick(false)
68+
''', 2)
69+
}
70+
71+
@Test
72+
void testNestedAbruptSyntaxCombinationDoesNotEmitSyntheticExitJump() {
73+
assertNoUnreachableBytecodeAndResult(method: 'runIt', '''
74+
int runIt() {
75+
int mode = 1
76+
if (mode == 1) {
77+
switch (mode) {
78+
case 1:
79+
return 10
80+
default:
81+
return 20
82+
}
83+
} else {
84+
return 30
85+
}
86+
}
87+
88+
runIt()
89+
''', 10)
90+
}
91+
92+
private void assertNoUnreachableBytecodeAndResult(final String source, final Object expected) {
93+
assertNoUnreachableBytecodeAndResult([:], source, expected)
94+
}
95+
96+
private void assertNoUnreachableBytecodeAndResult(final Map options, final String source, final Object expected) {
97+
def unreachable = compileAndFindUnreachableInstructions(options, source)
98+
assert unreachable.isEmpty(): "Unexpected unreachable instructions: ${unreachable}\n${sequence}"
99+
assert new GroovyShell().evaluate(source) == expected
100+
}
101+
}

0 commit comments

Comments
 (0)