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

cannot use pattern matching instanceof with subtype that adds specificity

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      when using a pattern matching instanceof where when the subtype is converted to the supertype has atleast one type argument that is more specific than the type argument supplied for the supertype and the type argument from the subtype contains another type argument that is supplied from a type argument in the subtype the javac compiler shows the pattern matching instanceof has an error even when the type argument in the pattern matching instanceof is a wildcard.

      The bug doesn't happen when the pattern matching instanceof only contains wildcards.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      For the pattern matching instanceof to compile without an error.
      ACTUAL -
      The pattern matching instanceof does not compile and says that the supertype cannot be safely cast to the subtype.

      ---------- BEGIN SOURCE ----------
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;

      public class TestInstanceof {

          public static void patternMatchingInstanceof(Map<Object, ?> map) {
              if (map instanceof SubType<Object, ?> subType) {

              }
          }

          public static class SubType<E, T> extends HashMap<E, List<T>> {}
      }
      ---------- END SOURCE ----------

            gbierman Gavin Bierman
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: