1 /* 2 * Copyright (c) 2023, 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 * @test 25 * @bug 8314226 26 * @summary Series of colon-style fallthrough switch cases with guards compiled incorrectly 27 * @enablePreview 28 * @compile T8314226.java 29 * @run main T8314226 30 * @ignore Verifier error 31 */ 32 33 public class T8314226 { 34 int multipleGuardedCases(Object obj) { 35 switch (obj) { 36 case Integer _ when ((Integer) obj) > 0: 37 case String _ when !((String) obj).isEmpty(): 38 return 1; 39 default: 40 return -1; 41 } 42 } 43 44 int multipleGuardedCases2a(Object obj) { 45 switch (obj) { 46 case Integer _ when ((Integer) obj) > 0: 47 case Float _ when ((Float) obj) > 0.0f: 48 case String _ when !((String) obj).isEmpty(): 49 return 1; 50 default: 51 return -1; 52 } 53 } 54 55 int multipleGuardedCases2b(Object obj) { 56 switch (obj) { 57 case Float _ when ((Float) obj) > 0.0f: // reversing the order 58 case Integer _ when ((Integer) obj) > 0: 59 case String _ when !((String) obj).isEmpty(): 60 return 1; 61 default: 62 return -1; 63 } 64 } 65 66 int multipleGuardedCasesMultiplePatterns(Object obj) { 67 switch (obj) { 68 case String _ when !((String) obj).isEmpty(): 69 case Integer _, Byte _ when ((Number) obj).intValue() > 0: 70 return 1; 71 default: 72 return -1; 73 } 74 } 75 76 int singleGuardedCase(Object obj) { 77 switch (obj) { 78 case String _ when !((String) obj).isEmpty(): 79 return 1; 80 default: 81 return -1; 82 } 83 } 84 85 int multipleCasesWithReturn(Object obj) { 86 switch (obj) { 87 case Integer _ when ((Integer) obj) > 0: 88 case String _ when !((String) obj).isEmpty(): 89 return 1; 90 case null: 91 return 2; 92 default: 93 return 3; 94 } 95 } 96 97 int multipleCasesWithEffectNoReturn(Object obj) { 98 int i = 0; 99 switch (obj) { 100 case Integer _ when ((Integer) obj) > 0: 101 case String _ when !((String) obj).isEmpty(): 102 i = i + 1; 103 case null: 104 i = i + 10; 105 default: 106 i = i + 100; 107 } 108 return i; 109 } 110 111 int multipleCasesWithLoop(Object obj) { 112 int i = 0; 113 switch (obj) { 114 case Integer _ when ((Integer) obj) > 0: 115 case String _ when !((String) obj).isEmpty(): 116 i = i + 1; 117 case null: 118 while (true) { 119 i = i + 10; 120 break; 121 } 122 default: 123 i = i + 100; 124 } 125 126 return i; 127 } 128 129 public static void main(String[] args) { 130 new T8314226().run(); 131 } 132 133 private void run() { 134 assertEquals(1, multipleGuardedCases(42)); 135 assertEquals(1, multipleGuardedCases("test")); 136 assertEquals(-1, multipleGuardedCases("")); 137 assertEquals(1, multipleGuardedCases2a(42.0f)); 138 assertEquals(1, multipleGuardedCases2a("test")); 139 assertEquals(-1, multipleGuardedCases2a("")); 140 assertEquals(1, multipleGuardedCases2b(42.0f)); 141 assertEquals(1, multipleGuardedCases2b("test")); 142 assertEquals(-1, multipleGuardedCases2b("")); 143 assertEquals(1, multipleGuardedCasesMultiplePatterns((byte) 42)); 144 assertEquals(1, multipleGuardedCasesMultiplePatterns("test")); 145 assertEquals(-1, multipleGuardedCasesMultiplePatterns("")); 146 assertEquals(-1, singleGuardedCase(42)); 147 assertEquals(1, singleGuardedCase("test")); 148 assertEquals(-1, singleGuardedCase("")); 149 assertEquals(1, multipleCasesWithReturn(42)); 150 assertEquals(1, multipleCasesWithReturn("test")); 151 assertEquals(2, multipleCasesWithReturn(null)); 152 assertEquals(3, multipleCasesWithReturn("")); 153 assertEquals(111, multipleCasesWithEffectNoReturn(42)); 154 assertEquals(111, multipleCasesWithEffectNoReturn("test")); 155 assertEquals(110, multipleCasesWithEffectNoReturn(null)); 156 assertEquals(100, multipleCasesWithEffectNoReturn("")); 157 assertEquals(111, multipleCasesWithLoop(42)); 158 assertEquals(111, multipleCasesWithLoop("test")); 159 assertEquals(110, multipleCasesWithLoop(null)); 160 assertEquals(100, multipleCasesWithLoop("")); 161 } 162 163 void assertEquals(Object expected, Object actual) { 164 if (expected != actual) { 165 throw new AssertionError("Expected: " + expected + ", but got: " + actual); 166 } 167 } 168 }