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 8214031 27 * @summary Verify switch expressions embedded in various statements work properly. 28 * @compile --enable-preview -source 12 ExpressionSwitchEmbedding.java 29 * @run main/othervm --enable-preview ExpressionSwitchEmbedding 30 */ 31 32 public class ExpressionSwitchEmbedding { 33 public static void main(String... args) { 34 new ExpressionSwitchEmbedding().run(); 35 } 36 37 private void run() { 38 { 39 int i = 6; 40 int o = 0; 41 while (switch (i) { 42 case 1: i = 0; break true; 43 case 2: i = 1; break true; 44 case 3, 4: i--; 45 if (i == 2 || i == 4) { 46 break switch (i) { 47 case 2 -> true; 48 case 4 -> false; 49 default -> throw new IllegalStateException(); 50 }; 51 } else { 52 break true; 53 } 54 default: i--; break switch (i) { 55 case -1 -> false; 56 case 3 -> true; 57 default -> true; 58 }; 59 }) { 60 o++; 61 } 62 if (o != 6 && i >= 0) { 63 throw new IllegalStateException(); 64 } 65 } 66 { 67 int i = 6; 68 int o = 0; 69 if (switch (i) { 70 case 1: i = 0; break true; 71 case 2: i = 1; break true; 72 case 3, 4: i--; 73 if (i == 2 || i == 4) { 74 break (switch (i) { 75 case 2 -> 3; 76 case 4 -> 5; 77 default -> throw new IllegalStateException(); 78 }) == i + 1; 79 } else { 80 break true; 81 } 82 default: i--; break switch (i) { 83 case -1 -> false; 84 case 3 -> true; 85 default -> true; 86 }; 87 }) { 88 o++; 89 } 90 if (o != 1 && i != 5) { 91 throw new IllegalStateException(); 92 } 93 } 94 { 95 int o = 0; 96 for (int i = 6; (switch (i) { 97 case 1: i = 0; break true; 98 case 2: i = 1; break true; 99 case 3, 4: i--; 100 if (i == 2 || i == 4) { 101 break switch (i) { 102 case 2 -> true; 103 case 4 -> false; 104 default -> throw new IllegalStateException(); 105 }; 106 } else { 107 break true; 108 } 109 default: i--; break switch (i) { 110 case -1 -> false; 111 case 3 -> true; 112 default -> true; 113 }; 114 }); ) { 115 o++; 116 } 117 if (o != 6) { 118 throw new IllegalStateException(); 119 } 120 } 121 { 122 int i = 6; 123 int o = 0; 124 do { 125 o++; 126 } while (switch (i) { 127 case 1: i = 0; break true; 128 case 2: i = 1; break true; 129 case 3, 4: i--; 130 if (i == 2 || i == 4) { 131 break switch (i) { 132 case 2 -> true; 133 case 4 -> false; 134 default -> throw new IllegalStateException(); 135 }; 136 } else { 137 break true; 138 } 139 default: i--; break switch (i) { 140 case -1 -> false; 141 case 3 -> true; 142 default -> true; 143 }; 144 }); 145 if (o != 6 && i >= 0) { 146 throw new IllegalStateException(); 147 } 148 } 149 } 150 151 }