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

Incorrect thrown types inference in lambdas

XMLWordPrintable

      Consider this source code:
      ---
      public class Test {

          interface I1<E extends Throwable> {
              public void run() throws E;
          }

          public <E extends Throwable> void sink1(I1<E> i) throws E {
          }

          public void sink2(Runnable r) {
          }

          public void lambdaThrows() {
              try {
                  throw new IllegalStateException();
              } catch (Throwable t) {
                  sink1(() -> { throw t; }); //this does not compile, while all the other do
                  sink1(() -> { });
                  sink1(() -> { try { throw new IllegalStateException(); } catch (Throwable t2) { throw t2; } });
                  sink2(() -> { throw t; });
              }
          }
      }
      ---

      The first call to "sink1" won't compile, because the inferred bound for E is "Throwable". But the inferred bound should be "IllegalStateException", and the code should compile.

      The reason for this behavior is that when javac infers the bound, it only analyzes the effectively final and precise re-throw for the lambda's body, not for the surrounding code.

            jlahoda Jan Lahoda
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: