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][Primitive patterns] Wasteful generation of throwing default in boolean switches #3462

Open
srikanth-sankaran opened this issue Dec 16, 2024 · 2 comments
Assignees
Milestone

Comments

@srikanth-sankaran
Copy link
Contributor

Found by code inspection and white box testing. While compiling this code:

public class X<T> {
	public static void main(String[] args) {
		Boolean b = true;
		switch (b) {
			case true -> System.out.println("True");
			case false -> System.out.println("False");
		}
	}
}

we generate this block of code:

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: iconst_1
         1: invokestatic  #18                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
         4: astore_1
         5: aload_1
         6: invokevirtual #24                 // Method java/lang/Boolean.booleanValue:()Z
         9: tableswitch   { // 0 to 1

                       0: 43

                       1: 32
                 default: 54
            }
        32: getstatic     #28                 // Field java/lang/System.out:Ljava/io/PrintStream;
        35: ldc           #34                 // String True
        37: invokevirtual #36                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        40: goto          64
        43: getstatic     #28                 // Field java/lang/System.out:Ljava/io/PrintStream;
        46: ldc           #42                 // String False
        48: invokevirtual #36                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        51: goto          64
        54: new           #44                 // class java/lang/MatchException
        57: dup
        58: aconst_null
        59: aconst_null
        60: invokespecial #46                 // Method java/lang/MatchException."<init>":(Ljava/lang/String;Ljava/lang/Throwable;)V
        63: athrow
        64: return

BCI 54-64 is dead code as the switch is exhaustive and there can be no further evolution that can cause it to cease to be exhaustive.

Javac has the same defect. Plus javac is also unable to generate the code it generates as of JDK23 (Bootstrap method init error)

@srikanth-sankaran srikanth-sankaran self-assigned this Dec 16, 2024
@srikanth-sankaran srikanth-sankaran added this to the 4.35 M1 milestone Dec 16, 2024
@srikanth-sankaran
Copy link
Contributor Author

I'll include a fix in #3460

@srikanth-sankaran
Copy link
Contributor Author

This comment which has been deleted from master may need addressing in the process of fixing this:

/* a default case is not needed for an exhaustive switch expression * we need to handle the default case to throw an error in order to make the stack map consistent. * All cases will return a value on the stack except the missing default case. * There is no returned value for the default case so we handle it with an exception thrown. */

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

No branches or pull requests

1 participant