eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
164 stars 130 forks source link

[Switch] Unnamed patterns inside of multi case patterns fail to parse #2956

Closed DarinM223 closed 1 month ago

DarinM223 commented 1 month ago

This code:

public class Main {
    public sealed interface MyType {
        record A() implements MyType {
        }

        record B(int value) implements MyType {
        }

        record C() implements MyType {
        }
    }

    public static void main(String[] args) {
        MyType myType = new MyType.A();
        switch (myType) {
            case MyType.A(), MyType.B(_) -> {}
            case MyType.C() -> {}
        }
    }
}

works in IntelliJ with OpenJDK 22 but in Eclipse 4.33 using the Eclipse compiler it gives the error messages:

Syntax error on token ")", delete this token
Syntax error on token(s), misplaced construct(s)

It seems that this may be because a wildcard pattern is used inside of an or pattern. Expanding the or pattern like:

        switch (myType) {
            case MyType.A() -> {}
            case MyType.B(_) -> {}
            case MyType.C() -> {}
        }

results in no compile errors.

srikanth-sankaran commented 1 month ago

Also reversing the order of the multi case arm as in case MyType.B(_), MyType.A() -> {} is accepted. This can be the workaround for the time being.

srikanth-sankaran commented 1 month ago

Problem is observable on 4.32 as well as 4.31 (with preview enabled)

srikanth-sankaran commented 1 month ago

See also https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2070 - very likely the same or closely related problem