test/langtools/tools/javac/patterns/MatchBindingScopeTest.java
changeset 59285 7799a51dbe30
equal deleted inserted replaced
59284:88502b1cf76f 59285:7799a51dbe30
       
     1 /*
       
     2  * @test /nodynamiccopyright/
       
     3  * @bug 8231827
       
     4  * @summary Basic pattern bindings scope test
       
     5  * @compile/fail/ref=MatchBindingScopeTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} MatchBindingScopeTest.java
       
     6  */
       
     7 public class MatchBindingScopeTest {
       
     8 
       
     9     static Integer i = 42;
       
    10     static String s = "Hello";
       
    11     static Object o1 = s;
       
    12     static Object o2 = i;
       
    13 
       
    14     public static void main(String[] args) {
       
    15 
       
    16         if (o1 instanceof String j && j.length() == 5) { // OK
       
    17             System.out.println(j); // OK
       
    18         } else {
       
    19             System.out.println(j); // NOT OK
       
    20         }
       
    21 
       
    22         // NOT OK, name reused.
       
    23         if (o1 instanceof String j && o2 instanceof Integer j) {
       
    24         }
       
    25 
       
    26         if (o1 instanceof String j && j.length() == 5 && o2 instanceof Integer k && k == 42) { // OK
       
    27             System.out.println(j); // OK
       
    28             System.out.println(k); // OK
       
    29         } else {
       
    30             System.out.println(j); // NOT OK
       
    31             System.out.println(k); // NOT OK
       
    32         }
       
    33 
       
    34         if (o1 instanceof String j || j.length() == 5) { // NOT OK
       
    35             System.out.println(j); // NOT OK
       
    36         }
       
    37 
       
    38         if (o1 instanceof String j || o2 instanceof Integer j) { // NOT OK, types differ
       
    39             System.out.println(j);
       
    40         } else {
       
    41             System.out.println(j); // NOT OK.
       
    42         }
       
    43 
       
    44         while (o1 instanceof String j && j.length() == 5) { // OK
       
    45             System.out.println(j); // OK
       
    46         }
       
    47 
       
    48         while (o1 instanceof String j || true) {
       
    49             System.out.println(j); // Not OK
       
    50         }
       
    51 
       
    52         for (; o1 instanceof String j; j.length()) { // OK
       
    53             System.out.println(j); // OK
       
    54         }
       
    55 
       
    56         for (; o1 instanceof String j || true; j.length()) { // NOT OK
       
    57             System.out.println(j); // Not OK
       
    58         }
       
    59 
       
    60         int x = o1 instanceof String j ?
       
    61                       j.length() : // OK.
       
    62                       j.length();  // NOT OK.
       
    63 
       
    64         x = !(o1 instanceof String j) ?
       
    65                       j.length() : // NOT OK.
       
    66                       j.length();  // OK.
       
    67     }
       
    68 }