JDK-8236911 : Extra checkcast instruction in compiled code with "Pattern Matching" feature
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 14,15
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2020-01-09
  • Updated: 2020-07-01
  • Resolved: 2020-07-01
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15Resolved
Related Reports
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
Windows 10, openjdk-14-ea+30_windows-x64

A DESCRIPTION OF THE PROBLEM :
Source code:

void m(Object obj) {
    if (obj instanceof String s) {
        s.hashCode();
    }
}

Emitted code:

  void m(java.lang.Object);
    Code:
       0: aload_1
       1: astore_3
       2: aload_3
       3: instanceof    #7                  // class java/lang/String
       6: ifeq          27
       9: aload_3
      10: checkcast     #7                  // class java/lang/String
      13: dup
      14: astore_2
      15: aload_3
      16: checkcast     #7                  // class java/lang/String
      19: if_acmpne     27
      22: aload_2
      23: invokevirtual #9                  // Method java/lang/String.hashCode:()I
      26: pop
      27: return

For me it looks like instructions 9-19 produce code:

if ((String)obj == (String)obj) {...}

which seems pointless.


FREQUENCY : always



Comments
Duplicate together with JDK-8237528.
02-03-2020

Reproduced with Oracle JDK 14
10-01-2020

steps to reproduce from the submitter: (1) Create source Java class PMFI.java: class PMFI { void m(Object obj) { if (obj instanceof String s) { s.hashCode(); } } } (2) Compile it with JDK 14: "c:\Program Files\Java\openjdk-14-ea+30_windows-x64_bin.zip\bin\javac.exe" PMFI.java --enable-preview --release 14 (3) Disassemble compiled code: "c:\Program Files\Java\openjdk-14-ea+30_windows-x64_bin.zip\bin\javap.exe" -c PMFI Compiled from "PMFI.java" class PMFI { PMFI(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return void m(java.lang.Object); Code: 0: aload_1 1: astore_3 2: aload_3 3: instanceof #7 // class java/lang/String 6: ifeq 27 9: aload_3 10: checkcast #7 // class java/lang/String 13: dup 14: astore_2 15: aload_3 16: checkcast #7 // class java/lang/String 19: if_acmpne 27 22: aload_2 23: invokevirtual #9 // Method java/lang/String.hashCode:()I 26: pop 27: return }
10-01-2020