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

javac fails to determine types correctly and fails to compile

XMLWordPrintable

    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_141"
      Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]

      A DESCRIPTION OF THE PROBLEM :
      A piece of code that looks legal does not compile with a range of javac versions.

      This code does compile with the Eclipse Mars.2 Release (4.5.2), i.e. the older versions of the JDT eclipse incremental compiler. The code executes as expected once compiled with the eclipse compiler, and executed with the oracle JRE.

      If the eclipse compiler generated class is decompiled, it produces source that can be compiled with javac.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to compile follow the code:

      import java.util.Arrays;
      import java.util.List;
      import java.util.stream.Stream;

      public class Puzzle {
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public static void main(String[] args) {
      List<List> outer = Arrays.asList(Arrays.asList(new Object(), new Object(), new Object()),
      Arrays.asList(new Object(), new Object()),
      Arrays.asList(new Object(), new Object()));

      Stream<Boolean> bs1 = outer.stream().flatMap(inner -> inner.stream()).map(obj -> obj.hashCode() % 2 == 0);
      boolean b0 = bs1.filter(b -> !b).findAny().isPresent();

      boolean b2 = outer.stream().flatMap(inner->inner.stream())
      .map(obj -> obj.hashCode() % 2 == 0)
      .filter(b -> !b).findAny().isPresent();

      System.out.printf("%s %s %s", outer, b0, b2);
      }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Given the two lines:

      Stream<Boolean> bs1 = outer.stream().flatMap(inner -> inner.stream()).map(obj -> obj.hashCode() % 2 == 0);
      boolean b0 = bs1.filter(b -> !b).findAny().isPresent();

      compile, it looks like it could be a bug that the following line does not compile:

      boolean b2 = outer.stream().flatMap(inner->inner.stream())
      .map(obj -> obj.hashCode() % 2 == 0)
      .filter(b -> !b).findAny().isPresent();
      ACTUAL -
      The javac compiler cannot work out that the type of b is Boolean, not Object.

      Puzzle.java:23: error: bad operand type Object for unary operator '!'
                                                              .filter(b -> !b).findAny().isPresent();

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Puzzle.java:23: error: bad operand type Object for unary operator '!'
                                                              .filter(b -> !b).findAny().isPresent();

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      import java.util.List;
      import java.util.stream.Stream;

      public class Puzzle {
      @SuppressWarnings({ "rawtypes", "unchecked" })
      public static void main(String[] args) {
      List<List> outer = Arrays.asList(Arrays.asList(new Object(), new Object(), new Object()),
      Arrays.asList(new Object(), new Object()),
      Arrays.asList(new Object(), new Object()));

      Stream<Boolean> bs1 = outer.stream().flatMap(inner -> inner.stream()).map(obj -> obj.hashCode() % 2 == 0);
      boolean b0 = bs1.filter(b -> !b).findAny().isPresent();

      boolean b2 = outer.stream().flatMap(inner->inner.stream())
      .map(obj -> obj.hashCode() % 2 == 0)
      .filter(b -> !b).findAny().isPresent();

      System.out.printf("%s %s %s", outer, b0, b2);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The stream code can be re-written to skip the map to boolean, however this piece of code may be indicative of a class of bugs within the java compiler.

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

              Created:
              Updated:
              Resolved: