eclipse-jdt / eclipse-jdt/eclipse.jdt.core

[Enhanced Switch][Primitive patterns] Wasteful generation of throwing default in boolean switches

Open
#3,462 3 comments 0 reactions 1 assignee Claimed by @srikanth-sankaran View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

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

```
public class X {
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."":(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)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.