eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
168 stars 132 forks source link

[Enhanced Switch] Problem with switch and enums - incorrect duplicate case error #3344

Open martinwyser opened 7 hours ago

martinwyser commented 7 hours ago

I have a piece of code which switches on objects of an interface. The interface is implemented by two enums, and the switch has objects of both enums. In one case, the enum objects are referenced with the enum name (MyEnum.A1), in the other, the enum objects are statically imported and referenced only with the enum name. When I compile with Eclipse 2024-9, the two cases behave differently. When I compile with javac from Java 21, they behave the same. I execute the code with java from Java 21 in both cases. For compiling with Java 21, I copied the source to some place outside of the workspace (directory java21), and compile it with:

javac -g java21\Main.java java21\p\X.java java21\p\A.java java21\p\B.java

I then run the code from the Eclipse compiler and the Java compiler with the Java 21 VM:

java -cp workspace\testli\bin Main java -cp java21 Main

This is the expected output, which I get when I compile and run the code in Java 21: A1 -> A1, A1 A2 -> A2, A2 B1 -> B1, B1 B2 -> B2, B2

This is what I get when I run the Eclipse compiled code in Eclipse or in Java 21: A1 -> unknown, A1 A2 -> unknown, A2 B1 -> unknown, B1 B2 -> unknown, B2

It looks like the enum literals are not matched when they are statically imported.

Here is the source code: Main.zip

I disassembled both Main class files, and the interface file, and one of the enums, and attach them here: Disassembled.zip

Versions:

java -version openjdk version "21.0.5" 2024-10-15 LTS OpenJDK Runtime Environment Microsoft-10377968 (build 21.0.5+11-LTS) OpenJDK 64-Bit Server VM Microsoft-10377968 (build 21.0.5+11-LTS, mixed mode, sharing)

Eclipse IDE for Java Developers (includes Incubating components) Version: 2024-09 (4.33.0) Build id: 20240905-0614

The project in eclipse also uses the same MS OpenJDK with compliance level 21.

srikanth-sankaran commented 5 hours ago

Reproduced the symptoms as described on 4.33.

On master we actually refuse to compile the program incorrectly complaining about Duplicate case - This behavior change came about when the BETA_JAVA23 work was merged into master via commit f8fb58210e5a04aa17773fe757e7e02b2b9ac5cd

I know the problem and will shortly propose a fix.

srikanth-sankaran commented 4 hours ago

I have included a fix and regression test here : https://github.com/eclipse-jdt/eclipse.jdt.core/pull/3310

Unfortunately, it is too late for 4.34 which is almost about to sail.

You can grab an integration build after this is merged.