test/langtools/tools/javac/patterns/MatchBindingScopeTest.java
author jlahoda
Wed, 27 Nov 2019 09:00:01 +0100
changeset 59285 7799a51dbe30
permissions -rw-r--r--
8231826: Implement javac changes for pattern matching for instanceof Reviewed-by: mcimadamore Contributed-by: brian.goetz@oracle.com, gavin.bierman@oracle.com, maurizio.cimadamore@oracle.com, srikanth.adayapalam@oracle.com, vicente.romero@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     1
/*
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     2
 * @test /nodynamiccopyright/
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     3
 * @bug 8231827
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     4
 * @summary Basic pattern bindings scope test
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     5
 * @compile/fail/ref=MatchBindingScopeTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} MatchBindingScopeTest.java
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     6
 */
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     7
public class MatchBindingScopeTest {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     8
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
     9
    static Integer i = 42;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    10
    static String s = "Hello";
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    11
    static Object o1 = s;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    12
    static Object o2 = i;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    13
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    14
    public static void main(String[] args) {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    15
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    16
        if (o1 instanceof String j && j.length() == 5) { // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    17
            System.out.println(j); // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    18
        } else {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    19
            System.out.println(j); // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    20
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    21
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    22
        // NOT OK, name reused.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    23
        if (o1 instanceof String j && o2 instanceof Integer j) {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    24
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    25
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    26
        if (o1 instanceof String j && j.length() == 5 && o2 instanceof Integer k && k == 42) { // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    27
            System.out.println(j); // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    28
            System.out.println(k); // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    29
        } else {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    30
            System.out.println(j); // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    31
            System.out.println(k); // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    32
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    33
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    34
        if (o1 instanceof String j || j.length() == 5) { // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    35
            System.out.println(j); // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    36
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    37
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    38
        if (o1 instanceof String j || o2 instanceof Integer j) { // NOT OK, types differ
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    39
            System.out.println(j);
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    40
        } else {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    41
            System.out.println(j); // NOT OK.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    42
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    43
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    44
        while (o1 instanceof String j && j.length() == 5) { // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    45
            System.out.println(j); // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    46
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    47
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    48
        while (o1 instanceof String j || true) {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    49
            System.out.println(j); // Not OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    50
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    51
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    52
        for (; o1 instanceof String j; j.length()) { // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    53
            System.out.println(j); // OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    54
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    55
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    56
        for (; o1 instanceof String j || true; j.length()) { // NOT OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    57
            System.out.println(j); // Not OK
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    58
        }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    59
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    60
        int x = o1 instanceof String j ?
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    61
                      j.length() : // OK.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    62
                      j.length();  // NOT OK.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    63
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    64
        x = !(o1 instanceof String j) ?
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    65
                      j.length() : // NOT OK.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    66
                      j.length();  // OK.
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    67
    }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents:
diff changeset
    68
}