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