janino-compiler / janino

Janino is a super-small, super-fast Java™ compiler.
http://janino-compiler.github.io/janino
Other
1.21k stars 205 forks source link

InternalCompilerException while compiling variables initialized in while condition #185

Closed snuyanzin closed 1 year ago

snuyanzin commented 1 year ago

We are trying to update Calcite from 1.27.0(janino 3.0.11) to 1.28.0(janino 3.1.6) Some tests starts failing with (also reproducible with 3.1.8)

Caused by: org.codehaus.commons.compiler.InternalCompilerException: Invalid local variable index 5
    at org.codehaus.janino.UnitCompiler.getLocalVariableTypeInfo(UnitCompiler.java:13498)

I noticed that it happens for this code snippet

...
/* 64 */              org.apache.flink.table.data.binary.BinaryRowData probeNext;
/* 65 */              while ((probeNext = probeIter.next()) != null) {
/* 66 */                processSortMergeJoinElement2(probeNext);
/* 67 */              }
...

At the same time janino 3.0.11 and 3.1.0 could compile it without issues. Currently I found this workaround which could make it compilable: rewrite it in a way

/* 64 */              org.apache.flink.table.data.binary.BinaryRowData probeNext = probeIter.next();
/* 65 */              while (probeNext != null) {
/* 66 */                processSortMergeJoinElement2(probeNext);
/* 67 */                probeNext = probeIter.next();
/* 68 */              }

a bit more trace


Caused by: org.codehaus.commons.compiler.InternalCompilerException: Line 66, Column 17: Compiling "processSortMergeJoinElement2(probeNext)"
    at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:5819)
    at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:4053)
    at org.codehaus.janino.UnitCompiler.access$6100(UnitCompiler.java:236)
    at org.codehaus.janino.UnitCompiler$13.visitMethodInvocation(UnitCompiler.java:4028)
    at org.codehaus.janino.UnitCompiler$13.visitMethodInvocation(UnitCompiler.java:4003)
    at org.codehaus.janino.Java$MethodInvocation.accept(Java.java:5470)
    at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:4003)
    at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:2487)
    ... 87 more
Caused by: org.codehaus.commons.compiler.InternalCompilerException: Line 66, Column 46: Compiling "probeNext"
    at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:5819)
    at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:5323)
    at org.codehaus.janino.UnitCompiler.access$9300(UnitCompiler.java:236)
    at org.codehaus.janino.UnitCompiler$16.visitMethodInvocation(UnitCompiler.java:4698)
    at org.codehaus.janino.UnitCompiler$16.visitMethodInvocation(UnitCompiler.java:4674)
    at org.codehaus.janino.Java$MethodInvocation.accept(Java.java:5470)
    at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:4674)
    at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:5817)
    ... 94 more
Caused by: org.codehaus.commons.compiler.InternalCompilerException: Invalid local variable index 5
    at org.codehaus.janino.UnitCompiler.getLocalVariableTypeInfo(UnitCompiler.java:13498)
    at org.codehaus.janino.UnitCompiler.load(UnitCompiler.java:12500)
    at org.codehaus.janino.UnitCompiler.load(UnitCompiler.java:12475)
    at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:4745)
    at org.codehaus.janino.UnitCompiler.access$8200(UnitCompiler.java:236)
    at org.codehaus.janino.UnitCompiler$16$1.visitLocalVariableAccess(UnitCompiler.java:4684)
    at org.codehaus.janino.UnitCompiler$16$1.visitLocalVariableAccess(UnitCompiler.java:4678)
    at org.codehaus.janino.Java$LocalVariableAccess.accept(Java.java:4661)
    at org.codehaus.janino.UnitCompiler$16.visitLvalue(UnitCompiler.java:4678)
    at org.codehaus.janino.UnitCompiler$16.visitLvalue(UnitCompiler.java:4674)
    at org.codehaus.janino.Java$Lvalue.accept(Java.java:4528)
    at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:4674)
    at org.codehaus.janino.UnitCompiler.compileGet2(UnitCompiler.java:4741)
    at org.codehaus.janino.UnitCompiler.access$7700(UnitCompiler.java:236)
    at org.codehaus.janino.UnitCompiler$16$1.visitAmbiguousName(UnitCompiler.java:4679)
    at org.codehaus.janino.UnitCompiler$16$1.visitAmbiguousName(UnitCompiler.java:4678)
    at org.codehaus.janino.Java$AmbiguousName.accept(Java.java:4603)
    at org.codehaus.janino.UnitCompiler$16.visitLvalue(UnitCompiler.java:4678)
    at org.codehaus.janino.UnitCompiler$16.visitLvalue(UnitCompiler.java:4674)
    at org.codehaus.janino.Java$Lvalue.accept(Java.java:4528)
    at org.codehaus.janino.UnitCompiler.compileGet(UnitCompiler.java:4674)
    at org.codehaus.janino.UnitCompiler.compileGetValue(UnitCompiler.java:5817)
    ... 101 more
snuyanzin commented 1 year ago

example of code failing with this

public class Issue185 {
    public static void test() {
      try {
          java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
          try {
              String line;
              while ((line = br.readLine()) != null) {
                  System.out.println(line);
              }
          } finally {
              br.close();
          }
      } catch (java.io.IOException e) {
          throw new RuntimeException(e);
      }
    }
}

initialization of variable before while helps like String line = null;

aunkrig commented 1 year ago

Reproduced.

aunkrig commented 1 year ago

Fixed. Please test.

snuyanzin commented 1 year ago

tested with corresponding flink tests initially failed with this issue, now they are passing. thank you very much!