eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
166 stars 131 forks source link

[Enhanced Switch][Patterns] ECJ generated code hangs #3320

Open srikanth-sankaran opened 3 hours ago

srikanth-sankaran commented 3 hours ago

Found by code inspection:

The program below when compiled and run with javac prints (correctly)

First guard
Second guard
Default

but when compiled with ECJ and runs gets into an infinite loop and goes on printing Second guard

Goes back to 4.32 when multi pattern case labels came in

Test case:

public class X {    
    static boolean guard(String prefix) {
        System.out.println(prefix + " guard");
        return false;
    }
    public static void main(String [] args) {
        Object o = new Object();
        switch (o) {
            case String _, Object _ when guard("First") -> System.out.println("First case");  // first println
            case String _, Object _ when guard("Second") -> System.out.println("Second case");  // first println
            default -> System.out.println("Default");
        }
    }
}  
srikanth-sankaran commented 2 hours ago

Problem arises in this line:

typeSwitchIndex += caseStatement.constantExpressions.length;

of org.eclipse.jdt.internal.compiler.ast.SwitchStatement.generateCode(BlockScope, CodeStream)

It should be

typeSwitchIndex += caseStatement.peeledLabelExpressions().length;