Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
martinwyser opened this issue Nov 25, 2024 · 4 comments · Fixed by #3310
Closed
Assignees
Milestone

Comments

@martinwyser
Copy link

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 srikanth-sankaran self-assigned this Nov 25, 2024
@srikanth-sankaran
Copy link
Contributor

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 f8fb582

I know the problem and will shortly propose a fix.

@srikanth-sankaran srikanth-sankaran added this to the 4.35 M1 milestone Nov 25, 2024
@srikanth-sankaran srikanth-sankaran changed the title Problem with switch and enums [Enhanced Switch] Problem with switch and enums - incorrect duplicate case error Nov 25, 2024
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Nov 25, 2024
@srikanth-sankaran
Copy link
Contributor

I have included a fix and regression test here : #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.

@martinwyser
Copy link
Author

Thank you very much for the prompt fix!

@srikanth-sankaran
Copy link
Contributor

Thank you very much for the prompt fix!

You are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants