eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
165 stars 130 forks source link

[Switch Expressions] Break/continue behavior difference vis a vis javac #3282

Open srikanth-sankaran opened 2 days ago

srikanth-sankaran commented 2 days ago

Found through code inspection and white box testing:

This program is rejected by ECJ (from at least since 4.20 days till date) but accepted by javac:

public class X {
    public static void main(String[] args) {
        int i = 0;
        switch (i) {
        case 10: 
            int j = switch(i) {
            case 10 -> 10;
            default -> {
                if (i == 13)
                    break;
                yield 42;
            }
            };
        }
    }
}

The behavior implemented by ECJ is that a break cannot transcend a switch expression while a yield can transcend a switch statement. But javac seems to allow a break to transcend a switch expression.

srikanth-sankaran commented 2 days ago

14.15 The break Statement:

...

A break statement with no label attempts to transfer control to the innermost enclosing switch, while, do, or for statement; this enclosing statement, which is called the break target, then immediately completes normally.

...

It is a compile-time error if the break target contains any method, constructor, instance initializer, static initializer, lambda expression, or switch expression that encloses the break statement. That is, there are no non-local jumps.

Emphasis mine. This seems to suggest ECJ is correct and javac is defective.

@jarthana @stephan-herrmann - FYI