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

unsoundness with wildcards

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 11
    • tools
    • None

      I think this is probably a duplicate of JDK-8054941, but it might be useful as another test case. This one doesn't involve lower bounds, or `null`.

      The following program contains no casts and compiles cleanly with -Xlint:unchecked,rawtypes, but fails at runtime with a CCE:

      ```
      abstract class T {

        public static void main(String[] args) {
          Box<String> a = new Box<>("hello");
          Box<Object> b = cast(a);
          b.set(42);
          System.err.println(a.get());
        }

        static class Box<T> {
          private T x;
          Box(T x) { this.x = x; }
          T get() { return x; }
          void set(T x) { this.x = x; }
        }

        static Box<Object> cast(Box<String> target) {
          return identity(flatten(new Box<Box<?>>(target)));
        }

        static <A> A identity(A x) {
          return x;
        }

        static <B> Box<? extends B> flatten(Box<Box<? extends B>> r) {
          return r.get();
        }
      }
      ```

      ```
      $ javac -fullversion
      javac full version "11-ea+13"
      $ javac -Xlint:all T.java
      $ java T
      ...
      Exception in thread "main" java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String
      at T.main(JavacBug.java:7)
      ```

            vromero Vicente Arturo Romero Zaldivar
            cushon Liam Miller-Cushon
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: