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

4.4: Allow array type as bound of type variable

XMLWordPrintable

    • Fix Understood

      From: Neal Gafter <###@###.###>
      Subject: javac bug? spec bug? (array type as a bound)
      Date: Fri, 22 Sep 2006 12:11:29 -0700


      Javac rejects this:

      class X<T extends Object[]> {}


      While examining JLS3 to see if it is allowed, I find 3.3 says

      Type variables have an optional bound, T & I1 ... In. The bound consists of either a type variable, or a class or interface type T possibly followed by further interface types I 1 , ..., In. ...


      According to 4.3, array types are not class types, so javac is correct. However, JLS does allow a type argument to be an array type:

      interface Foo<T> {}
      class Main {
        public static void main(String[] args) {
          Foo<Object[]> f = null;
        }
      }


      Here's the problem: the capture conversion as specified will infer a type variable with an array as a bound, even though no such type exists according to 3.3:

      interface Foo<T> {}
      class Main {
        static <T> Foo<? extends T> foo(T t) { return null; }
        public static void main() {
          int x = foo(new Integer[0]);
        }
      }

      // X.java: incompatible types
      // found : Foo<capture of ? extends java.lang.Integer[]>
      // required: int
      // int x = foo(new Integer[0]);
      // ^


      Further, there are various places in the spec (particularly 15.12.2.7) that say things like "If X is a type variable with an upper bound that is an array type...", even though such bounds are forbidden by 3.3.

      I recommend that the JLS and javac uniformly allow array types as bounds for type variables in the surface syntax.

            Unassigned Unassigned
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: