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

Boolean switch inconsistency between ECJ and javac #3559

Closed
nlisker opened this issue Jan 15, 2025 · 5 comments · Fixed by #3576
Closed

Boolean switch inconsistency between ECJ and javac #3559

nlisker opened this issue Jan 15, 2025 · 5 comments · Fixed by #3576
Assignees
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues regression Something was broken by a previous change
Milestone

Comments

@nlisker
Copy link

nlisker commented Jan 15, 2025

public class Test {

	public static void main(String[] args) {
		new Test().d(true);
	}

	void d(Boolean b) {
		switch (b) {
			case true  -> System.out.println("1");
			case false -> System.out.println("2");
		};
	}
}

In Java 22, this compiles and runs in ECJ, but javac reports

Test.java:11: error: constant label of type boolean is not compatible with switch selector type Boolean
			case true -> System.out.println("1");
			     ^
Test.java:12: error: constant label of type boolean is not compatible with switch selector type Boolean
			case false -> System.out.println("2");
			     ^

However, adding a guard pattern makes it not compile anymore in ECJ:

case true when b.toString().length() == 1 -> System.out.println("1");

Perhaps related to #3558.

@srikanth-sankaran
Copy link
Contributor

srikanth-sankaran commented Jan 16, 2025

See also

#3409

#3128

#3462

@srikanth-sankaran
Copy link
Contributor

@stephan-herrmann - do you want to look at these issues with Bollean switches? If not please assign to me. Thanks.

@srikanth-sankaran srikanth-sankaran self-assigned this Jan 20, 2025
@srikanth-sankaran srikanth-sankaran added this to the 4.35 M2 milestone Jan 20, 2025
@srikanth-sankaran
Copy link
Contributor

Behavior change happened at 4.34 M1. Upto 4.33 we reject the code with:

Description Resource Path Location Type
Type mismatch: cannot convert from boolean to Boolean Test.java /pasted_code/src line 9 Java Problem
Type mismatch: cannot convert from boolean to Boolean Test.java /pasted_code/src line 10 Java Problem

@srikanth-sankaran
Copy link
Contributor

Confirmed that this is a regression brought on by BETA_JAVA23 branch being merged onto master via commit f8fb582

@srikanth-sankaran srikanth-sankaran added bug Something isn't working regression Something was broken by a previous change compiler Eclipse Java Compiler (ecj) related issues labels Jan 20, 2025
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Jan 20, 2025
@srikanth-sankaran
Copy link
Contributor

JDK23 compiles the code (without any preview flags) - that looks certainly suspect!

And I don't think the problem is particular to booleans.

For example, the following was rejected by JDK22, but accepted by JDK23 (without any preview flags):

public class X {

	public static void main(String[] args) {
		new X().d(10L);
	}

	void d(Long l) {
		switch (l) {
			case 10L  -> System.out.println("1");
			case 20L -> System.out.println("2");
                       default -> System.out.println("Default");
		};
	}
}

@stephan-herrmann - do you agree that JDK23 behavior is incorrect ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler Eclipse Java Compiler (ecj) related issues regression Something was broken by a previous change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants