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

Multiple type bounds cause a compilation error

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • None
    • 22, 24
    • tools
    • None

      From the mailing list: https://mail.openjdk.org/pipermail/compiler-dev/2025-July/031126.html

      Initially reported in the Eclipse compiler since ECJ accepts code that
      javac does not: https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4226.
      The current assessment is that the fault is with javac.

      Copying the example from there:

      ```
      interface Culprit {}

      class EntityInfo<E extends NumberGenerator<?> & Culprit> {}

      class NumberGenerator<N extends Number> implements NumberSupplier<N> {

      @Override
          public N getNumber() {
              return null;
          }
      }

      interface NumberSupplier<N extends Number> {

          N getNumber();
      }
      ```

      javac fails with:
      ```
      error: getNumber() in NumberGenerator cannot implement getNumber() in
      NumberSupplier
      class EntityInfo<E extends NumberGenerator<?> & Culprit> {}
                       ^
        return type CAP#1 is not compatible with CAP#2
        where N#1,N#2 are type-variables:
          N#1 extends Number declared in class NumberGenerator
          N#2 extends Number declared in interface NumberSupplier
        where CAP#1,CAP#2 are fresh type-variables:
          CAP#1 extends Number from capture of ?
          CAP#2 extends Number from capture of ?
      1 error
      ```

      Removing the Culprit bound:
      class EntityInfo<E extends NumberGenerator<?>> {}

      satisfies javac.

      Also noted that combining the supertypes explicitly is accepted:
      ```
      class X<T extends Number> extends NumberGenerator<T> implements Culprit {
      }

      class EI2<E extends X<?>> {}
      ```

            Unassigned Unassigned
            nlisker Nir Lisker
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: