Possible compiler bug: continue is skipped after or-condition

Roland Illig roland.illig at gmx.de
Sat Feb 8 19:05:46 UTC 2020


/* - */ public class Demo {
/* - */     static String[] strings = {"1a", "1b", "2", "3"};
/* - */
/* - */     public static void main(String[] args) {
/* 5 */         for (String s : strings) {
/* 6 */             if (s.equals("1a") || s.equals("1b")) {
/* 7 */                 continue;
/* - */             }
/* 9 */             if (s.equals("2")) {
/* - */                 continue;
/* - */             }
/* - */             System.out.println(s);
/* - */         }
/* - */     }
/* - */ }

The generated code for the if statement in line 6 is:

if (token.equals("1a"))
     goto line 5 to continue the loop;
if (!token.equals("1b"))
     goto line 9;
implicitly goto line 7;

Instead of the shortcut "goto line 5 to continue the loop" I had
expected the more obvious "goto line 7", so that a possible breakpoint
in line 7 is visited.

This affects not only breakpoints but also code coverage tools, which
will report that line 7 is only reached once, even though the source
code says it is reached twice: once for 1a and once for 1b.

I tested this using JDK 1.8.0_201 and JDK 13.0.1. Both compile to the
same bytecode.


More information about the compiler-dev mailing list