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

Invalid code is compiled by Java without error

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8, 9
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • Introduced In Version:
      8
    • CPU:
      x86
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :
      $ java -version
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin da0801a-dhcp85.apple.com 15.0.0 Darwin Kernel Version 15.0.0: Wed Aug 26 16:57:32 PDT 2015; root:xnu-3247.1.106~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      The code found below compiles on javac 1.8.0_60, even though it does not conform to the JLS spec. (Confirmed by spec author)

      The impact is low interoperability between javac and other java compilers, which impacts developer productivity in general.

      In reality, though this particular issue is a strange edge-case, this type of issue (different interpretations of JLS, or different quirks in compiler behavior) is fairly common, which has the effect of engineering projects expressly supporting only certain tool chains but not others, which is overall bad for the Java ecosystem.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use javac to compile "CompilerBug" class

      $ javac -version
      javac 1.8.0_60

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect a compiler error: Can only iterate over an array or an instance of java.lang.Iterable
      ACTUAL -
      The class compiles without error and generates 2 .class files.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class CompilerBug {
          
          static class Wrapper<T> {
              T value;
              public T getValue() {
                  return null;
              }
          }
          
          public static Optional<? extends Wrapper<String>[]> optionalArrayOfStringWrappers() {
           return Optional.empty();
          }
          
          public static <T> Optional<T> findEnglishAttribute(Wrapper<T>[] attributes) {
              return Optional.empty();
          }
          
          public static void main(String[] args) {
              for (Wrapper<String> attribute: optionalArrayOfStringWrappers().get()) {
                  // Expect compiler error on for loop: Can only iterate over an array or an instance of java.lang.Iterable
              }
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                mcimadamore Maurizio Cimadamore
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: