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

Special case where the compiler does not infer generic exception type

    Details

    • Subcomponent:
    • Introduced In Build:
      b148
    • Introduced In Version:
      9
    • CPU:
      x86_64
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Microsoft Windows [Version 10.0.17134.1]

      openjdk version "10.0.1" 2018-04-17
      OpenJDK Runtime Environment Zulu10.2+3 (build 10.0.1+9)
      OpenJDK 64-Bit Server VM Zulu10.2+3 (build 10.0.1+9, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      given a specific generic method signature, the compiler behaved differently if the signature is defined in a class being compiled from the same directory or from a class on the classpath.

      REGRESSION : Last worked in version 8u172

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      I reduced the scenario to 3 java classes:

      Test calls a method on FromDir and FromClasspath. FromDir and FromClasspath are identical, except FromClasspath is already compiled and available on the classpath, while FromDir is compiled from the same directory as Test. The statement making use of FromDir compiles fine while the statement making use of FromClasspath fails with the below error, which is unexpected since the code is exactly the same (bar the class names). All commands and source code in the source code box below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      successful compilation
      ACTUAL -
      test\Test.java:9: error: unreported exception Exception; must be caught or declared to be thrown
              block(FromClasspath.exec(() -> CompletableFuture.completedFuture(null)));
                                      ^
      1 error

      ---------- BEGIN SOURCE ----------
      Assuming javac is available on the path, in an arbitrary parent folder, create 2 folders main and lib

      in lib\test\FromClasspath.java
      package test;

      import java.util.concurrent.CompletionStage;

      public class FromClasspath {

          @FunctionalInterface
          public interface CallableThrows<T, E extends Throwable> {

              T call() throws E;

          }

          public static <T, E extends Exception> CompletionStage<T> exec(final CallableThrows<CompletionStage<T>, E> callable) throws E {
              return callable.call();
          }

      }

      in main\test\FromDir.java
      package test;

      import java.util.concurrent.CompletionStage;

      public class FromDir {

          @FunctionalInterface
          public interface CallableThrows<T, E extends Throwable> {

              T call() throws E;

          }

          public static <T, E extends Exception> CompletionStage<T> exec(final CallableThrows<CompletionStage<T>, E> callable) throws E {
              return callable.call();
          }

      }

      in main\test\Test.java
      package test;

      import java.util.concurrent.CompletableFuture;

      public class Test {

          public static void main(final String... args) {
              block(FromDir.exec(() -> CompletableFuture.completedFuture(null)));
              block(FromClasspath.exec(() -> CompletableFuture.completedFuture(null)));
          }

          public static <T> void block(final T stage) {

          }

      }

      from directory lib execute javac test/*.java
      from directory main execute javac test/*.java -cp ../lib
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Splitting into 2 statements compiles:

          public static void main(final String... args) {
              block(FromDir.exec(() -> CompletableFuture.completedFuture(null)));
              CompletionStage<Void> exec = FromClasspath.exec(() -> CompletableFuture.completedFuture(null));
              block(exec);
      // block(FromClasspath.exec(() -> CompletableFuture.completedFuture(null)));
          }

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              vromero Vicente Arturo Romero Zaldivar
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: