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

Unspecified generic type leads to bogus compilation failure in iteration

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P3
    • None
    • 8
    • tools
    • b30
    • 8
    • x86
    • windows_8

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_31"
      Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]


      A DESCRIPTION OF THE PROBLEM :
      Compiliing this class with javac 1.8.0_31 fails:

      import java.util.Map;
      import java.util.Map.Entry;
      import java.io.Serializable;

      class DoesNotCompile {

        private static Map<String, String> testMethod(Map<Integer, Integer> map) {
      return null;
        }

        public static void main(String[] args) {
      Map map = null;
      for (Entry<String, String> entry : testMethod(map).entrySet()) { }
        }
      }

      The error message is:
      DoesNotCompile.java:14: error: incompatible types: Object cannot be converted to Entry<String,String>
              for (Entry<String, String> entry : testMethod(map).entrySet()) { }

      As far as I can see, this is valid java code (compiles with a warning with javac 1.7.0_51).

      Interestingly, the following class does compile (the only difference being that the generic types of the local variable "map" are now specified).

      import java.util.Map;
      import java.util.Map.Entry;
      import java.io.Serializable;

      class DoesCompile {

        private static Map<String, String> testMethod(Map<Integer, Integer> map) {
      return null;
        }

        public static void main(String[] args) {
      Map<Integer,Integer> map = null;
      for (Entry<String, String> entry : testMethod(map).entrySet()) { }
        }
      }

      REGRESSION. Last worked in version 7u75

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try to compile the following source code with javac version 1.8.0_31:

      import java.util.Map;
      import java.util.Map.Entry;
      import java.io.Serializable;

      class DoesNotCompile {

        private static Map<String, String> testMethod(Map<Integer, Integer> map) {
      return null;
        }

        public static void main(String[] args) {
      Map map = null;
      for (Entry<String, String> entry : testMethod(map).entrySet()) { }
        }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should compile (with a warning).
      ACTUAL -
      Compilation fails:

      DoesNotCompile.java:14: error: incompatible types: Object cannot be converted to Entry<String,String>
              for (Entry<String, String> entry : testMethod(map).entrySet()) { }
                                                                         ^
      Note: DoesNotCompile.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      1 error

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      DoesNotCompile.java:14: error: incompatible types: Object cannot be converted to Entry<String,String>
              for (Entry<String, String> entry : testMethod(map).entrySet()) { }
                                                                         ^
      Note: DoesNotCompile.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Map;
      import java.util.Map.Entry;
      import java.io.Serializable;

      class DoesNotCompile {

        private static Map<String, String> testMethod(Map<Integer, Integer> map) {
      return null;
        }

        public static void main(String[] args) {
      Map map = null;
      for (Entry<String, String> entry : testMethod(map).entrySet()) { }
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Several workarounds, e.g. specify generic types.

      Attachments

        Issue Links

          Activity

            People

              aroy Abhijit Roy (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: