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

Safe cast is rejected (with warning) by javac

XMLWordPrintable

    • b120
    • x86
    • linux
    • Verified

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Javac rejects a cast that is perfectly safe even without a warning.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile attached program and observe that it fails

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Should compile without warning.
      ACTUAL -
      Compiler rejects program

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      cast.java:4: incompatible types
      found : java.lang.Iterable<capture of ? extends java.lang.Number>
      required: java.util.Collection<? extends java.lang.Number>
        Collection<? extends Number> x2 = x1; // javac reports error here
                                          ^
      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;
      class cast {
          Iterable<? extends Number> x1 = null;
          Collection<? extends Number> x2 = x1; // javac reports error here
      }

      ---------- END SOURCE ----------
      Actually the problem is the following (I checked with Neal Gafter, the submitter of this bug)

      import java.util.*;
      class cast {
         Iterable<? extends Number> x1 = null;
         Collection<? extends Number> x2 = (Collection<? extends Number>)x1; //javac generates a warning here
      }

      ACTUAL BEHAVIOR

      The output is (both 6-open and 6u06)

      $ javac Test.java -Xlint
      Test.java:4: warning: [unchecked] unchecked cast
      found : java.lang.Iterable<capture#156 of ? extends java.lang.Number>
      required: java.util.Collection<? extends java.lang.Number>
         Collection<? extends Number> x2 = (Collection<? extends Number>)x1; //javac generates a warning here
                                                                         ^
      1 warning

      EXPECTED BEHAVIOR

      The program should compile without any warning.

            mcimadamore Maurizio Cimadamore
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: