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

Use of raw type as function parameter causes compiler confusion with returned type

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      $ java -version
      java version "1.8.0_111"
      Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
      $

      ADDITIONAL OS VERSION INFORMATION :
      $ uname -a
      Linux marc-ThinkPad-T530 3.13.0-106-generic #153-Ubuntu SMP Tue Dec 6 15:44:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
      $

      A DESCRIPTION OF THE PROBLEM :
      Simple, legal, java program does not compile. See source code for details.


      REGRESSION. Last worked in version 7u76

      ADDITIONAL REGRESSION INFORMATION:
      $ java -version
      java -version
      java version "1.7.0_21"
      Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
      $ javac Test.java
      javac Test.java
      Note: Test.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      $

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      $ javac Test.java
      Test.java:11: error: incompatible types: Object cannot be converted to String
      String s = f(rawInterable).iterator().next();
      ^
      Note: Test.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      1 error
      $

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected successful compilation as shown in Additional Regression Information.
      ACTUAL -
      Provided with Steps to Reproduce.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Provided with Steps to Reproduce.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Iterator;

      public class Test {

      void method1() {
      Iterable rawInterable = null;

      // This statement fails to compile with Java 8 but succeeds with Java 7.
      // The error message in Java 8 is
      // incompatible types: Object cannot be converted to String
      String s = f(rawInterable).iterator().next();

      // These two statements do the same as above but through a temporary variable.
      // This compiles with Java 7 and 8 with just the usual unchecked conversion warnings.
      Iterator<String> u = f(rawInterable).iterator();
      String v = u.next();
      }

      Iterable<String> f(Iterable<Integer> params) {
      // It doesn't matter what we return, we're just verifying type-checking.
      return null;
      }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Provided with Source code.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: