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

yield statements doesn't allow cast expressions with more than 1 type arguments

XMLWordPrintable

    • b15
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      JDK 21.0.2

      A DESCRIPTION OF THE PROBLEM :
      Similar bug to https://bugs.openjdk.org/browse/JDK-8268670

      A yield expression with cast to generic type with more than 1 type argument fails to compile.

      For example,

         yield (Map<String, String>) obj;

      doesn't compile, while a cast expression with just 1 type arguments works:

         yield (List<String>) obj;

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try compiling provided source code example

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program compiles successfully
      ACTUAL -
      > javac YieldWith2TypeArguments.java

      YieldWith2TypeArguments.java:8: error: illegal start of expression
                      yield (Map<String, String>) value;
                                                ^
      YieldWith2TypeArguments.java:8: error: invalid use of a restricted identifier 'yield'
                      yield (Map<String, String>) value;
                      ^
        (to invoke a method called yield, qualify the yield with a receiver or type name)
      YieldWith2TypeArguments.java:8: error: ';' expected
                      yield (Map<String, String>) value;
                                                 ^
      YieldWith2TypeArguments.java:8: error: not a statement
                      yield (Map<String, String>) value;
                                                  ^
      4 errors


      ---------- BEGIN SOURCE ----------
      import java.util.*;

      class YieldWith2TypeArguments {
          public static void main(String[] args) {
              Object value = new HashMap<String, String>();
              Object returnedValue = switch (1) {
                  default -> {
                      yield (Map<String, String>) value;
                  }
              };
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Wrap the expression into parenthesized expression:
        yield ((Map<String, String>) obj);

      FREQUENCY : always


            abimpoudis Angelos Bimpoudis
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: