1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8206986 8214114 8214529
  27  * @summary Verify various corner cases with nested switch expressions.
  28  * @compile --enable-preview -source 13 ExpressionSwitchBugs.java
  29  * @run main/othervm --enable-preview ExpressionSwitchBugs
  30  */
  31 
  32 public class ExpressionSwitchBugs {
  33     public static void main(String... args) {
  34         new ExpressionSwitchBugs().testNested();
  35         new ExpressionSwitchBugs().testAnonymousClasses();
  36         new ExpressionSwitchBugs().testFields();
  37     }
  38 
  39     private void testNested() {
  40         int i = 0;
  41         check(42, id(switch (42) {
  42             default: i++; break 42;
  43         }));
  44         i = 0;
  45         check(43, id(switch (42) {
  46             case 42: while (i == 0) {
  47                 i++;
  48             }
  49             break 42 + i;
  50             default: i++; break 42;
  51         }));
  52         i = 0;
  53         check(42, id(switch (42) {
  54             case 42: if (i == 0) {
  55                 break 42;
  56             }
  57             default: i++; break 43;
  58         }));
  59         i = 0;
  60         check(42, id(switch (42) {
  61             case 42: if (i == 0) {
  62                 break 41 + switch (0) {
  63                     case 0 -> 1;
  64                     default -> -1;
  65                 };
  66             }
  67             default: i++; break 43;
  68         }));
  69     }
  70 
  71     private void testAnonymousClasses() {
  72         for (int i : new int[] {1, 2}) {
  73             check(3, id((switch (i) {
  74                 case 1: break new I() {
  75                     public int g() { return 3; }
  76                 };
  77                 default: break (I) () -> { return 3; };
  78             }).g()));
  79             check(3, id((switch (i) {
  80                 case 1 -> new I() {
  81                     public int g() { return 3; }
  82                 };
  83                 default -> (I) () -> { return 3; };
  84             }).g()));
  85         }
  86     }
  87 
  88     private void testFields() {
  89         check(3, field);
  90         check(3, ExpressionSwitchBugs.staticField);
  91     }
  92 
  93     private final int value = 2;
  94     private final int field = id(switch(value) {
  95         case 0 -> -1;
  96         case 2 -> {
  97             int temp = 0;
  98             temp += 3;
  99             break temp;
 100         }
 101         default -> throw new IllegalStateException();
 102     });
 103 
 104     private static final int staticValue = 2;
 105     private static final int staticField = new ExpressionSwitchBugs().id(switch(staticValue) {
 106         case 0 -> -1;
 107         case 2 -> {
 108             int temp = 0;
 109             temp += 3;
 110             break temp;
 111         }
 112         default -> throw new IllegalStateException();
 113     });
 114 
 115     private int id(int i) {
 116         return i;
 117     }
 118 
 119     private int id(Object o) {
 120         return -1;
 121     }
 122 
 123     private void check(int actual, int expected) {
 124         if (actual != expected) {
 125             throw new AssertionError("Unexpected result: " + actual);
 126         }
 127     }
 128 
 129     public interface I {
 130         public int g();
 131     }
 132 }