1 /*
  2  * Copyright (c) 2019, 2021, 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 8236670
 27  * @summary Verify proper scope of binding related to loops and breaks.
 28  * @enablePreview
 29  * @library /tools/lib /tools/javac/lib
 30  * @modules
 31  *      jdk.compiler/com.sun.tools.javac.api
 32  *      jdk.compiler/com.sun.tools.javac.file
 33  *      jdk.compiler/com.sun.tools.javac.main
 34  *      jdk.compiler/com.sun.tools.javac.util
 35  * @build toolbox.ToolBox toolbox.JavacTask
 36  * @build combo.ComboTestHelper
 37  * @compile ConditionalTest.java
 38  * @run main ConditionalTest
 39  */
 40 
 41 import combo.ComboInstance;
 42 import combo.ComboParameter;
 43 import combo.ComboTask;
 44 import combo.ComboTestHelper;
 45 import java.nio.file.Path;
 46 import java.nio.file.Paths;
 47 import toolbox.ToolBox;
 48 
 49 public class ConditionalTest extends ComboInstance<ConditionalTest> {
 50     protected ToolBox tb;
 51 
 52     ConditionalTest() {
 53         super();
 54         tb = new ToolBox();
 55     }
 56 
 57     public static void main(String... args) throws Exception {
 58         new ComboTestHelper<ConditionalTest>()
 59                 .withDimension("COND", (x, cond) -> x.cond = cond, Pattern.values())
 60                 .withDimension("TRUE", (x, trueSec) -> x.trueSec = trueSec, Pattern.values())
 61                 .withDimension("FALSE", (x, falseSec) -> x.falseSec = falseSec, Pattern.values())
 62                 .run(ConditionalTest::new);
 63     }
 64 
 65     private Pattern cond;
 66     private Pattern trueSec;
 67     private Pattern falseSec;
 68 
 69     private static final String MAIN_TEMPLATE =
 70             """
 71             public class Test {
 72                 public static boolean doTest(Object o, boolean b) {
 73                     return #{COND} ? #{TRUE} : #{FALSE}
 74                 }
 75             }
 76             """;
 77 
 78     @Override
 79     protected void doWork() throws Throwable {
 80         Path base = Paths.get(".");
 81 
 82         ComboTask task = newCompilationTask()
 83                 .withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
 84                         case "COND" -> cond;
 85                         case "TRUE" -> trueSec;
 86                         case "FALSE" -> falseSec;
 87                         default -> throw new UnsupportedOperationException(pname);
 88                     });
 89 
 90         task.analyze(result -> {
 91             boolean shouldPass;
 92             if (cond == Pattern.TRUE && (trueSec == Pattern.TRUE || trueSec == Pattern.FALSE)) { //6 cases covered
 93                 shouldPass = false; //already in scope in true section
 94             } else if (cond == Pattern.FALSE && (falseSec == Pattern.TRUE || falseSec == Pattern.FALSE)) { //6 cases covered
 95                 shouldPass = false; //already in scope in false section
 96             } else if (cond == Pattern.TRUE && falseSec == Pattern.TRUE) {
 97                 shouldPass = false; //JLS 6.3.1.4
 98             } else if (cond == Pattern.FALSE && trueSec == Pattern.TRUE) {
 99                 shouldPass = false; //JLS 6.3.1.4
100             } else if (trueSec == Pattern.TRUE && falseSec == Pattern.TRUE) {
101                 shouldPass = false; //JLS 6.3.1.4
102             } else if (trueSec == Pattern.TRUE && falseSec == Pattern.TRUE) {
103                 shouldPass = false; //JLS 6.3.1.4
104             } else if (cond == Pattern.TRUE && falseSec == Pattern.FALSE) {
105                 shouldPass = false; //JLS 6.3.1.4
106             } else if (cond == Pattern.FALSE && trueSec == Pattern.FALSE) {
107                 shouldPass = false; //JLS 6.3.1.4
108             } else if (trueSec == Pattern.FALSE && falseSec == Pattern.FALSE) {
109                 shouldPass = false; //JLS 6.3.1.4
110             } else {
111                 shouldPass = true;
112             }
113 
114             if (!shouldPass) {
115                 result.containsKey("Blabla");
116             }
117         });
118     }
119 
120     public enum Pattern implements ComboParameter {
121         NONE("b"),
122         TRUE("o instanceof String s"),
123         FALSE("!(o instanceof String s)");
124         private final String code;
125 
126         private Pattern(String code) {
127             this.code = code;
128         }
129 
130         @Override
131         public String expand(String optParameter) {
132             return code;
133         }
134     }
135 
136 }