eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
141 stars 107 forks source link

Internal compiler error: java.lang.AssertionError: Anomalous/Inconsistent operand stack! #2451

Closed srikanth-sankaran closed 2 weeks ago

srikanth-sankaran commented 2 weeks ago

The error above is generated by the compiler in the PR https://github.com/eclipse-jdt/eclipse.jdt.core/pull/2449 but that PR doesn't build yet because it is finding inconsistencies/anomalies against itself and so the PR build will fail 😀 😀 😀

Here is an isolated test case where we mess up the operand stack:

import java.util.HashMap;
import java.util.Map;

public class X {
    private static Map<String, Long> getLevelMapTable() {
        Map<String, Long> t = new HashMap<>();
        t.put(null, 0l);
        return null;
    }
}

How can we ascertain that we do indeed mess up the operand stack ? Simply insert an exception handling switch expression into the code above as in and compile with 3.29/3.30/3.31 and run it to see the verify error!

import java.util.HashMap;
import java.util.Map;

public class X {

    static void foo(long l) {

    }

    private static Map<String, Long> getLevelMapTable() {
        Map<String, Long> t = new HashMap<>();
        t.put(null, 0l);

        System.out.println(switch (42) {
        default -> {
            try {
                yield 42;
            } finally {

            }
        }
        });
        return null;
    }

    public static void main(String[] args) {
        getLevelMapTable();
    }
}

Compiling and running this program on 4.29 results in:

Error: Unable to initialize main class X
Caused by: java.lang.VerifyError: Operand stack underflow
Exception Details:
  Location:
    X.getLevelMapTable()Ljava/util/Map; @24: astore_2
  Reason:
    Attempt to pop empty stack.
srikanth-sankaran commented 2 weeks ago

Problem dates back to the fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=545567