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

Inference: weird propagation of thrown inference variables

XMLWordPrintable

    • b120
    • Verified

      There seems to be some problems with inference of exception -variables. Javac rejects it, though I don’t quite understand how and why:

       

      class Main {
          {
              computeException(this::computeThrowable);
          }

          public <T, E extends Exception> void computeException(ThrowableComputable<T, E> c) throws E {}

          public <E1 extends Throwable> Integer computeThrowable() throws E1 {
              return 0;
          }

          public interface ThrowableComputable<T, E extends Throwable> {
              T compute() throws E;
          }
      }

       

       

      It looks like javac sees the following bounds

       

      var T - upper bounds = java.lang.Object, lower bounds = java.lang.Integer, eq bounds = \n
      var E - upper bounds = java.lang.Exception, lower bounds = E1, eq bounds = \n
      var E1 - upper bounds = E,java.lang.Exception,java.lang.Throwable, lower bounds = , eq bounds = \n";

       

      But I don’t understand why E1 participates in the computeException inference at all.

       

      If I remove T from the code, e.g. would define:

      public interface ThrowableComputable<T, E extends Throwable> {
          Integer compute() throws E;
      }

      then javac would accept the code, E1 won’t be included.

       

      With the initial sample, if I convert to lambda and extract variable

       

      computeException(() -> {
          Integer integer = computeThrowable();
          return integer;
      });

       

      then code would also compile.

       

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: