Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8268885

duplicate checkcast when destination type is not first type of intersection type

XMLWordPrintable

    • 14
    • b15
    • generic
    • generic

        A DESCRIPTION OF THE PROBLEM :
        Relates to JDK-8263642, possibly introduced by JDK-8209022

        When an intersection type cast, e.g. `I1 & I2`, is used and the result of it is then used in a way where the expected type is one of the intersection type supertypes other then the first, e.g. `I2`, a duplicate `checkcast` is emitted for this type.

        However, maybe this is a rare corner case which never actually occurs in reality because such Java source code makes no sense. And care must be taken to not reintroduce JDK-8209022.

        REGRESSION : Last worked in version 11.0.11

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create a source file `IntersectionTypeTest.java` with the code provided below
        2. Compile it
        3. Inspect the bytecode, e.g. using `javap -v`

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        No duplicate `checkcast`
        ACTUAL -
        There are duplicate checkcasts e.g. here for `I2`:
        void testSecond();
            descriptor: ()V
            flags: (0x0000)
            Code:
              stack=2, locals=2, args_size=1
                 0: new #7 // class IntersectionTypeTest$C
                 3: dup
                 4: invokespecial #9 // Method IntersectionTypeTest$C."<init>":()V
                 7: checkcast #10 // class IntersectionTypeTest$I2
                10: checkcast #12 // class IntersectionTypeTest$I3
                13: checkcast #14 // class IntersectionTypeTest$I1
                16: checkcast #10 // class IntersectionTypeTest$I2

        ---------- BEGIN SOURCE ----------
        class IntersectionTypeTest {
            interface I1 { }
            interface I2 { }
            interface I3 { }
            static class C { }
           
            void testFirst() {
                // No duplicate checkcast; first intersection type supertype is same as variable type
                I1 i = (I1 & I2 & I3) new C();
            }
            
            void testSecond() {
                // Duplicate checkcast for `I2`
                I2 i = (I1 & I2 & I3) new C();
            }
            
            I3 testThirdReturn() {
                // Also affects for example `return` statement; duplicate `checkcast` for `I3`
                return (I1 & I2 & I3) new C();
            }
        }
        ---------- END SOURCE ----------

              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: